summaryrefslogtreecommitdiff
path: root/src/services/update-chart.ts
blob: 78834ba60141b044e6ddffc7008173dc6736f551 (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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { INote } from '../models/note';
import Stats, { IStats } from '../models/stats';
import { isLocalUser, IUser } from '../models/user';
import { IDriveFile } from '../models/drive-file';

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

async function getCurrentStats(span: 'day' | 'hour'): Promise<IStats> {
	const now = new Date();
	const y = now.getFullYear();
	const m = now.getMonth();
	const d = now.getDate();
	const h = now.getHours();

	const current =
		span == 'day' ? new Date(y, m, d) :
		span == 'hour' ? new Date(y, m, d, h) :
		null;

	// 現在(今日または今のHour)の統計
	const currentStats = await Stats.findOne({
		span: span,
		date: current
	});

	if (currentStats) {
		return currentStats;
	} else {
		// 集計期間が変わってから、初めてのチャート更新なら
		// 最も最近の統計を持ってくる
		// * 例えば集計期間が「日」である場合で考えると、
		// * 昨日何もチャートを更新するような出来事がなかった場合は、
		// * 統計がそもそも作られずドキュメントが存在しないということがあり得るため、
		// * 「昨日の」と決め打ちせずに「もっとも最近の」とします
		const mostRecentStats = await Stats.findOne({
			span: span
		}, {
			sort: {
				date: -1
			}
		});

		if (mostRecentStats) {
			// 現在の統計を初期挿入
			const data: Omit<IStats, '_id'> = {
				span: span,
				date: current,
				users: {
					local: {
						total: mostRecentStats.users.local.total,
						inc: 0,
						dec: 0
					},
					remote: {
						total: mostRecentStats.users.remote.total,
						inc: 0,
						dec: 0
					}
				},
				notes: {
					local: {
						total: mostRecentStats.notes.local.total,
						inc: 0,
						dec: 0,
						diffs: {
							normal: 0,
							reply: 0,
							renote: 0
						}
					},
					remote: {
						total: mostRecentStats.notes.remote.total,
						inc: 0,
						dec: 0,
						diffs: {
							normal: 0,
							reply: 0,
							renote: 0
						}
					}
				},
				drive: {
					local: {
						totalCount: mostRecentStats.drive.local.totalCount,
						totalSize: mostRecentStats.drive.local.totalSize,
						incCount: 0,
						incSize: 0,
						decCount: 0,
						decSize: 0
					},
					remote: {
						totalCount: mostRecentStats.drive.remote.totalCount,
						totalSize: mostRecentStats.drive.remote.totalSize,
						incCount: 0,
						incSize: 0,
						decCount: 0,
						decSize: 0
					}
				},
				network: {
					requests: 0,
					totalTime: 0,
					incomingBytes: 0,
					outgoingBytes: 0
				}
			};

			const stats = await Stats.insert(data);

			return stats;
		} else {
			// 統計が存在しなかったら
			// * Misskeyインスタンスを建てて初めてのチャート更新時など

			// 空の統計を作成
			const emptyStat: Omit<IStats, '_id'> = {
				span: span,
				date: current,
				users: {
					local: {
						total: 0,
						inc: 0,
						dec: 0
					},
					remote: {
						total: 0,
						inc: 0,
						dec: 0
					}
				},
				notes: {
					local: {
						total: 0,
						inc: 0,
						dec: 0,
						diffs: {
							normal: 0,
							reply: 0,
							renote: 0
						}
					},
					remote: {
						total: 0,
						inc: 0,
						dec: 0,
						diffs: {
							normal: 0,
							reply: 0,
							renote: 0
						}
					}
				},
				drive: {
					local: {
						totalCount: 0,
						totalSize: 0,
						incCount: 0,
						incSize: 0,
						decCount: 0,
						decSize: 0
					},
					remote: {
						totalCount: 0,
						totalSize: 0,
						incCount: 0,
						incSize: 0,
						decCount: 0,
						decSize: 0
					}
				},
				network: {
					requests: 0,
					totalTime: 0,
					incomingBytes: 0,
					outgoingBytes: 0
				}
			};

			const stats = await Stats.insert(emptyStat);

			return stats;
		}
	}
}

function update(inc: any) {
	getCurrentStats('day').then(stats => {
		Stats.findOneAndUpdate({
			_id: stats._id
		}, {
			$inc: inc
		});
	});

	getCurrentStats('hour').then(stats => {
		Stats.findOneAndUpdate({
			_id: stats._id
		}, {
			$inc: inc
		});
	});
}

export async function updateUserStats(user: IUser, isAdditional: boolean) {
	const origin = isLocalUser(user) ? 'local' : 'remote';

	const inc = {} as any;
	inc[`users.${origin}.total`] = isAdditional ? 1 : -1;
	if (isAdditional) {
		inc[`users.${origin}.inc`] = 1;
	} else {
		inc[`users.${origin}.dec`] = 1;
	}

	await update(inc);
}

export async function updateNoteStats(note: INote, isAdditional: boolean) {
	const origin = isLocalUser(note._user) ? 'local' : 'remote';

	const inc = {} as any;

	inc[`notes.${origin}.total`] = isAdditional ? 1 : -1;

	if (isAdditional) {
		inc[`notes.${origin}.inc`] = 1;
	} else {
		inc[`notes.${origin}.dec`] = 1;
	}

	if (note.replyId != null) {
		inc[`notes.${origin}.diffs.reply`] = isAdditional ? 1 : -1;
	} else if (note.renoteId != null) {
		inc[`notes.${origin}.diffs.renote`] = isAdditional ? 1 : -1;
	} else {
		inc[`notes.${origin}.diffs.normal`] = isAdditional ? 1 : -1;
	}

	await update(inc);
}

export async function updateDriveStats(file: IDriveFile, isAdditional: boolean) {
	const origin = isLocalUser(file.metadata._user) ? 'local' : 'remote';

	const inc = {} as any;
	inc[`drive.${origin}.totalCount`] = isAdditional ? 1 : -1;
	inc[`drive.${origin}.totalSize`] = isAdditional ? file.length : -file.length;
	if (isAdditional) {
		inc[`drive.${origin}.incCount`] = 1;
		inc[`drive.${origin}.incSize`] = file.length;
	} else {
		inc[`drive.${origin}.decCount`] = 1;
		inc[`drive.${origin}.decSize`] = file.length;
	}

	await update(inc);
}

export async function updateNetworkStats(requests: number, time: number, incomingBytes: number, outgoingBytes: number) {
	const inc = {} as any;
	inc['network.requests'] = requests;
	inc['network.totalTime'] = time;
	inc['network.incomingBytes'] = incomingBytes;
	inc['network.outgoingBytes'] = outgoingBytes;

	await update(inc);
}