Skip to content

Commit 6bc500c

Browse files
authored
Debug 2 (#2)
1 parent 8702fa7 commit 6bc500c

File tree

15 files changed

+113
-51
lines changed

15 files changed

+113
-51
lines changed

bun.lockb

-79 Bytes
Binary file not shown.

lefthook.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ pre-commit:
1313
glob: "**/wrangler.toml"
1414
run: cd view && bun generate && cd ../worker && bun generate
1515
stage_fixed: true
16+
check:
17+
run: bun check

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"prettier-plugin-tailwindcss": "^0.6.9"
1212
},
1313
"scripts": {
14-
"prepare": "bunx lefthook install && cd worker; bun i --frozen-lockfile && cd ../view && bun i --frozen-lockfile",
14+
"prepare": "bunx lefthook install",
1515
"generate": "cd view && bun generate && cd ../worker && bun generate",
1616
"check": "cd view && bun check && cd ../worker && bun check"
1717
},

view/bun.lockb

-3.81 KB
Binary file not shown.

view/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"dev": "vite dev",
77
"build": "vite build",
88
"generate": "bun run cf-typegen && svelte-kit sync",
9-
"prepare": "bun generate",
109
"preview": "bun run build && wrangler pages dev",
1110
"check": "bun generate && svelte-check --tsconfig ./tsconfig.json",
1211
"db:push": "drizzle-kit push",
@@ -39,7 +38,6 @@
3938
"apexcharts": "^4.0.0",
4039
"daisyui": "^4.12.14",
4140
"drizzle-orm": "^0.33.0",
42-
"svelte-apexcharts": "^1.0.2",
4341
"valibot": "^1.0.0-beta.7"
4442
}
4543
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<script lang="ts">
2-
const { total, perDay }: { total: number; perDay: number } = $props();
2+
const { total, duration }: { total: number; duration: number } = $props();
3+
import { HOUR, DAY } from '$lib/consts';
4+
const perHour = $derived(total / (duration / HOUR));
5+
const perDay = $derived(total / (duration / DAY));
36
</script>
47

58
<div class="card card-compact inline-block w-96 bg-base-100 align-top shadow-xl">
@@ -8,7 +11,7 @@
811
<span class="text-xl">{total}</span>
912
<hr />
1013
<span class="text-xl">
11-
{perDay.toFixed(1)} / day
14+
{perHour.toFixed(1)} / Hour, {perDay.toFixed(1)} / Day
1215
</span>
1316
</div>
1417
</div>

view/src/components/charts/Line.svelte

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
44
type Props = { dataset: { name: string; data: number[] }[]; titles: Date[] };
55
const { dataset, titles }: Props = $props();
6-
$inspect(titles);
6+
onMount(() => {
7+
console.log(titles);
8+
console.log(dataset);
9+
});
710
const options = {
811
series: dataset,
912
chart: {
@@ -18,15 +21,17 @@
1821
},
1922
xaxis: {
2023
type: 'datetime',
21-
categories: titles.map((title) => title.toLocaleDateString())
24+
// it's written in UTC time for some reason. converting to JST.
25+
categories: titles.map((title) =>
26+
new Date(title.getTime() + 9 * 60 * 60 * 1000).toISOString()
27+
)
2228
},
2329
tooltip: {
2430
x: {
25-
format: 'dd/MM/yy HH:mm'
31+
format: 'MM/dd HH:mm'
2632
}
2733
}
2834
};
29-
const id = Math.random().toFixed(6).toString();
3035
onMount(async () => {
3136
const { default: ApexCharts } = await import('apexcharts');
3237
const chart = new ApexCharts(document.querySelector('#chart-line'), options);

view/src/lib/consts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const DAY = 24 * 60 * 60 * 1000;
2+
export const HOUR = 60 * 60 * 1000;

view/src/lib/utils.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { test, expect } from 'bun:test';
2+
import { groupByTime } from './utils';
3+
4+
const now = new Date();
5+
const then = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000);
6+
7+
test('group by time: 1', () => {
8+
const got = groupByTime(now, then, [new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 + 50)], 3);
9+
expect(got).toEqual([0, 0, 1]);
10+
});
11+
test('group by time: 2', () => {
12+
const got = groupByTime(now, then, [new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 - 50)], 3);
13+
expect(got).toEqual([0, 1, 0]);
14+
});
15+
test('group by time: 3', () => {
16+
const got = groupByTime(now, then, [new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000 + 50)], 3);
17+
expect(got).toEqual([1, 0, 0]);
18+
});
19+
test('group by time: 4', () => {
20+
const got = groupByTime(
21+
now,
22+
then,
23+
[
24+
new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 + 50),
25+
new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 + 50),
26+
new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 + 50),
27+
new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 + 50),
28+
new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 - 50)
29+
],
30+
3
31+
);
32+
expect(got).toEqual([0, 1, 4]);
33+
});
34+
test('group by time: 5', () => {
35+
const got = groupByTime(
36+
now,
37+
then,
38+
[
39+
new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000 + 50),
40+
new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000 + 50),
41+
new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000 + 50),
42+
new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000 + 50)
43+
],
44+
3
45+
);
46+
expect(got).toEqual([4, 0, 0]);
47+
});

