Skip to content

Commit ccf1946

Browse files
author
Chris Noble
committed
Analytics
1 parent da9743a commit ccf1946

File tree

7 files changed

+265
-55
lines changed

7 files changed

+265
-55
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ settings.json
3232
*_old
3333
*/*/local_settings.pychef_files/data_bag_key
3434
celery*.db
35-
35+
platforms/*
36+
plugins/*
3637

3738

3839

www/index.html

+23-4
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,53 @@
4747
<script type="text/javascript" src="js/lib/underscore-min.js"></script>
4848
<script type="text/javascript" src="js/lib/backbone-min.js"></script>
4949
<script type="text/javascript" src="js/lib/mustache.min.js"></script>
50+
<script type="text/javascript" src="js/lib/moment.min.js"></script>
5051
<script type="text/javascript" src="js/models/question-model.js"></script>
5152
<script type="text/javascript" src="js/models/treatment-model.js"></script>
5253
<script type="text/javascript" src="js/collections/question-collection.js"></script>
5354
<script type="text/javascript" src="js/collections/current-treatment-collection.js"></script>
5455
<script type="text/javascript" src="js/collections/past-treatment-collection.js"></script>
5556
<script type="text/javascript" src="js/views/question-view.js"></script>
57+
<script type="text/javascript" src="js/views/result-view.js"></script>
5658
<script type="text/javascript" src="js/views/app-view.js"></script>
5759
<script type="text/javascript">
5860
var questions = [
5961
{
6062
'id': 1,
6163
'text': 'Please rate your pain at its WORST in the last 24 hours.',
64+
'avgShort': 'Worst Pain',
6265
'type': 'range',
6366
'min': 'No pain',
6467
'max': 'Extreme pain',
6568
},
6669
{
6770
'id': 2,
6871
'text': 'Please rate your pain at its LEAST in the last 24 hours.',
72+
'avgShort': 'Least Pain',
6973
'type': 'range',
7074
'min': 'No pain',
7175
'max': 'Extreme pain',
7276
},
7377
{
7478
'id': 3,
7579
'text': 'Please rate your pain on the AVERAGE.',
80+
'avgShort': 'Average Pain',
7681
'type': 'range',
7782
'min': 'No pain',
7883
'max': 'Extreme pain',
7984
},
8085
{
8186
'id': 4,
8287
'text': 'How much pain you have RIGHT NOW.',
88+
'avgShort': 'Pain Right Now',
8389
'type': 'range',
8490
'min': 'No pain',
8591
'max': 'Extreme pain',
8692
},
8793
{
8894
'id': 5,
8995
'text': 'In the last 24 hours, how much relief have pain treatments or medications provided?',
96+
'avgShort': 'Pain Relief',
9097
'type': 'range',
9198
'min': 'No relief',
9299
'max': 'Complete relief',
@@ -95,48 +102,58 @@
95102
'id': 7,
96103
'text': 'During the past 24 hours, how much has pain interfered with your General activity:',
97104
'type': 'range',
105+
'avgShort': 'General Activity',
98106
'min': 'No interference',
99107
'max': 'Complete interference',
100108
},
101109
{
102110
'id': 8,
103111
'text': 'During the past 24 hours, how much has pain interfered with your Mood:',
104112
'type': 'range',
113+
'avgShort': 'Mood',
105114
'min': 'No interference',
106115
'max': 'Complete interference',
107116
},
108117
{
109118
'id': 9,
110119
'text': 'During the past 24 hours, how much has pain interfered with your Walking ability:',
111120
'type': 'range',
121+
'avgShort': 'Walking',
112122
'min': 'No interference',
113123
'max': 'Complete interference',
114124
},
115125
{
116126
'id': 10,
117127
'text': 'During the past 24 hours, how much has pain interfered with your Normal work (includes both work outside the home and housework):',
118128
'type': 'range',
129+
'avgShort': 'Normal Work',
130+
119131
'min': 'No interference',
120132
'max': 'Complete interference',
121133
},
122134
{
123135
'id': 11,
124136
'text': 'During the past 24 hours, how much has pain interfered with your Relations with other people:',
125137
'type': 'range',
138+
'avgShort': 'Relations',
139+
126140
'min': 'No interference',
127141
'max': 'Complete interference',
128142
},
129143
{
130144
'id': 12,
131145
'text': 'During the past 24 hours, how much has pain interfered with your Sleep:',
132146
'type': 'range',
147+
'avgShort': 'Sleep',
133148
'min': 'No interference',
134149
'max': 'Complete interference',
135150
},
136151
{
137152
'id': 13,
138153
'text': 'During the past 24 hours, how much has pain interfered with your Enjoyment of life:',
139154
'type': 'range',
155+
'avgShort': 'Enjoyment',
156+
140157
'min': 'No interference',
141158
'max': 'Complete interference',
142159
}
@@ -171,14 +188,16 @@ <h1 class="text-center">{{{ text }}}</h1>
171188

172189
<script id="questionResultsTemplate" type="html/template">
173190
<div class="row questionResult">
174-
<h2 class="questionShortText">Average Pain</h2>
191+
192+
<h2 class="questionShortText">{{avgShort}}</h2>
193+
175194
<div class="col-xs-6 questionCurrentResult">
176-
<span class="questionAverageResult">6.4</span>
195+
<span class="questionAverageResult">{{avg}}</span>
177196
</div>
178197
<div class="col-xs-6 comparisons" >
179-
<div class="improved"><span class="glyphicon glyphicon-time"></span><span class="glyphicon glyphicon-arrow-up"></span><span class="changeValue">0.4</span></div>
198+
<div class="{{#avgTimeUp}}improved{{/avgTimeUp}}{{^avgTimeUp}}worse{{/avgTimeUp}}"><span class="glyphicon glyphicon-time"></span><span class="glyphicon glyphicon-arrow-{{#avgTimeUp}}up{{/avgTimeUp}}{{^avgTimeUp}}down{{/avgTimeUp}}"></span><span class="changeValue">{{avgTime}}</span></div>
180199
<hr class="separator" />
181-
<div class="worse"><span class="glyphicon glyphicon-glass"></span><span class="glyphicon glyphicon-arrow-down"></span><span class="changeValue">1.4</span></div>
200+
<div class="{{#avgTreatmentUp}}improved{{/avgTreatmentUp}}{{^avgTreatmentUp}}worse{{/avgTreatmentUp}}"><img class="icon icon-treatment report" src="img/icon_treatment.svg"><span class="glyphicon glyphicon-arrow-{{#avgTreatmentUp}}up{{/avgTreatmentUp}}{{^avgTreatmentUp}}down{{/avgTreatmentUp}}"></span><span class="changeValue">{{avgTreatment}}</span></div>
182201
</div>
183202
</div>
184203
</script>

www/js/lib/moment.min.js

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

www/js/models/question-model.js

+125-44
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,132 @@
88
*/
99
Question = Backbone.Model.extend({
1010

11-
defaults: {
12-
"name": null,
13-
"type": null,
14-
"value": 5,
15-
"seen": false
16-
},
17-
18-
key: 'question-',
19-
20-
/**
21-
* initialize
22-
* Called after the constructor.
23-
*/
24-
initialize: function () {
25-
this.loadFromLocal();
26-
},
27-
28-
29-
/**
30-
* loadFromLocal
31-
* Loads the value to local storage
32-
*/
33-
loadFromLocal:function() {
34-
var data = localStorage.getItem(this.key+this.get('id'));
35-
if (data !== null){
36-
data = JSON.parse(data);
37-
}else{
38-
data = {};
11+
defaults: {
12+
"name": null,
13+
"type": null,
14+
"value": 5,
15+
"seen": false,
16+
"avgShort": '',
17+
"avg": 5,
18+
"avgTime": 5,
19+
"avgTimeUp": false,
20+
"avgTreatment": 5,
21+
"avgTreatmentUp": false,
22+
},
23+
24+
key: 'question-',
25+
26+
/**
27+
* initialize
28+
* Called after the constructor.
29+
*/
30+
initialize: function () {
31+
this.loadFromLocal();
32+
},
33+
34+
35+
/**
36+
* loadFromLocal
37+
* Loads the value to local storage
38+
*/
39+
loadFromLocal:function() {
40+
var data = localStorage.getItem(this.key+this.get('id'));
41+
if (data !== null){
42+
data = JSON.parse(data);
43+
}else{
44+
data = [];
45+
}
46+
//Make sure we are only using ints
47+
for (var i in data) {
48+
data[i]['value'] = parseInt(data[i]['value']);
49+
};
50+
this.set('data',data);
51+
},
52+
53+
/**
54+
* saveToLocal
55+
* Saves the value to local storage
56+
* @param Timestamp - Timestamp fo the session
57+
*/
58+
saveToLocal:function(Timestamp){
59+
60+
var data = this.get('data');
61+
var dataObj = {};
62+
dataObj['timestamp'] = Timestamp;
63+
dataObj['value'] = this.get('value');;
64+
data.push(dataObj);
65+
var stringify = JSON.stringify(data);
66+
//Get the date
67+
localStorage.setItem(this.key+this.get('id'), stringify);
68+
},
69+
70+
71+
/**
72+
* calculateResults
73+
* Calculates the analytics for this question
74+
* @param TreatmentTimestamp - Timestamp from the start of the last treatment
75+
* @param DateTimestamp - Timestamp from the start of the date we want to compare
76+
*/
77+
calculateResults: function(TreatmentTimestamp, DateTimestamp) {
78+
79+
80+
//Running Average
81+
var data = this.get('data');
82+
83+
var avg = this.findAverage(data);
84+
this.set('avg',avg);
85+
86+
var avgTime = this.findDiffAverage(data, DateTimestamp);
87+
if (avgTime > 0){
88+
this.set('avgTimeUp',true);
89+
}
90+
this.set('avgTime', avgTime);
91+
92+
var avgTreatment = this.findDiffAverage(data, TreatmentTimestamp);
93+
this.set('avgTreatment', avgTreatment);
94+
if (avgTreatment > 0){
95+
this.set('avgTreatmentUp',true);
96+
}
97+
98+
},
99+
100+
101+
findDiffAverage: function(dataSet, timestamp) {
102+
//Treatment Average
103+
var timestampBefore = _.filter(dataSet, function(dataObj) {
104+
return dataObj.timestamp < timestamp;
105+
106+
});
107+
108+
var beforeAverage = this.findAverage(timestampBefore);
109+
110+
var timestampAfter = _.filter(dataSet, function(dataObj) {
111+
return dataObj.timestamp > timestamp;
112+
});
113+
114+
var afterAverage = this.findAverage(timestampAfter);
115+
116+
var diff = (afterAverage - beforeAverage);
117+
return diff;
118+
119+
},
120+
121+
findAverage: function(dataSet) {
122+
123+
var total = 0;
124+
_.each(dataSet, function(dataObj){
125+
total += dataObj.value;
126+
});
127+
var avg;
128+
if (total == 0 || dataSet.length == 0){
129+
avg = 0;
130+
}
131+
else{
132+
avg = total/dataSet.length;
133+
}
134+
avg = parseFloat(avg).toFixed(1);
135+
return avg;
39136
}
40-
this.set('data',data);
41-
},
42-
43-
/**
44-
* saveToLocal
45-
* Saves the value to local storage
46-
* @param Timestamp - Timestamp fo the session
47-
*/
48-
saveToLocal:function(Timestamp){
49-
50-
var data = this.get('data');
51-
data[Timestamp] = this.get('value');
52-
var stringify = JSON.stringify(data);
53-
//Get the date
54-
localStorage.setItem(this.key+this.get('id'), stringify);
55-
}
56137

57138

58139
});

www/js/views/app-view.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ var AppView = Backbone.View.extend({
1717
questionContainer: '.questionContainer',
1818
allDoneTemplate: '#doneTemplate',
1919
appContainer: '.appContainer',
20+
questionResultsContainer: '.questionResultsContainer',
2021
timestamp: null,
2122
currQuestion: null,
23+
reportsCalculated: false,
2224
events:{
23-
'click .next': 'getNextQuestion'
25+
'click .next': 'getNextQuestion',
26+
'click .report': 'showReport'
2427
},
2528

2629
/**
@@ -31,7 +34,7 @@ var AppView = Backbone.View.extend({
3134
initialize : function(options) {
3235

3336
//Bind this to the following functions
34-
_.bindAll(this, "getNextQuestion", "showEndScreen");
37+
_.bindAll(this, "getNextQuestion", "showEndScreen", "showReport", "hideAllContainers");
3538

3639
var that = this;
3740
this.questionCollection = options.questionCollection;
@@ -75,6 +78,42 @@ var AppView = Backbone.View.extend({
7578
var rendered = Mustache.to_html(templater, {});
7679
this.$(this.appContainer).html(rendered);
7780
return this;
81+
},
82+
83+
84+
85+
/**
86+
* showReport
87+
* Shows the report section
88+
*/
89+
showReport: function(event) {
90+
91+
var $resultContainer = this.$(this.questionResultsContainer);
92+
//Just show the report if it has been calculated
93+
if (!this.reportsCalculated) {
94+
95+
var twoWeeks = moment().subtract('days', 14);
96+
//Go through and calculate the analytics
97+
this.questionCollection.each(function(question){
98+
question.calculateResults(0,twoWeeks.valueOf());
99+
var resultView = new ResultView({model:question});
100+
$resultContainer.append(resultView.render().el);
101+
});
102+
this.reportsCalculated = true;
103+
}
104+
this.hideAllContainers();
105+
$resultContainer.show();
106+
},
107+
108+
109+
/**
110+
* hideAllContainers
111+
* Hides all of the containers
112+
*/
113+
hideAllContainers:function() {
114+
this.$('footer').hide();
115+
this.$(this.questionContainer).hide();
116+
this.$(this.questionResultsContainer).hide();
78117
}
79118

80119

0 commit comments

Comments
 (0)