From 9daab31535639aaf0d3d3ddcd0c5ba2b2b98ed9f Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Tue, 21 Nov 2017 11:59:07 +0100 Subject: [PATCH] feature: Added contribution overtime in login overtime chart --- server.py | 10 ++++++++++ static/js/users.js | 22 +++++++++++++++++----- templates/users.html | 4 ++-- users_helper.py | 24 +++++++++++++++++++++--- util.py | 12 ++++++++++++ 5 files changed, 62 insertions(+), 10 deletions(-) diff --git a/server.py b/server.py index e17302b..359a01d 100755 --- a/server.py +++ b/server.py @@ -480,6 +480,16 @@ def getLoginVSCOntribution(): data = users_helper.getLoginVSCOntribution(date) return jsonify(data) +@app.route("/_getUserLoginsAndContribOvertime") +def getUserLoginsAndContribOvertime(): + try: + date = datetime.datetime.fromtimestamp(float(request.args.get('date'))) + except: + date = datetime.datetime.now() + + data = users_helper.getUserLoginsAndContribOvertime(date) + return jsonify(data) + if __name__ == '__main__': app.run(host='localhost', port=8001, threaded=True) diff --git a/static/js/users.js b/static/js/users.js index 0c04401..9bc625a 100644 --- a/static/js/users.js +++ b/static/js/users.js @@ -123,13 +123,25 @@ function updateDateOvertime() { } else { date.setTime(date.getTime() + (24*60*60*1000-1)); // include data of selected date } - $.getJSON( url_getUserLoginsOvertime+"?date="+parseInt(date.getTime()/1000), function( data ) { - temp = []; + $.getJSON( url_getUserLoginsAndContribOvertime+"?date="+parseInt(date.getTime()/1000), function( data ) { + console.log(data); + data_log = data['login']; + data_contrib = data['contrib']; + temp_log = []; var i=0; - for (item of data) { - temp.push([new Date(item[0]*1000), item[1]]); + for (item of data_log) { + var date = new Date(item[0]*1000); + date = new Date(date.valueOf() - date.getTimezoneOffset() * 60000); // center the data around the day + temp_log.push([date, item[1]]); } - toPlot = [{label: 'Login overtime', data: temp}]; + temp_contrib= []; + var i=0; + for (item of data_contrib) { + var date = new Date(item[0]*1000); + date = new Date(date.valueOf() - date.getTimezoneOffset() * 60000); // center the data around the day + temp_contrib.push([date, item[1]]); + } + toPlot = [{label: 'Login', data: temp_log}, {label: 'Contribution', data: temp_contrib}]; if (!(overtimeWidget === undefined)) { overtimeWidget.setData(toPlot); overtimeWidget.setupGrid(); diff --git a/templates/users.html b/templates/users.html index d7ad981..5b59634 100644 --- a/templates/users.html +++ b/templates/users.html @@ -166,7 +166,7 @@ small {
- Login overtime + Login and contribution overtime Dates: @@ -194,7 +194,7 @@ small { var url_getUserLogins = "{{ url_for('getUserLogins') }}"; var url_getTopOrglogin = "{{ url_for('getTopOrglogin') }}"; var url_getLoginVSCOntribution = "{{ url_for('getLoginVSCOntribution') }}"; - var url_getUserLoginsOvertime = "{{ url_for('getUserLoginsOvertime') }}"; + var url_getUserLoginsAndContribOvertime = "{{ url_for('getUserLoginsAndContribOvertime') }}"; /* DATA FROM CONF */ diff --git a/users_helper.py b/users_helper.py index badf901..e70cd5e 100644 --- a/users_helper.py +++ b/users_helper.py @@ -136,13 +136,22 @@ class Users_helper: data = [data[6]]+data[:6] return data - def getUserLoginsOvertime(self, date, prev_days=6): + def getUserLoginsAndContribOvertime(self, date, prev_days=6): + dico_hours_contrib = {} dico_hours = {} for curDate in util.getXPrevHoursSpan(date, prev_days*24): dico_hours[util.getTimestamp(curDate)] = 0 # populate with empty data + dico_hours_contrib[util.getTimestamp(curDate)] = 0 # populate with empty data for curDate in util.getXPrevDaysSpan(date, prev_days): timestamps = self.getUserLogins(curDate) + keyname = "CONTRIB_DAY:{}".format(util.getDateStrFormat(curDate)) + + orgs_contri = self.serv_redis_db.zrange(keyname, 0, -1, desc=True, withscores=False) + orgs_contri_num = len(orgs_contri) + for curDate in util.getHoursSpanOfDate(curDate, adaptToFitCurrentTime=True): #fill hole day + dico_hours_contrib[util.getTimestamp(curDate)] = orgs_contri_num + for timestamp in timestamps: # sum occurence during the current hour dateTimestamp = datetime.datetime.fromtimestamp(float(timestamp)) dateTimestamp = dateTimestamp.replace(minute=0, second=0, microsecond=0) @@ -152,9 +161,18 @@ class Users_helper: pass # Format data + # login + to_ret = {} data = [] for curDate, occ in dico_hours.items(): data.append([curDate, occ]) - data.sort(key=lambda x: x[0]) - return data + to_ret['login'] = data + # contrib + data = [] + for curDate, occ in dico_hours_contrib.items(): + data.append([curDate, occ]) + data.sort(key=lambda x: x[0]) + to_ret['contrib'] = data + + return to_ret diff --git a/util.py b/util.py index 1b79a40..c22e539 100644 --- a/util.py +++ b/util.py @@ -35,6 +35,18 @@ def getXPrevHoursSpan(date, hours): to_return.append(de - datetime.timedelta(hours=i)) return to_return +def getHoursSpanOfDate(date, adaptToFitCurrentTime=True, daySpanned=6): + ds = date + ds = ds.replace(hour=0, minute=0, second=0, microsecond=0) + to_return = [] + now = datetime.datetime.now() + for i in range(0, 24): + the_date = ds + datetime.timedelta(hours=i) + if the_date > now or the_date < now - datetime.timedelta(days=daySpanned): # avoid going outside + continue + to_return.append(the_date) + return to_return + def getDateStrFormat(date): return str(date.year)+str(date.month).zfill(2)+str(date.day).zfill(2)