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 17, 2021
1 parent 9817060 commit 9f8191b
Show file tree
Hide file tree
Showing 5 changed files with 119 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
49 changes: 48 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 { CountOptions, FindOptions, Includeable, literal, Op, ScopeOptions } from 'sequelize'
import {
AllowNull,
BeforeDestroy,
Expand Down Expand Up @@ -316,6 +316,53 @@ export class VideoChannelModel extends Model {
return VideoChannelModel.count(query)
}

static async getStats () {
function getActiveVideoChannels (days: number) {
const query = {
include: [
{
model: AccountModel.scope({
method: [ AccountModelScopeNames.SUMMARY, {
whereActor: {
serverId: null
}
} as AccountSummaryOptions ]
}),
required: true
},
{
model: VideoModel,
required: true,
where: {
[Op.and]: [
literal(`"Videos"."createdAt" > NOW() - INTERVAL '${days}d'`)
]
}
}
],
distinct: true
} as CountOptions

return VideoChannelModel.unscoped()
.scope([ ScopeNames.WITH_VIDEOS ])
.count(query)
}

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
47 changes: 34 additions & 13 deletions server/models/video/video-playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ enum ScopeNames {
}

type AvailableForListOptions = {
followerActorId: number
followerActorId?: number
type?: VideoPlaylistType
accountId?: number
videoChannelId?: number
Expand Down Expand Up @@ -133,20 +133,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 @@ -494,6 +500,21 @@ export class VideoPlaylistModel extends Model {
return '/video-playlists/embed/' + this.uuid
}

static async getStats () {
const totalLocalPlaylists = await VideoPlaylistModel.scope({
method: [
ScopeNames.AVAILABLE_FOR_LIST,
{ // not providing followerActorId will only list local playlists
type: VideoPlaylistType.REGULAR
} as AvailableForListOptions
]
}).count()

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 9f8191b

Please sign in to comment.