Skip to content

Commit 27b0c81

Browse files
committed
fix start date
1 parent a0d668a commit 27b0c81

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

view/src/lib/utils.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,23 @@ export function groupBy<T, U>(list: T[], groupFn: (t: T) => U): { key: U; val: T
2525
return ret;
2626
}
2727

28-
const STEP_WIDTH = 2 * 60 * 60 * 10000;
29-
export function groupByHour(current: Date, list: Visit[]): [number, Date][] {
28+
export function groupSteps(
29+
current: Date,
30+
list: Visit[],
31+
start: Date,
32+
steps: number
33+
): [number, Date][] {
3034
const currentTime = current.getTime();
35+
const totalDuration = currentTime - start.getTime();
36+
const stepWidth = totalDuration / steps;
3137
const values = list.map((i) => currentTime - i.at.getTime()); // all should be pos
32-
const result: [number, Date][] = groupInSteps(values, STEP_WIDTH).map((count, index) => [
33-
count,
34-
new Date(currentTime - index * STEP_WIDTH)
35-
]);
38+
const result: [number, Date][] = groupInSteps(values, stepWidth).map(
39+
(count, index) => [count, new Date(currentTime - index * stepWidth)] as const
40+
);
3641
return result;
3742
}
3843

39-
export function groupInSteps(list: number[], stepWidth: number): number[] {
44+
function groupInSteps(list: number[], stepWidth: number): number[] {
4045
const maxVal = list.reduce((a, b) => Math.max(a, b));
4146
const steps = Math.ceil(maxVal / stepWidth);
4247
const arr: number[] = new Array(steps + 1).fill(0);

view/src/pages/Dashboard.svelte

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import Line from '~/components/charts/Line.svelte';
44
import PieChart from '~/components/charts/PieChart.svelte';
55
import type { Visit } from '~/db/schema';
6-
import { groupBy, groupByHour } from '~/lib/utils';
6+
import { groupBy, groupSteps } from '~/lib/utils';
77
import type { Kind } from '~/share/schema';
88
99
const URL_LABELS = new Map([
@@ -15,13 +15,17 @@
1515
['security.utcode.net', 'セキュリティ']
1616
]);
1717
18+
const SAMPLING_COUNT = 20;
19+
1820
type Props = {
1921
data: Visit[];
2022
duration: number;
2123
lastFetch: Date;
2224
kind: Kind | 'all';
2325
};
26+
const MILLISECS_PER_DAY = 24 * 60 * 60 * 1000;
2427
const { data, duration, lastFetch }: Props = $props();
28+
const start = $derived(new Date(lastFetch.getTime() - duration * MILLISECS_PER_DAY));
2529
const sanitizedData = $derived(
2630
data.map((item) => {
2731
const sanitized = item.url.split('://')[1]?.split('/')[0];
@@ -46,11 +50,13 @@
4650
})
4751
);
4852
49-
const titles = $derived(groupByHour(lastFetch, grouped[0].val).map((v) => v[1]));
53+
const titles = $derived(
54+
groupSteps(lastFetch, grouped[0].val, start, SAMPLING_COUNT).map((v) => v[1])
55+
);
5056
const linedata = $derived(
5157
grouped.map((e) => ({
5258
name: URL_LABELS.get(e.key) ?? e.key,
53-
data: groupByHour(lastFetch, e.val).map((row) => row[0])
59+
data: groupSteps(lastFetch, e.val, start, SAMPLING_COUNT).map((row) => row[0])
5460
}))
5561
);
5662
</script>

0 commit comments

Comments
 (0)