Linked MISP - Users with server (nto done for API usage yet)

pull/8/head
Sami Mokaddem 2017-11-15 13:45:01 +01:00
parent c1105aba73
commit b913f86e7b
4 changed files with 223 additions and 86 deletions

View File

@ -201,26 +201,6 @@ def users():
) )
@app.route("/_getUserLogins")
def getUserLogins():
try:
date = datetime.datetime.fromtimestamp(float(request.args.get('date')))
except:
date = datetime.datetime.now()
data = users_helper.getUserLoginsForPunchCard(date)
return jsonify(data)
@app.route("/_getUserLoginsOvertime")
def getUserLoginsOvertime():
try:
date = datetime.datetime.fromtimestamp(float(request.args.get('date')))
except:
date = datetime.datetime.now()
data = users_helper.getUserLoginsOvertime(date)
return jsonify(data)
''' INDEX ''' ''' INDEX '''
@app.route("/_logs") @app.route("/_logs")
@ -457,5 +437,38 @@ def getTrophies():
org = '' org = ''
return jsonify(contributor_helper.getOrgTrophies(org)) return jsonify(contributor_helper.getOrgTrophies(org))
''' USERS '''
@app.route("/_getUserLogins")
def getUserLogins():
try:
date = datetime.datetime.fromtimestamp(float(request.args.get('date')))
except:
date = datetime.datetime.now()
data = users_helper.getUserLoginsForPunchCard(date)
return jsonify(data)
@app.route("/_getUserLoginsOvertime")
def getUserLoginsOvertime():
try:
date = datetime.datetime.fromtimestamp(float(request.args.get('date')))
except:
date = datetime.datetime.now()
data = users_helper.getUserLoginsOvertime(date)
return jsonify(data)
@app.route("/_getTopOrglogin")
def getTopOrglogin():
try:
date = datetime.datetime.fromtimestamp(float(request.args.get('date')))
except:
date = datetime.datetime.now()
data = users_helper.getTopOrglogin(date)
return jsonify(data)
if __name__ == '__main__': if __name__ == '__main__':
app.run(host='localhost', port=8001, threaded=True) app.run(host='localhost', port=8001, threaded=True)

View File

