From b172126880b06e7a8560b8bc27375f43558d59b8 Mon Sep 17 00:00:00 2001 From: Konark Date: Tue, 6 Jun 2017 23:03:52 +0200 Subject: [PATCH 1/2] adding returning visitors, os, browsers on the dashboard --- web/controllers/metric_detail.py | 104 ++++++++++++++++++++++++++++ web/static/dashboard.js | 26 ++++++- web/templates/dashboard.html | 49 +++++++++++++ web/templates/template_landing.html | 2 +- 4 files changed, 179 insertions(+), 2 deletions(-) diff --git a/web/controllers/metric_detail.py b/web/controllers/metric_detail.py index bc781c2..ee284bf 100644 --- a/web/controllers/metric_detail.py +++ b/web/controllers/metric_detail.py @@ -64,6 +64,109 @@ def report_site_visits(self, args): def report_domains(self, args): return self.get_site_hostnames(args.get('token')) + def report_returning_visitors(self, args): + + s = self.Session() + since = datetime.utcnow() - timedelta(30) + until = datetime.utcnow() + token = args.get('token') + domain = args.get('domain') + options = { + 'site_key': token, + 'since': since, + 'until': until, + 'domain': domain, + } + + query = ''' + SELECT extract(epoch from date_trunc('day', ts)) AS day, + count(message->>'returning_from_ts') + FROM messages WHERE + message->>'returning_from_ts' is not null AND + message->>'type' ='site_visit_by_day' AND + site_id = (SELECT site_id FROM sites WHERE site_key = :site_key) AND + message->>'p' LIKE :domain AND + ts >= :since AND + ts < :until + GROUP BY + day; + ''' + + grouped_counts = s.execute(query,options).fetchall() + return [[int(ts), count] for ts, count in grouped_counts] + + def report_os(self, args): + + s = self.Session() + token = args.get('token') + + options = { + 'site_key': token + } + + query = ''' + select message->>'os' as os, count(1) as count + from messages WHERE + message->>'os' is not null AND + message->>'type'='new_year' AND + site_id = (SELECT site_id FROM sites WHERE site_key = :site_key) + GROUP BY os + ORDER BY count DESC + LIMIT 5 + ''' + + grouped_counts = s.execute(query,options).fetchall() + + return [[ua, count] for ua, count in grouped_counts] + + def report_ua(self, args): + + s = self.Session() + token = args.get('token') + + options = { + 'site_key': token + } + + query = ''' + select message->>'browser' as browser, count(1) as count + from messages WHERE + message->>'browser' is not null AND + message->>'type'='new_year' AND + site_id = (SELECT site_id FROM sites WHERE site_key = :site_key) + GROUP BY browser + ORDER BY count DESC + LIMIT 5 + ''' + + grouped_counts = s.execute(query,options).fetchall() + + return [[ua, count] for ua, count in grouped_counts] + + def report_top_pages(self, args): + + s = self.Session() + token = args.get('token') + domain = 'http%://' + args.get('domain') + '%' + options = { + 'site_key': token, + 'domain': domain + } + + query = ''' + select message->>'p' as p, count(1) as count + from messages WHERE + message->>'type'='page_visit_by_year' AND + message->>'p' LIKE :domain AND + site_id = (SELECT site_id FROM sites WHERE site_key = :site_key) + GROUP BY p + ORDER BY count DESC + LIMIT 5 + ''' + + grouped_counts = s.execute(query,options).fetchall() + + return [[ua, count] for ua, count in grouped_counts] def messages_per_interval(self, token, message_type, interval_size='day', domain=None): @@ -101,6 +204,7 @@ def messages_per_interval(self, token, message_type, grouped_counts = s.execute(query, options).fetchall() + print grouped_counts return [[int(ts), count] for ts, count in grouped_counts] def get_site_hostnames(self, token, since=datetime.utcnow() - timedelta(30), diff --git a/web/static/dashboard.js b/web/static/dashboard.js index 0bd8a96..9276062 100644 --- a/web/static/dashboard.js +++ b/web/static/dashboard.js @@ -35,7 +35,7 @@ $(function () { show: true, barWidth: (response.period == 'daily' ? 24 * 60 * 60 * 1000 : 24 * 60 * 60 * 7 * 1000), fillColor: green, - lineWidth: 1, + lineWidth: 0, align: "center", }; @@ -53,6 +53,30 @@ $(function () { }); }); +// e.click(function() { +// window.location = "/metric_konark/" + e.data('metric') + '/'; +// }) + }); + + + $("div.tabular").each(function (index, elem) { + var e = $(elem); + + var url = "/metric_details?token=" + token + "&name=" + e.data('metric') + "&domain=" + site; + //var url = "http://192.168.5.239/dashboard_json_files/" + e.data('metric') + ".json"; + //var url = "konark.php?url=http://192.168.5.239/dashboard_json_files/" + e.data('metric') + ".json"; + $.getJSON(url, function(response) { + console.log(response); + // table = ``; + table = ''; + response.data.forEach( e => { + table += `${e[0]} : ${e[1]}
`; + }); + + // table += `
`; + e.html(table); + }); + // e.click(function() { // window.location = "/metric_konark/" + e.data('metric') + '/'; // }) diff --git a/web/templates/dashboard.html b/web/templates/dashboard.html index 6fac5c3..88708f3 100644 --- a/web/templates/dashboard.html +++ b/web/templates/dashboard.html @@ -72,6 +72,55 @@

Page views

+ + +
+
+

Daily Returning Visitors

+
+
+ - +
+
Total
+
+ + +
+
+
+ +
+
+

Top Pages

+
+
+ - +
+
+
+
+ +
+
+

OS

+
+
+ - +
+
+
+
+ +
+
+

Browsers

+
+
+ - +
+
+
+
diff --git a/web/templates/template_landing.html b/web/templates/template_landing.html index 64896b7..1a432e8 100644 --- a/web/templates/template_landing.html +++ b/web/templates/template_landing.html @@ -164,7 +164,7 @@

Try it

Accessing Dashboard

-

{{ context.dashboard }}?token={{ context.site_key }} , +

{{ context.dashboard }}?token={{ context.site_key }} ,
If you lose this link, you will have to regenerate the tokens.

Dashboard Token: {{ context.site_key }}

From 4b0163da23e4bb9b7c6626b8d709c04c346af57e Mon Sep 17 00:00:00 2001 From: Konark Date: Tue, 6 Jun 2017 23:44:23 +0200 Subject: [PATCH 2/2] layout 3 widgets a row --- web/templates/dashboard.html | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/web/templates/dashboard.html b/web/templates/dashboard.html index 88708f3..52b66a2 100644 --- a/web/templates/dashboard.html +++ b/web/templates/dashboard.html @@ -42,9 +42,6 @@

Unique Visitors

- - -

Site loads

@@ -58,7 +55,6 @@

Site loads

-

Page views

@@ -71,10 +67,6 @@

Page views

-
- - -

Daily Returning Visitors

@@ -89,12 +81,12 @@

Daily Returning Visitors

+
-

Top Pages

+

OS

-
- - +
@@ -102,10 +94,9 @@

Top Pages

-

OS

+

Browsers

-
- - +
@@ -113,9 +104,9 @@

OS

-

Browsers

+

Top Pages

-
+
-