diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2021-04-12 11:19:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-12 11:19:07 +0200 |
commit | fe19f600dab0f6b00a7aa146ba4bd4bb96536155 (patch) | |
tree | be388f89a41cbc257fc9a642a9205b4910b7a6b7 /server/models/video/video-channel.ts | |
parent | a472cf033003cf96b69a80808b2dce1fe382e09b (diff) | |
download | PeerTube-fe19f600dab0f6b00a7aa146ba4bd4bb96536155.tar.gz PeerTube-fe19f600dab0f6b00a7aa146ba4bd4bb96536155.tar.zst PeerTube-fe19f600dab0f6b00a7aa146ba4bd4bb96536155.zip |
add channel and playlist stats to server stats endpoint (#3747)
* add channel and playlist stats to nodeinfo
* add tests for active video channels stats
* fix tests for active channel stats
Diffstat (limited to 'server/models/video/video-channel.ts')
-rw-r--r-- | server/models/video/video-channel.ts | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index d2a055f5b..b7ffbd3b1 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import { FindOptions, Includeable, literal, Op, ScopeOptions } from 'sequelize' | 1 | import { FindOptions, Includeable, literal, Op, QueryTypes, ScopeOptions } from 'sequelize' |
2 | import { | 2 | import { |
3 | AllowNull, | 3 | AllowNull, |
4 | BeforeDestroy, | 4 | BeforeDestroy, |
@@ -338,6 +338,47 @@ export class VideoChannelModel extends Model { | |||
338 | return VideoChannelModel.count(query) | 338 | return VideoChannelModel.count(query) |
339 | } | 339 | } |
340 | 340 | ||
341 | static async getStats () { | ||
342 | |||
343 | function getActiveVideoChannels (days: number) { | ||
344 | const options = { | ||
345 | type: QueryTypes.SELECT as QueryTypes.SELECT, | ||
346 | raw: true | ||
347 | } | ||
348 | |||
349 | const query = ` | ||
350 | SELECT COUNT(DISTINCT("VideoChannelModel"."id")) AS "count" | ||
351 | FROM "videoChannel" AS "VideoChannelModel" | ||
352 | INNER JOIN "video" AS "Videos" | ||
353 | ON "VideoChannelModel"."id" = "Videos"."channelId" | ||
354 | AND ("Videos"."publishedAt" > Now() - interval '${days}d') | ||
355 | INNER JOIN "account" AS "Account" | ||
356 | ON "VideoChannelModel"."accountId" = "Account"."id" | ||
357 | INNER JOIN "actor" AS "Account->Actor" | ||
358 | ON "Account"."actorId" = "Account->Actor"."id" | ||
359 | AND "Account->Actor"."serverId" IS NULL | ||
360 | LEFT OUTER JOIN "server" AS "Account->Actor->Server" | ||
361 | ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"` | ||
362 | |||
363 | return VideoChannelModel.sequelize.query<{ count: string }>(query, options) | ||
364 | .then(r => parseInt(r[0].count, 10)) | ||
365 | } | ||
366 | |||
367 | const totalLocalVideoChannels = await VideoChannelModel.count() | ||
368 | const totalLocalDailyActiveVideoChannels = await getActiveVideoChannels(1) | ||
369 | const totalLocalWeeklyActiveVideoChannels = await getActiveVideoChannels(7) | ||
370 | const totalLocalMonthlyActiveVideoChannels = await getActiveVideoChannels(30) | ||
371 | const totalHalfYearActiveVideoChannels = await getActiveVideoChannels(180) | ||
372 | |||
373 | return { | ||
374 | totalLocalVideoChannels, | ||
375 | totalLocalDailyActiveVideoChannels, | ||
376 | totalLocalWeeklyActiveVideoChannels, | ||
377 | totalLocalMonthlyActiveVideoChannels, | ||
378 | totalHalfYearActiveVideoChannels | ||
379 | } | ||
380 | } | ||
381 | |||
341 | static listForApi (parameters: { | 382 | static listForApi (parameters: { |
342 | actorId: number | 383 | actorId: number |
343 | start: number | 384 | start: number |