예제 #1
0
async def get_graph(after='1y'):
    epoch = get_epoch(after)
    db = DB(DB_FILE)
    await db.connect()

    results = {}
    if 'd' in after:
        for i in range(24):
            results[str(i)] = {'good_votes': 0, 'bad_votes': 0}
        data = await db.get_timeline_data(epoch, '%H')
        async for row in data:
            key, good_votes, bad_votes = row
            results[str(int(key))] = {
                'good_votes': good_votes,
                'bad_votes': bad_votes
            }

    elif 'w' in after:
        days_of_week = [
            'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
            'Saturday'
        ]
        for day in days_of_week:
            results[day] = {'good_votes': 0, 'bad_votes': 0}
        data = await db.get_timeline_data(epoch, '%w')
        async for row in data:
            key, good_votes, bad_votes = row
            results[days_of_week[int(key)]] = {
                'good_votes': good_votes,
                'bad_votes': bad_votes
            }

    elif 'M' in after:
        for i in range(31):
            results[str(i)] = {'good_votes': 0, 'bad_votes': 0}
        data = await db.get_timeline_data(epoch, '%d')
        async for row in data:
            key, good_votes, bad_votes = row
            results[str(int(key))] = {
                'good_votes': good_votes,
                'bad_votes': bad_votes
            }

    else:
        months_of_year = [
            'January', 'February', 'March', 'April', 'May', 'June', 'July',
            'August', 'September', 'October', 'November', 'December'
        ]
        for month in months_of_year:
            results[month] = {'good_votes': 0, 'bad_votes': 0}
        data = await db.get_timeline_data(epoch, '%m')
        async for row in data:
            key, good_votes, bad_votes = row
            results[months_of_year[int(key) - 1]] = {
                'good_votes': good_votes,
                'bad_votes': bad_votes
            }

    graph = Graph()
    graph.labels = []
    graph.votes = []
    for key in results:
        good_votes = results[key]['good_votes']
        bad_votes = results[key]['bad_votes']
        graph.labels.append(key)
        votes = Votes()
        votes.good = good_votes
        votes.bad = bad_votes
        graph.votes.append(votes)

    await db.close()
    return graph