Index: trunk/fundraiser-statistics/fundraiser-scripts/web_reporting/campaigns/views.py |
— | — | @@ -18,6 +18,8 @@ |
19 | 19 | __revision__ = "$Rev$" |
20 | 20 | __date__ = "June 20th, 2011" |
21 | 21 | |
| 22 | + |
| 23 | +""" Import django modules """ |
22 | 24 | from django.shortcuts import render_to_response |
23 | 25 | from django.http import Http404 |
24 | 26 | from django.shortcuts import render_to_response, get_object_or_404 |
— | — | @@ -25,19 +27,19 @@ |
26 | 28 | from django.http import HttpResponseRedirect, HttpResponse |
27 | 29 | from django.core.urlresolvers import reverse |
28 | 30 | |
29 | | -import sys |
30 | | -import os |
31 | | -import datetime |
| 31 | +""" Import python base modules """ |
| 32 | +import sys, os, datetime, operator |
32 | 33 | |
| 34 | +""" Import Analytics modules """ |
33 | 35 | import Fundraiser_Tools.classes.Helper as Hlp |
34 | 36 | import Fundraiser_Tools.classes.DataReporting as DR |
35 | 37 | import Fundraiser_Tools.classes.DataLoader as DL |
36 | 38 | import Fundraiser_Tools.classes.FundraiserDataHandler as FDH |
37 | 39 | import Fundraiser_Tools.classes.TimestampProcessor as TP |
38 | 40 | import Fundraiser_Tools.settings as projSet |
39 | | -import operator |
40 | 41 | |
41 | 42 | |
| 43 | + |
42 | 44 | """ |
43 | 45 | Index page for finding the latest camapigns. Displays a list of recent campaigns with more than k donations over the last n hours. |
44 | 46 | Also if a report exists a link is provided. |
— | — | @@ -45,6 +47,18 @@ |
46 | 48 | """ |
47 | 49 | def index(request): |
48 | 50 | |
| 51 | + err_msg = '' |
| 52 | + |
| 53 | + """ Parse the filter fields """ |
| 54 | + filter_data = True |
| 55 | + try: |
| 56 | + min_donations_var = request.POST['min_donations'] |
| 57 | + earliest_utc_ts_var = request.POST['utc_ts'] |
| 58 | + |
| 59 | + except KeyError as e: |
| 60 | + filter_data = False |
| 61 | + |
| 62 | + |
49 | 63 | """ Interface with the DataLoader """ |
50 | 64 | |
51 | 65 | os.chdir(projSet.__project_home__ + 'classes') |
— | — | @@ -58,10 +72,27 @@ |
59 | 73 | end_time = TP.timestamp_from_obj(datetime.datetime.now() + datetime.timedelta(hours=8),1,3) |
60 | 74 | |
61 | 75 | campaigns, all_data = crl.run_query({'metric_name':'earliest_timestamp','start_time':start_time,'end_time':end_time}) |
62 | | - |
| 76 | + |
63 | 77 | sorted_campaigns = sorted(campaigns.iteritems(), key=operator.itemgetter(1)) |
64 | 78 | sorted_campaigns.reverse() |
65 | 79 | |
| 80 | + if filter_data: |
| 81 | + try: |
| 82 | + if min_donations_var == '': |
| 83 | + min_donations = 0 |
| 84 | + else: |
| 85 | + min_donations = int(min_donations_var) |
| 86 | + |
| 87 | + earliest_utc_ts = int(earliest_utc_ts_var) |
| 88 | + except: |
| 89 | + err_msg = 'Filter fields are incorrect.' |
| 90 | + |
| 91 | + filter_data = False |
| 92 | + else: |
| 93 | + min_donations_var = '' |
| 94 | + earliest_utc_ts_var = '' |
| 95 | + |
| 96 | + |
66 | 97 | new_sorted_campaigns = list() |
67 | 98 | for campaign in sorted_campaigns: |
68 | 99 | key = campaign[0] |
— | — | @@ -72,13 +103,18 @@ |
73 | 104 | name = 'none' |
74 | 105 | # timestamp = TP.timestamp_from_obj(all_data[key][3], 2, 2) |
75 | 106 | timestamp = TP.timestamp_convert_format(all_data[key][3], 1, 2) |
76 | | - new_sorted_campaigns.append([campaign[0], campaign[1], name, timestamp, all_data[key][2], all_data[key][4]]) |
| 107 | + |
| 108 | + if filter_data: |
| 109 | + if all_data[key][2] > min_donations and int(all_data[key][3]) > earliest_utc_ts: |
| 110 | + new_sorted_campaigns.append([campaign[0], campaign[1], name, timestamp, all_data[key][2], all_data[key][4]]) |
| 111 | + else: |
| 112 | + new_sorted_campaigns.append([campaign[0], campaign[1], name, timestamp, all_data[key][2], all_data[key][4]]) |
77 | 113 | |
78 | 114 | sorted_campaigns = new_sorted_campaigns |
79 | 115 | |
80 | 116 | os.chdir(projSet.__project_home__ + 'web_reporting') |
81 | 117 | |
82 | | - return render_to_response('campaigns/index.html', {'campaigns' : sorted_campaigns}) |
| 118 | + return render_to_response('campaigns/index.html', {'campaigns' : sorted_campaigns, 'err_msg' : err_msg, 'min_donations' : min_donations_var, 'earliest_utc' : earliest_utc_ts_var}, context_instance=RequestContext(request)) |
83 | 119 | |
84 | 120 | |
85 | 121 | |
— | — | @@ -106,7 +142,7 @@ |
107 | 143 | |
108 | 144 | """ Estimate start/end time of campaign """ |
109 | 145 | """ This generates an image for campaign views """ |
110 | | - ir = DR.IntervalReporting(use_labels=False, font_size=20, plot_type='line', query_type='campaign', file_path=projSet.__web_home__ + 'campaigns/static/images/') |
| 146 | + ir = DR.IntervalReporting(was_run=False, use_labels=False, font_size=20, plot_type='line', query_type='campaign', file_path=projSet.__web_home__ + 'campaigns/static/images/') |
111 | 147 | |
112 | 148 | """ |
113 | 149 | Try to produce analysis on the campaign view data |
— | — | @@ -116,16 +152,14 @@ |
117 | 153 | ir.run(start_time, end_time, interval, 'views', utm_campaign, []) |
118 | 154 | |
119 | 155 | except Exception as inst: |
120 | | - |
121 | 156 | print >> sys.stderr, type(inst) # the exception instance |
122 | 157 | print >> sys.stderr, inst.args # arguments stored in .args |
123 | 158 | print >> sys.stderr, inst # __str__ allows args to printed directly |
124 | 159 | |
125 | 160 | err_msg = 'There is insufficient data to analyze this campaign %s.' % utm_campaign |
126 | 161 | return HttpResponseRedirect(reverse('campaigns.views.index')) |
127 | | - |
| 162 | + |
128 | 163 | """ search for start_time and end_time """ |
129 | | - |
130 | 164 | top_view_interval = max(ir._counts_[utm_campaign]) |
131 | 165 | |
132 | 166 | start_count = 0 |
— | — | @@ -145,6 +179,7 @@ |
146 | 180 | break |
147 | 181 | |
148 | 182 | count = count + 1 |
| 183 | + |
149 | 184 | |
150 | 185 | count = 0 |
151 | 186 | ir._counts_[utm_campaign].reverse() |
— | — | @@ -170,9 +205,11 @@ |
171 | 206 | row = ttl.get_test_row(utm_campaign) |
172 | 207 | test_name = ttl.get_test_field(row ,'test_name') |
173 | 208 | |
174 | | - """ Regenerate the data using the estimated start and end times """ |
| 209 | + """ Regenerate the data using the estimated start and end times !! FIXME / MODIFY -- this is cumbersome .. should just generate the plot !! """ |
| 210 | + ir = DR.IntervalReporting(was_run=False, use_labels=False, font_size=20, plot_type='line', query_type='campaign', file_path=projSet.__web_home__ + 'campaigns/static/images/') |
175 | 211 | ir.run(start_time_est, end_time_est, interval, 'views', utm_campaign, []) |
176 | | - |
| 212 | + |
| 213 | + |
177 | 214 | """ determine the type of test """ |
178 | 215 | """ Get the banners """ |
179 | 216 | test_type, artifact_name_list = FDH.get_test_type(utm_campaign, start_time, end_time, DL.CampaignReportingLoader('')) |