@ -43,6 +43,11 @@
<style> <style>
.highlightDay {
background-color: #edc240;
box-shadow: black 0px 0px 2px;
}
.panel { .panel {
margin-bottom: 10px; margin-bottom: 10px;
} }
@ -110,6 +115,9 @@ small {
<div class="panel panel-default" style=""> <div class="panel panel-default" style="">
<div class="panel-heading bg-info" style="font-weight: bold;"> <div class="panel-heading bg-info" style="font-weight: bold;">
<b>Punch card - login</b> <b>Punch card - login</b>
<strong class='leftSepa textTopHeader' style="float: none; padding: 11px;">Dates:
<input type="text" id="datepickerPunch" size="10" style="">
</strong>
</div> </div>
<div id="panelbody" class="panel-body" style=""> <div id="panelbody" class="panel-body" style="">
<div id="punchcard" style="width:100%; height: 100%;"></div> <div id="punchcard" style="width:100%; height: 100%;"></div>
@ -120,19 +128,25 @@ small {
<div class="col-lg-4"> <div class="col-lg-4">
<div class="panel panel-default" style=""> <div class="panel panel-default" style="">
<div class="panel-heading bg-info" style="font-weight: bold;"> <div class="panel-heading bg-info" style="font-weight: bold;">
<b>Top login per organisation</b> <b>Top organisation login</b>
<strong class='leftSepa textTopHeader' style="float: none; padding: 11px;">Dates:
<input type="text" id="datepickerOrgLogin" size="10" style="">
</strong>
</div> </div>
<div id="panelbody" class="panel-body" style=""> <div id="panelbody" class="panel-body" style="">
<div id="barChart" style="width:100%; height: 20vh;"></div> <div id="pieOrg" style="width:100%; height: 20vh;"></div>
</div> </div>
</div><!-- /.panel-body --> </div><!-- /.panel-body -->
<div class="panel panel-default" style=""> <div class="panel panel-default" style="">
<div class="panel-heading bg-info" style="font-weight: bold;"> <div class="panel-heading bg-info" style="font-weight: bold;">
<b>Contribution after login (API vs non-API)</b> <b>Contribution after login (API vs non-API)</b>
<strong class='leftSepa textTopHeader' style="float: none; padding: 11px;">Dates:
<input type="text" id="datepickerApi" size="10" style="">
</strong>
</div> </div>
<div id="panelbody" class="panel-body" style=""> <div id="panelbody" class="panel-body" style="">
<div id="pieChart" style="width:100%; height: 20vh;"></div> <div id="pieApi" style="width:100%; height: 20vh;"></div>
</div> </div>
</div><!-- /.panel-body --> </div><!-- /.panel-body -->
</div> </div>
@ -142,6 +156,9 @@ small {
<div class="panel panel-default" style=""> <div class="panel panel-default" style="">
<div class="panel-heading bg-info" style="font-weight: bold;"> <div class="panel-heading bg-info" style="font-weight: bold;">
<b>Login overtime</b> <b>Login overtime</b>
<strong class='leftSepa textTopHeader' style="float: none; padding: 11px;">Dates:
<input type="text" id="datepickerOvertimeLogin" size="10" style="">
</strong>
</div> </div>
<div id="panelbody" class="panel-body" style=""> <div id="panelbody" class="panel-body" style="">
<div id="lineChart" style="width:100%; height: 20vh;"></div> <div id="lineChart" style="width:100%; height: 20vh;"></div>
@ -162,79 +179,174 @@ small {
<!-- Index --> <!-- Index -->
<script> <script>
/* URL */ /* URL */
var urlIconCalendar = "{{ url_for('static', filename='pics/calendar.gif') }}";
/* DATA FROM CONF */ /* DATA FROM CONF */
</script> </script>
<script> <script>
$(document).ready(function () { var punchcardWidget;
var pieOrgWidget;
var pieApiWidget;
var overtimeWidget;
$.getJSON( "{{ url_for('getUserLogins') }}", function( data ) { var div_day;
$('#punchcard').punchcard({ function highlight_punchDay() {
data: data, if (!(div_day === undefined)) {
singular: 'login', div_day.removeClass('highlightDay');
plural: 'logins', }
timezones: ['local'],
timezoneIndex:0 var curDay = datePickerWidgetPunch.datepicker( "getDate" ).getDay();
}); if (curDay == 7)
curDay = 0;
div_day = $('[data-daynum="'+curDay+'"]');
div_day.addClass('highlightDay')
}
function updateDatePunch() {
var date = datePickerWidgetPunch.datepicker( "getDate" );
$.getJSON( "{{ url_for('getUserLogins') }}"+"?date="+date.getTime()/1000, function( data ) {
if (!(punchcardWidget === undefined)) {
punchcardWidget.settings.data = data;
punchcardWidget.refresh();
highlight_punchDay();
} else {
punchcardWidget = $('#punchcard').punchcard({
data: data,
singular: 'login',
plural: 'logins',
timezones: ['local'],
timezoneIndex:0
});
punchcardWidget = punchcardWidget.data("plugin_" + "punchcard");
highlight_punchDay();
}
}); });
}
$.getJSON( "{{ url_for('getUserLogins') }}", function( data ) { function updateDatePieOrg() {
data = [{ var date = datePickerWidgetOrg.datepicker( "getDate" );
label: "Contribution with login", $.getJSON( "{{ url_for('getTopOrglogin') }}"+"?date="+date.getTime()/1000, function( data ) {
data: Math.floor(Math.random() * 100) + 1 toPlot = [];
}, for (item of data) { toPlot.push({ label: item[0], data: item[1] }); }
{ if (toPlot.length == 0) {
label: "Contribution without login", toPlot = [{ label: 'No data', data: 100 }];
data: Math.floor(Math.random() * 100) + 1 }
} if (!pieOrgWidget === undefined) {
]; pieOrgWidget.setData(toPlot);
$.plot('#pieChart', data, { pieOrgWidget.draw();
series: { } else {
pie: { pieOrgWidget = $.plot('#pieOrg', toPlot, {
innerRadius: 0.5, series: {
show: true pie: {
innerRadius: 0.5,
show: true
}
} }
} });
}); }
}); });
}
$.getJSON( "{{ url_for('getUserLogins') }}", function( data ) { function updateDatePieApi() {
var date = datePickerWidgetApi.datepicker( "getDate" );
$.getJSON( "{{ url_for('getUserLogins') }}"+"?date="+date.getTime()/1000, function( data ) {
data = [{label: 'org1', data:[ Math.floor(Math.random() * 100) + 1]}, data = [{label: 'org1', data:[ Math.floor(Math.random() * 100) + 1]},
{label: 'org2', data:[ Math.floor(Math.random() * 100) + 1]}, {label: 'org2', data:[ Math.floor(Math.random() * 100) + 1]},
{label: 'org3', data:[ Math.floor(Math.random() * 100) + 1]}] {label: 'org3', data:[ Math.floor(Math.random() * 100) + 1]}]
$.plot('#barChart', data, {
series: {
pie: {
innerRadius: 0.5,
show: true
}
}
});
});
$.getJSON( "{{ url_for('getUserLoginsOvertime') }}", function( data ) { if (!pieApiWidget === undefined) {
pieApiWidget.setData(toPlot);
pieApiWidget.draw();
} else {
pieApiWidget = $.plot('#pieApi', data, {
series: {
pie: {
innerRadius: 0.5,
show: true
}
}
});
}
});
}
function updateDateOvertime() {
var date = datePickerWidgetOvertime.datepicker( "getDate" );
var now = new Date();
if (date.toDateString() == now.toDateString()) {
date = now;
} else {
date.setTime(date.getTime() + (24*60*60*1000-1)); // include data of selected date
}
$.getJSON( "{{ url_for('getUserLoginsOvertime') }}"+"?date="+parseInt(date.getTime()/1000), function( data ) {
temp = []; temp = [];
var i=0; var i=0;
for (item of data) { for (item of data) {
temp.push([new Date(item[0]*1000), item[1]]); temp.push([new Date(item[0]*1000), item[1]]);
} }
console.log(temp);
data = {label: 'Login overtime', data: temp} data = {label: 'Login overtime', data: temp}
$.plot('#lineChart', [data], { if (!overtimeWidget === undefined) {
lines: { overtimeWidget.setData(toPlot);
show: true, overtimeWidget.draw();
steps: true, } else {
fill: true overtimeWidget = $.plot('#lineChart', [data], {
}, lines: {
xaxis: { show: true,
mode: "time", steps: true,
minTickSize: [1, "hour"], fill: true
} },
}); xaxis: {
mode: "time",
minTickSize: [1, "day"],
}
});
}
}); });
}
</script>
<script>
$(document).ready(function () {
var datePickerOptions = {
showOn: "button",
// minDate: -31,
maxDate: 0,
buttonImage: urlIconCalendar,
buttonImageOnly: true,
buttonText: "Select date",
showAnim: "slideDown"
};
// punch
var datePickerOptionsPunch = jQuery.extend({}, datePickerOptions);
datePickerOptionsPunch['onSelect'] = updateDatePunch;
datePickerWidgetPunch = $( "#datepickerPunch" );
datePickerWidgetPunch.datepicker(datePickerOptionsPunch);
datePickerWidgetPunch.datepicker("setDate", new Date());
// org login
var datePickerOptionsOrg = jQuery.extend({}, datePickerOptions);
datePickerOptionsOrg['onSelect'] = updateDatePieOrg;
datePickerWidgetOrg = $( "#datepickerOrgLogin" );
datePickerWidgetOrg.datepicker(datePickerOptionsOrg);
datePickerWidgetOrg.datepicker("setDate", new Date());
// api
var datePickerOptionsApi = jQuery.extend({}, datePickerOptions);
datePickerOptionsApi['onSelect'] = updateDatePieApi;
datePickerWidgetApi = $( "#datepickerApi" );
datePickerWidgetApi.datepicker(datePickerOptionsApi);
datePickerWidgetApi.datepicker("setDate", new Date());
// overtime
var datePickerOptionsOvertime = jQuery.extend({}, datePickerOptions);
datePickerOptionsOvertime['onSelect'] = updateDateOvertime;
datePickerWidgetOvertime = $( "#datepickerOvertimeLogin" );
datePickerWidgetOvertime.datepicker(datePickerOptionsOvertime);
datePickerWidgetOvertime.datepicker("setDate", new Date());
updateDatePunch();
updateDatePieOrg();
updateDatePieApi();
updateDateOvertime();
}); });
</script> </script>

View File

@ -10,18 +10,28 @@ class Users_helper:
self.serv_redis_db = serv_redis_db self.serv_redis_db = serv_redis_db
self.cfg = cfg self.cfg = cfg
def add_user_login(self, timestamp): def add_user_login(self, timestamp, org):
timestampDate = datetime.datetime.fromtimestamp(float(timestamp)) timestampDate = datetime.datetime.fromtimestamp(float(timestamp))
timestampDate_str = util.getDateStrFormat(timestampDate) timestampDate_str = util.getDateStrFormat(timestampDate)
keyname = "{}:{}".format('USER_LOGIN', timestampDate_str)
self.serv_redis_db.sadd(keyname, timestamp) keyname_timestamp = "{}:{}".format('LOGIN_TIMESTAMP', timestampDate_str)
self.serv_redis_db.sadd(keyname_timestamp, timestamp)
keyname_org = "{}:{}".format('LOGIN_ORG', timestampDate_str)
self.serv_redis_db.zincrby(keyname_org, org, 1)
def getUserLogins(self, date): def getUserLogins(self, date):
keyname = "USER_LOGIN:{}" keyname = "LOGIN_TIMESTAMP:{}".format(util.getDateStrFormat(date))
timestamps = self.serv_redis_db.smembers(keyname.format(util.getDateStrFormat(date))) timestamps = self.serv_redis_db.smembers(keyname)
timestamps = [int(timestamp.decode('utf8')) for timestamp in timestamps] timestamps = [int(timestamp.decode('utf8')) for timestamp in timestamps]
return timestamps return timestamps
def getTopOrglogin(self, date, topNum=0):
keyname = "LOGIN_ORG:{}".format(util.getDateStrFormat(date))
data = self.serv_redis_db.zrange(keyname, 0, topNum-1, desc=True, withscores=True)
data = [ [record[0].decode('utf8'), record[1]] for record in data ]
return data
def getUserLoginsForPunchCard(self, date, prev_days=6): def getUserLoginsForPunchCard(self, date, prev_days=6):
week = {} week = {}
for curDate in util.getXPrevDaysSpan(date, prev_days): for curDate in util.getXPrevDaysSpan(date, prev_days):
@ -49,7 +59,8 @@ class Users_helper:
data.append(temp) data.append(temp)
except KeyError: # no data except KeyError: # no data
data.append([0 for x in range(24)]) data.append([0 for x in range(24)])
# swap: punchcard day starts on monday
data = [data[6]]+data[:6]
return data return data
def getUserLoginsOvertime(self, date, prev_days=6): def getUserLoginsOvertime(self, date, prev_days=6):
@ -60,14 +71,14 @@ class Users_helper:
for curDate in util.getXPrevDaysSpan(date, prev_days): for curDate in util.getXPrevDaysSpan(date, prev_days):
timestamps = self.getUserLogins(curDate) timestamps = self.getUserLogins(curDate)
for timestamp in timestamps: # sum occurence during the current hour for timestamp in timestamps: # sum occurence during the current hour
date = datetime.datetime.fromtimestamp(float(timestamp)) dateTimestamp = datetime.datetime.fromtimestamp(float(timestamp))
date = date.replace(minute=0, second=0, microsecond=0) dateTimestamp = dateTimestamp.replace(minute=0, second=0, microsecond=0)
dico_hours[util.getTimestamp(date)] += 1 dico_hours[util.getTimestamp(dateTimestamp)] += 1
# Format data # Format data
data = [] data = []
for date, occ in dico_hours.items(): for curDate, occ in dico_hours.items():
data.append([date, occ]) data.append([curDate, occ])
data.sort(key=lambda x: x[0]) data.sort(key=lambda x: x[0])
return data return data

View File

@ -192,12 +192,13 @@ def handler_keepalive(zmq_name, jsonevent):
def handler_user(zmq_name, jsondata): def handler_user(zmq_name, jsondata):
json_user = jsondata['User'] json_user = jsondata['User']
userID = json_user['id'] userID = json_user['id']
org = userID
try: #only consider user login try: #only consider user login
timestamp = json_user['current_login'] timestamp = json_user['current_login']
except KeyError: except KeyError:
return return
if timestamp != 0: # "invited_by": "xxxx" ??? if timestamp != 0: # "invited_by": "xxxx" ???
users_helper.add_user_login(timestamp) users_helper.add_user_login(timestamp, org)
def handler_conversation(zmq_name, jsonevent): def handler_conversation(zmq_name, jsonevent):
try: #only consider POST, not THREAD try: #only consider POST, not THREAD