Commit bf11cf839a59b5b30feb3b5ca8f87377f3bf6d87

Authored by Felipe Bormann
1 parent 74e082e1

finished date count as well as calendar heatmap, still has tooltip to go and sty…

…le select option above it
analytics/static/analytics/js/behavior.js
1 1  
2 2 $(document).ready(function(){
3 3 selectors_options.init();
  4 +
  5 + //for month selector
  6 +
  7 + $('#month_selector').change(function(){
  8 + $.get('/analytics/amount_active_users_per_day', {month: $(this).val() }).done(function(data){
  9 + console.log(data);
  10 + charts.month_heatmap(data);
  11 +
  12 + });
  13 + });
  14 +
  15 +
4 16 });
5 17  
6 18  
  19 +
7 20 var selectors_options = {
8 21 init: function(){
9 22 selectors = $("div.selector");
... ... @@ -66,3 +79,5 @@ var selectors_options = {
66 79 $(e).removeAttr("opened"); //remove attribute so it can call API again
67 80 },
68 81 };
  82 +
  83 +
... ...
analytics/static/analytics/js/charts.js
... ... @@ -448,6 +448,51 @@ var charts = {
448 448 }
449 449  
450 450 });
  451 + },
  452 + month_heatmap: function(data){
  453 +
  454 + if($('#month-chart').length != 0){
  455 + $('#month-chart').fadeOut();
  456 + $('#month-chart').remove();
  457 + }
  458 + var svg = d3.select('#right-chart-body').append('svg')
  459 + .attr('width', 300)
  460 + .attr('height', 200)
  461 + .attr('id', 'month-chart');
  462 +
  463 + //color range
  464 + var color = d3.scaleLinear().range(["#29c8b8", '#149e91']).domain([0, d3.max(data, function(d){ return d.count; })]);
  465 +
  466 + var rects = svg.selectAll("rect")
  467 + .data(data)
  468 + .enter()
  469 + .append("g");
  470 + rect_width = 40;
  471 + rect_height = 40;
  472 + rects.append("rect")
  473 + .attr("width", rect_width)
  474 + .attr("height", rect_height)
  475 + .attr("class", "day_rect")
  476 + .attr("x", function(d){
  477 +
  478 + return rect_width* (d.day % 7);
  479 + }).attr("y", function(d){
  480 + return rect_height*(Math.floor(d.day / 7));
  481 + }).attr("fill", function(d){
  482 + return color(d.count);
  483 + });
  484 +
  485 + rects.append("text")
  486 + .text( function(d){
  487 + return d.day;
  488 + }).attr("fill", "white")
  489 + .attr("text-anchor", "middle")
  490 + .attr("x", function(d){
  491 + return rect_width* (d.day % 7) + rect_width/2;
  492 + }).attr("y", function(d){
  493 + return rect_height*(Math.floor(d.day / 7)) + rect_height/2;
  494 + });
  495 +
451 496 }
452 497 }
453 498  
... ...
analytics/templates/analytics/general.html
... ... @@ -93,8 +93,18 @@
93 93  
94 94 </div>
95 95 <div class="chart right-chart">
96   -
97   -
  96 + <div>
  97 + <p>{% trans "amount of access in: " %}</p>
  98 + <select id="month_selector">
  99 + {% for month in months %}
  100 + <option>{{month}}</option>
  101 + {% endfor %}
  102 +
  103 + </select>
  104 + </div>
  105 + <div id="right-chart-body">
  106 +
  107 + </div>
98 108 </div>
99 109 </section>
100 110  
... ...
analytics/urls.py
... ... @@ -11,4 +11,5 @@ urlpatterns = [
11 11 url(r'^most_accessed_categories/$', views.most_accessed_categories, name = "most_accessed_categories"),
12 12 url(r'^most_accessed_resources/$', views.most_accessed_resource_kind, name= "most_accessed_resources"),
13 13 url(r'^most_active_users/$', views.most_active_users, name= "most_active_users"),
  14 + url(r'^amount_active_users_per_day/$', views.most_active_users_in_a_month, name="most_active_users_in_a_month")
14 15 ]
15 16 \ No newline at end of file
... ...
analytics/views.py
... ... @@ -13,6 +13,9 @@ import operator
13 13 from django.utils.translation import ugettext_lazy as _
14 14 from django.shortcuts import render, get_object_or_404, redirect
15 15  
  16 +from datetime import date
  17 +import calendar
  18 +
16 19  
17 20 class GeneralView(generic.TemplateView):
18 21 template_name = "analytics/general.html"
... ... @@ -26,6 +29,9 @@ class GeneralView(generic.TemplateView):
26 29  
27 30 def get_context_data(self, **kwargs):
28 31 context = {}
  32 +
  33 + context['months'] = [_('January'), _('February'), _('March'), _('April'), _('May'), _('June'), _('July'), _('August'),
  34 + _('September'), _('October'), _('November'), _('December')]
29 35  
30 36 return context
31 37  
... ... @@ -55,8 +61,23 @@ def most_used_tags(request):
55 61 return JsonResponse(data, safe= False)
56 62  
57 63  
58   -def heatmap(request):
59   - return None
  64 +def most_active_users_in_a_month(request):
  65 + params = request.GET
  66 + days = get_days_of_the_month(params['month'])
  67 + print(days)
  68 + data = {}
  69 + mappings = {_('January'): 1, _('February'): 2, _('March'): 3, _('April'): 4, _('May'): 5, _('June'): 6, _('July'): 7
  70 + , _('August'): 8, _('September'): 9, _('October'): 10, _('November'): 11, _('December'): 12}
  71 +
  72 +
  73 + for day in days:
  74 + built_date = date(date.today().year, mappings[params['month']], day)
  75 + print(built_date)
  76 + day_count = Log.objects.filter(datetime__date = built_date).count()
  77 + data[day] = day_count
  78 +
  79 + data = [{"day": day, "count": day_count} for day, day_count in data.items()]
  80 + return JsonResponse(data, safe=False)
60 81  
61 82  
62 83  
... ... @@ -136,4 +157,25 @@ def most_active_users(request):
136 157 user_object = User.objects.get(id=user['user_id'])
137 158 user['image'] = user_object.image_url
138 159 user['user'] = user_object.social_name
139   - return JsonResponse(fifty_users, safe=False)
140 160 \ No newline at end of file
  161 + return JsonResponse(fifty_users, safe=False)
  162 +
  163 +
  164 +def get_days_of_the_month(month):
  165 +
  166 + #get current year
  167 + year = date.today().year
  168 + print(year)
  169 + print(month)
  170 + mappings = {_('January'): 1, _('February'): 2, _('March'): 3, _('April'): 4, _('May'): 5, _('June'): 6, _('July'): 7
  171 + , _('August'): 8, _('September'): 9, _('October'): 10, _('November'): 11, _('December'): 12}
  172 +
  173 + c = calendar.Calendar()
  174 + days = c.itermonthdays(year, mappings[month])
  175 + days_set = set()
  176 + for day in days:
  177 + days_set.add(day)
  178 +
  179 + days_set.remove(0) #because 0 is not aan actual day from that month
  180 + return days_set
  181 +
  182 +
... ...