From c959ff5ba93462647550ed54ddf992fc76593858 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 03 2016 20:34:19 +0000 Subject: Add an API endpoint to retrieve stats about an user's activity --- diff --git a/pagure/api/user.py b/pagure/api/user.py index 9280052..33f9123 100644 --- a/pagure/api/user.py +++ b/pagure/api/user.py @@ -8,6 +8,8 @@ """ +import datetime + import flask import pagure @@ -104,3 +106,116 @@ def api_view_user(username): jsonout = flask.jsonify(output) jsonout.status_code = httpcode return jsonout + + + +@API.route('/user//activity/stats') +@api_method +def api_view_user_activity_stats(username): + """ + User activity + ------------- + Use this endpoint to retrieve activity information about a specific user + over the last year. + + :: + + GET /api/0/user//activity/stats + + :: + + GET /api/0/user/ralph/activity/stats + + Parameters + ^^^^^^^^^^ + + +---------------+----------+--------------+----------------------------+ + | Key | Type | Optionality | Description | + +===============+==========+==============+============================+ + | ``username`` | string | Mandatory | | The username of the user | + | | | | whose activity you are | + | | | | interested in. | + +---------------+----------+--------------+----------------------------+ + | ``format`` | string | Optional | | Allows changing the | + | | | | of the date/time returned| + | | | | can be: `timestamp` | + +---------------+----------+--------------+----------------------------+ + + + Sample response + ^^^^^^^^^^^^^^^ + + :: + + { + "2016-05-04 00:00:00": 5, + "2016-05-09 00:00:00": 4, + "2016-05-28 00:00:00": 1, + "2016-06-27 00:00:00": 4, + "2016-08-06 00:00:00": 2, + "2016-08-08 00:00:00": 5, + "2016-08-09 00:00:00": 41, + "2016-08-12 00:00:00": 36, + "2016-08-30 00:00:00": 1, + "2016-09-12 00:00:00": 1, + "2016-09-13 00:00:00": 1, + "2016-09-18 00:00:00": 3, + "2016-09-30 00:00:00": 2, + "2016-10-03 00:00:00": 6, + "2016-10-04 00:00:00": 7, + "2016-10-06 00:00:00": 1, + "2016-10-13 00:00:00": 11, + "2016-10-17 00:00:00": 1, + "2016-10-20 00:00:00": 5 + } + + or:: + + { + "1462312800": 5, + "1462744800": 4, + "1464386400": 1, + "1466978400": 4, + "1470434400": 2, + "1470607200": 5, + "1470693600": 41, + "1470952800": 36, + "1472508000": 1, + "1473631200": 1, + "1473717600": 1, + "1474149600": 3, + "1475186400": 2, + "1475445600": 6, + "1475532000": 7, + "1475704800": 1, + "1476309600": 11, + "1476655200": 1, + "1476914400": 5 + } + + """ + httpcode = 200 + + date_format = flask.request.args.get('format') + + user = pagure.lib.search_user(SESSION, username=username) + if not user: + raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOUSER) + + stats = pagure.lib.get_yearly_stats_user( + SESSION, user, datetime.datetime.utcnow().date() + ) + def format_date(d): + if date_format == 'timestamp': + d = d.strftime('%s') + else: + d = d.strftime('%Y-%m-%d %H:%M:%S') + return d + stats = [ + (format_date(d[0]), d[1]) + for d in stats + ] + + jsonout = flask.jsonify(stats) + jsonout.status_code = httpcode + return jsonout