2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2022-12-28 06:13:47 +01:00
|
|
|
<template>
|
|
|
|
<div ref="rootEl">
|
|
|
|
<MkLoading v-if="fetching"/>
|
|
|
|
<div v-else>
|
|
|
|
<canvas ref="chartEl"></canvas>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2023-12-07 06:42:09 +01:00
|
|
|
import { onMounted, nextTick, shallowRef, ref } from 'vue';
|
2023-01-02 02:18:47 +01:00
|
|
|
import { Chart } from 'chart.js';
|
2024-01-04 10:32:46 +01:00
|
|
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { defaultStore } from '@/store.js';
|
|
|
|
import { useChartTooltip } from '@/scripts/use-chart-tooltip.js';
|
|
|
|
import { alpha } from '@/scripts/color.js';
|
|
|
|
import { initChart } from '@/scripts/init-chart.js';
|
2022-12-28 06:13:47 +01:00
|
|
|
|
2023-01-02 02:18:47 +01:00
|
|
|
initChart();
|
2022-12-28 06:13:47 +01:00
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
const rootEl = shallowRef<HTMLDivElement | null>(null);
|
|
|
|
const chartEl = shallowRef<HTMLCanvasElement | null>(null);
|
|
|
|
let chartInstance: Chart | null = null;
|
2023-12-07 06:42:09 +01:00
|
|
|
const fetching = ref(true);
|
2022-12-28 06:13:47 +01:00
|
|
|
|
|
|
|
const { handler: externalTooltipHandler } = useChartTooltip({
|
|
|
|
position: 'middle',
|
|
|
|
});
|
|
|
|
|
|
|
|
async function renderChart() {
|
2024-01-30 11:53:53 +01:00
|
|
|
if (rootEl.value == null) return;
|
2022-12-28 06:13:47 +01:00
|
|
|
if (chartInstance) {
|
|
|
|
chartInstance.destroy();
|
|
|
|
}
|
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
const wide = rootEl.value.offsetWidth > 600;
|
|
|
|
const narrow = rootEl.value.offsetWidth < 400;
|
2022-12-28 06:13:47 +01:00
|
|
|
|
2023-03-15 09:43:13 +01:00
|
|
|
const maxDays = wide ? 10 : narrow ? 5 : 7;
|
2022-12-28 06:13:47 +01:00
|
|
|
|
2024-01-04 10:32:46 +01:00
|
|
|
let raw = await misskeyApi('retention', { });
|
2023-03-15 09:43:13 +01:00
|
|
|
|
2023-05-11 11:20:48 +02:00
|
|
|
raw = raw.slice(0, maxDays + 1);
|
2022-12-28 06:13:47 +01:00
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
const data: {
|
|
|
|
x: number;
|
|
|
|
y: string;
|
|
|
|
v: number;
|
|
|
|
}[] = [];
|
|
|
|
|
2022-12-28 06:13:47 +01:00
|
|
|
for (const record of raw) {
|
2023-04-20 03:41:09 +02:00
|
|
|
data.push({
|
|
|
|
x: 0,
|
|
|
|
y: record.createdAt,
|
|
|
|
v: record.users,
|
|
|
|
});
|
|
|
|
|
|
|
|
let i = 1;
|
2022-12-28 06:13:47 +01:00
|
|
|
for (const date of Object.keys(record.data).sort((a, b) => new Date(a).getTime() - new Date(b).getTime())) {
|
|
|
|
data.push({
|
|
|
|
x: i,
|
|
|
|
y: record.createdAt,
|
|
|
|
v: record.data[date],
|
|
|
|
});
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
fetching.value = false;
|
2022-12-28 06:13:47 +01:00
|
|
|
|
|
|
|
await nextTick();
|
|
|
|
|
|
|
|
const color = defaultStore.state.darkMode ? '#b4e900' : '#86b300';
|
|
|
|
|
2023-04-20 03:41:09 +02:00
|
|
|
const getYYYYMMDD = (date: Date) => {
|
|
|
|
const y = date.getFullYear().toString().padStart(2, '0');
|
|
|
|
const m = (date.getMonth() + 1).toString().padStart(2, '0');
|
|
|
|
const d = date.getDate().toString().padStart(2, '0');
|
|
|
|
return `${y}/${m}/${d}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const max = (createdAt: string) => raw.find(x => x.createdAt === createdAt)!.users;
|
2022-12-28 06:13:47 +01:00
|
|
|
|
2023-03-15 09:43:13 +01:00
|
|
|
const marginEachCell = 12;
|
2022-12-28 06:13:47 +01:00
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
if (chartEl.value == null) return;
|
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
chartInstance = new Chart(chartEl.value, {
|
2022-12-28 06:13:47 +01:00
|
|
|
type: 'matrix',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'Active',
|
2024-01-30 11:53:53 +01:00
|
|
|
data: data as any,
|
2022-12-28 06:13:47 +01:00
|
|
|
borderWidth: 0,
|
|
|
|
borderRadius: 3,
|
|
|
|
backgroundColor(c) {
|
2024-01-30 11:53:53 +01:00
|
|
|
const v = c.dataset.data[c.dataIndex] as unknown as typeof data[0];
|
|
|
|
const value = v.v;
|
|
|
|
const m = max(v.y);
|
2023-05-12 05:15:54 +02:00
|
|
|
if (m === 0) {
|
|
|
|
return alpha(color, 0);
|
|
|
|
} else {
|
|
|
|
const a = value / m;
|
|
|
|
return alpha(color, a);
|
|
|
|
}
|
2022-12-28 06:13:47 +01:00
|
|
|
},
|
|
|
|
width(c) {
|
|
|
|
const a = c.chart.chartArea ?? {};
|
|
|
|
return (a.right - a.left) / maxDays - marginEachCell;
|
|
|
|
},
|
|
|
|
height(c) {
|
|
|
|
const a = c.chart.chartArea ?? {};
|
|
|
|
return (a.bottom - a.top) / maxDays - (marginEachCell / 1.5);
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
aspectRatio: wide ? 2 : narrow ? 2 : 2,
|
|
|
|
layout: {
|
|
|
|
padding: {
|
|
|
|
left: 8,
|
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
x: {
|
|
|
|
position: 'top',
|
|
|
|
suggestedMax: maxDays,
|
|
|
|
grid: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
display: true,
|
2022-12-29 01:58:11 +01:00
|
|
|
padding: 0,
|
2022-12-28 06:13:47 +01:00
|
|
|
maxRotation: 0,
|
2022-12-29 01:58:11 +01:00
|
|
|
autoSkipPadding: 0,
|
|
|
|
autoSkip: false,
|
2023-04-20 03:41:09 +02:00
|
|
|
callback: (value, index, values) => value,
|
2022-12-28 06:13:47 +01:00
|
|
|
},
|
2023-05-12 03:29:27 +02:00
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: 'Days later',
|
|
|
|
},
|
2022-12-28 06:13:47 +01:00
|
|
|
},
|
|
|
|
y: {
|
|
|
|
type: 'time',
|
|
|
|
offset: true,
|
|
|
|
reverse: true,
|
|
|
|
position: 'left',
|
|
|
|
time: {
|
|
|
|
unit: 'day',
|
|
|
|
round: 'day',
|
|
|
|
},
|
|
|
|
grid: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
maxRotation: 0,
|
|
|
|
autoSkip: true,
|
|
|
|
padding: 1,
|
|
|
|
font: {
|
|
|
|
size: 9,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
tooltip: {
|
|
|
|
enabled: false,
|
|
|
|
callbacks: {
|
|
|
|
title(context) {
|
|
|
|
const v = context[0].dataset.data[context[0].dataIndex];
|
2023-04-20 03:41:09 +02:00
|
|
|
return getYYYYMMDD(new Date(new Date(v.y).getTime() + (v.x * 86400000)));
|
2022-12-28 06:13:47 +01:00
|
|
|
},
|
|
|
|
label(context) {
|
2024-01-30 11:53:53 +01:00
|
|
|
const v = context.dataset.data[context.dataIndex] as unknown as typeof data[0];
|
2023-05-12 05:15:54 +02:00
|
|
|
const m = max(v.y);
|
|
|
|
if (m === 0) {
|
|
|
|
return [`Active: ${v.v} (-%)`];
|
|
|
|
} else {
|
|
|
|
return [`Active: ${v.v} (${Math.round((v.v / m) * 100)}%)`];
|
|
|
|
}
|
2022-12-28 06:13:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
//mode: 'index',
|
|
|
|
animation: {
|
|
|
|
duration: 0,
|
|
|
|
},
|
|
|
|
external: externalTooltipHandler,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(async () => {
|
|
|
|
renderChart();
|
|
|
|
});
|
|
|
|
</script>
|