示例#1
0
def process_filter_data(request):

    err_msg = ""

    time_curr = datetime.datetime.utcnow()
    time_dayback = time_curr + datetime.timedelta(hours=-4)

    _beginning_time_ = TP.timestamp_from_obj(time_dayback, 1, 3)
    _end_time_ = TP.timestamp_from_obj(time_curr, 1, 3)

    """ 
        PROCESS POST VARS 
        =================
    """

    try:

        latest_utc_ts_var = MySQLdb._mysql.escape_string(request.POST["latest_utc_ts"].strip())

        if not (TP.is_timestamp(latest_utc_ts_var, 1)) and not (TP.is_timestamp(latest_utc_ts_var, 2)):
            raise TypeError

        if latest_utc_ts_var == "":
            latest_utc_ts_var = _end_time_

        ts_format = TP.getTimestampFormat(latest_utc_ts_var)
        if ts_format == TP.TS_FORMAT_FORMAT1:
            latest_utc_ts_var = TP.timestamp_convert_format(latest_utc_ts_var, TP.TS_FORMAT_FORMAT1, TP.TS_FORMAT_FLAT)

    except KeyError:
        latest_utc_ts_var = _end_time_

    except TypeError:
        err_msg = "Please enter a valid end-timestamp."
        latest_utc_ts_var = _end_time_

    try:

        earliest_utc_ts_var = MySQLdb._mysql.escape_string(request.POST["earliest_utc_ts"].strip())

        if not (TP.is_timestamp(earliest_utc_ts_var, 1)) and not (TP.is_timestamp(earliest_utc_ts_var, 2)):
            raise TypeError

        if earliest_utc_ts_var == "":
            earliest_utc_ts_var = _beginning_time_

        ts_format = TP.getTimestampFormat(earliest_utc_ts_var)
        if ts_format == TP.TS_FORMAT_FORMAT1:
            earliest_utc_ts_var = TP.timestamp_convert_format(
                earliest_utc_ts_var, TP.TS_FORMAT_FORMAT1, TP.TS_FORMAT_FLAT
            )

    except KeyError:
        earliest_utc_ts_var = _beginning_time_

    except TypeError:
        err_msg = "Please enter a valid start-timestamp."
        earliest_utc_ts_var = _beginning_time_

    return err_msg, earliest_utc_ts_var, latest_utc_ts_var
示例#2
0
def index(request):
    
    err_msg, earliest_utc_ts_var, latest_utc_ts_var = process_filter_data(request)
    
    sltl = DL.SquidLogTableLoader()
    
    """ Show the squid log table """
    squid_table = sltl.get_all_rows_unique_start_time()
    filtered_squid_table = list()
    
    for row in squid_table:
         
        log_start_time = sltl.get_squid_log_record_field(row, 'start_time')
        
        """ Ensure the timestamp is properly formatted """
        if TP.is_timestamp(log_start_time, 2):
            log_start_time = TP.timestamp_convert_format(log_start_time, 2, 1)
            
        if int(log_start_time) > int(earliest_utc_ts_var) and int(log_start_time) < int(latest_utc_ts_var):
            filtered_squid_table.append(row)
    
    squid_table = filtered_squid_table
    squid_table.reverse()
    column_names = sltl.get_column_names()
    new_column_names = list()
    
    for name in column_names:
        new_column_names.append(sltl.get_verbose_column(name))
        
    squid_table = DR.DataReporting()._write_html_table(squid_table, new_column_names)
    
    """ Show the latest log that has been or is loading and its progress """
    completion_rate = sltl.get_completion_rate_of_latest_log()
    
    return render_to_response('LML/index.html', {'err_msg' : err_msg, 'squid_table' : squid_table, 'completion_rate' : completion_rate},  context_instance=RequestContext(request))
