# 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





class HealthCheck(View):
    def get(self, request, *args, **kwargs):
        last_access = cache.get('health_access', None)
        content = {
            'health': 'ok',
            'last_access': last_access,
        }
        cache.set('health_access', datetime.datetime.now(), 60)
        return JsonResponse(content)


class Dashboard(TemplateView):
    template_name = "dashboard.html"

    def get_context_data(self, **kwargs):
        data = cache.get('dashboard_data', [])
        updated = summaries.latest_update()
        context = dict(data=data, updated=updated)
        return context


class Forecast(TemplateView):
    template_name = "forecast.html"

    def find_summaries_or_404(self, filter_id):

# 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






class HealthCheck(View):
    def get(self, request, *args, **kwargs):
        last_access = cache.get('health_access', None)
        content = {
            'health': 'ok',
            'last_access': last_access,
        }
        cache.set('health_access', datetime.datetime.now(), 60)
        return JsonResponse(content)


class Dashboard(TemplateView):
    template_name = "dashboard.html"

    def get_context_data(self, **kwargs):
        data = cache.get('dashboard_data', [])
        updated = summaries.latest_update()
        context = dict(data=data, updated=updated)
        return context


class Forecast(TemplateView):
    template_name = "forecast.html"

    def find_summaries_or_404(self, filter_id):

        self.filter_summaries = summaries.for_date_range(filter_id, self.start_date)