Commit 89aa5e15dd2c603a57e3d377a7068c1e7afeb226
1 parent
59f8fd90
Exists in
master
and in
2 other branches
monthly chart is updating by category and filter by month as well inside category view
Showing
6 changed files
with
154 additions
and
45 deletions
Show diff stats
analytics/views.py
@@ -64,15 +64,22 @@ def most_active_users_in_a_month(request): | @@ -64,15 +64,22 @@ def most_active_users_in_a_month(request): | ||
64 | for day in days: | 64 | for day in days: |
65 | built_date = date(int(year), mappings[_(month)], day) | 65 | built_date = date(int(year), mappings[_(month)], day) |
66 | days_list.append(built_date) | 66 | days_list.append(built_date) |
67 | - data = activity_in_timestamp(days_list) | 67 | + data = activity_in_timestamp(days_list, params = params) |
68 | data = [{"day": day.day, "count": day_count} for day, day_count in data.items()] | 68 | data = [{"day": day.day, "count": day_count} for day, day_count in data.items()] |
69 | return JsonResponse(data, safe=False) | 69 | return JsonResponse(data, safe=False) |
70 | 70 | ||
71 | 71 | ||
72 | -def activity_in_timestamp(days): | 72 | +def activity_in_timestamp(days, **kwargs): |
73 | data = {} | 73 | data = {} |
74 | + params = kwargs.get('params') | ||
75 | + print(params) | ||
74 | for day in days: | 76 | for day in days: |
75 | - day_count = Log.objects.filter(datetime__date = day).count() | 77 | + if params.get('category_id'): |
78 | + category_id = params['category_id'] | ||
79 | + day_count = Log.objects.filter(datetime__date = day, context__contains = {"category_id" : int(category_id)}).count() | ||
80 | + | ||
81 | + else: | ||
82 | + day_count = Log.objects.filter(datetime__date = day).count() | ||
76 | data[day] = day_count | 83 | data[day] = day_count |
77 | 84 | ||
78 | return data | 85 | return data |
@@ -184,10 +191,12 @@ def most_active_users(request): | @@ -184,10 +191,12 @@ def most_active_users(request): | ||
184 | 191 | ||
185 | 192 | ||
186 | def get_days_of_the_week_log(request): | 193 | def get_days_of_the_week_log(request): |
187 | - date = request.GET['date'] | 194 | + |
195 | + params = request.GET | ||
196 | + date = params['date'] | ||
188 | date = datetime.strptime( date, '%m/%d/%Y',) | 197 | date = datetime.strptime( date, '%m/%d/%Y',) |
189 | days = get_days_of_the_week(date) | 198 | days = get_days_of_the_week(date) |
190 | - data = activity_in_timestamp(days) | 199 | + data = activity_in_timestamp(days, params = params) |
191 | #mapping of number to days | 200 | #mapping of number to days |
192 | mapping = {0: _("Mon"), 1: _("Tue"), 2: _("Wed"), 3: _("Thu"), 4: _("Fri"), 5: _("Sat"), 6: _("Sun")} | 201 | mapping = {0: _("Mon"), 1: _("Tue"), 2: _("Wed"), 3: _("Thu"), 4: _("Fri"), 5: _("Sat"), 6: _("Sun")} |
193 | data = [{"day": mapping[day.weekday()], "count": day_count} for day, day_count in data.items()] | 202 | data = [{"day": mapping[day.weekday()], "count": day_count} for day, day_count in data.items()] |
dashboards/static/dashboards/js/behavior.js
@@ -7,28 +7,6 @@ $(document).ready(function(){ | @@ -7,28 +7,6 @@ $(document).ready(function(){ | ||
7 | charts.most_used_tags('/analytics/most_used_tags'); | 7 | charts.most_used_tags('/analytics/most_used_tags'); |
8 | charts.build_bubble_user('/analytics/most_active_users/'); | 8 | charts.build_bubble_user('/analytics/most_active_users/'); |
9 | 9 | ||
10 | - | ||
11 | - $('#month_selector').change(function(){ | ||
12 | - | ||
13 | - var date = $(this).val().split("/"); | ||
14 | - $.get('/analytics/amount_active_users_per_day', {month: date[0], year: date[1] }).done(function(data){ | ||
15 | - charts.month_heatmap(data, '#right-chart-body', 'month-chart'); | ||
16 | - | ||
17 | - }); | ||
18 | - }); | ||
19 | - //week date selector at the right-chart field | ||
20 | - $('input.datepicker').datetimepicker({ | ||
21 | - format: 'L', | ||
22 | - defaultDate: new Date(), | ||
23 | - }).on('dp.change', function(ev){ | ||
24 | - new_date = new Date(ev.date); | ||
25 | - var date = (new_date.getMonth() + 1) + '/' + new_date.getDate() + '/' + new_date.getFullYear(); | ||
26 | - $.get('/analytics/get_days_of_the_week_log', {date: date}).done(function(data){ | ||
27 | - charts.month_heatmap(data, '#bottom-right-chart-body', 'weekly-chart'); | ||
28 | - }); | ||
29 | - | ||
30 | - }); | ||
31 | - | ||
32 | 10 | ||
33 | //first call to month selector | 11 | //first call to month selector |
34 | var month = new Array(); | 12 | var month = new Array(); |
@@ -56,6 +34,28 @@ $(document).ready(function(){ | @@ -56,6 +34,28 @@ $(document).ready(function(){ | ||
56 | }); | 34 | }); |
57 | 35 | ||
58 | 36 | ||
37 | + //update month heatmap when the month selector is changed | ||
38 | + $('#month_selector').change(function(){ | ||
39 | + | ||
40 | + var date = $(this).val().split("/"); | ||
41 | + $.get('/analytics/amount_active_users_per_day', {month: date[0], year: date[1] }).done(function(data){ | ||
42 | + charts.month_heatmap(data, '#right-chart-body', 'month-chart'); | ||
43 | + | ||
44 | + }); | ||
45 | + }); | ||
46 | + | ||
47 | + //week date selector at the right-chart field | ||
48 | + $('input.datepicker').datetimepicker({ | ||
49 | + format: 'L', | ||
50 | + defaultDate: new Date(), | ||
51 | + }).on('dp.change', function(ev){ | ||
52 | + new_date = new Date(ev.date); | ||
53 | + var date = (new_date.getMonth() + 1) + '/' + new_date.getDate() + '/' + new_date.getFullYear(); | ||
54 | + $.get('/analytics/get_days_of_the_week_log', {date: date}).done(function(data){ | ||
55 | + charts.month_heatmap(data, '#bottom-right-chart-body', 'weekly-chart'); | ||
56 | + }); | ||
57 | + | ||
58 | + }); | ||
59 | 59 | ||
60 | }); | 60 | }); |
61 | 61 |
dashboards/static/dashboards/js/behavior_categories.js
@@ -3,8 +3,58 @@ $(document).ready(function(){ | @@ -3,8 +3,58 @@ $(document).ready(function(){ | ||
3 | 3 | ||
4 | $("#category-selector").on("change", function(e){ | 4 | $("#category-selector").on("change", function(e){ |
5 | //when it changes, the tag chart is replaced and all others are. | 5 | //when it changes, the tag chart is replaced and all others are. |
6 | - category_id = $(e.target).val(); | 6 | + var category_id = $(e.target).val(); |
7 | charts.most_used_tags('/analytics/get_category_tags/?category_id='+category_id); | 7 | charts.most_used_tags('/analytics/get_category_tags/?category_id='+category_id); |
8 | + | ||
9 | + | ||
10 | + //first call to month selector | ||
11 | + var month = new Array(); | ||
12 | + month[0] = "January"; | ||
13 | + month[1] = "February"; | ||
14 | + month[2] = "March"; | ||
15 | + month[3] = "April"; | ||
16 | + month[4] = "May"; | ||
17 | + month[5] = "June"; | ||
18 | + month[6] = "July"; | ||
19 | + month[7] = "August"; | ||
20 | + month[8] = "September"; | ||
21 | + month[9] = "October"; | ||
22 | + month[10] = "November"; | ||
23 | + month[11] = "December"; | ||
24 | + | ||
25 | + | ||
26 | + $.get('/analytics/amount_active_users_per_day', { month: month[(new Date()).getMonth()], category_id: category_id }).done(function(data){ | ||
27 | + charts.month_heatmap(data, '#right-side-heatmaps', 'month-chart'); | ||
28 | + }); | ||
29 | + | ||
8 | }); | 30 | }); |
9 | -}); | ||
10 | 31 | ||
32 | + | ||
33 | + | ||
34 | + | ||
35 | + | ||
36 | + $('#month_selector').change(function(){ | ||
37 | + | ||
38 | + var date = $(this).val().split("/"); | ||
39 | + $.get('/analytics/amount_active_users_per_day', {month: date[0], year: date[1], category_id: $("#category-selector").val() }).done(function(data){ | ||
40 | + charts.month_heatmap(data, '#right-side-heatmaps', 'month-chart'); | ||
41 | + | ||
42 | + }); | ||
43 | + }); | ||
44 | + | ||
45 | + //week date selector at the right-chart field | ||
46 | + $('input.datepicker').datetimepicker({ | ||
47 | + format: 'L', | ||
48 | + defaultDate: new Date(), | ||
49 | + }).on('dp.change', function(ev){ | ||
50 | + new_date = new Date(ev.date); | ||
51 | + var date = (new_date.getMonth() + 1) + '/' + new_date.getDate() + '/' + new_date.getFullYear(); | ||
52 | + $.get('/analytics/get_days_of_the_week_log', {date: date}).done(function(data){ | ||
53 | + charts.month_heatmap(data, '#bottom-right-chart-body', 'weekly-chart'); | ||
54 | + }); | ||
55 | + | ||
56 | + }); | ||
57 | + | ||
58 | + | ||
59 | + | ||
60 | +}); |
dashboards/static/dashboards/js/logbehavior.js
@@ -70,7 +70,9 @@ var log = { | @@ -70,7 +70,9 @@ var log = { | ||
70 | "limit": 20, //maximum number of elements per page | 70 | "limit": 20, //maximum number of elements per page |
71 | });*/ | 71 | });*/ |
72 | 72 | ||
73 | - $('#log-table').DataTable(); | 73 | + $('#log-table').DataTable({ |
74 | + "pageLength": 100 | ||
75 | + }); | ||
74 | 76 | ||
75 | }, | 77 | }, |
76 | } | 78 | } |
dashboards/templates/dashboards/category.html
@@ -3,13 +3,16 @@ | @@ -3,13 +3,16 @@ | ||
3 | {% load static i18n pagination %} | 3 | {% load static i18n pagination %} |
4 | {% load django_bootstrap_breadcrumbs %} | 4 | {% load django_bootstrap_breadcrumbs %} |
5 | {% block style %} | 5 | {% block style %} |
6 | - <link rel="stylesheet" type="text/css" href="{% static 'dashboards/css/general.css' %}"> | ||
7 | - <link rel="stylesheet" type="text/css" href="{% static 'dashboards/css/dashboards_category.css' %}"> | 6 | + {% for file in style_files %} |
7 | + <link rel="stylesheet" type="text/css" href="{% static file %}"> | ||
8 | + {% endfor %} | ||
8 | {% endblock style %} | 9 | {% endblock style %} |
9 | 10 | ||
10 | {% block javascript %} | 11 | {% block javascript %} |
11 | - <script type="text/javascript" src="{% static "analytics/js/charts.js" %}"></script> | ||
12 | - <script type="text/javascript" src="{% static "dashboards/js/behavior_categories.js" %}"></script> | 12 | + |
13 | + {% for file in javascript_files %} | ||
14 | + <script type="text/javascript" src="{% static file %}"></script> | ||
15 | + {% endfor %} | ||
13 | {% endblock javascript %} | 16 | {% endblock javascript %} |
14 | 17 | ||
15 | {% block breadcrumbs %} | 18 | {% block breadcrumbs %} |
@@ -27,14 +30,18 @@ | @@ -27,14 +30,18 @@ | ||
27 | <nav> | 30 | <nav> |
28 | <ul class="core-subjects-options report-menu-choice"> | 31 | <ul class="core-subjects-options report-menu-choice"> |
29 | {% if user.is_staff %} | 32 | {% if user.is_staff %} |
30 | - <a href="{% url 'dashboards:view_general' %}"><li> | ||
31 | - {% trans "Platform Report" %} | ||
32 | - </li></a> | 33 | + <a href="{% url 'dashboards:view_general' %}"> |
34 | + <li> | ||
35 | + {% trans "Platform Report" %} | ||
36 | + </li> | ||
37 | + </a> | ||
33 | {% endif %} | 38 | {% endif %} |
34 | 39 | ||
35 | - <a href="{% url 'dashboards:view_categories' %}"><li class="active"> | ||
36 | - {% trans "Category Report" %} | ||
37 | - </li></a> | 40 | + <a href="{% url 'dashboards:view_categories' %}"> |
41 | + <li class="active"> | ||
42 | + {% trans "Category Report" %} | ||
43 | + </li> | ||
44 | + </a> | ||
38 | <li> | 45 | <li> |
39 | {% trans "Subject Report" %} | 46 | {% trans "Subject Report" %} |
40 | </li> | 47 | </li> |
@@ -52,18 +59,38 @@ | @@ -52,18 +59,38 @@ | ||
52 | {% endfor %} | 59 | {% endfor %} |
53 | </select> | 60 | </select> |
54 | <ul id="report-header-options"> | 61 | <ul id="report-header-options"> |
55 | - <li class="selected"> | 62 | + <li class="tab-option selected"> |
56 | {% trans "Focus" %} | 63 | {% trans "Focus" %} |
57 | </li> | 64 | </li> |
58 | - <li> | 65 | + <a href="{% url 'dashboards:view_general_log' %}"><li class="tab-option"> |
59 | {% trans "Log" %} | 66 | {% trans "Log" %} |
60 | - </li> | 67 | + </li></a> |
61 | </ul> | 68 | </ul> |
62 | </header> | 69 | </header> |
63 | 70 | ||
64 | <article class="report-body"> | 71 | <article class="report-body"> |
65 | 72 | ||
66 | {% include "dashboards/tags_body.html" %} | 73 | {% include "dashboards/tags_body.html" %} |
74 | + | ||
75 | + <div id="bottom-part"> | ||
76 | + <div id="left-chart"> | ||
77 | + | ||
78 | + </div> | ||
79 | + <div id="month_selector_div"> | ||
80 | + <h4>{% trans "Amount of access in: " %} | ||
81 | + <select id="month_selector"> | ||
82 | + {% for month in months %} | ||
83 | + <option>{{month}}</option> | ||
84 | + {% endfor %} | ||
85 | + | ||
86 | + </select> | ||
87 | + </h4> | ||
88 | + </div> | ||
89 | + | ||
90 | + <div id="right-side-heatmaps"> | ||
91 | + | ||
92 | + </div> | ||
93 | + </div> | ||
67 | </article> | 94 | </article> |
68 | </section> | 95 | </section> |
69 | {% endblock content %} | 96 | {% endblock content %} |
70 | \ No newline at end of file | 97 | \ No newline at end of file |
dashboards/views.py
@@ -90,11 +90,32 @@ class CategoryView(LogMixin, generic.TemplateView): | @@ -90,11 +90,32 @@ class CategoryView(LogMixin, generic.TemplateView): | ||
90 | context = {} | 90 | context = {} |
91 | self.createLog(actor = self.request.user) | 91 | self.createLog(actor = self.request.user) |
92 | 92 | ||
93 | + context['months'] = self.get_last_twelve_months() | ||
94 | + | ||
93 | context['categories'] = self.categories_associated_with_user(self.request.user) | 95 | context['categories'] = self.categories_associated_with_user(self.request.user) |
94 | - context['javascript_files'] = ["analytics/js/charts.js", "dashboards/js/behavior.js"] | ||
95 | - context['style_files'] = ['dashboards/css/general.css'] | 96 | + context['javascript_files'] = ["analytics/js/charts.js", "dashboards/js/behavior_categories.js"] |
97 | + context['style_files'] = ['dashboards/css/general.css', 'dashboards/css/dashboards_category.css'] | ||
96 | return context | 98 | return context |
97 | 99 | ||
100 | + | ||
101 | + def get_last_twelve_months(self): | ||
102 | + today = date.today() | ||
103 | + months = [] | ||
104 | + month_mappings = { 1: _('January'), 2: _('February'), 3: _('March'), 4: _('April'), 5: _('May'), 6: _('June'), 7: _('July') | ||
105 | + , 8: _('August'), 9: _('September'), 10: _('October'), 11: _('November'), 12: _('December')} | ||
106 | + date_used = today #the date used for solving the inital month problem | ||
107 | + offset = 0 #offset is the amount of weeks I had to shift so I could change the month if 4 weeks weren't enough | ||
108 | + for i in range(12): | ||
109 | + | ||
110 | + operation_date = today - timedelta(weeks= (4*i + offset)) | ||
111 | + while date_used.month == operation_date.month: | ||
112 | + offset += 2 | ||
113 | + operation_date = today - timedelta(weeks= (4*i + offset)) | ||
114 | + | ||
115 | + months.append(month_mappings[date_used.month] + '/' + str(date_used.year)) | ||
116 | + date_used = operation_date | ||
117 | + return months | ||
118 | + | ||
98 | def categories_associated_with_user(self, user): | 119 | def categories_associated_with_user(self, user): |
99 | if user.is_staff: | 120 | if user.is_staff: |
100 | categories = Category.objects.all() | 121 | categories = Category.objects.all() |