示例#3
0
def index(request):

    err_msg, earliest_utc_ts_var, latest_utc_ts_var = process_filter_data(request)

    sltl = DL.SquidLogTableLoader()

    """ Show the squid log table """
    squid_table = sltl.get_all_rows_unique_start_time()
    filtered_squid_table = list()

    for row in squid_table:

        log_start_time = sltl.get_squid_log_record_field(row, "start_time")

        """ Ensure the timestamp is properly formatted """
        if TP.is_timestamp(log_start_time, 2):
            log_start_time = TP.timestamp_convert_format(log_start_time, 2, 1)

        if int(log_start_time) > int(earliest_utc_ts_var) and int(log_start_time) < int(latest_utc_ts_var):
            filtered_squid_table.append(row)

    squid_table = filtered_squid_table
    squid_table.reverse()
    column_names = sltl.get_column_names()
    new_column_names = list()

    for name in column_names:
        new_column_names.append(sltl.get_verbose_column(name))

    squid_table = DR.DataReporting()._write_html_table(squid_table, new_column_names)

    """ Show the latest log that has been or is loading and its progress """
    completion_rate = sltl.get_completion_rate_of_latest_log()

    return render_to_response(
        "LML/index.html",
        {"err_msg": err_msg, "squid_table": squid_table, "completion_rate": completion_rate},
        context_instance=RequestContext(request),
    )
示例#4
0
def generate_summary(request):
    
    try:
        
        err_msg = ''
        
        """ 
            PROCESS POST DATA
            ================= 
            
            Escape all user input that can be entered in text fields 
            
        """
        if 'utm_campaign' in request.POST:
            utm_campaign = MySQLdb._mysql.escape_string(request.POST['utm_campaign'])
        
        if 'start_time' in request.POST:
            start_time = MySQLdb._mysql.escape_string(request.POST['start_time'].strip())
            
            if not(TP.is_timestamp(start_time, 1)) and not(TP.is_timestamp(start_time, 2)):            
                err_msg = 'Incorrectly formatted start timestamp.'
                raise Exception()
            
        if 'end_time' in request.POST:
            end_time = MySQLdb._mysql.escape_string(request.POST['end_time'].strip())
            
            if not(TP.is_timestamp(end_time, 1)) and not(TP.is_timestamp(end_time, 2)):            
                err_msg = 'Incorrectly formatted end timestamp.'
                raise Exception()
        
        if 'iso_filter' in request.POST:
            country = MySQLdb._mysql.escape_string(request.POST['iso_filter'])
        else:
            country = '.{2}'
            
        if 'measure_confidence' in request.POST:
            if cmp(request.POST['measure_confidence'], 'yes') == 0:
                measure_confidence = True 
            else:
                measure_confidence = False
        else:
            measure_confidence = False
        
        if 'one_step' in request.POST:
            if cmp(request.POST['one_step'], 'yes') == 0:
                use_one_step = True 
            else:
                use_one_step = False
        else:
            use_one_step = False
            
        if 'donations_only' in request.POST:
            if cmp(request.POST['donations_only'], 'yes') == 0:
                donations_only = True 
            else:
                donations_only = False
        else:
            donations_only = False

            
        """ Convert timestamp format if necessary """
        
        if TP.is_timestamp(start_time, 2):
            start_time = TP.timestamp_convert_format(start_time, 2, 1)
        if TP.is_timestamp(end_time, 2):
            end_time = TP.timestamp_convert_format(end_time, 2, 1)
        
    
        """ =============================================== """
        
        """ 
            GENERATE A REPORT SUMMARY TABLE
            ===============================
        """
        
        if donations_only:
            srl = DL.SummaryReportingLoader(query_type=FDH._TESTTYPE_DONATIONS_)
        else:
            srl = DL.SummaryReportingLoader(query_type=FDH._TESTTYPE_BANNER_LP_)
            
        srl.run_query(start_time, end_time, utm_campaign, min_views=-1, country=country)            
        
        column_names = srl.get_column_names()
        summary_results = srl.get_results()
         
        if not(summary_results):
            html_table = '<h3>No artifact summary data available for %s.</h3>' % utm_campaign
            
        else:
            summary_results_list = list()
            for row in summary_results:
                summary_results_list.append(list(row))
            summary_results = summary_results_list
            
            """ 
                Format results to encode html table cell markup in results        
            """
            if measure_confidence:
                
                ret = DR.ConfidenceReporting(query_type='', hyp_test='').get_confidence_on_time_range(start_time, end_time, utm_campaign, one_step=use_one_step, country=country) # first get color codes on confidence
                conf_colour_code = ret[0]
                
                for row_index in range(len(summary_results)):
                    
                    artifact_index = summary_results[row_index][0] + '-' + summary_results[row_index][1] + '-' + summary_results[row_index][2]
                    
                    for col_index in range(len(column_names)):
                        
                        is_coloured_cell = False
                        if column_names[col_index] in conf_colour_code.keys():
                            if artifact_index in conf_colour_code[column_names[col_index]].keys():
                                summary_results[row_index][col_index] = '<td style="background-color:' + conf_colour_code[column_names[col_index]][artifact_index] + ';">' + str(summary_results[row_index][col_index]) + '</td>'
                                is_coloured_cell = True
                                
                        if not(is_coloured_cell):
                            summary_results[row_index][col_index] = '<td>' + str(summary_results[row_index][col_index]) + '</td>'
            
                html_table = DR.DataReporting()._write_html_table(summary_results, column_names, use_standard_metric_names=True, omit_cell_markup=True)
                
            else:
                
                html_table = DR.DataReporting()._write_html_table(summary_results, column_names, use_standard_metric_names=True)    
        
        """ Generate totals only if it's a non-donation-only query """
        
        if donations_only:
            srl = DL.SummaryReportingLoader(query_type=FDH._QTYPE_TOTAL_DONATIONS_)
        else:
            srl = DL.SummaryReportingLoader(query_type=FDH._QTYPE_TOTAL_)
            
        srl.run_query(start_time, end_time, utm_campaign, min_views=-1, country=country)
        
        total_summary_results = srl.get_results()
        
        if not(total_summary_results):
            html_table = html_table + '<div class="spacer"></div><div class="spacer"></div><h3>No data available for %s Totals.</h3>' % utm_campaign
        
        else: 
            html_table = html_table + '<div class="spacer"></div><div class="spacer"></div>' + DR.DataReporting()._write_html_table(total_summary_results, srl.get_column_names(), use_standard_metric_names=True)
        
        metric_legend_table = DR.DataReporting().get_standard_metrics_legend()
        conf_legend_table = DR.ConfidenceReporting(query_type='bannerlp', hyp_test='TTest').get_confidence_legend_table()
        
        html_table = '<h4><u>Metrics Legend:</u></h4><div class="spacer"></div>' + metric_legend_table + \
        '<div class="spacer"></div><h4><u>Confidence Legend for Hypothesis Testing:</u></h4><div class="spacer"></div>' + conf_legend_table + '<div class="spacer"></div><div class="spacer"></div>' + html_table
        
        """ 
            DETERMINE PAYMENT METHODS 
            =========================
        """
        
        ccl = DL.CiviCRMLoader()
        pm_data_counts, pm_data_conversions  = ccl.get_payment_methods(utm_campaign, start_time, end_time, country=country)

        html_table_pm_counts = DR.IntervalReporting().write_html_table_from_rowlists(pm_data_counts, ['Payment Method', 'Portion of Donations (%)'], 'Landing Page')
        html_table_pm_conversions = DR.IntervalReporting().write_html_table_from_rowlists(pm_data_conversions, ['Payment Method', 'Visits', 'Conversions', 'Conversion Rate (%)', 'Amount', 'Amount 25'], 'Landing Page')
        
        html_table = html_table + '<div class="spacer"></div><h4><u>Payment Methods Breakdown:</u></h4><div class="spacer"></div>' + html_table_pm_counts + \
        '<div class="spacer"></div><div class="spacer"></div>' + html_table_pm_conversions + '<div class="spacer"></div><div class="spacer"></div>'
        
        return render_to_response('tests/table_summary.html', {'html_table' : html_table, 'utm_campaign' : utm_campaign}, context_instance=RequestContext(request))

    except Exception as inst:
        
        if cmp(err_msg, '') == 0:
            err_msg = 'Could not generate campaign tabular results.'
        
        return index(request, err_msg=err_msg)
