Skip to content

Commit

Permalink
user analytics curr iter, session duration bug
Browse files Browse the repository at this point in the history
  • Loading branch information
phamduylong committed Oct 20, 2022
1 parent c60b618 commit cc70343
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
5 changes: 5 additions & 0 deletions js/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,8 @@ const addUser = (newUser) => {
});
}

const isSameDay = (d1, d2) => {
return d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate();
}
55 changes: 31 additions & 24 deletions js/views/user_stats.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
</head>

<body onload="fetching()">
<div><canvas id="myChart"></canvas></div>
<div style="width: 50%; position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%);"><canvas id="chart"></canvas></div>
<h2 id = "averageTime"></h2>
<script>
function fetching() {
fetch('/user_data').then(tmp => tmp.json()).then(arr => {
const x = arr.x;
const y = arr.y;
const diff = arr.y;
const time_converted = [];
/*const time_converted = [];
diff.forEach(ms => {
const days = Math.floor(ms / (24*60*60*1000));
const daysms = ms % (24*60*60*1000);
Expand All @@ -27,40 +28,46 @@
const sec = Math.floor(minutesms / 1000);
time_converted.push(days + ":" + hours + ":" + minutes + ":" + sec);
});
console.log(time_converted);
console.log(time_converted);*/
for (let i = 0; i < x.length; ++i) {
x[i] = new Date(x[i]).toString().substring(0, 21);
const d1 = new Date(x[i]);
const d2 = new Date(x[i+1]);
if (d1.getFullYear() === d2.getFullYear()
&& d1.getMonth() === d2.getMonth()
&& d1.getDate() === d2.getDate()) {
diff[i] += diff[i+1];
x.splice(i+1, 1);
diff.splice(i+1 ,1);
i--;
}
x[i] = new Date(x[i]).toString().substring(4, 21);
}
new Chart("myChart", {
type: "line",
console.log(diff);
console.log(x);
new Chart("chart", {
type: "bar",
data: {
labels: arr.x,
datasets: [
{
label: "Time used",
label: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
data: arr.y,
borderColor: "blue",
fill: false,
backgroundColor: [
'rgb(255, 99, 132)',
'rgb(255, 159, 64)',
'rgb(255, 205, 86)',
'rgb(75, 192, 192)',
'rgb(54, 162, 235)',
'rgb(153, 102, 255)',
'rgb(201, 203, 207)'
]
}
]
},
options: {
interaction: {
mode: 'index',
intersect: false,
},
stacked: false,
legend: {display: false},
responsive: true,
title: {
display: true,
text: "User login statistics"
}
}
});
Expand Down

0 comments on commit cc70343

Please sign in to comment.