<s> import sys <EOL> import settings <EOL> import models . data <EOL> import datetime <EOL> from google . appengine . ext import db <EOL> sys . path . insert ( <NUM_LIT:0> , settings . APP_ROOT_DIR + '<STR_LIT>' ) <EOL> from graphy . backends import google_chart_api <EOL> from graphy import formatters <EOL> from graphy import line_chart <EOL> class SalesChart ( object ) : <EOL> def units_chart ( self , pid ) : <EOL> overall_chart = google_chart_api . LineChart ( ) <EOL> sales_query = db . Query ( models . data . Sale ) <EOL> sales_query . filter ( '<STR_LIT>' , pid ) <EOL> sales_query . order ( '<STR_LIT>' ) <EOL> sales = [ ] <EOL> for sale in sales_query : <EOL> sales . append ( [ sale . income_units , sale . report_date ] ) <EOL> if len ( sales ) == <NUM_LIT:0> : return ( None , None ) <EOL> sales , dates = zip ( * sales ) <EOL> dates = [ date . strftime ( '<STR_LIT>' ) for date in dates ] <EOL> overall_chart . AddLine ( sales , width = line_chart . LineStyle . THICK , label = '<STR_LIT>' ) <EOL> sales_start = sales_query . get ( ) . report_date <EOL> versions = settings . PRODUCTS [ pid ] [ '<STR_LIT>' ] <EOL> upgrades = [ ] <EOL> if len ( versions ) > <NUM_LIT:1> : <EOL> upgrades_start = datetime . datetime . combine ( versions [ <NUM_LIT:1> ] [ '<STR_LIT:date>' ] , datetime . time ( sales_start . hour , sales_start . minute ) ) <EOL> difference_in_days = ( upgrades_start - sales_start ) . days <EOL> upgrades_query = db . Query ( models . data . Upgrade ) <EOL> upgrades_query . filter ( '<STR_LIT>' , pid ) <EOL> upgrades_query . order ( '<STR_LIT>' ) <EOL> upgrades_query . filter ( '<STR_LIT>' , upgrades_start ) <EOL> for i in range ( <NUM_LIT:0> , difference_in_days ) : <EOL> upgrades . append ( <NUM_LIT:0> ) <EOL> for upgrade in upgrades_query : <EOL> upgrades . append ( upgrade . income_units ) <EOL> overall_chart . AddLine ( upgrades , width = line_chart . LineStyle . THICK , label = '<STR_LIT>' ) <EOL> max_num_horizontal_labels = <NUM_LIT:15> <EOL> segment_gap = <NUM_LIT:1> <EOL> if len ( dates ) > max_num_horizontal_labels : <EOL> segment_gap = len ( dates ) / max_num_horizontal_labels <EOL> overall_chart . bottom . min = <NUM_LIT:0> <EOL> overall_chart . bottom . max = max_num_horizontal_labels <EOL> overall_chart . bottom . labels = dates <EOL> overall_chart . bottom . labels = dates [ : : segment_gap ] <EOL> max_num_vertical_labels = <NUM_LIT:15> <EOL> max_sales = <NUM_LIT:0> <EOL> min_sales = <NUM_LIT:0> <EOL> max_upgrades = <NUM_LIT:0> <EOL> min_upgrades = <NUM_LIT:0> <EOL> if sales : <EOL> max_sales = max ( sales ) <EOL> min_sales = min ( sales ) <EOL> if upgrades : <EOL> max_upgrades = max ( upgrades ) <EOL> min_upgrades = min ( upgrades ) <EOL> overall_chart . left . max = max_upgrades if max_upgrades > max_sales else max_sales <EOL> overall_chart . left . min = min_upgrades if min_upgrades < min_sales else min_sales <EOL> vertical_labels = [ ] <EOL> segment_gap = overall_chart . left . max / max_num_vertical_labels <EOL> for i in range ( <NUM_LIT:0> , max_num_vertical_labels + <NUM_LIT:1> ) : <EOL> vertical_labels . append ( i * segment_gap ) <EOL> if len ( vertical_labels ) == max_num_vertical_labels + <NUM_LIT:1> : break <EOL> overall_chart . left . labels = vertical_labels <EOL> overall_chart . bottom . label_gridlines = True <EOL> concentrated_chart = self . concentrated_units_chart ( sales , upgrades , dates ) <EOL> if concentrated_chart != None : <EOL> concentrated_chart = concentrated_chart . display . Url ( <NUM_LIT:1000> , <NUM_LIT> ) <EOL> return ( overall_chart . display . Url ( <NUM_LIT:1000> , <NUM_LIT> ) , concentrated_chart ) <EOL> def concentrated_units_chart ( self , sales , upgrades , dates ) : <EOL> concentrated_result_set_num = <NUM_LIT> <EOL> concentrated_chart = None <EOL> if len ( sales ) > concentrated_result_set_num : <EOL> concentrated_chart = google_chart_api . LineChart ( ) <EOL> calc_concentrated_result_set = lambda x : x [ len ( sales ) - concentrated_result_set_num : len ( sales ) ] <EOL> sales_concentrated = calc_concentrated_result_set ( sales ) <EOL> dates_concentrated = calc_concentrated_result_set ( dates ) <EOL> upgrades_concentrated = calc_concentrated_result_set ( upgrades ) <EOL> concentrated_chart . AddLine ( sales_concentrated , width = line_chart . LineStyle . THICK , label = '<STR_LIT>' ) <EOL> if len ( upgrades_concentrated ) == concentrated_result_set_num - <NUM_LIT:1> : <EOL> concentrated_chart . AddLine ( upgrades_concentrated , width = line_chart . LineStyle . THICK , label = '<STR_LIT>' ) <EOL> concentrated_chart . left . min = <NUM_LIT:0> <EOL> max_upgrades_concentrated = <NUM_LIT:0> <EOL> max_sales_concentrated = <NUM_LIT:0> <EOL> if upgrades_concentrated : <EOL> max_upgrades_concentrated = max ( upgrades_concentrated ) <EOL> if sales_concentrated : <EOL> max_sales_concentrated = max ( sales_concentrated ) <EOL> concentrated_chart . left . max = max_upgrades_concentrated if max_upgrades_concentrated > max_sales_concentrated else max_sales_concentrated <EOL> segment_gap = concentrated_chart . left . max / concentrated_result_set_num <EOL> concentrated_vertical_labels = [ ] <EOL> for i in range ( <NUM_LIT:0> , concentrated_result_set_num + <NUM_LIT:1> ) : <EOL> concentrated_vertical_labels . append ( i * segment_gap ) <EOL> if len ( concentrated_vertical_labels ) == concentrated_result_set_num + <NUM_LIT:1> : break <EOL> if concentrated_vertical_labels [ - <NUM_LIT:1> ] < concentrated_chart . left . max : <EOL> new_max = concentrated_vertical_labels [ - <NUM_LIT:1> ] + segment_gap <EOL> concentrated_vertical_labels . append ( new_max ) <EOL> concentrated_chart . left . max = new_max <EOL> concentrated_chart . left . labels = concentrated_vertical_labels <EOL> concentrated_chart . bottom . labels = dates_concentrated <EOL> concentrated_chart . left . label_gridlines = True <EOL> concentrated_chart . bottom . label_gridlines = True <EOL> return concentrated_chart <EOL> else : <EOL> return None </s>