示例#5
0
def index(request, **kwargs):
    
    """ 
        PROCESS POST DATA
        ================= 
    """
    
    if 'err_msg' in kwargs:
        err_msg = kwargs['err_msg']
    else:        
        err_msg = ''

    try:
        
        latest_utc_ts_var = MySQLdb._mysql.escape_string(request.POST['latest_utc_ts'].strip())
        earliest_utc_ts_var = MySQLdb._mysql.escape_string(request.POST['earliest_utc_ts'].strip())
        
        if not TP.is_timestamp(earliest_utc_ts_var, 1) or  not TP.is_timestamp(earliest_utc_ts_var, 1):
            raise TypeError      
            
        if latest_utc_ts_var == '':
            latest_utc_ts_var = _end_time_
            
    except KeyError:
        
        earliest_utc_ts_var = _beginning_time_
        latest_utc_ts_var = _end_time_
    
    except TypeError:
        
        err_msg = 'Please enter a valid timestamp.'
        
        earliest_utc_ts_var = _beginning_time_
        latest_utc_ts_var = _end_time_
            
    ttl = DL.TestTableLoader()
    columns = ttl.get_column_names()
    test_rows = ttl.get_all_test_rows()
    
    """ Build a list of tests -- apply filters """
    l = []
    
    utm_campaign_index = ttl.get_test_index('utm_campaign')
    html_report_index = ttl.get_test_index('html_report')
    
    for i in test_rows:
        test_start_time = ttl.get_test_field(i, 'start_time')
        new_row = list(i)
                
        """ Ensure the timestamp is properly formatted """
        if TP.is_timestamp(test_start_time, 2):
            test_start_time = TP.timestamp_convert_format(test_start_time, 2, 1)
        
        new_row[html_report_index] = '<a href="/tests/report/%s">view</a>' % new_row[utm_campaign_index]
        
        if int(test_start_time) > int(earliest_utc_ts_var) and int(test_start_time) < int(latest_utc_ts_var):
            l.append(new_row)
        
    l.reverse()
    
    test_table = DR.DataReporting()._write_html_table(l, columns, use_standard_metric_names=True)
    
    return render_to_response('tests/index.html', {'err_msg' : err_msg, 'test_table' : test_table},  context_instance=RequestContext(request))
