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/views/shared/video-viewer-stats.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/views/shared/video-viewer-stats.ts')
-rw-r--r-- | server/lib/views/shared/video-viewer-stats.ts | 196 |
1 files changed, 0 insertions, 196 deletions
diff --git a/server/lib/views/shared/video-viewer-stats.ts b/server/lib/views/shared/video-viewer-stats.ts deleted file mode 100644 index ebd963e59..000000000 --- a/server/lib/views/shared/video-viewer-stats.ts +++ /dev/null | |||
@@ -1,196 +0,0 @@ | |||
1 | import { Transaction } from 'sequelize/types' | ||
2 | import { isTestOrDevInstance } from '@server/helpers/core-utils' | ||
3 | import { GeoIP } from '@server/helpers/geo-ip' | ||
4 | import { logger, loggerTagsFactory } from '@server/helpers/logger' | ||
5 | import { MAX_LOCAL_VIEWER_WATCH_SECTIONS, VIEW_LIFETIME } from '@server/initializers/constants' | ||
6 | import { sequelizeTypescript } from '@server/initializers/database' | ||
7 | import { sendCreateWatchAction } from '@server/lib/activitypub/send' | ||
8 | import { getLocalVideoViewerActivityPubUrl } from '@server/lib/activitypub/url' | ||
9 | import { Redis } from '@server/lib/redis' | ||
10 | import { VideoModel } from '@server/models/video/video' | ||
11 | import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer' | ||
12 | import { LocalVideoViewerWatchSectionModel } from '@server/models/view/local-video-viewer-watch-section' | ||
13 | import { MVideo, MVideoImmutable } from '@server/types/models' | ||
14 | import { VideoViewEvent } from '@shared/models' | ||
15 | |||
16 | const lTags = loggerTagsFactory('views') | ||
17 | |||
18 | type LocalViewerStats = { | ||
19 | firstUpdated: number // Date.getTime() | ||
20 | lastUpdated: number // Date.getTime() | ||
21 | |||
22 | watchSections: { | ||
23 | start: number | ||
24 | end: number | ||
25 | }[] | ||
26 | |||
27 | watchTime: number | ||
28 | |||
29 | country: string | ||
30 | |||
31 | videoId: number | ||
32 | } | ||
33 | |||
34 | export class VideoViewerStats { | ||
35 | private processingViewersStats = false | ||
36 | |||
37 | constructor () { | ||
38 | setInterval(() => this.processViewerStats(), VIEW_LIFETIME.VIEWER_STATS) | ||
39 | } | ||
40 | |||
41 | // --------------------------------------------------------------------------- | ||
42 | |||
43 | async addLocalViewer (options: { | ||
44 | video: MVideoImmutable | ||
45 | currentTime: number | ||
46 | ip: string | ||
47 | viewEvent?: VideoViewEvent | ||
48 | }) { | ||
49 | const { video, ip, viewEvent, currentTime } = options | ||
50 | |||
51 | logger.debug('Adding local viewer to video stats %s.', video.uuid, { currentTime, viewEvent, ...lTags(video.uuid) }) | ||
52 | |||
53 | return this.updateLocalViewerStats({ video, viewEvent, currentTime, ip }) | ||
54 | } | ||
55 | |||
56 | // --------------------------------------------------------------------------- | ||
57 | |||
58 | async getWatchTime (videoId: number, ip: string) { | ||
59 | const stats: LocalViewerStats = await Redis.Instance.getLocalVideoViewer({ ip, videoId }) | ||
60 | |||
61 | return stats?.watchTime || 0 | ||
62 | } | ||
63 | |||
64 | // --------------------------------------------------------------------------- | ||
65 | |||
66 | private async updateLocalViewerStats (options: { | ||
67 | video: MVideoImmutable | ||
68 | ip: string | ||
69 | currentTime: number | ||
70 | viewEvent?: VideoViewEvent | ||
71 | }) { | ||
72 | const { video, ip, viewEvent, currentTime } = options | ||
73 | const nowMs = new Date().getTime() | ||
74 | |||
75 | let stats: LocalViewerStats = await Redis.Instance.getLocalVideoViewer({ ip, videoId: video.id }) | ||
76 | |||
77 | if (stats && stats.watchSections.length >= MAX_LOCAL_VIEWER_WATCH_SECTIONS) { | ||
78 | logger.warn('Too much watch section to store for a viewer, skipping this one', { currentTime, viewEvent, ...lTags(video.uuid) }) | ||
79 | return | ||
80 | } | ||
81 | |||
82 | if (!stats) { | ||
83 | const country = await GeoIP.Instance.safeCountryISOLookup(ip) | ||
84 | |||
85 | stats = { | ||
86 | firstUpdated: nowMs, | ||
87 | lastUpdated: nowMs, | ||
88 | |||
89 | watchSections: [], | ||
90 | |||
91 | watchTime: 0, | ||
92 | |||
93 | country, | ||
94 | videoId: video.id | ||
95 | } | ||
96 | } | ||
97 | |||
98 | stats.lastUpdated = nowMs | ||
99 | |||
100 | if (viewEvent === 'seek' || stats.watchSections.length === 0) { | ||
101 | stats.watchSections.push({ | ||
102 | start: currentTime, | ||
103 | end: currentTime | ||
104 | }) | ||
105 | } else { | ||
106 | const lastSection = stats.watchSections[stats.watchSections.length - 1] | ||
107 | |||
108 | if (lastSection.start > currentTime) { | ||
109 | logger.debug('Invalid end watch section %d. Last start record was at %d. Starting a new section.', currentTime, lastSection.start) | ||
110 | |||
111 | stats.watchSections.push({ | ||
112 | start: currentTime, | ||
113 | end: currentTime | ||
114 | }) | ||
115 | } else { | ||
116 | lastSection.end = currentTime | ||
117 | } | ||
118 | } | ||
119 | |||
120 | stats.watchTime = this.buildWatchTimeFromSections(stats.watchSections) | ||
121 | |||
122 | logger.debug('Set local video viewer stats for video %s.', video.uuid, { stats, ...lTags(video.uuid) }) | ||
123 | |||
124 | await Redis.Instance.setLocalVideoViewer(ip, video.id, stats) | ||
125 | } | ||
126 | |||
127 | async processViewerStats () { | ||
128 | if (this.processingViewersStats) return | ||
129 | this.processingViewersStats = true | ||
130 | |||
131 | if (!isTestOrDevInstance()) logger.info('Processing viewer statistics.', lTags()) | ||
132 | |||
133 | const now = new Date().getTime() | ||
134 | |||
135 | try { | ||
136 | const allKeys = await Redis.Instance.listLocalVideoViewerKeys() | ||
137 | |||
138 | for (const key of allKeys) { | ||
139 | const stats: LocalViewerStats = await Redis.Instance.getLocalVideoViewer({ key }) | ||
140 | |||
141 | // Process expired stats | ||
142 | if (stats.lastUpdated > now - VIEW_LIFETIME.VIEWER_STATS) { | ||
143 | continue | ||
144 | } | ||
145 | |||
146 | try { | ||
147 | await sequelizeTypescript.transaction(async t => { | ||
148 | const video = await VideoModel.load(stats.videoId, t) | ||
149 | if (!video) return | ||
150 | |||
151 | const statsModel = await this.saveViewerStats(video, stats, t) | ||
152 | |||
153 | if (video.remote) { | ||
154 | await sendCreateWatchAction(statsModel, t) | ||
155 | } | ||
156 | }) | ||
157 | |||
158 | await Redis.Instance.deleteLocalVideoViewersKeys(key) | ||
159 | } catch (err) { | ||
160 | logger.error('Cannot process viewer stats for Redis key %s.', key, { err, ...lTags() }) | ||
161 | } | ||
162 | } | ||
163 | } catch (err) { | ||
164 | logger.error('Error in video save viewers stats scheduler.', { err, ...lTags() }) | ||
165 | } | ||
166 | |||
167 | this.processingViewersStats = false | ||
168 | } | ||
169 | |||
170 | private async saveViewerStats (video: MVideo, stats: LocalViewerStats, transaction: Transaction) { | ||
171 | const statsModel = new LocalVideoViewerModel({ | ||
172 | startDate: new Date(stats.firstUpdated), | ||
173 | endDate: new Date(stats.lastUpdated), | ||
174 | watchTime: stats.watchTime, | ||
175 | country: stats.country, | ||
176 | videoId: video.id | ||
177 | }) | ||
178 | |||
179 | statsModel.url = getLocalVideoViewerActivityPubUrl(statsModel) | ||
180 | statsModel.Video = video as VideoModel | ||
181 | |||
182 | await statsModel.save({ transaction }) | ||
183 | |||
184 | statsModel.WatchSections = await LocalVideoViewerWatchSectionModel.bulkCreateSections({ | ||
185 | localVideoViewerId: statsModel.id, | ||
186 | watchSections: stats.watchSections, | ||
187 | transaction | ||
188 | }) | ||
189 | |||
190 | return statsModel | ||
191 | } | ||
192 | |||
193 | private buildWatchTimeFromSections (sections: { start: number, end: number }[]) { | ||
194 | return sections.reduce((p, current) => p + (current.end - current.start), 0) | ||
195 | } | ||
196 | } | ||