Repository navigation
Expand file tree
/
Copy pathdata.php
More file actions
364 lines (282 loc) · 9.82 KB
/
Copy pathdata.php
File metadata and controls
364 lines (282 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
include_once("config.php");
// Get Date or set current date
$date = date("Y-m-d");
if (isset($_GET['date'])) {
$date = $_GET['date'];
}
if (isset($_GET['next'])) {
$date = $_GET['next'];
}
// Get session vars from cookies if possible
if (isset($_COOKIE['fb_client_id']) && $_COOKIE['fb_client_id'] != '') {
$fb_client_id = $_COOKIE['fb_client_id'];
$_SESSION['fb_client_id'] = $fb_client_id;
}
if (isset($_COOKIE['fb_access_token']) && $_COOKIE['fb_access_token'] != '') {
$fb_access_token = base64_decode($_COOKIE['fb_access_token']);
$_SESSION['fb_access_token'] = $fb_access_token;
}
// VALIDATION
// Step 1 - Date Validation
if (!validateDate("$date 00:00:00")) {
exit("Date is not valid. Please use YYYY-MM-DD format.");
} else {
$curr_date = new DateTime($date);
$next_date = new DateTime($date);
$prev_date = new DateTime($date);
$next_date->add(new DateInterval('P1D'));
$prev_date->sub(new DateInterval('P1D'));
$next = $next_date->format("Y-m-d");
$prev = $prev_date->format("Y-m-d");
}
if (isset($_GET['fb_client_id']) && trim($_GET['fb_client_id']) != "") {
// Register Client ID in the Session
$fb_client_id = safe(trim($_GET['fb_client_id']));
$_SESSION['fb_client_id'] = $fb_client_id;
}
// Step 2 - Do not proceed if Client ID is not set or available
if (!isset($_SESSION['fb_client_id']) || $_SESSION['fb_client_id'] == '') {
exit("Client ID not available. Please <a href='index.php'>try again</a>");
} else {
$fb_client_id = $_SESSION['fb_client_id'];
}
// GET DATA & SHOW THE CHART
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Fitbit Heart Rate Data for <?php echo $fb_client_id; ?></title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- <script src='https://code.highcharts.com/10/highcharts.js'></script> -->
<!-- <script src='https://code.highcharts.com/10/modules/exporting.js'></script> -->
<script src='https://code.highcharts.com/highcharts.js'></script>
<script src='https://code.highcharts.com/modules/exporting.js'></script>
<!-- date picker -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker3.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
<link rel="icon" href="/assets/favicon32.ico" type="image/x-icon" />
</head>
<body>
<!-- Header -->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="./"><strong>Fitbit Heart Rate Intraday Data</strong></a>
</div>
</div>
</nav>
<div style='height:60px;'>
</div>
<!-- Primary data -->
<div class="container-fluid">
<div class="well" id="setdate">
<form action='data.php' method=get id='form'>
<div class="row">
<div class="col-md-1 pull-left text-left">
<a class="btn btn-success" title="Previous Day" href='data.php?date=<?php echo $prev; ?>'> ◄ </a>
</div> <!-- /col-md-1 -->
<!-- Column for Showing Date Picker -->
<div class="col-md-2">
<div class="input-group date" id="datepicker">
<input type=text class="form-control" readonly name=date placeholder='yyyy-mm-dd' value='<?php echo $date; ?>'>
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
</div>
</div><!-- /col-md-2 -->
<div class="col-md-3 pull-right text-right">
<a class="btn btn-success" title="Fetch and Next" href='getdata.php?date=<?php echo $date; ?>&next=<?php echo $next; ?>'>↻ & ►</a>
<a class="btn btn-success" title="Refresh" href='getdata.php?date=<?php echo $date; ?>'>↻</a>
<a class="btn btn-success" title="Next Day" href='data.php?date=<?php echo $next; ?>'> ► </a>
</div> <!-- /col-md-3 -->
</div> <!-- /row -->
</form>
</div> <!-- /well -->
<?php
// Query the data. Get the raw data and process it
$q = "select id, fbjsondata from `data_raw` where fbdate='$date' and fbuser='$fb_client_id'";
$result = mysqli_query($config_conn, $q);
$seriesarr = array();
if (mysqli_num_rows($result) > 0) {
$series_raw = mysqli_fetch_row($result);
$series = objectToArray(json_decode($series_raw[1]));
if (!is_null($series)) {
foreach ($series['activities-heart-intraday']['dataset'] as $key => $value) {
$time = $value['time'];
$hb = $value['value'];
$timearr = explode(":", $time);
$datearr = explode("-", $date);
$seriesarr[] = "[Date.UTC($datearr[0],$datearr[1]-1,$datearr[2],$timearr[0],$timearr[1],$timearr[2]), $hb]";
}
}
}
// Print the data / Show Chart
?>
<div id='charts'></div>
<script>
Highcharts.chart('charts', {
title: {
text: 'Fitbit Heart Rate Data for <?php echo $fb_client_id; ?>'
},
chart: {
zoomType: 'x',
height: (7 / 16 * 100) + '%' // 16:9 ratio
},
subtitle: {
text: '<?php echo $date; ?>'
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // don't display the dummy year
hour: '%I %p',
minute: '%I:%M %p'
},
title: {
text: 'Time'
}
},
yAxis: {
title: {
text: 'Heart Rate'
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle'
},
plotOptions: {
series: {
pointStart: Date.UTC(1970, 1, 1, 0, 0, 0)
}
},
series: [{
name: 'Heart Rate',
showInLegend: false,
data: [
<?php
echo implode(",", $seriesarr);
?>
]
}]
});
</script>
<!-- /charts -->
<hr>
<!-- Other data including detailed table and summary -->
<div class="row">
<!-- headers for tables shown later -->
<div class="col-md-3 pull-left">
<h4>Heart Rate Summary</h4>
</div>
<?php
if (mysqli_num_rows($result) > 0) {
if (!is_null($series)) {
$hbsummary = $series['activities-heart'][0]['value'];
$rhr = isset($hbsummary['restingHeartRate']) ? $hbsummary['restingHeartRate'] : "Unknown";
}
} else {
$rhr = "Unknown";
}
?>
<div class="col-md-3">
<h4>Resting Heart Rate: <?php echo $rhr; ?></h4>
</div>
<div class="col-md-6 pull-right">
<a class="btn btn-primary pull-right" href="download.php?date=<?php echo $date; ?>">Export to Excel</a>
<h4>Heart Rate Detailed Data</h4>
</div>
<!-- /headers -->
<div class="clear"><!-- --></div>
<!-- Table with Heart Rate Summary -->
<div class="col-md-5 pull-left">
<table class="table table-bordered table-striped">
<thead>
<th>Name</th>
<th>Minutes Spent</th>
<th>Calories</th>
<th>HB Range</th>
</thead>
<?php
if (mysqli_num_rows($result) > 0) {
if (!is_null($series)) {
$i = 0;
$color = array('active', 'success', 'warning', 'danger', 'active', 'active', 'active', 'active');
foreach ($hbsummary['heartRateZones'] as $hbsummarydata1) {
echo "<tr class='$color[$i]'>";
echo "<td>" . $hbsummarydata1['name'] . "</td>\n";
echo "<td>" . $hbsummarydata1['minutes'] . "</td>\n";
echo "<td>" . $hbsummarydata1['caloriesOut'] . "</td>\n";
echo "<td>" . $hbsummarydata1['min'] . '-' . $hbsummarydata1['max'] . "</td>\n";
echo "</tr>";
$i++;
}
}
}
?>
</table>
</div> <!-- /col-md-5 summary -->
<!-- Table with Heart Rate Detailed Data -->
<div class="col-md-6 pull-right pre-scrollable">
<table class="table table-bordered" id='hrdata'>
<thead>
<th>Date & Time</th>
<th>Heart Rate</th>
</thead>
<?php
if (mysqli_num_rows($result) == '0') {
echo "<tr><td colspan=2 style='height:200px;'>No data available</td></tr>\n";
}
if (mysqli_num_rows($result) > 0) {
if (!is_null($series)) {
foreach ($series['activities-heart-intraday']['dataset'] as $key => $value) {
echo "<tr><td>$date $value[time]</td><td>$value[value]</td></tr>\n";
}
}
}
?>
</table>
</div> <!-- /col-md-6 detailed -->
<div style='clear:both;'><!-- --></div>
<hr>
</div> <!-- /container -->
<script>
// Date Picker Configuration
$('#datepicker').datepicker({
autoclose: true,
format: "yyyy-mm-dd"
});
$('#datepicker').on('show', function(e) {
if (e.date) {
$(this).data('stickyDate', e.date);
} else {
$(this).data('stickyDate', null);
}
});
$('#datepicker').on('hide', function(e) {
var stickyDate = $(this).data('stickyDate');
if (!e.date && stickyDate) {
$(this).datepicker('setDate', stickyDate);
$(this).data('stickyDate', null);
}
$('#form').submit();
});
</script>
</body>
</html>