def index(request): """ Process POST """ start_time, end_time, min_donation, view_order = process_post_vars(request) logging.info('Finding live landing pages from %s to %s.' % (start_time, end_time)) live_lps, columns = DL.CampaignReportingLoader( query_type='').query_live_landing_pages(start_time, end_time, min_donation=min_donation, view_order=view_order) if len(live_lps) == 0: html_table = '<br><p color="red"><b>No landing page data found.<b></p><br>' else: html_table = DR.DataReporting()._write_html_table(live_lps, columns) return render_to_response('live_lps/index.html', {'html_table': html_table}, context_instance=RequestContext(request))
def index(request, **kwargs): crl = DL.CampaignReportingLoader(query_type='totals') filter_data = True """ Determine the start and end times for the query """ start_time_obj = datetime.datetime.utcnow() + datetime.timedelta(days=-1) end_time = TP.timestamp_from_obj(datetime.datetime.utcnow(), 1, 3) start_time = TP.timestamp_from_obj(start_time_obj, 1, 3) """ PROCESS POST KWARGS =================== """ err_msg = '' try: err_msg = str(kwargs['kwargs']['err_msg']) except: pass """ PROCESS POST VARS ================= """ """ Process error message """ try: err_msg = MySQLdb._mysql.escape_string(request.POST['err_msg']) except KeyError: pass """ If the filter form was submitted extract the POST vars """ try: min_donations_var = MySQLdb._mysql.escape_string( request.POST['min_donations'].strip()) earliest_utc_ts_var = MySQLdb._mysql.escape_string( request.POST['utc_ts'].strip()) """ If the user timestamp is earlier than the default start time run the query for the earlier start time """ ts_format = TP.getTimestampFormat(earliest_utc_ts_var) """ Ensure the validity of the timestamp input """ if ts_format == TP.TS_FORMAT_FORMAT1: start_time = TP.timestamp_convert_format(earliest_utc_ts_var, TP.TS_FORMAT_FORMAT1, TP.TS_FORMAT_FLAT) elif ts_format == TP.TS_FORMAT_FLAT: start_time = earliest_utc_ts_var elif cmp(earliest_utc_ts_var, '') == 0: start_time = TP.timestamp_from_obj(start_time_obj, 1, 3) else: raise Exception() if cmp(min_donations_var, '') == 0: min_donations_var = -1 else: min_donations_var = int(min_donations_var) except KeyError: # In the case the form was not submitted set minimum donations and retain the default start time min_donations_var = -1 pass except Exception: # In the case the form was incorrectly formatted notify the user min_donations_var = -1 start_time = TP.timestamp_from_obj(start_time_obj, 1, 3) err_msg = 'Filter fields are incorrect.' """ GENERATE CAMPAIGN DATA ====================== """ campaigns, all_data = crl.run_query({ 'metric_name': 'earliest_timestamp', 'start_time': start_time, 'end_time': end_time }) """ Sort campaigns by earliest access """ sorted_campaigns = sorted(campaigns.iteritems(), key=operator.itemgetter(1)) sorted_campaigns.reverse() """ FILTER CAMPAIGN DATA ==================== """ new_sorted_campaigns = list() for campaign in sorted_campaigns: key = campaign[0] if campaign[1] > 0: name = all_data[key][0] if name == None: name = 'none' timestamp = TP.timestamp_convert_format(all_data[key][3], 1, 2) if filter_data: if all_data[key][2] > min_donations_var: new_sorted_campaigns.append([ campaign[0], campaign[1], name, timestamp, all_data[key][2], all_data[key][4] ]) else: new_sorted_campaigns.append([ campaign[0], campaign[1], name, timestamp, all_data[key][2], all_data[key][4] ]) sorted_campaigns = new_sorted_campaigns return render_to_response('campaigns/index.html', { 'campaigns': sorted_campaigns, 'err_msg': err_msg }, context_instance=RequestContext(request))
def show_campaigns(request, utm_campaign, **kwargs): """ PROCESS POST KWARGS =================== """ err_msg = '' try: err_msg = str(kwargs['kwargs']['err_msg']) except: pass test_type_override = '' try: test_type_override = MySQLdb._mysql.escape_string( request.POST['test_type_override']) if test_type_override == 'Banner': test_type_var = FDH._TESTTYPE_BANNER_ elif test_type_override == 'Landing Page': test_type_var = FDH._TESTTYPE_LP_ elif test_type_override == 'Banner and LP': test_type_var = FDH._TESTTYPE_BANNER_LP_ except: test_type_var = '' pass try: """ Find the earliest and latest page views for a given campaign """ lptl = DL.LandingPageTableLoader() ccrml = DL.CiviCRMLoader() start_time = ccrml.get_earliest_donation(utm_campaign) end_time = ccrml.get_latest_donation(utm_campaign) one_step = lptl.is_one_step(start_time, end_time, utm_campaign) if not (one_step): start_time = lptl.get_earliest_campaign_view(utm_campaign) end_time = lptl.get_latest_campaign_view(utm_campaign) interval = 1 """ Create reporting object to retrieve campaign data and write plots to image repo on disk """ 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/') """ Produce analysis on the campaign view data """ ir.run(start_time, end_time, interval, 'views', utm_campaign, {}, one_step=one_step) """ ESTIMATE THE START AND END TIME OF THE CAMPAIGN =============================================== Search for the first instance when more than 10 views are observed over a sampling period """ col_names = ir._data_loader_.get_column_names() views_index = col_names.index('views') ts_index = col_names.index('ts') row_list = list(ir._data_loader_._results_) # copy the query results for row in row_list: if row[views_index] > 100: start_time_est = row[ts_index] break row_list.reverse() for row in row_list: if row[views_index] > 100: end_time_est = row[ts_index] break """ BUILD THE VISUALIZATION FOR THE TEST VIEWS OF THIS CAMAPAIGN ============================================================ """ """ Read the test name """ ttl = DL.TestTableLoader() row = ttl.get_test_row(utm_campaign) test_name = ttl.get_test_field(row, 'test_name') """ Regenerate the data using the estimated start and end times """ 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/') ir.run(start_time_est, end_time_est, interval, 'views', utm_campaign, {}, one_step=one_step) """ Determine the type of test (if not overridden) and retrieve the artifacts """ test_type, artifact_name_list = FDH.get_test_type( utm_campaign, start_time, end_time, DL.CampaignReportingLoader(query_type=''), test_type_var) 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, 'one_step' : one_step, \ 'artifacts' : artifact_name_list, 'test_type' : test_type, 'err_msg' : err_msg}, context_instance=RequestContext(request)) except Exception as inst: logging.error('Failed to correctly produce campaign diagnostics.') logging.error(type(inst)) logging.error(inst.args) logging.error(inst) """ Return to the index page with an error """ err_msg = 'There is insufficient data to analyze this campaign: %s. Check to see if the <a href="/LML/">impressions have been loaded</a>. <br><br>ERROR:<br><br>%s' % ( utm_campaign, inst.__str__()) return index(request, kwargs={'err_msg': err_msg})
def test(request): try: """ PROCESS POST DATA ================= Escape all user input that can be entered in text fields """ test_name_var = MySQLdb._mysql.escape_string( request.POST['test_name'].strip()) utm_campaign_var = MySQLdb._mysql.escape_string( request.POST['utm_campaign'].strip()) start_time_var = MySQLdb._mysql.escape_string( request.POST['start_time'].strip()) end_time_var = MySQLdb._mysql.escape_string( request.POST['end_time'].strip()) one_step_var = MySQLdb._mysql.escape_string( request.POST['one_step'].strip()) country = MySQLdb._mysql.escape_string(request.POST['iso_filter']) """ Convert timestamp format if necessary """ if TP.is_timestamp(start_time_var, 2): start_time_var = TP.timestamp_convert_format(start_time_var, 2, 1) if TP.is_timestamp(end_time_var, 2): end_time_var = TP.timestamp_convert_format(end_time_var, 2, 1) if cmp(one_step_var, 'True') == 0: one_step_var = True else: one_step_var = False try: test_type_var = MySQLdb._mysql.escape_string( request.POST['test_type']) labels = request.POST['artifacts'] except KeyError: test_type_var, labels = FDH.get_test_type( utm_campaign_var, start_time_var, end_time_var, DL.CampaignReportingLoader( query_type='')) # submit an empty query type labels = labels.__str__() label_dict = dict() label_dict_full = dict() labels = labels[1:-1].split(',') """ Parse the labels """ for i in range(len(labels)): labels[i] = labels[i] label = labels[i].split('\'')[1] label = label.strip() pieces = label.split(' ') label = pieces[0] for j in range(len(pieces) - 1): label = label + '_' + pieces[j + 1] """ Escape the label parameters """ label = MySQLdb._mysql.escape_string(label) label_dict_full[label] = label """ Look at the artifact names and map them into a dict() - Determine if artifacts were chosen by the user """ if request.POST.__contains__('artifacts_chosen'): artifacts_chosen = request.POST.getlist('artifacts_chosen') """ Ensure that only two items are selected """ if len(artifacts_chosen) > 2: raise Exception( 'Please select (checkboxes) exactly two items to test') for elem in artifacts_chosen: esc_elem = MySQLdb._mysql.escape_string(str(elem)) label_dict[esc_elem] = esc_elem else: label_dict = label_dict_full """ Parse the added labels IF they are not empty """ for key in label_dict.keys(): try: if not (request.POST[key] == ''): label_dict[key] = MySQLdb._mysql.escape_string( str(request.POST[key])) else: label_dict[key] = key except: logging.error('Could not find %s in the POST QueryDict.' % key) for key in label_dict_full.keys(): try: if not (request.POST[key] == ''): label_dict_full[key] = MySQLdb._mysql.escape_string( str(request.POST[key])) else: label_dict_full[key] = key except: logging.error('Could not find %s in the POST QueryDict.' % key) """ EXECUTE REPORT GENERATION ========================= setup time parameters determine test metrics execute queries """ sample_interval = 1 start_time_obj = TP.timestamp_to_obj(start_time_var, 1) end_time_obj = TP.timestamp_to_obj(end_time_var, 1) time_diff = end_time_obj - start_time_obj time_diff_min = time_diff.seconds / 60.0 test_interval = int(math.floor(time_diff_min / sample_interval)) # 2 is the interval metric_types = FDH.get_test_type_metrics(test_type_var) metric_types_full = dict() """ Get the full (descriptive) version of the metric names !! FIXME / TODO -- order these properly !! """ for i in range(len(metric_types)): metric_types_full[metric_types[i]] = QD.get_metric_full_name( metric_types[i]) """ USE generate_reporting_objects() TO GENERATE THE REPORT DATA - dependent on test type """ measured_metric, winner, loser, percent_win, confidence, html_table_pm_banner, html_table_pm_lp, html_table_language, html_table \ = generate_reporting_objects(test_name_var, start_time_var, end_time_var, utm_campaign_var, label_dict, label_dict_full, \ sample_interval, test_interval, test_type_var, metric_types, one_step_var, country) winner_var = winner[0] results = list() for index in range(len(winner)): results.append({ 'metric': measured_metric[index], 'winner': winner[index], 'loser': loser[index], 'percent_win': percent_win[index], 'confidence': confidence[index] }) template_var_dict = {'results' : results, \ 'utm_campaign' : utm_campaign_var, 'metric_names_full' : metric_types_full, \ 'summary_table': html_table, 'sample_interval' : sample_interval, \ 'banner_pm_table' : html_table_pm_banner, 'lp_pm_table' : html_table_pm_lp, 'html_table_language' : html_table_language, \ 'start_time' : TP.timestamp_convert_format(start_time_var, 1, 2) , 'end_time' : TP.timestamp_convert_format(end_time_var, 1, 2)} html = render_to_response('tests/results_' + test_type_var + '.html', template_var_dict, context_instance=RequestContext(request)) """ WRITE TO TEST TABLE =================== """ ttl = DL.TestTableLoader() """ Format the html string """ html_string = html.__str__() html_string = html_string.replace('"', '\\"') if ttl.record_exists(utm_campaign=utm_campaign_var): ttl.update_test_row(test_name=test_name_var, test_type=test_type_var, utm_campaign=utm_campaign_var, start_time=start_time_var, end_time=end_time_var, html_report=html_string, winner=winner_var) else: ttl.insert_row(test_name=test_name_var, test_type=test_type_var, utm_campaign=utm_campaign_var, start_time=start_time_var, end_time=end_time_var, html_report=html_string, winner=winner_var) return html except Exception as inst: logging.error('Failed to correctly generate test report.') logging.error(type(inst)) logging.error(inst.args) logging.error(inst) """ Return to the index page with an error """ try: err_msg = 'Test Generation failed for: %s. Check the fields submitted for generation. <br><br>ERROR:<br><br>%s' % ( utm_campaign_var, inst.__str__()) except: err_msg = 'Test Generation failed. Check the fields submitted for generation. <br><br>ERROR:<br><br>%s' % inst.__str__( ) return campaigns_index(request, kwargs={'err_msg': err_msg}) return show_campaigns(request, utm_campaign_var, kwargs={'err_msg': err_msg})