summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/admin/chart.ts
blob: c351c7167d4c7a1dbb9e8da43f612d7bee022498 (plain)
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
import Stats, { IStats } from '../../../../models/stats';

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export const meta = {
	requireCredential: true,
	requireAdmin: true
};

export default (params: any) => new Promise(async (res, rej) => {
	const now = new Date();
	const y = now.getFullYear();
	const m = now.getMonth();
	const d = now.getDate();

	const stats = await Stats.find({
		span: 'day',
		date: {
			$gt: new Date(y - 1, m, d)
		}
	}, {
		sort: {
			date: -1
		},
		fields: {
			_id: 0
		}
	});

	const chart: Array<Omit<IStats, '_id'>> = [];

	for (let i = 364; i >= 0; i--) {
		const day = new Date(y, m, d - i);

		const stat = stats.find(s => s.date.getTime() == day.getTime());

		if (stat) {
			chart.unshift(stat);
		} else { // 隙間埋め
			const mostRecent = stats.find(s => s.date.getTime() < day.getTime());
			if (mostRecent) {
				chart.unshift(Object.assign({}, mostRecent, {
					date: day
				}));
			} else {
				chart.unshift({
					date: day,
					span: 'day',
					users: {
						local: {
							total: 0,
							diff: 0
						},
						remote: {
							total: 0,
							diff: 0
						}
					},
					notes: {
						local: {
							total: 0,
							diff: 0,
							diffs: {
								normal: 0,
								reply: 0,
								renote: 0
							}
						},
						remote: {
							total: 0,
							diff: 0,
							diffs: {
								normal: 0,
								reply: 0,
								renote: 0
							}
						}
					},
					drive: {
						local: {
							totalCount: 0,
							totalSize: 0,
							diffCount: 0,
							diffSize: 0
						},
						remote: {
							totalCount: 0,
							totalSize: 0,
							diffCount: 0,
							diffSize: 0
						}
					}
				});
			}
		}
	}

	chart.forEach(x => {
		delete x.date;
	});

	res(chart);
});