Skip to content

Commit

Permalink
add channel and playlist stats to nodeinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelk committed Feb 26, 2021
1 parent cb2e366 commit ebd14ab
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 20 deletions.
27 changes: 22 additions & 5 deletions server/lib/stat-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { UserModel } from '@server/models/account/user'
import { ActorFollowModel } from '@server/models/activitypub/actor-follow'
import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
import { VideoModel } from '@server/models/video/video'
import { VideoChannelModel } from '@server/models/video/video-channel'
import { VideoCommentModel } from '@server/models/video/video-comment'
import { VideoFileModel } from '@server/models/video/video-file'
import { VideoPlaylistModel } from '@server/models/video/video-playlist'
import { ServerStats, VideoRedundancyStrategyWithManual } from '@shared/models'

class StatsManager {
Expand All @@ -29,21 +31,36 @@ class StatsManager {
const { totalUsers, totalDailyActiveUsers, totalWeeklyActiveUsers, totalMonthlyActiveUsers } = await UserModel.getStats()
const { totalInstanceFollowers, totalInstanceFollowing } = await ActorFollowModel.getStats()
const { totalLocalVideoFilesSize } = await VideoFileModel.getStats()
const {
totalLocalVideoChannels,
totalLocalDailyActiveVideoChannels,
totalLocalWeeklyActiveVideoChannels,
totalLocalMonthlyActiveVideoChannels
} = await VideoChannelModel.getStats()
const { totalLocalPlaylists } = await VideoPlaylistModel.getStats()

const videosRedundancyStats = await this.buildRedundancyStats()

const data: ServerStats = {
totalUsers,
totalDailyActiveUsers,
totalWeeklyActiveUsers,
totalMonthlyActiveUsers,

totalLocalVideos,
totalLocalVideoViews,
totalLocalVideoFilesSize,
totalLocalVideoComments,
totalLocalVideoFilesSize,

totalVideos,
totalVideoComments,

totalUsers,
totalDailyActiveUsers,
totalWeeklyActiveUsers,
totalMonthlyActiveUsers,
totalLocalVideoChannels,
totalLocalDailyActiveVideoChannels,
totalLocalWeeklyActiveVideoChannels,
totalLocalMonthlyActiveVideoChannels,

totalLocalPlaylists,

totalInstanceFollowers,
totalInstanceFollowing,
Expand Down
43 changes: 42 additions & 1 deletion server/models/video/video-channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FindOptions, Includeable, literal, Op, ScopeOptions } from 'sequelize'
import { FindOptions, Includeable, literal, Op, QueryTypes, ScopeOptions } from 'sequelize'
import {
AllowNull,
BeforeDestroy,
Expand Down Expand Up @@ -316,6 +316,47 @@ export class VideoChannelModel extends Model {
return VideoChannelModel.count(query)
}

static async getStats () {

function getActiveVideoChannels (days: number) {
const options = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
raw: true
}

const query = `
SELECT Count(DISTINCT("VideoChannelModel"."id")) AS "count"
FROM "videoChannel" AS "VideoChannelModel"
INNER JOIN "video" AS "Videos"
ON "VideoChannelModel"."id" = "Videos"."channelId"
AND ("Videos"."createdAt" > Now() - interval '${days}d')
INNER JOIN "account" AS "Account"
ON "VideoChannelModel"."accountId" = "Account"."id"
INNER JOIN "actor" AS "Account->Actor"
ON "Account"."actorId" = "Account->Actor"."id"
AND "Account->Actor"."serverId" IS NULL
LEFT OUTER JOIN "server" AS "Account->Actor->Server"
ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"`

return VideoChannelModel.sequelize.query<{ count: string }>(query, options)
.then(r => parseInt(r[0].count, 10))
}

const totalLocalVideoChannels = await VideoChannelModel.count()
const totalLocalDailyActiveVideoChannels = await getActiveVideoChannels(1)
const totalLocalWeeklyActiveVideoChannels = await getActiveVideoChannels(7)
const totalLocalMonthlyActiveVideoChannels = await getActiveVideoChannels(30)
const totalHalfYearActiveVideoChannels = await getActiveVideoChannels(180)

return {
totalLocalVideoChannels,
totalLocalDailyActiveVideoChannels,
totalLocalWeeklyActiveVideoChannels,
totalLocalMonthlyActiveVideoChannels,
totalHalfYearActiveVideoChannels
}
}

static listForApi (parameters: {
actorId: number
start: number
Expand Down
60 changes: 47 additions & 13 deletions server/models/video/video-playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getPlaylistSort, isOutdat
import { ThumbnailModel } from './thumbnail'
import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel'
import { VideoPlaylistElementModel } from './video-playlist-element'
import { ActorModel } from '../activitypub/actor'

enum ScopeNames {
AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
Expand All @@ -65,7 +66,7 @@ enum ScopeNames {
}

type AvailableForListOptions = {
followerActorId: number
followerActorId?: number
type?: VideoPlaylistType
accountId?: number
videoChannelId?: number
Expand Down Expand Up @@ -134,20 +135,26 @@ type AvailableForListOptions = {
privacy: VideoPlaylistPrivacy.PUBLIC
})

// Only list local playlists OR playlists that are on an instance followed by actorId
const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
// Only list local playlists
const whereActorOr: WhereOptions[] = [
{
serverId: null
}
]

whereActor = {
[Op.or]: [
{
serverId: null
},
{
serverId: {
[Op.in]: literal(inQueryInstanceFollow)
}
// … OR playlists that are on an instance followed by actorId
if (options.followerActorId) {
const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)

whereActorOr.push({
serverId: {
[Op.in]: literal(inQueryInstanceFollow)
}
]
})
}

whereActor = {
[Op.or]: whereActorOr
}
}

Expand Down Expand Up @@ -495,6 +502,33 @@ export class VideoPlaylistModel extends Model {
return '/video-playlists/embed/' + this.uuid
}

static async getStats () {
const totalLocalPlaylists = await VideoPlaylistModel.count({
include: [
{
model: AccountModel,
required: true,
include: [
{
model: ActorModel,
required: true,
where: {
serverId: null
}
}
]
}
],
where: {
privacy: VideoPlaylistPrivacy.PUBLIC
}
})

return {
totalLocalPlaylists
}
}

setAsRefreshed () {
this.changed('updatedAt', true)

Expand Down
9 changes: 8 additions & 1 deletion server/tests/api/server/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('Test stats (excluding redundancy)', function () {
expect(data.totalVideos).to.equal(1)
expect(data.totalInstanceFollowers).to.equal(2)
expect(data.totalInstanceFollowing).to.equal(1)
expect(data.totalLocalPlaylists).to.equal(0)
})

it('Should have the correct stats on instance 2', async function () {
Expand All @@ -84,6 +85,7 @@ describe('Test stats (excluding redundancy)', function () {
expect(data.totalVideos).to.equal(1)
expect(data.totalInstanceFollowers).to.equal(1)
expect(data.totalInstanceFollowing).to.equal(1)
expect(data.totalLocalPlaylists).to.equal(0)
})

it('Should have the correct stats on instance 3', async function () {
Expand All @@ -98,6 +100,7 @@ describe('Test stats (excluding redundancy)', function () {
expect(data.totalVideos).to.equal(1)
expect(data.totalInstanceFollowing).to.equal(1)
expect(data.totalInstanceFollowers).to.equal(0)
expect(data.totalLocalPlaylists).to.equal(0)
})

it('Should have the correct total videos stats after an unfollow', async function () {
Expand All @@ -112,7 +115,7 @@ describe('Test stats (excluding redundancy)', function () {
expect(data.totalVideos).to.equal(0)
})

it('Should have the correct active users stats', async function () {
it('Should have the correct active user/channels stats', async function () {
const server = servers[0]

{
Expand All @@ -121,6 +124,10 @@ describe('Test stats (excluding redundancy)', function () {
expect(data.totalDailyActiveUsers).to.equal(1)
expect(data.totalWeeklyActiveUsers).to.equal(1)
expect(data.totalMonthlyActiveUsers).to.equal(1)

expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
}

{
Expand Down
7 changes: 7 additions & 0 deletions shared/models/server/server-stats.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export interface ServerStats {
totalVideos: number
totalVideoComments: number

totalLocalVideoChannels: number
totalLocalDailyActiveVideoChannels: number
totalLocalWeeklyActiveVideoChannels: number
totalLocalMonthlyActiveVideoChannels: number

totalLocalPlaylists: number

totalInstanceFollowers: number
totalInstanceFollowing: number

Expand Down

0 comments on commit ebd14ab

Please sign in to comment.