Index: trunk/fundraiser-statistics/fundraiser-scripts/web_reporting/campaigns/views.py |
— | — | @@ -1,4 +1,23 @@ |
2 | 2 | |
| 3 | +""" |
| 4 | + DJANGO VIEW DEFINITIONS: |
| 5 | + ======================== |
| 6 | + |
| 7 | + Defines the views for campaigns application. This is meant to serve as a view into currently running campaigns and |
| 8 | + generating tests for them. |
| 9 | + |
| 10 | + Views: |
| 11 | + |
| 12 | + index -- the index page shows a listing of campaigns and the donations they have received along with a link to a generated report if exists |
| 13 | + show_campaigns -- this view shows the view stats for a campaign and allows the user to execute a test and generate a report |
| 14 | + show_report -- this view simply enables linking to existing reports |
| 15 | + |
| 16 | +""" |
| 17 | + |
| 18 | +__author__ = "Ryan Faulkner" |
| 19 | +__revision__ = "$Rev$" |
| 20 | +__date__ = "June 20th, 2011" |
| 21 | + |
3 | 22 | from django.shortcuts import render_to_response |
4 | 23 | from django.http import Http404 |
5 | 24 | from django.shortcuts import render_to_response, get_object_or_404 |
— | — | @@ -21,17 +40,25 @@ |
22 | 41 | |
23 | 42 | """ |
24 | 43 | Index page for finding the latest camapigns. Displays a list of recent campaigns with more than k donations over the last n hours. |
| 44 | + Also if a report exists a link is provided. |
25 | 45 | |
26 | 46 | """ |
27 | 47 | def index(request): |
28 | 48 | |
29 | 49 | """ Interface with the DataLoader """ |
30 | 50 | |
31 | | - os.chdir(projSet.__project_home__ + '/Fundraiser_Tools/classes') |
| 51 | + os.chdir(projSet.__project_home__ + 'classes') |
32 | 52 | |
33 | | - crl = DL.CampaignReportingLoader() |
34 | | - campaigns, all_data = crl.run_query('totals',{'metric_name':'donations','start_time':'20101230130400','end_time':'20101230154400'}) |
| 53 | + crl = DL.CampaignReportingLoader('totals') |
35 | 54 | |
| 55 | + #end_time, start_time = TP.timestamps_for_interval(datetime.datetime.now() + datetime.timedelta(hours=8), 1, hours=-24) |
| 56 | + #start_time = '20101230130400' |
| 57 | + #end_time = '20101230154400' |
| 58 | + start_time = '20110531120000' |
| 59 | + end_time = TP.timestamp_from_obj(datetime.datetime.now() + datetime.timedelta(hours=8),1,3) |
| 60 | + |
| 61 | + campaigns, all_data = crl.run_query({'metric_name':'earliest_timestamp','start_time':start_time,'end_time':end_time}) |
| 62 | + |
36 | 63 | sorted_campaigns = sorted(campaigns.iteritems(), key=operator.itemgetter(1)) |
37 | 64 | sorted_campaigns.reverse() |
38 | 65 | |
— | — | @@ -39,16 +66,17 @@ |
40 | 67 | for campaign in sorted_campaigns: |
41 | 68 | key = campaign[0] |
42 | 69 | |
43 | | - if campaign[1] > 50: |
| 70 | + if campaign[1] > 0: |
44 | 71 | name = all_data[key][0] |
45 | 72 | if name == None: |
46 | 73 | name = 'none' |
47 | | - timestamp = TP.timestamp_from_obj(all_data[key][3], 2, 2) |
48 | | - new_sorted_campaigns.append([campaign[0], campaign[1], name, timestamp, all_data[key][4]]) |
| 74 | + # timestamp = TP.timestamp_from_obj(all_data[key][3], 2, 2) |
| 75 | + 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]]) |
49 | 77 | |
50 | 78 | sorted_campaigns = new_sorted_campaigns |
51 | 79 | |
52 | | - os.chdir(projSet.__project_home__ + '/Fundraiser_Tools/web_reporting') |
| 80 | + os.chdir(projSet.__project_home__ + 'web_reporting') |
53 | 81 | |
54 | 82 | return render_to_response('campaigns/index.html', {'campaigns' : sorted_campaigns}) |
55 | 83 | |
— | — | @@ -57,23 +85,45 @@ |
58 | 86 | |
59 | 87 | """ |
60 | 88 | |
61 | | - Use time interval views of the data to display campaign information |
| 89 | + Shows view stats over the time range for which the campaign receives landing page hits. A form is also generated from the associated template |
| 90 | + that allows a test report to be generated. |
62 | 91 | |
63 | 92 | """ |
64 | 93 | def show_campaigns(request, utm_campaign): |
65 | 94 | |
66 | | - """ Look 5 hrs into the past? """ |
67 | | - start_time = '20101230130400' |
68 | | - end_time = '20101230154400' |
| 95 | + """ Look 10 hrs into the past? """ |
| 96 | + # end_time, start_time = TP.timestamps_for_interval(datetime.datetime.now() + datetime.timedelta(hours=8), 1, hours=-24) |
| 97 | + |
| 98 | + #start_time = '20101230130400' |
| 99 | + #end_time = '20101230154400' |
| 100 | + """ currently the start time is hard coded to the beginning of FR testing """ |
| 101 | + start_time = '20110531120000' |
| 102 | + end_time = TP.timestamp_from_obj(datetime.datetime.now() + datetime.timedelta(hours=8),1,3) |
| 103 | + |
69 | 104 | interval = 2 |
70 | | - |
71 | | - os.chdir(projSet.__project_home__ + '/Fundraiser_Tools/classes') |
72 | 105 | |
| 106 | + os.chdir(projSet.__project_home__ + 'classes') |
| 107 | + |
73 | 108 | """ Estimate start/end time of campaign """ |
74 | 109 | """ This generates an image for campaign views """ |
75 | | - ir = DR.IntervalReporting(use_labels=False, font_size=20, plot_type='line', data_loader='campaign', file_path=projSet.__web_home__ + 'campaigns/static/images/') |
76 | | - ir.run(start_time, end_time, interval, 'campaign', 'views', utm_campaign, []) |
| 110 | + ir = DR.IntervalReporting(use_labels=False, font_size=20, plot_type='line', query_type='campaign', file_path=projSet.__web_home__ + 'campaigns/static/images/') |
77 | 111 | |
| 112 | + """ |
| 113 | + Try to produce analysis on the campaign view data |
| 114 | + If unsuccessful reverse to index view and emit error message |
| 115 | + """ |
| 116 | + try: |
| 117 | + ir.run(start_time, end_time, interval, 'views', utm_campaign, []) |
| 118 | + |
| 119 | + except Exception as inst: |
| 120 | + |
| 121 | + print >> sys.stderr, type(inst) # the exception instance |
| 122 | + print >> sys.stderr, inst.args # arguments stored in .args |
| 123 | + print >> sys.stderr, inst # __str__ allows args to printed directly |
| 124 | + |
| 125 | + err_msg = 'There is insufficient data to analyze this campaign %s.' % utm_campaign |
| 126 | + return HttpResponseRedirect(reverse('campaigns.views.index')) |
| 127 | + |
78 | 128 | """ search for start_time and end_time """ |
79 | 129 | |
80 | 130 | top_view_interval = max(ir._counts_[utm_campaign]) |
— | — | @@ -83,31 +133,53 @@ |
84 | 134 | begin_count = False |
85 | 135 | count = 0 |
86 | 136 | |
87 | | - """ Define the start time as the first interval at least80% of the maximum view interval |
88 | | - Define the end time as the first interval following the estimated start time and less than 50% of the maximum view interval """ |
| 137 | + """ |
| 138 | + ESTIMATE THE START AND END TIME OF THE CAMAPIGN |
| 139 | + |
| 140 | + Search for the first instance when more than 10 views are observed over a smapling period |
| 141 | + """ |
89 | 142 | for i in ir._counts_[utm_campaign]: |
90 | | - if i > (0.5 * top_view_interval) and not(begin_count): |
| 143 | + # if i > (0.5 * top_view_interval) and not(begin_count): |
| 144 | + if i > 10: |
91 | 145 | start_count = count |
92 | | - begin_count = True |
93 | | - |
94 | | - if begin_count and i < (0.5 * top_view_interval) and (end_count > count): |
95 | | - end_count = count - 3 |
96 | | - |
| 146 | + break |
| 147 | + |
97 | 148 | count = count + 1 |
98 | 149 | |
| 150 | + count = 0 |
| 151 | + ir._counts_[utm_campaign].reverse() |
| 152 | + for i in ir._counts_[utm_campaign]: |
| 153 | + |
| 154 | + if i > 10: |
| 155 | + end_count = count |
| 156 | + break |
| 157 | + |
| 158 | + count = count + 1 |
| 159 | + |
| 160 | + ir._counts_[utm_campaign].reverse() |
| 161 | + |
| 162 | + """ Based on where the first and last number of views/interval are observed to be greater that 10, generate the associated timestamps """ |
| 163 | + end_count = len(ir._counts_[utm_campaign]) - end_count |
99 | 164 | start_time_est = TP.timestamp_to_obj(start_time, 1) + datetime.timedelta(minutes=interval * start_count) |
100 | 165 | end_time_est = TP.timestamp_to_obj(start_time, 1) + datetime.timedelta(minutes=interval * end_count) |
101 | 166 | start_time_est = TP.timestamp_from_obj(start_time_est, 1, 2) |
102 | 167 | end_time_est = TP.timestamp_from_obj(end_time_est, 1, 2) |
103 | 168 | |
| 169 | + """ Read the test name """ |
| 170 | + ttl = DL.TestTableLoader() |
| 171 | + row = ttl.get_test_row(utm_campaign) |
| 172 | + test_name = ttl.get_test_field(row ,'test_name') |
104 | 173 | |
| 174 | + """ Regenerate the data using the estimated start and end times """ |
| 175 | + ir.run(start_time_est, end_time_est, interval, 'views', utm_campaign, []) |
| 176 | + |
105 | 177 | """ determine the type of test """ |
106 | 178 | """ Get the banners """ |
107 | | - test_type, artifact_name_list = FDH.get_test_type(utm_campaign, start_time_est, end_time_est) |
108 | | - |
109 | | - os.chdir(projSet.__project_home__ + '/Fundraiser_Tools/web_reporting') |
| 179 | + test_type, artifact_name_list = FDH.get_test_type(utm_campaign, start_time, end_time, DL.CampaignReportingLoader('')) |
| 180 | + |
| 181 | + os.chdir(projSet.__project_home__ + 'web_reporting') |
110 | 182 | |
111 | | - return render_to_response('campaigns/show_campaigns.html', {'utm_campaign' : utm_campaign, 'start_time' : start_time_est, 'end_time' : end_time_est, 'artifacts' : artifact_name_list, 'test_type' : test_type}, context_instance=RequestContext(request)) |
| 183 | + return render_to_response('campaigns/show_campaigns.html', {'utm_campaign' : utm_campaign, 'test_name' : test_name, 'start_time' : start_time_est, 'end_time' : end_time_est, 'artifacts' : artifact_name_list, 'test_type' : test_type}, context_instance=RequestContext(request)) |
112 | 184 | |
113 | 185 | # return HttpResponseRedirect(reverse('campaigns.views.index')) |
114 | 186 | |
Index: trunk/fundraiser-statistics/fundraiser-scripts/web_reporting/campaigns/urls.py |
— | — | @@ -2,6 +2,7 @@ |
3 | 3 | |
4 | 4 | |
5 | 5 | urlpatterns = patterns('', |
| 6 | + # url(r'^$', 'campaigns.views.index', kwargs={'message':''}), |
6 | 7 | (r'^$', 'campaigns.views.index'), |
7 | 8 | (r'^(?P<utm_campaign>[a-zA-Z0-9_]+)$', 'campaigns.views.show_campaigns'), |
8 | 9 | ) |