示例#6
0
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})
示例#7
0
def generate_summary(request):

    try:

        err_msg = ''
        """ 
            PROCESS POST DATA
            ================= 
            
            Escape all user input that can be entered in text fields 
            
        """
        if 'utm_campaign' in request.POST:
            utm_campaign = MySQLdb._mysql.escape_string(
                request.POST['utm_campaign'])

        if 'start_time' in request.POST:
            start_time = MySQLdb._mysql.escape_string(
                request.POST['start_time'].strip())

            if not (TP.is_timestamp(start_time, 1)) and not (TP.is_timestamp(
                    start_time, 2)):
                err_msg = 'Incorrectly formatted start timestamp.'
                raise Exception()

        if 'end_time' in request.POST:
            end_time = MySQLdb._mysql.escape_string(
                request.POST['end_time'].strip())

            if not (TP.is_timestamp(end_time, 1)) and not (TP.is_timestamp(
                    end_time, 2)):
                err_msg = 'Incorrectly formatted end timestamp.'
                raise Exception()

        if 'iso_filter' in request.POST:
            country = MySQLdb._mysql.escape_string(request.POST['iso_filter'])
        else:
            country = '.{2}'

        if 'measure_confidence' in request.POST:
            if cmp(request.POST['measure_confidence'], 'yes') == 0:
                measure_confidence = True
            else:
                measure_confidence = False
        else:
            measure_confidence = False

        if 'one_step' in request.POST:
            if cmp(request.POST['one_step'], 'yes') == 0:
                use_one_step = True
            else:
                use_one_step = False
        else:
            use_one_step = False

        if 'donations_only' in request.POST:
            if cmp(request.POST['donations_only'], 'yes') == 0:
                donations_only = True
            else:
                donations_only = False
        else:
            donations_only = False
        """ Convert timestamp format if necessary """

        if TP.is_timestamp(start_time, 2):
            start_time = TP.timestamp_convert_format(start_time, 2, 1)
        if TP.is_timestamp(end_time, 2):
            end_time = TP.timestamp_convert_format(end_time, 2, 1)
        """ =============================================== """
        """ 
            GENERATE A REPORT SUMMARY TABLE
            ===============================
        """

        if donations_only:
            srl = DL.SummaryReportingLoader(
                query_type=FDH._TESTTYPE_DONATIONS_)
        else:
            srl = DL.SummaryReportingLoader(
                query_type=FDH._TESTTYPE_BANNER_LP_)

        srl.run_query(start_time,
                      end_time,
                      utm_campaign,
                      min_views=-1,
                      country=country)

        column_names = srl.get_column_names()
        summary_results = srl.get_results()

        if not (summary_results):
            html_table = '<h3>No artifact summary data available for %s.</h3>' % utm_campaign

        else:
            summary_results_list = list()
            for row in summary_results:
                summary_results_list.append(list(row))
            summary_results = summary_results_list
            """ 
                Format results to encode html table cell markup in results        
            """
            if measure_confidence:

                ret = DR.ConfidenceReporting(
                    query_type='', hyp_test='').get_confidence_on_time_range(
                        start_time,
                        end_time,
                        utm_campaign,
                        one_step=use_one_step,
                        country=country)  # first get color codes on confidence
                conf_colour_code = ret[0]

                for row_index in range(len(summary_results)):

                    artifact_index = summary_results[row_index][
                        0] + '-' + summary_results[row_index][
                            1] + '-' + summary_results[row_index][2]

                    for col_index in range(len(column_names)):

                        is_coloured_cell = False
                        if column_names[col_index] in conf_colour_code.keys():
                            if artifact_index in conf_colour_code[
                                    column_names[col_index]].keys():
                                summary_results[row_index][
                                    col_index] = '<td style="background-color:' + conf_colour_code[
                                        column_names[col_index]][
                                            artifact_index] + ';">' + str(
                                                summary_results[row_index]
                                                [col_index]) + '</td>'
                                is_coloured_cell = True

                        if not (is_coloured_cell):
                            summary_results[row_index][
                                col_index] = '<td>' + str(
                                    summary_results[row_index]
                                    [col_index]) + '</td>'

                html_table = DR.DataReporting()._write_html_table(
                    summary_results,
                    column_names,
                    use_standard_metric_names=True,
                    omit_cell_markup=True)

            else:

                html_table = DR.DataReporting()._write_html_table(
                    summary_results,
                    column_names,
                    use_standard_metric_names=True)
        """ Generate totals only if it's a non-donation-only query """

        if donations_only:
            srl = DL.SummaryReportingLoader(
                query_type=FDH._QTYPE_TOTAL_DONATIONS_)
        else:
            srl = DL.SummaryReportingLoader(query_type=FDH._QTYPE_TOTAL_)

        srl.run_query(start_time,
                      end_time,
                      utm_campaign,
                      min_views=-1,
                      country=country)

        total_summary_results = srl.get_results()

        if not (total_summary_results):
            html_table = html_table + '<div class="spacer"></div><div class="spacer"></div><h3>No data available for %s Totals.</h3>' % utm_campaign

        else:
            html_table = html_table + '<div class="spacer"></div><div class="spacer"></div>' + DR.DataReporting(
            )._write_html_table(total_summary_results,
                                srl.get_column_names(),
                                use_standard_metric_names=True)

        metric_legend_table = DR.DataReporting().get_standard_metrics_legend()
        conf_legend_table = DR.ConfidenceReporting(
            query_type='bannerlp',
            hyp_test='TTest').get_confidence_legend_table()

        html_table = '<h4><u>Metrics Legend:</u></h4><div class="spacer"></div>' + metric_legend_table + \
        '<div class="spacer"></div><h4><u>Confidence Legend for Hypothesis Testing:</u></h4><div class="spacer"></div>' + conf_legend_table + '<div class="spacer"></div><div class="spacer"></div>' + html_table
        """ 
            DETERMINE PAYMENT METHODS 
            =========================
        """

        ccl = DL.CiviCRMLoader()
        pm_data_counts, pm_data_conversions = ccl.get_payment_methods(
            utm_campaign, start_time, end_time, country=country)

        html_table_pm_counts = DR.IntervalReporting(
        ).write_html_table_from_rowlists(
            pm_data_counts, ['Payment Method', 'Portion of Donations (%)'],
            'Landing Page')
        html_table_pm_conversions = DR.IntervalReporting(
        ).write_html_table_from_rowlists(pm_data_conversions, [
            'Payment Method', 'Visits', 'Conversions', 'Conversion Rate (%)',
            'Amount', 'Amount 25'
        ], 'Landing Page')

        html_table = html_table + '<div class="spacer"></div><h4><u>Payment Methods Breakdown:</u></h4><div class="spacer"></div>' + html_table_pm_counts + \
        '<div class="spacer"></div><div class="spacer"></div>' + html_table_pm_conversions + '<div class="spacer"></div><div class="spacer"></div>'

        return render_to_response('tests/table_summary.html', {
            'html_table': html_table,
            'utm_campaign': utm_campaign
        },
                                  context_instance=RequestContext(request))

    except Exception as inst:

        if cmp(err_msg, '') == 0:
            err_msg = 'Could not generate campaign tabular results.'

        return index(request, err_msg=err_msg)
