Skip to content

Commit

Permalink
Use real dates for rendering metrics chart.
Browse files Browse the repository at this point in the history
By changing this chart to actually use a time axis, it provides better
axis labels and can also ensure that if the data contains any gaps those
are rendered properly instead of it looking like all missing data is
actually present.
  • Loading branch information
GUI committed Jul 18, 2024
1 parent 0d6de92 commit 7e4a9ae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
53 changes: 23 additions & 30 deletions assets/javascripts/metrics/components/UsageChart.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as echarts from "echarts";
import debounce from "lodash-es/debounce";
import {
computed,
defineComponent,
onMounted,
ref,
Expand Down Expand Up @@ -34,33 +33,20 @@ export default defineComponent({
hitsTotal: Number,
activeApiKeys: Array,
activeApiKeysTotal: Number,
dateFormatOptions: Object,
},

setup(props) {
const chartData = computed(() => {
const data = {
labels: [],
hits: [],
activeApiKeys: [],
};

props.hits.forEach((day) => {
data.labels.push(day[0]);
data.hits.push(day[1]);
});

props.activeApiKeys.forEach((day) => {
data.activeApiKeys.push(day[1]);
});

return data;
});

const chartEl = ref();
let chart;
onMounted(() => {
const colors = ["#30A64A", "#0E5FC2"];

const dateFormatter = new Intl.DateTimeFormat(
undefined,
props.dateFormatOptions,
);

chart = echarts.init(chartEl.value);
chart.setOption({
animation: false,
Expand All @@ -73,10 +59,17 @@ export default defineComponent({
},
tooltip: {
trigger: "axis",
axisPointer: {
label: {
formatter: (params) => {
const date = new Date(params.value);
return dateFormatter.format(date);
},
},
},
},
xAxis: {
type: "category",
data: chartData.value.labels,
type: "time",
axisLine: {
show: false,
},
Expand Down Expand Up @@ -109,34 +102,34 @@ export default defineComponent({
name: "Hits",
type: "line",
yAxisIndex: 0,
data: chartData.value.hits,
data: props.hits,
label: {
formatter: "{b}: {@score}",
},
},
{
name: "Unique API Keys",
type: "line",
yAxisIndex: 1,
data: chartData.value.activeApiKeys,
data: props.activeApiKeys,
},
],
});

window.addEventListener("resize", debounce(chart.resize));
});

watch(chartData, () => {
watch(props, () => {
if (chart) {
chart.setOption({
xAxis: {
data: chartData.value.labels,
},
series: [
{
name: "Hits",
data: chartData.value.hits,
data: props.hits,
},
{
name: "Unique API Keys",
data: chartData.value.activeApiKeys,
data: props.activeApiKeys,
},
],
});
Expand Down
2 changes: 2 additions & 0 deletions assets/javascripts/metrics/views/Metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default defineComponent({
v-bind:hits-total="store.hitsRecentTotal(selectedOrganization)"
v-bind:active-api-keys="store.activeApiKeysRecentDaily(selectedOrganization)"
v-bind:active-api-keys-total="store.activeApiKeysRecentTotal(selectedOrganization)"
v-bind:date-format-options="{ year: 'numeric', month: 'long', day: 'numeric' }"
/>
</div>
<div class="desktop:grid-col">
Expand All @@ -44,6 +45,7 @@ export default defineComponent({
v-bind:hits-total="store.hitsTotal(selectedOrganization)"
v-bind:active-api-keys="store.activeApiKeysMonthly(selectedOrganization)"
v-bind:active-api-keys-total="store.activeApiKeysTotal(selectedOrganization)"
v-bind:date-format-options="{ year: 'numeric', month: 'long' }"
/>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: "3"
services:
web:
build:
Expand Down

0 comments on commit 7e4a9ae

Please sign in to comment.