<s> import datetime <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from app_metrics . models import Metric , MetricItem , MetricDay , MetricWeek , MetricMonth , MetricYear <EOL> from app_metrics . utils import week_for_date , month_for_date , year_for_date , get_previous_month , get_previous_year <EOL> class InvalidMetric ( Exception ) : pass <EOL> def trending_for_metric ( metric = None , date = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( metric , Metric ) : <EOL> raise InvalidMetric ( '<STR_LIT>' ) <EOL> if not date : <EOL> date = datetime . date . today ( ) <EOL> data = { } <EOL> if date == datetime . date . today ( ) : <EOL> data [ '<STR_LIT>' ] = _trending_for_current_day ( metric ) <EOL> data [ '<STR_LIT>' ] = _trending_for_yesterday ( metric ) <EOL> data [ '<STR_LIT>' ] = _trending_for_week ( metric ) <EOL> data [ '<STR_LIT>' ] = _trending_for_month ( metric ) <EOL> data [ '<STR_LIT>' ] = _trending_for_year ( metric ) <EOL> return data <EOL> def _trending_for_current_day ( metric = None ) : <EOL> date = datetime . date . today ( ) <EOL> unaggregated_values = MetricItem . objects . filter ( metric = metric ) <EOL> aggregated_values = MetricDay . objects . filter ( metric = metric , created = date ) <EOL> count = <NUM_LIT:0> <EOL> for u in unaggregated_values : <EOL> count = count + u . num <EOL> for a in aggregated_values : <EOL> count = count + a . num <EOL> return count <EOL> def _trending_for_yesterday ( metric = None ) : <EOL> today = datetime . date . today ( ) <EOL> yesterday_date = today - datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> previous_week_date = today - datetime . timedelta ( weeks = <NUM_LIT:1> ) <EOL> previous_month_date = get_previous_month ( today ) <EOL> data = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> try : <EOL> yesterday = MetricDay . objects . get ( metric = metric , created = yesterday_date ) <EOL> data [ '<STR_LIT>' ] = yesterday . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> try : <EOL> previous_week = MetricDay . objects . get ( metric = metric , created = previous_week_date ) <EOL> data [ '<STR_LIT>' ] = previous_week . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> try : <EOL> previous_month = MetricDay . objects . get ( metric = metric , created = previous_month_date ) <EOL> data [ '<STR_LIT>' ] = previous_month . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> return data <EOL> def _trending_for_week ( metric = None ) : <EOL> this_week_date = week_for_date ( datetime . date . today ( ) ) <EOL> previous_week_date = this_week_date - datetime . timedelta ( weeks = <NUM_LIT:1> ) <EOL> previous_month_week_date = get_previous_month ( this_week_date ) <EOL> previous_year_week_date = get_previous_year ( this_week_date ) <EOL> data = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> try : <EOL> week = MetricWeek . objects . get ( metric = metric , created = this_week_date ) <EOL> data [ '<STR_LIT>' ] = week . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> try : <EOL> previous_week = MetricWeek . objects . get ( metric = metric , created = previous_week_date ) <EOL> data [ '<STR_LIT>' ] = previous_week . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> try : <EOL> previous_month_week = MetricWeek . objects . get ( metric = metric , created = previous_month_week_date ) <EOL> data [ '<STR_LIT>' ] = previous_month_week . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> try : <EOL> previous_year_week = MetricWeek . objects . get ( metric = metric , created = previous_year_week_date ) <EOL> data [ '<STR_LIT>' ] = previous_year_week . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> return data <EOL> def _trending_for_month ( metric = None ) : <EOL> this_month_date = month_for_date ( datetime . date . today ( ) ) <EOL> previous_month_date = get_previous_month ( this_month_date ) <EOL> previous_month_year_date = get_previous_year ( this_month_date ) <EOL> data = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> <EOL> } <EOL> try : <EOL> month = MetricMonth . objects . get ( metric = metric , created = this_month_date ) <EOL> data [ '<STR_LIT>' ] = month . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> try : <EOL> previous_month = MetricMonth . objects . get ( metric = metric , created = previous_month_date ) <EOL> data [ '<STR_LIT>' ] = previous_month . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> try : <EOL> previous_month_year = MetricMonth . objects . get ( metric = metric , created = previous_month_year_date ) <EOL> data [ '<STR_LIT>' ] = previous_month_year . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> return data <EOL> def _trending_for_year ( metric = None ) : <EOL> this_year_date = year_for_date ( datetime . date . today ( ) ) <EOL> previous_year_date = get_previous_year ( this_year_date ) <EOL> data = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> try : <EOL> year = MetricYear . objects . get ( metric = metric , created = this_year_date ) <EOL> data [ '<STR_LIT>' ] = year . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> try : <EOL> previous_year = MetricYear . objects . get ( metric = metric , created = previous_year_date ) <EOL> data [ '<STR_LIT>' ] = previous_year . num <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> return data </s>