view/src/lib/utils.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ export function unwrap<T>(val: T | null | undefined): T {
88
export function panic(message: string): never {
99
throw new Error(message);
1010
}
11+
export function stairs(len: number, top: number): number[] {
12+
const ret: number[] = [];
13+
for (let i = 0; i < len; i++) {
14+
ret.push((top / len) * i);
15+
}
16+
return ret;
17+
}
1118

1219
export function groupBy<T, U>(list: T[], groupFn: (t: T) => U): { key: U; val: T[] }[] {
1320
const map = new Map<U, T[]>();
@@ -25,34 +32,19 @@ export function groupBy<T, U>(list: T[], groupFn: (t: T) => U): { key: U; val: T
2532
return ret;
2633
}
2734

28-
export function groupSteps(
29-
current: Date,
30-
list: Visit[],
31-
start: Date,
32-
steps: number
33-
): [number, Date][] {
35+
export function groupByTime(current: Date, start: Date, list: Date[], steps: number): number[] {
3436
const currentTime = current.getTime();
3537
const totalDuration = currentTime - start.getTime();
3638
const stepWidth = totalDuration / steps;
37-
const values = list.map((i) => currentTime - i.at.getTime()); // all should be pos
38-
const result: [number, Date][] = groupInSteps(values, stepWidth).map(
39-
(count, index) => [count, new Date(currentTime - index * stepWidth)] as const
40-
);
41-
return result;
39+
const values = list.map((i) => currentTime - i.getTime()); // all should be pos
40+
const result: number[] = groupInSteps(values, totalDuration, steps);
41+
return result.reverse();
4242
}
4343

44-
export function stairs(len: number, top: number): number[] {
45-
const ret: number[] = [];
46-
for (let i = 0; i < len; i++) {
47-
ret.push((top / len) * i);
48-
}
49-
return ret;
50-
}
51-
function groupInSteps(list: number[], stepWidth: number): number[] {
52-
const maxVal = list.reduce((a, b) => Math.max(a, b));
53-
const steps = Math.ceil(maxVal / stepWidth);
54-
const arr: number[] = new Array(steps + 1).fill(0);
55-
for (const datapoint of list) {
44+
export function groupInSteps(input: number[], maxVal: number, length: number): number[] {
45+
const stepWidth = Math.ceil(maxVal / length);
46+
const arr: number[] = new Array(length).fill(0);
47+
for (const datapoint of input) {
5648
const idx = Math.floor(datapoint / stepWidth);
5749
const val = arr[idx];
5850
if (val === undefined)

0 commit comments

Comments
 (0)