# Path: dashboard/jobs.py
# @job
# def generate_dashboard():
#     logger = logging.getLogger("dashboard.jobs.generate_dashboard")
#     logger.info("Start")
#     sheet_id = settings.GOOGLE_SPREADSHEET_ID
#     data = sheets.load_sheet(sheet_id, settings.GOOGLE_SPREADSHEET_AUTH_FILE)
#     for row in data:
#         row.xtras = _add_target_date(row.xtras, row.xtras.get('_target_date'))
#         if row.xtras.get('_jira_filter'):
#             row.xtras = _add_current_jira_summary(row.xtras, row.xtras['_jira_filter'], logger)
#         if row.xtras.get('jira_summary'):
#             row.xtras = _add_week_ago_summary(row.xtras, row.xtras['jira_summary'], logger)
#             row.xtras = _add_forecasts(row.xtras, row.xtras['jira_summary'], logger)
#     cache.set('dashboard_data', data, None)
#     cache.set('dashboard_data_updated', datetime.datetime.now(get_default_timezone()), None)
#     logger.info("End")
#     return True
# 
# 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
from dateutil.relativedelta import relativedelta
from django.core.cache import cache
from django.http import JsonResponse, Http404
from django.shortcuts import redirect
from django.views.generic import View, TemplateView
from .jobs import generate_dashboard
from .services import summaries, predictions
    def find_summaries_or_404(self, filter_id):
        self.filter_summaries = summaries.for_date_range(filter_id, self.start_date)
        if not self.filter_summaries:
            raise Http404("No filter with id: {}".format(filter_id))
        self.latest_summary = self.filter_summaries.last()

    @property
    def days_ago(self):
        try:
            days_ago = int(self.request.GET.get('days_ago', 30))
        except ValueError:
            days_ago = 30
        return days_ago

    @property
    def start_date(self):
        return datetime.date.today() - relativedelta(days=self.days_ago - 1)

    @property
    def scope(self):
        try:
            scope = int(self.request.GET.get('scope', ''))
        except (TypeError, ValueError):
            scope = self.latest_summary.incomplete
        return scope

    def _generate_forecast(self):
        throughputs = predictions.throughput_history(self.filter_summaries)

        try:

# Path: dashboard/jobs.py
# @job
# def generate_dashboard():
#     logger = logging.getLogger("dashboard.jobs.generate_dashboard")
#     logger.info("Start")
#     sheet_id = settings.GOOGLE_SPREADSHEET_ID
#     data = sheets.load_sheet(sheet_id, settings.GOOGLE_SPREADSHEET_AUTH_FILE)
#     for row in data:
#         row.xtras = _add_target_date(row.xtras, row.xtras.get('_target_date'))
#         if row.xtras.get('_jira_filter'):
#             row.xtras = _add_current_jira_summary(row.xtras, row.xtras['_jira_filter'], logger)
#         if row.xtras.get('jira_summary'):
#             row.xtras = _add_week_ago_summary(row.xtras, row.xtras['jira_summary'], logger)
#             row.xtras = _add_forecasts(row.xtras, row.xtras['jira_summary'], logger)
#     cache.set('dashboard_data', data, None)
#     cache.set('dashboard_data_updated', datetime.datetime.now(get_default_timezone()), None)
#     logger.info("End")
#     return True
# 
# 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/views.py
import datetime
from dateutil.relativedelta import relativedelta
from django.core.cache import cache
from django.http import JsonResponse, Http404
from django.shortcuts import redirect
from django.views.generic import View, TemplateView
from .jobs import generate_dashboard
from .services import summaries, predictions

    def find_summaries_or_404(self, filter_id):
        self.filter_summaries = summaries.for_date_range(filter_id, self.start_date)
        if not self.filter_summaries:
            raise Http404("No filter with id: {}".format(filter_id))
        self.latest_summary = self.filter_summaries.last()

    @property
    def days_ago(self):
        try:
            days_ago = int(self.request.GET.get('days_ago', 30))
        except ValueError:
            days_ago = 30
        return days_ago

    @property
    def start_date(self):
        return datetime.date.today() - relativedelta(days=self.days_ago - 1)

    @property
    def scope(self):
        try:
            scope = int(self.request.GET.get('scope', ''))
        except (TypeError, ValueError):
            scope = self.latest_summary.incomplete
        return scope

    def _generate_forecast(self):
        throughputs = predictions.throughput_history(self.filter_summaries)

        try:

            forecast = predictions.forecast(throughputs, self.scope)