-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathusage.svelte
More file actions
148 lines (137 loc) · 4.71 KB
/
usage.svelte
File metadata and controls
148 lines (137 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<script context="module" lang="ts">
import { ProjectUsageRange, type Models } from '@appwrite.io/console';
export type UsagePeriods = '24h' | '30d' | '90d';
export function periodToDates(period: UsagePeriods): {
start: string;
end: string;
period: ProjectUsageRange;
} {
const start = new Date();
switch (period) {
case '24h':
start.setUTCHours(start.getHours() - 24);
break;
case '30d':
start.setUTCHours(0, 0, 0, 0);
start.setUTCDate(start.getDate() - 30);
break;
case '90d':
start.setUTCHours(0, 0, 0, 0);
start.setUTCDate(start.getDate() - 90);
break;
}
const end = new Date();
end.setUTCDate(end.getUTCDate() + 1);
end.setUTCHours(0, 0, 0, 0);
return {
start: start.toISOString(),
end: end.toISOString(),
period: period === '24h' ? ProjectUsageRange.OneHour : ProjectUsageRange.OneDay
};
}
export function last(set: Models.Metric[]): Models.Metric | null {
if (!set) return null;
return set.slice(-1)[0] ?? null;
}
export function total(set: Models.Metric[]): number {
return set?.reduce((prev, curr) => prev + curr.value, 0) ?? 0;
}
export function accumulateFromEndingTotal(
metrics: Models.Metric[],
endingTotal: number,
startingDayToFillZero: Date = null
): Array<[string, number]> {
return (metrics ?? []).reduceRight(
(acc, curr) => {
if (startingDayToFillZero !== null && startingDayToFillZero instanceof Date) {
const date = new Date(curr.date);
if (date > startingDayToFillZero) {
acc.data.unshift([date.toISOString(), 0]);
acc.total -= 0;
return acc;
}
}
acc.data.unshift([curr.date, acc.total]);
acc.total -= curr.value;
return acc;
},
{
total: endingTotal,
data: []
}
).data;
}
</script>
<script lang="ts">
import { page } from '$app/state';
import { goto } from '$app/navigation';
import { BarChart } from '$lib/charts';
import { Card } from '$lib/components';
import { InputSelect } from '$lib/elements/forms';
import { formatNumberWithCommas } from '$lib/helpers/numbers';
import { Layout, Typography } from '@appwrite.io/pink-svelte';
type MetricMetadata = {
title: string;
legend: string;
};
export let total: number = 0;
export let count: Models.Metric[];
export let countMetadata: MetricMetadata;
export let path: string = null;
export let hidePeriodSelect = false;
export let isCumulative: boolean = false;
export let showAggregateTotal: boolean = true;
</script>
<Layout.Stack gap="s">
{#if !hidePeriodSelect}
<div
style:max-width="250px"
style:--input-background-color="var(--bgcolor-neutral-primary)">
<InputSelect
on:change={(e) => goto(`${path}/${e.detail}`)}
id="period"
options={[
{
label: '24 hours',
value: '24h'
},
{
label: '30 days',
value: '30d'
},
{
label: '90 days',
value: '90d'
}
]}
value={page.params.period ?? '30d'} />
</div>
{/if}
<Card>
{#if count}
<Layout.Stack gap="xs">
{#if showAggregateTotal}
<Typography.Title>{formatNumberWithCommas(total)}</Typography.Title>
{/if}
<Typography.Text>{countMetadata.title}</Typography.Text>
</Layout.Stack>
<div class="chart-container">
<BarChart
formatted={page.params.period === '24h' ? 'hours' : 'days'}
series={[
{
name: countMetadata.legend,
data: isCumulative
? count.map((m) => [m.date, m.value])
: accumulateFromEndingTotal(count, total)
}
]} />
</div>
{/if}
</Card>
</Layout.Stack>
<style lang="scss">
.chart-container {
height: 12rem;
}
</style>