Exemplo n.º 1
0
Arquivo: ajax.py Projeto: mpresh/rf
def analytics_clicks_reshares_bar(req):

    dict_vals = {}
    SPLITS = 10

    date_regex = "\d\d/\d\d/\d\d\d\d"
    try:
        mo = re.match(date_regex, req.GET['end'])
        if mo != None:
            (month, day, year) = req.GET['end'].split('/')
            end = datetime(int(year),int(month),int(day))
        end
    except:
        today = datetime.now()
        end = datetime(today.year,today.month,today.day)    

    end = end + timedelta(days=1)

    try:
        mo = re.match(date_regex, req.GET['start'])
        if mo != None:
            (month, day, year) = req.GET['start'].split('/')
            start = datetime(int(year),int(month),int(day))
        start
    except:
        start = datetime('2010','01','01')

    try:
        data_type = req.GET['data_type']
    except:
        data_type = "total"

    try:
        abs_per = req.GET['abs_per']
    except:
        abs_per = "absolute"

    try:
        customer_id = req.GET["customer_id"] 
    except:
        customer_id = None

    shares = Share.objects.all()

    if customer_id:
        shares = shares.filter(campaign=customer_id)

    shares = shares.exclude(created_at__gte=end)
    shares = shares.exclude(created_at__lte=start)

    data = {}
    column_list = []
    column_list.append({"id" : "range_type", "label" : "Time", "type" : "string"})
    if abs_per == "absolute":
        column_list.append({"id" : "shares", "label" : "Shares", "type" : "number"})
        column_list.append({"id" : "clicks", "label" : "Clicks", "type" : "number"})
        column_list.append({"id" : "reshares", "label" : "Reshares", "type" : "number"})
    else:
        column_list.append({"id" : "clicks_percent", "label" : "Clicks_percent", "type" : "number"})
    data["cols"] = column_list

    td = end - start
    diff_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 

    inc_diff = diff_seconds / SPLITS
    temp_end = start
    temp_start = start

    rows_list = []
    for incr in range(0, SPLITS):
        temp_shares = shares.exclude(created_at__lte=temp_start)
        temp_end = temp_start + timedelta(seconds=inc_diff)

        temp_shares = temp_shares.exclude(created_at__gte=temp_end)

        label = analytics_util.getChartLabel(temp_start, temp_end, SPLITS, start, end)

        temp_start = temp_end

        temp_shares_facebook = temp_shares.filter(from_account_type="F")
        temp_shares_facebook_reach = 0
        temp_shares_facebook_clicks = 0
        temp_shares_facebook_count = temp_shares_facebook.count()

        temp_reshares_facebook = 0
        for share in temp_shares_facebook:
            temp_shares_facebook_reach += share.reach
            temp_shares_facebook_clicks += share.page_views
            temp_reshares_facebook += share.children().count()

        temp_shares_twitter = temp_shares.filter(from_account_type="T")
        temp_shares_twitter_reach = 0
        temp_shares_twitter_clicks = 0
        temp_shares_twitter_count = temp_shares_twitter.count()

        temp_reshares_twitter = 0
        
        for share in temp_shares_twitter:
            temp_shares_twitter_reach += share.reach
            temp_shares_twitter_clicks += share.page_views
            temp_reshares_twitter += share.children().count()

        if data_type == "total":
            reach = temp_shares_twitter_reach + temp_shares_facebook_reach
            clicks = temp_shares_twitter_clicks + temp_shares_facebook_clicks
            shares_count = temp_shares_twitter_count + temp_shares_facebook_count
            reshares = temp_reshares_twitter + temp_reshares_facebook
        elif data_type == "twitter":
            reach = temp_shares_twitter_reach
            clicks = temp_shares_twitter_clicks
            shares_count = temp_shares_twitter_count
            reshares = temp_reshares_twitter
        else:
            reach = temp_shares_facebook_reach
            clicks = temp_shares_facebook_clicks
            shares_count = temp_shares_facebook_count
            reshares = temp_reshares_facebook

        if clicks == 0:
            clicks_percentage = 0
        else:
            clicks_percentage = (clicks / (reach * 1.0)) * 100
    

        if abs_per == "absolute":
            rows_list.append({"c" : [{"v" : label},
                                     {"v" : shares_count},
                                     {"v" : clicks},
                                     {"v" : reshares}]
                         })
        else:
            rows_list.append({"c" : [{"v" : label},
                                     {"v" : clicks_percentage}]
                         })

        
    data["rows"] = rows_list
    dict_vals["status"] = 200
    dict_vals["data"] = data
    return HttpResponse(json.dumps(dict_vals))
Exemplo n.º 2
0
Arquivo: ajax.py Projeto: mpresh/rf
def analytics_campaignscreated_line(req):

    dict_vals = {}
    SPLITS = 10

    date_regex = "\d\d/\d\d/\d\d\d\d"
    try:
        mo = re.match(date_regex, req.GET['end'])
        if mo != None:
            (month, day, year) = req.GET['end'].split('/')
            end = datetime(int(year),int(month),int(day))
        end
    except:
        today = datetime.now()
        end = datetime(today.year,today.month,today.day)    

    end = end + timedelta(days=1)

    try:
        mo = re.match(date_regex, req.GET['start'])
        if mo != None:
            (month, day, year) = req.GET['start'].split('/')
            start = datetime(int(year),int(month),int(day))
        start
    except:
        start = datetime('2010','01','01')

    try:
        customer_id = req.GET["customer_id"] 
    except:
        customer_id = None

    campaigns = Campaign.objects.all()

    if customer_id:
        campaigns = campaigns.filter(campaign=customer_id)

    campaigns = campaigns.exclude(created_at__gte=end)
    campaigns = campaigns.exclude(created_at__lte=start)

    data = {}
    column_list = []
    column_list.append({"id" : "range_type", "label" : "Time", "type" : "string"})
    column_list.append({"id" : "twitter", "label" : "Raffle", "type" : "number"})
    column_list.append({"id" : "facebook", "label" : "Discount", "type" : "number"})
    data["cols"] = column_list

    td = end - start
    diff_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 

    inc_diff = diff_seconds / SPLITS
    temp_end = start
    temp_start = start

    rows_list = []

    for incr in range(0, SPLITS):
        temp_camps = campaigns.exclude(created_at__lte=temp_start)
        temp_end = temp_start + timedelta(seconds=inc_diff)
        temp_camps = temp_camps.exclude(created_at__gte=temp_end)

        label = analytics_util.getChartLabel(temp_start, temp_end, SPLITS, start, end)

        temp_start = temp_end
        temp_camps_raffle = temp_camps.filter(campaign_type="raffle").count()
        temp_camps_discount = temp_camps.filter(campaign_type="discount").count()

        rows_list.append({"c" : [{"v" : label},
                                 {"v" : temp_camps_raffle},
                                 {"v" : temp_camps_discount}]
                         })

        
    data["rows"] = rows_list
    dict_vals["status"] = 200
    dict_vals["data"] = data
    return HttpResponse(json.dumps(dict_vals))