# Path: dashboard/services/sheets.py
# class Row(OrderedDict):
#     def __init__(self, *args, **kwargs):
# def load_sheet(sheet_id, sheet_auth_file=""):
# def _load_via_api(sheet_id, sheet_auth_file):
# def _load_via_csv(sheet_id):
# def parse_csv(csv_text):
# 
# Path: dashboard/services/jira.py
# LOGGER = logging.getLogger("dashboard.services.jira")
# def query_url(filter_id, jira_url=None):
# def fetch_query_results(filter_id, requests=requests, logger=LOGGER):
# def summarize_results(results):
# def summarize_query(filter_id):
# 
# Path: dashboard/services/summaries.py
# SAVED = "saved"
# UPDATED = "updated"
# def fill_updated_at(summary):
# def create(filter_id, complete, incomplete, total, created_on=None):
# def store(summary_obj):
# def for_date(filter_id, date):
# def for_date_range(filter_id, start_date, end_date=None):
# def latest_update():
# 
# Path: dashboard/services/predictions.py
# def throughput_history(summaries):
# def forecast(throughputs, backlog_size, num_simulations=10000, seed=None):
# def for_project(filter_id, backlog_size, start_date):


import datetime
import logging
from dateutil.relativedelta import relativedelta
from dateutil.parser import parse
from django.conf import settings
from django.core.cache import cache
from django.utils.timezone import get_default_timezone
from django_rq import job
from .services import sheets, jira, summaries, predictions
            created_on=datetime.date.today(),
        )
        summaries.store(p)
        xtras['jira_summary'] = p
        logger.debug("Filter {} summary stored".format(p.filter_id))
    return xtras


def _add_week_ago_summary(xtras, current_summary, logger):
    week_ago = current_summary.created_on - relativedelta(days=7)
    week_ago_summary = summaries.for_date(filter_id=current_summary.filter_id, date=week_ago)
    xtras['week_ago_summary'] = week_ago_summary
    logger.info("Filter {} week_ago summary retrieved".format(current_summary.filter_id))
    return xtras


def _add_forecasts(xtras, summary, logger):
    a_month_ago = summary.created_on - relativedelta(days=30)
    try:
        forecasts = predictions.for_project(filter_id=summary.filter_id, backlog_size=summary.incomplete, start_date=a_month_ago)
    except ValueError as e:
        forecasts = []
        logger.warn("Filter {} predictions error: {}".format(summary.filter_id, str(e)))

    xtras['predictions'] = [summary.created_on + relativedelta(days=int(f)) for f in forecasts]
    logger.debug("Filter {} predictions created".format(summary.filter_id))
    return xtras


def _add_target_date(xtras, target_date_string):

# Path: dashboard/services/sheets.py
# class Row(OrderedDict):
#     def __init__(self, *args, **kwargs):
# def load_sheet(sheet_id, sheet_auth_file=""):
# def _load_via_api(sheet_id, sheet_auth_file):
# def _load_via_csv(sheet_id):
# def parse_csv(csv_text):
# 
# Path: dashboard/services/jira.py
# LOGGER = logging.getLogger("dashboard.services.jira")
# def query_url(filter_id, jira_url=None):
# def fetch_query_results(filter_id, requests=requests, logger=LOGGER):
# def summarize_results(results):
# def summarize_query(filter_id):
# 
# Path: dashboard/services/summaries.py
# SAVED = "saved"
# UPDATED = "updated"
# def fill_updated_at(summary):
# def create(filter_id, complete, incomplete, total, created_on=None):
# def store(summary_obj):
# def for_date(filter_id, date):
# def for_date_range(filter_id, start_date, end_date=None):
# def latest_update():
# 
# Path: dashboard/services/predictions.py
# def throughput_history(summaries):
# def forecast(throughputs, backlog_size, num_simulations=10000, seed=None):
# def for_project(filter_id, backlog_size, start_date):


# Path: dashboard/jobs.py
import datetime
import logging
from dateutil.relativedelta import relativedelta
from dateutil.parser import parse
from django.conf import settings
from django.core.cache import cache
from django.utils.timezone import get_default_timezone
from django_rq import job
from .services import sheets, jira, summaries, predictions

            created_on=datetime.date.today(),
        )
        summaries.store(p)
        xtras['jira_summary'] = p
        logger.debug("Filter {} summary stored".format(p.filter_id))
    return xtras


def _add_week_ago_summary(xtras, current_summary, logger):
    week_ago = current_summary.created_on - relativedelta(days=7)
    week_ago_summary = summaries.for_date(filter_id=current_summary.filter_id, date=week_ago)
    xtras['week_ago_summary'] = week_ago_summary
    logger.info("Filter {} week_ago summary retrieved".format(current_summary.filter_id))
    return xtras


def _add_forecasts(xtras, summary, logger):
    a_month_ago = summary.created_on - relativedelta(days=30)
    try:
        forecasts = predictions.for_project(filter_id=summary.filter_id, backlog_size=summary.incomplete, start_date=a_month_ago)
    except ValueError as e:
        forecasts = []
        logger.warn("Filter {} predictions error: {}".format(summary.filter_id, str(e)))

    xtras['predictions'] = [summary.created_on + relativedelta(days=int(f)) for f in forecasts]
    logger.debug("Filter {} predictions created".format(summary.filter_id))
    return xtras


def _add_target_date(xtras, target_date_string):

    xtras['target_date'] = ""