From 55772129088930de0b9190f7c64c0a51cef2a616 Mon Sep 17 00:00:00 2001 From: Sergey Nikitin Date: Thu, 26 Sep 2019 23:42:30 +0400 Subject: [PATCH] Added getting group stats by weeks Change-Id: Ide638159ca0df2faf424d145f31f279c08426d73 --- stackalytics/dashboard/helpers.py | 39 +++++++++++++++++++++++++++++++ stackalytics/dashboard/web.py | 9 +++++++ 2 files changed, 48 insertions(+) diff --git a/stackalytics/dashboard/helpers.py b/stackalytics/dashboard/helpers.py index c47d48df5..065d76338 100644 --- a/stackalytics/dashboard/helpers.py +++ b/stackalytics/dashboard/helpers.py @@ -194,6 +194,45 @@ def get_activity(records, start_record, page_size, query_message=None): return result +def week_to_timestamp(week): + return week * 7 * 24 * 3600 + 3 * 24 * 3600 + + +def get_commits_by_week(records): + raw_weeks = _get_commits_by_week(records) + dict_weeks = [ + {'w': week_to_timestamp(k), 'c': raw_weeks[k]} + for k in raw_weeks + ] + sorted_weeks = sorted(dict_weeks, key=lambda w: w['w']) + + result = { + 'total': sum(raw_weeks.values()), + 'weeks': sorted_weeks} + return result + + +def _get_commits_by_week(record_iterator): + records = list(record_iterator) + weeks = {} + + if len(records) == 0: + return weeks + + max_week = min_week = records[0].week + for record in records: + week = record.week + min_week = min(min_week, week) + max_week = max(max_week, week) + weeks[week] = weeks.get(week, 0) + 1 + + for week in range(min_week, max_week + 1): + if week not in weeks: + weeks[week] = 0 + + return weeks + + def get_contribution_summary(records): marks = dict((m, 0) for m in [-2, -1, 0, 1, 2, 'A', 'WIP', 'x', 's']) commit_count = 0 diff --git a/stackalytics/dashboard/web.py b/stackalytics/dashboard/web.py index 4d6db9d5d..1580aea41 100644 --- a/stackalytics/dashboard/web.py +++ b/stackalytics/dashboard/web.py @@ -266,6 +266,15 @@ def get_activity_json(records, **kwargs): query_message) +@app.route('/api/1.0/commits_by_week') +@decorators.exception_handler() +@decorators.response() +@decorators.jsonify('stats') +@decorators.record_filter() +def get_commits_by_week(records, **kwargs): + return helpers.get_commits_by_week(records) + + @app.route('/api/1.0/contribution') @decorators.exception_handler() @decorators.response()