示例#8
0
def index(request, **kwargs):
    """ 
        PROCESS POST DATA
        ================= 
    """

    if 'err_msg' in kwargs:
        err_msg = kwargs['err_msg']
    else:
        err_msg = ''

    try:

        latest_utc_ts_var = MySQLdb._mysql.escape_string(
            request.POST['latest_utc_ts'].strip())
        earliest_utc_ts_var = MySQLdb._mysql.escape_string(
            request.POST['earliest_utc_ts'].strip())

        if not TP.is_timestamp(earliest_utc_ts_var, 1) or not TP.is_timestamp(
                earliest_utc_ts_var, 1):
            raise TypeError

        if latest_utc_ts_var == '':
            latest_utc_ts_var = _end_time_

    except KeyError:

        earliest_utc_ts_var = _beginning_time_
        latest_utc_ts_var = _end_time_

    except TypeError:

        err_msg = 'Please enter a valid timestamp.'

        earliest_utc_ts_var = _beginning_time_
        latest_utc_ts_var = _end_time_

    ttl = DL.TestTableLoader()
    columns = ttl.get_column_names()
    test_rows = ttl.get_all_test_rows()
    """ Build a list of tests -- apply filters """
    l = []

    utm_campaign_index = ttl.get_test_index('utm_campaign')
    html_report_index = ttl.get_test_index('html_report')

    for i in test_rows:
        test_start_time = ttl.get_test_field(i, 'start_time')
        new_row = list(i)
        """ Ensure the timestamp is properly formatted """
        if TP.is_timestamp(test_start_time, 2):
            test_start_time = TP.timestamp_convert_format(
                test_start_time, 2, 1)

        new_row[
            html_report_index] = '<a href="/tests/report/%s">view</a>' % new_row[
                utm_campaign_index]

        if int(test_start_time) > int(earliest_utc_ts_var) and int(
                test_start_time) < int(latest_utc_ts_var):
            l.append(new_row)

    l.reverse()

    test_table = DR.DataReporting()._write_html_table(
        l, columns, use_standard_metric_names=True)

    return render_to_response('tests/index.html', {
        'err_msg': err_msg,
        'test_table': test_table
    },
                              context_instance=RequestContext(request))
