Exemplo n.º 1
0
def showBookRankGraph(request):
    params = request.GET
    startDateISO = params.get('startDate', None)
    endDateISO = params.get('endDate', None)
    synthesizedParams = {}
    if startDateISO is None or endDateISO is None:
        today = datetime.datetime.today() + datetime.timedelta(1)
        endDate = datetime.datetime.strptime(toYYMMDD(today), _isoFmt)
        startDate = endDate - datetime.timedelta(7)
        startDateISO = toYYMMDD(startDate)
        endDateISO = toYYMMDD(endDate)
        numIntervals = (endDate - startDate).days
    else:
        startDate = awsRequest.parseISODate(startDateISO)
        endDate = awsRequest.parseISODate(endDateISO) + datetime.timedelta(1)
        numIntervals = (endDate - startDate).days
        if numIntervals < 0:
            msg = "The start-date needs to come before the end-date."
        elif numIntervals < 2:
            msg = "The start and end dates must be between 2 and 14 days apart."
        elif numIntervals > 14:
            msg = "The start and end dates must be between 2 and 14 days apart."
        else:
            msg = None
        if msg:
            return showBookRankGraphError(msg, params)
        msg = awsRequest.checkRange(startDate, endDate)
        if msg:
            return showBookRankGraphError(msg, params)
    return showBookRangeGraphHelper(params, synthesizedParams, startDate, endDate, startDateISO, endDateISO, numIntervals)
Exemplo n.º 2
0
def showBookRangeGraphHelper(params, synthesizedParams, startDate, endDate, startDateISO, endDateISO, numIntervals):
    info = awsRequest.Cmds().interval_by_day(False, startDate, endDate, numIntervals)
    class DataTable(object):
        pass
    class ColumnDescriptor(object):
        def __init__(self, columnType, value):
            self.columnType = columnType
            self.value = value
            
        def __str__(self):
            return self.value
        
        __unicode__ = __str__
            
    asin_interval_scores = info['asin_interval_scores']
    date_intervals = info['date_intervals']
    interval_asin_scores = info['interval_asin_scores']
    book_titles_by_asin = info['book_titles_by_asin']
    interval_asin_tweets = info['interval_asin_tweets']
    interval_asin_tweet_percentages = info['interval_asin_tweet_percentages']
    ranked_asins = sortedAsinsByRank(asin_interval_scores)
    dateFormatter = awsRequest.DateFormatterSelector(date_intervals)
    num_asins = len(ranked_asins)
    dataTable = DataTable()
    dataTable.addColumns = ([ColumnDescriptor('string', 'month')]
                            + [ColumnDescriptor('number', book_titles_by_asin[asin])
                               for asin in ranked_asins
                               ])
    rows = []
    for i in range(len(date_intervals)):
        this_interval = date_intervals[i]
        row = (["'%s'" % dateFormatter.format(this_interval)]
            + ["%.3g" % (calcAvg(interval_asin_scores[i].get(this_asin, []))
                         + (10.0 * interval_asin_tweet_percentages[i].get(this_asin, 0)))
               for this_asin in ranked_asins])
        rows.append(row)
    dataTable.addRows = rows
    
    asinPosTweetCounts = dict([(this_asin, []) for this_asin in ranked_asins])
    for i in range(len(date_intervals)):
        for this_asin in ranked_asins:
            tweets = interval_asin_tweets[i].get(this_asin, [])
            asinPosTweetCounts[this_asin].append(len(tweets))
    asinPosCollapsedTweetCounts = []
    for this_asin in ranked_asins:
        asinPosCollapsedTweetCounts.append(",".join([str(a) for a in asinPosTweetCounts[this_asin]]))
    dataTable.asinPosCollapsedTweetCounts = asinPosCollapsedTweetCounts
        
    import pprint
    print "Hey, dataTable.rows...."
    pprint.pprint(dataTable.addRows)
    books = Book.objects.all()
    book_dict = dict([(bookObject.asin, bookObject) for bookObject in books])
    import sys
    sys.stderr.write("medium urls: %s\n" %
                     ", ".join([book_dict[asin].mediumProductImageURL for asin in ranked_asins]))
    prevWeekStartDate = startDate - datetime.timedelta(numIntervals)
    prevWeekEndDate = startDate;
    prevWeekLinkOK = not awsRequest.checkRange(prevWeekStartDate, prevWeekEndDate)
    nextWeekStartDate = endDate
    nextWeekEndDate = endDate + datetime.timedelta(numIntervals)
    nextWeekLinkOK = not awsRequest.checkRange(nextWeekStartDate, nextWeekEndDate)
    return render_to_response("amazon/showBookRankGraph.django.html",
                              { 'book_asins': ranked_asins,
                               'book_titles': [book_titles_by_asin[asin] for asin in ranked_asins],
                               'product_small_urls': [book_dict[asin].smallProductImageURL for asin in ranked_asins],
                               'product_medium_urls': [book_dict[asin].mediumProductImageURL for asin in ranked_asins],
                               'startDate': startDate,
                               'endDate': endDate,
                               'isoDates': [toYYMMDD(dt) for dt in date_intervals],
                               'startDateISO': startDateISO,
                               'endDateISO': endDateISO,
                               'endDateSub1ISO': toYYMMDD(endDate - datetime.timedelta(1)),
                               'dataTable': dataTable,
                               'prevWeekStartDate': toYYMMDD(prevWeekStartDate),
                               'prevWeekEndDate': toYYMMDD(prevWeekEndDate),
                               'prevWeekLinkOK': prevWeekLinkOK,
                               'nextWeekStartDate': toYYMMDD(nextWeekStartDate),
                               'nextWeekEndDate': toYYMMDD(nextWeekEndDate),
                               'nextWeekLinkOK': nextWeekLinkOK,
                               })