예제 #1
0
def fetch_ratings():
    """
    fetches ratings from the database, or from the API if they don't exist
    """

    #if the DB is empty, fetch the data
    if (Rating.select().count() == 0):
        clean_and_update_ratings(clean=False)

    print('getting ratings from DB')
    date_labels, average_axis, count_axis = format_averages_for_chartjs(
        calc_ratings_over_time(
            request.args.get('date_filter', 'all')
        )
    )
    print('returning data')

    current_average = udemy_api.get_current_average()
    return jsonify({
        'chart_data': {
            'date_labels': date_labels,
            'average_axis': average_axis,
            'count_axis': count_axis,
        },
        'current_average': current_average,
    })
예제 #2
0
def get_averages_over_time_sql(date_filter='all'):
    """
    returns ( year-month-day-hour, num_ratings, total_rating ) for that hour
    to be processed elsewhere
    """
    date_expr = get_date_filter_expr(date_filter)

    ratings = Rating.select(
        peewee.fn.strftime('%Y-%m-%d-%H', Rating.created),
        peewee.fn.Count(Rating.rating_score),
        peewee.fn.Sum(Rating.rating_score)
    ) \
    .where(date_expr) \
    .order_by(Rating.created) \
    .group_by(
        peewee.fn.strftime('%Y-%m-%d-%H', Rating.created)
    ).tuples()

    ratings = list(map(lambda data: RatingAggregate(*data), ratings))

    return ratings