<s> from __future__ import unicode_literals <EOL> import json <EOL> from celery . schedules import crontab <EOL> from djcelery . models import PeriodicTask <EOL> from mayan . celery import app <EOL> from . models import StatisticResult <EOL> class StatisticNamespace ( object ) : <EOL> _registry = { } <EOL> @ classmethod <EOL> def get_all ( cls ) : <EOL> return cls . _registry . values ( ) <EOL> @ classmethod <EOL> def get ( cls , slug ) : <EOL> return cls . _registry [ slug ] <EOL> def __init__ ( self , slug , label ) : <EOL> self . slug = slug <EOL> self . label = label <EOL> self . _statistics = [ ] <EOL> self . __class__ . _registry [ slug ] = self <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . label ) <EOL> def add_statistic ( self , * args , ** kwargs ) : <EOL> statistic = Statistic ( * args , ** kwargs ) <EOL> statistic . namespace = self <EOL> self . _statistics . append ( statistic ) <EOL> @ property <EOL> def statistics ( self ) : <EOL> return self . _statistics <EOL> class Statistic ( object ) : <EOL> _registry = { } <EOL> @ staticmethod <EOL> def purge_schedules ( ) : <EOL> queryset = PeriodicTask . objects . filter ( name__startswith = '<STR_LIT>' ) . exclude ( name__in = Statistic . get_task_names ( ) ) <EOL> for periodic_task in queryset : <EOL> crontab_instance = periodic_task . crontab <EOL> periodic_task . delete ( ) <EOL> if crontab_instance and not crontab_instance . periodictask_set . all ( ) : <EOL> crontab_instance . delete ( ) <EOL> StatisticResult . objects . filter ( <EOL> slug__in = queryset . values_list ( '<STR_LIT:name>' , flat = True ) <EOL> ) . delete ( ) <EOL> @ classmethod <EOL> def get_all ( cls ) : <EOL> return cls . _registry . values ( ) <EOL> @ classmethod <EOL> def get ( cls , slug ) : <EOL> return cls . _registry [ slug ] <EOL> @ classmethod <EOL> def get_task_names ( cls ) : <EOL> return [ task . get_task_name ( ) for task in cls . get_all ( ) ] <EOL> def __init__ ( self , slug , label , func , renderer , minute = '<STR_LIT:*>' , hour = '<STR_LIT:*>' , day_of_week = '<STR_LIT:*>' , day_of_month = '<STR_LIT:*>' , month_of_year = '<STR_LIT:*>' ) : <EOL> self . slug = slug <EOL> self . label = label <EOL> self . func = func <EOL> self . renderer = renderer <EOL> self . schedule = crontab ( <EOL> minute = minute , hour = hour , day_of_week = day_of_week , <EOL> day_of_month = day_of_month , month_of_year = month_of_year , <EOL> ) <EOL> app . conf . CELERYBEAT_SCHEDULE . update ( <EOL> { <EOL> self . get_task_name ( ) : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . schedule , <EOL> '<STR_LIT:args>' : ( self . slug , ) <EOL> } , <EOL> } <EOL> ) <EOL> app . conf . CELERY_ROUTES . update ( <EOL> { <EOL> self . get_task_name ( ) : { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> } <EOL> ) <EOL> self . __class__ . _registry [ slug ] = self <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . label ) <EOL> def execute ( self ) : <EOL> self . store_results ( results = self . func ( ) ) <EOL> def get_task_name ( self ) : <EOL> return '<STR_LIT>' . format ( self . slug ) <EOL> def store_results ( self , results ) : <EOL> StatisticResult . objects . filter ( slug = self . slug ) . delete ( ) <EOL> statistic_result , created = StatisticResult . objects . get_or_create ( slug = self . slug ) <EOL> statistic_result . store_data ( data = results ) <EOL> def get_results ( self ) : <EOL> try : <EOL> return StatisticResult . objects . get ( slug = self . slug ) . get_data ( ) <EOL> except StatisticResult . DoesNotExist : <EOL> return { '<STR_LIT>' : { } } <EOL> def get_chart_data ( self ) : <EOL> return self . renderer ( data = self . get_results ( ) ) . get_chart_data ( ) <EOL> class ChartRenderer ( object ) : <EOL> def __init__ ( self , data ) : <EOL> self . data = data <EOL> def get_chart_data ( self ) : <EOL> raise NotImplementedError <EOL> class CharJSLine ( ChartRenderer ) : <EOL> template_name = '<STR_LIT>' <EOL> dataset_palette = ( <EOL> { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } , <EOL> ) <EOL> def get_chart_data ( self ) : <EOL> labels = [ ] <EOL> datasets = [ ] <EOL> for count , serie in enumerate ( self . data [ '<STR_LIT>' ] . items ( ) ) : <EOL> series_name , series_data = serie <EOL> dataset_labels = [ ] <EOL> dataset_values = [ ] <EOL> for data_point in series_data : <EOL> dataset_labels . extend ( data_point . keys ( ) ) <EOL> dataset_values . extend ( data_point . values ( ) ) <EOL> labels = dataset_labels <EOL> dataset = { <EOL> '<STR_LIT:label>' : series_name , <EOL> '<STR_LIT:data>' : dataset_values , <EOL> } <EOL> dataset . update ( <EOL> CharJSLine . dataset_palette [ <EOL> count % len ( CharJSLine . dataset_palette ) <EOL> ] <EOL> ) <EOL> datasets . append ( dataset ) <EOL> data = { <EOL> '<STR_LIT>' : labels , <EOL> '<STR_LIT>' : datasets , <EOL> } <EOL> return json . dumps ( data ) </s>