示例#9
0
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})
示例#10
0
def process_filter_data(request):
        
    err_msg = ''
    
    time_curr = datetime.datetime.utcnow()
    time_dayback = time_curr + datetime.timedelta(hours = -4)
     
    _beginning_time_ = TP.timestamp_from_obj(time_dayback, 1, 3)
    _end_time_ = TP.timestamp_from_obj(time_curr, 1, 3)
    
    
    """ 
        PROCESS POST VARS 
        =================
    """
    
    try:
        
        latest_utc_ts_var = MySQLdb._mysql.escape_string(request.POST['latest_utc_ts'].strip())
        
        if not(TP.is_timestamp(latest_utc_ts_var, 1)) and not(TP.is_timestamp(latest_utc_ts_var, 2)):
            raise TypeError

        if latest_utc_ts_var == '':
            latest_utc_ts_var = _end_time_
        
        ts_format = TP.getTimestampFormat(latest_utc_ts_var)
        if ts_format == TP.TS_FORMAT_FORMAT1:
            latest_utc_ts_var = TP.timestamp_convert_format(latest_utc_ts_var, TP.TS_FORMAT_FORMAT1, TP.TS_FORMAT_FLAT)
            
    except KeyError:        
        latest_utc_ts_var = _end_time_
    
    except TypeError:        
        err_msg = 'Please enter a valid end-timestamp.'        
        latest_utc_ts_var = _end_time_


    try:
        
        earliest_utc_ts_var = MySQLdb._mysql.escape_string(request.POST['earliest_utc_ts'].strip())
        
        if not(TP.is_timestamp(earliest_utc_ts_var, 1)) and not(TP.is_timestamp(earliest_utc_ts_var, 2)):
            raise TypeError

        if earliest_utc_ts_var == '':
            earliest_utc_ts_var = _beginning_time_
        
        ts_format = TP.getTimestampFormat(earliest_utc_ts_var)
        if ts_format == TP.TS_FORMAT_FORMAT1:
            earliest_utc_ts_var = TP.timestamp_convert_format(earliest_utc_ts_var, TP.TS_FORMAT_FORMAT1, TP.TS_FORMAT_FLAT)
            
    except KeyError:
        earliest_utc_ts_var = _beginning_time_
    
    except TypeError:        
        err_msg = 'Please enter a valid start-timestamp.'        
        earliest_utc_ts_var = _beginning_time_
        
        
    return err_msg, earliest_utc_ts_var, latest_utc_ts_var