forked from rubyforgood/casa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rubyforgood#4784 from rubyforgood/4753-diplay-app-…
…metric Health Metric using Chart.js
- Loading branch information
Showing
10 changed files
with
198 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { Chart, registerables } from 'chart.js' | ||
import 'chartjs-adapter-luxon' | ||
|
||
Chart.register(...registerables) | ||
|
||
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] | ||
|
||
$(document).ready(function () { | ||
$.ajax({ | ||
type: 'GET', | ||
url: '/health/case_contacts_creation_times_in_last_week', | ||
success: function (data) { | ||
const timestamps = data.timestamps | ||
|
||
const counts = getCountsByDayAndHour(timestamps) | ||
const dataset = getDatasetFromCounts(counts) | ||
|
||
createChart(dataset) | ||
} | ||
}) | ||
}) | ||
|
||
function getCountsByDayAndHour (timestamps) { | ||
const counts = {} | ||
|
||
for (let i = 0; i < timestamps.length; i++) { | ||
const timestamp = new Date(timestamps[i] * 1000) | ||
const day = days[timestamp.getUTCDay()] | ||
const hour = timestamp.getUTCHours() | ||
const key = day + ' ' + hour | ||
counts[key] = (counts[key] || 0) + 1 | ||
} | ||
|
||
return counts | ||
} | ||
|
||
function getDatasetFromCounts (counts) { | ||
const dataset = [] | ||
|
||
for (const key in counts) { | ||
const parts = key.split(' ') | ||
const day = parts[0] | ||
const hour = parseInt(parts[1]) | ||
const count = counts[key] | ||
|
||
dataset.push({ | ||
x: hour, | ||
y: days.indexOf(day), | ||
r: Math.sqrt(count) * 2, | ||
count | ||
}) | ||
} | ||
|
||
return dataset | ||
} | ||
|
||
function createChart (dataset) { | ||
const ctx = document.getElementById('myChart').getContext('2d') | ||
// eslint-disable-next-line no-unused-vars | ||
const myChart = new Chart(ctx, { | ||
type: 'bubble', | ||
data: { | ||
datasets: [{ | ||
label: 'Case Contacts Creation Times in Last Week', | ||
data: dataset, | ||
backgroundColor: 'rgba(255, 99, 132, 0.2)', | ||
borderColor: 'rgba(255, 99, 132, 1)' | ||
}] | ||
}, | ||
options: getChartOptions() | ||
}) | ||
} | ||
|
||
function getChartOptions () { | ||
return { | ||
scales: { | ||
x: getXScale(), | ||
y: getYScale() | ||
}, | ||
plugins: { | ||
tooltip: { | ||
callbacks: { | ||
label: getTooltipLabelCallback() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function getXScale () { | ||
return { | ||
ticks: { | ||
beginAtZero: true, | ||
stepSize: 1 | ||
} | ||
} | ||
} | ||
|
||
function getYScale () { | ||
return { | ||
ticks: { | ||
beginAtZero: true, | ||
stepSize: 1, | ||
callback: getYTickCallback() | ||
} | ||
} | ||
} | ||
|
||
function getYTickCallback () { | ||
return function (value, index, values) { | ||
return days[value] | ||
} | ||
} | ||
|
||
function getTooltipLabelCallback () { | ||
return function (context) { | ||
const datum = context.dataset.data[context.dataIndex] | ||
return datum.count + ' case contacts created on ' + days[datum.y] + ' at ' + datum.x + ':00' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
<div id="health"> | ||
<canvas id="myChart"></canvas> | ||
<p class="text-center mt-5">Chart: Display App Metric</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1670,6 +1670,11 @@ | |
"@jridgewell/resolve-uri" "3.1.0" | ||
"@jridgewell/sourcemap-codec" "1.4.14" | ||
|
||
"@kurkle/color@^0.3.0": | ||
version "0.3.2" | ||
resolved "https://registry.yarnpkg.com/@kurkle/color/-/color-0.3.2.tgz#5acd38242e8bde4f9986e7913c8fdf49d3aa199f" | ||
integrity sha512-fuscdXJ9G1qb7W8VdHi+IwRqij3lBkosAm4ydQtEmbY58OzHXqQhvlxqEkoz0yssNVn38bcpRWgA9PP+OGoisw== | ||
|
||
"@nodelib/[email protected]": | ||
version "2.1.5" | ||
resolved "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz" | ||
|
@@ -2288,6 +2293,18 @@ char-regex@^1.0.2: | |
resolved "https://registry.npmjs.org/char-regex/-/char-regex-1.0.2.tgz" | ||
integrity sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw== | ||
|
||
chart.js@^4.2.1: | ||
version "4.2.1" | ||
resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-4.2.1.tgz#d2bd5c98e9a0ae35408975b638f40513b067ba1d" | ||
integrity sha512-6YbpQ0nt3NovAgOzbkSSeeAQu/3za1319dPUQTXn9WcOpywM8rGKxJHrhS8V8xEkAlk8YhEfjbuAPfUyp6jIsw== | ||
dependencies: | ||
"@kurkle/color" "^0.3.0" | ||
|
||
chartjs-adapter-luxon@^1.3.1: | ||
version "1.3.1" | ||
resolved "https://registry.yarnpkg.com/chartjs-adapter-luxon/-/chartjs-adapter-luxon-1.3.1.tgz#8ad8be7cc1521bacd6cd2ff4b013669d4dd49364" | ||
integrity sha512-yxHov3X8y+reIibl1o+j18xzrcdddCLqsXhriV2+aQ4hCR66IYFchlRXUvrJVoxglJ380pgytU7YWtoqdIgqhg== | ||
|
||
[email protected]: | ||
version "2.24.0" | ||
resolved "https://registry.npmjs.org/check-more-types/-/check-more-types-2.24.0.tgz" | ||
|
@@ -4283,6 +4300,11 @@ lru-cache@^6.0.0: | |
dependencies: | ||
yallist "^4.0.0" | ||
|
||
luxon@^3.3.0: | ||
version "3.3.0" | ||
resolved "https://registry.yarnpkg.com/luxon/-/luxon-3.3.0.tgz#d73ab5b5d2b49a461c47cedbc7e73309b4805b48" | ||
integrity sha512-An0UCfG/rSiqtAIiBPO0Y9/zAnHUZxAMiCpTd5h2smgsj7GGmcenvrvww2cqNA8/4A5ZrD1gJpHN2mIHZQF+Mg== | ||
|
||
make-dir@^3.0.0: | ||
version "3.1.0" | ||
resolved "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz" | ||
|