diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/lib/stat-manager.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/lib/stat-manager.ts')
-rw-r--r-- | server/lib/stat-manager.ts | 182 |
1 files changed, 0 insertions, 182 deletions
diff --git a/server/lib/stat-manager.ts b/server/lib/stat-manager.ts deleted file mode 100644 index 0516e7f1a..000000000 --- a/server/lib/stat-manager.ts +++ /dev/null | |||
@@ -1,182 +0,0 @@ | |||
1 | import { mapSeries } from 'bluebird' | ||
2 | import { CONFIG } from '@server/initializers/config' | ||
3 | import { ActorFollowModel } from '@server/models/actor/actor-follow' | ||
4 | import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy' | ||
5 | import { UserModel } from '@server/models/user/user' | ||
6 | import { VideoModel } from '@server/models/video/video' | ||
7 | import { VideoChannelModel } from '@server/models/video/video-channel' | ||
8 | import { VideoCommentModel } from '@server/models/video/video-comment' | ||
9 | import { VideoFileModel } from '@server/models/video/video-file' | ||
10 | import { VideoPlaylistModel } from '@server/models/video/video-playlist' | ||
11 | import { ActivityType, ServerStats, VideoRedundancyStrategyWithManual } from '@shared/models' | ||
12 | |||
13 | class StatsManager { | ||
14 | |||
15 | private static instance: StatsManager | ||
16 | |||
17 | private readonly instanceStartDate = new Date() | ||
18 | |||
19 | private readonly inboxMessages = { | ||
20 | processed: 0, | ||
21 | errors: 0, | ||
22 | successes: 0, | ||
23 | waiting: 0, | ||
24 | errorsPerType: this.buildAPPerType(), | ||
25 | successesPerType: this.buildAPPerType() | ||
26 | } | ||
27 | |||
28 | private constructor () {} | ||
29 | |||
30 | updateInboxWaiting (inboxMessagesWaiting: number) { | ||
31 | this.inboxMessages.waiting = inboxMessagesWaiting | ||
32 | } | ||
33 | |||
34 | addInboxProcessedSuccess (type: ActivityType) { | ||
35 | this.inboxMessages.processed++ | ||
36 | this.inboxMessages.successes++ | ||
37 | this.inboxMessages.successesPerType[type]++ | ||
38 | } | ||
39 | |||
40 | addInboxProcessedError (type: ActivityType) { | ||
41 | this.inboxMessages.processed++ | ||
42 | this.inboxMessages.errors++ | ||
43 | this.inboxMessages.errorsPerType[type]++ | ||
44 | } | ||
45 | |||
46 | async getStats () { | ||
47 | const { totalLocalVideos, totalLocalVideoViews, totalVideos } = await VideoModel.getStats() | ||
48 | const { totalLocalVideoComments, totalVideoComments } = await VideoCommentModel.getStats() | ||
49 | const { totalUsers, totalDailyActiveUsers, totalWeeklyActiveUsers, totalMonthlyActiveUsers } = await UserModel.getStats() | ||
50 | const { totalInstanceFollowers, totalInstanceFollowing } = await ActorFollowModel.getStats() | ||
51 | const { totalLocalVideoFilesSize } = await VideoFileModel.getStats() | ||
52 | const { | ||
53 | totalLocalVideoChannels, | ||
54 | totalLocalDailyActiveVideoChannels, | ||
55 | totalLocalWeeklyActiveVideoChannels, | ||
56 | totalLocalMonthlyActiveVideoChannels | ||
57 | } = await VideoChannelModel.getStats() | ||
58 | const { totalLocalPlaylists } = await VideoPlaylistModel.getStats() | ||
59 | |||
60 | const videosRedundancyStats = await this.buildRedundancyStats() | ||
61 | |||
62 | const data: ServerStats = { | ||
63 | totalUsers, | ||
64 | totalDailyActiveUsers, | ||
65 | totalWeeklyActiveUsers, | ||
66 | totalMonthlyActiveUsers, | ||
67 | |||
68 | totalLocalVideos, | ||
69 | totalLocalVideoViews, | ||
70 | totalLocalVideoComments, | ||
71 | totalLocalVideoFilesSize, | ||
72 | |||
73 | totalVideos, | ||
74 | totalVideoComments, | ||
75 | |||
76 | totalLocalVideoChannels, | ||
77 | totalLocalDailyActiveVideoChannels, | ||
78 | totalLocalWeeklyActiveVideoChannels, | ||
79 | totalLocalMonthlyActiveVideoChannels, | ||
80 | |||
81 | totalLocalPlaylists, | ||
82 | |||
83 | totalInstanceFollowers, | ||
84 | totalInstanceFollowing, | ||
85 | |||
86 | videosRedundancy: videosRedundancyStats, | ||
87 | |||
88 | ...this.buildAPStats() | ||
89 | } | ||
90 | |||
91 | return data | ||
92 | } | ||
93 | |||
94 | private buildActivityPubMessagesProcessedPerSecond () { | ||
95 | const now = new Date() | ||
96 | const startedSeconds = (now.getTime() - this.instanceStartDate.getTime()) / 1000 | ||
97 | |||
98 | return this.inboxMessages.processed / startedSeconds | ||
99 | } | ||
100 | |||
101 | private buildRedundancyStats () { | ||
102 | const strategies = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES | ||
103 | .map(r => ({ | ||
104 | strategy: r.strategy as VideoRedundancyStrategyWithManual, | ||
105 | size: r.size | ||
106 | })) | ||
107 | |||
108 | strategies.push({ strategy: 'manual', size: null }) | ||
109 | |||
110 | return mapSeries(strategies, r => { | ||
111 | return VideoRedundancyModel.getStats(r.strategy) | ||
112 | .then(stats => Object.assign(stats, { strategy: r.strategy, totalSize: r.size })) | ||
113 | }) | ||
114 | } | ||
115 | |||
116 | private buildAPPerType () { | ||
117 | return { | ||
118 | Create: 0, | ||
119 | Update: 0, | ||
120 | Delete: 0, | ||
121 | Follow: 0, | ||
122 | Accept: 0, | ||
123 | Reject: 0, | ||
124 | Announce: 0, | ||
125 | Undo: 0, | ||
126 | Like: 0, | ||
127 | Dislike: 0, | ||
128 | Flag: 0, | ||
129 | View: 0 | ||
130 | } | ||
131 | } | ||
132 | |||
133 | private buildAPStats () { | ||
134 | return { | ||
135 | totalActivityPubMessagesProcessed: this.inboxMessages.processed, | ||
136 | |||
137 | totalActivityPubMessagesSuccesses: this.inboxMessages.successes, | ||
138 | |||
139 | // Dirty, but simpler and with type checking | ||
140 | totalActivityPubCreateMessagesSuccesses: this.inboxMessages.successesPerType.Create, | ||
141 | totalActivityPubUpdateMessagesSuccesses: this.inboxMessages.successesPerType.Update, | ||
142 | totalActivityPubDeleteMessagesSuccesses: this.inboxMessages.successesPerType.Delete, | ||
143 | totalActivityPubFollowMessagesSuccesses: this.inboxMessages.successesPerType.Follow, | ||
144 | totalActivityPubAcceptMessagesSuccesses: this.inboxMessages.successesPerType.Accept, | ||
145 | totalActivityPubRejectMessagesSuccesses: this.inboxMessages.successesPerType.Reject, | ||
146 | totalActivityPubAnnounceMessagesSuccesses: this.inboxMessages.successesPerType.Announce, | ||
147 | totalActivityPubUndoMessagesSuccesses: this.inboxMessages.successesPerType.Undo, | ||
148 | totalActivityPubLikeMessagesSuccesses: this.inboxMessages.successesPerType.Like, | ||
149 | totalActivityPubDislikeMessagesSuccesses: this.inboxMessages.successesPerType.Dislike, | ||
150 | totalActivityPubFlagMessagesSuccesses: this.inboxMessages.successesPerType.Flag, | ||
151 | totalActivityPubViewMessagesSuccesses: this.inboxMessages.successesPerType.View, | ||
152 | |||
153 | totalActivityPubCreateMessagesErrors: this.inboxMessages.errorsPerType.Create, | ||
154 | totalActivityPubUpdateMessagesErrors: this.inboxMessages.errorsPerType.Update, | ||
155 | totalActivityPubDeleteMessagesErrors: this.inboxMessages.errorsPerType.Delete, | ||
156 | totalActivityPubFollowMessagesErrors: this.inboxMessages.errorsPerType.Follow, | ||
157 | totalActivityPubAcceptMessagesErrors: this.inboxMessages.errorsPerType.Accept, | ||
158 | totalActivityPubRejectMessagesErrors: this.inboxMessages.errorsPerType.Reject, | ||
159 | totalActivityPubAnnounceMessagesErrors: this.inboxMessages.errorsPerType.Announce, | ||
160 | totalActivityPubUndoMessagesErrors: this.inboxMessages.errorsPerType.Undo, | ||
161 | totalActivityPubLikeMessagesErrors: this.inboxMessages.errorsPerType.Like, | ||
162 | totalActivityPubDislikeMessagesErrors: this.inboxMessages.errorsPerType.Dislike, | ||
163 | totalActivityPubFlagMessagesErrors: this.inboxMessages.errorsPerType.Flag, | ||
164 | totalActivityPubViewMessagesErrors: this.inboxMessages.errorsPerType.View, | ||
165 | |||
166 | totalActivityPubMessagesErrors: this.inboxMessages.errors, | ||
167 | |||
168 | activityPubMessagesProcessedPerSecond: this.buildActivityPubMessagesProcessedPerSecond(), | ||
169 | totalActivityPubMessagesWaiting: this.inboxMessages.waiting | ||
170 | } | ||
171 | } | ||
172 | |||
173 | static get Instance () { | ||
174 | return this.instance || (this.instance = new this()) | ||
175 | } | ||
176 | } | ||
177 | |||
178 | // --------------------------------------------------------------------------- | ||
179 | |||
180 | export { | ||
181 | StatsManager | ||
182 | } | ||