From 09cababd79f9d445aa027c93cdfe823745fa041a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 28 Feb 2018 18:04:46 +0100 Subject: Add stats route --- server/models/account/user.ts | 9 ++++++ server/models/activitypub/actor-follow.ts | 50 ++++++++++++++----------------- server/models/video/video-comment.ts | 26 ++++++++++++++++ server/models/video/video.ts | 23 ++++++++++++++ 4 files changed, 80 insertions(+), 28 deletions(-) (limited to 'server/models') diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 74cf0f4a8..afa9d7be0 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -13,6 +13,7 @@ import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' import { OAuthTokenModel } from '../oauth/oauth-token' import { getSort, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' +import { VideoCommentModel } from '../video/video-comment' import { AccountModel } from './account' @DefaultScope({ @@ -226,6 +227,14 @@ export class UserModel extends Model { }) } + static async getStats () { + const totalUsers = await UserModel.count() + + return { + totalUsers + } + } + hasRight (right: UserRight) { return hasUserRight(this.role, right) } diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index 8260904a1..3c11d1b67 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -8,6 +8,7 @@ import { import { FollowState } from '../../../shared/models/actors' import { AccountFollow } from '../../../shared/models/actors/follow.model' import { logger } from '../../helpers/logger' +import { getServerActor } from '../../helpers/utils' import { ACTOR_FOLLOW_SCORE } from '../../initializers' import { FOLLOW_STATES } from '../../initializers/constants' import { ServerModel } from '../server/server' @@ -182,34 +183,6 @@ export class ActorFollowModel extends Model { return ActorFollowModel.findOne(query) } - static loadByFollowerInbox (url: string, t?: Sequelize.Transaction) { - const query = { - where: { - state: 'accepted' - }, - include: [ - { - model: ActorModel, - required: true, - as: 'ActorFollower', - where: { - [Sequelize.Op.or]: [ - { - inboxUrl: url - }, - { - sharedInboxUrl: url - } - ] - } - } - ], - transaction: t - } as any // FIXME: typings does not work - - return ActorFollowModel.findOne(query) - } - static listFollowingForApi (id: number, start: number, count: number, sort: string) { const query = { distinct: true, @@ -296,6 +269,27 @@ export class ActorFollowModel extends Model { return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count) } + static async getStats () { + const serverActor = await getServerActor() + + const totalInstanceFollowing = await ActorFollowModel.count({ + where: { + actorId: serverActor.id + } + }) + + const totalInstanceFollowers = await ActorFollowModel.count({ + where: { + targetActorId: serverActor.id + } + }) + + return { + totalInstanceFollowing, + totalInstanceFollowers + } + } + private static async createListAcceptedFollowForApiQuery ( type: 'followers' | 'following', actorIds: number[], diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 47e3211a3..bf8da924d 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -326,6 +326,32 @@ export class VideoCommentModel extends Model { .findAll(query) } + static async getStats () { + const totalLocalVideoComments = await VideoCommentModel.count({ + include: [ + { + model: AccountModel, + required: true, + include: [ + { + model: ActorModel, + required: true, + where: { + serverId: null + } + } + ] + } + ] + }) + const totalVideoComments = await VideoCommentModel.count() + + return { + totalLocalVideoComments, + totalVideoComments + } + } + getThreadId (): number { return this.originCommentId || this.id } diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 80ca513bf..f6a21814c 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -761,6 +761,29 @@ export class VideoModel extends Model { .findOne(options) } + static async getStats () { + const totalLocalVideos = await VideoModel.count({ + where: { + remote: false + } + }) + const totalVideos = await VideoModel.count() + + let totalLocalVideoViews = await VideoModel.sum('views', { + where: { + remote: false + } + }) + // Sequelize could return null... + if (!totalLocalVideoViews) totalLocalVideoViews = 0 + + return { + totalLocalVideos, + totalLocalVideoViews, + totalVideos + } + } + getOriginalFile () { if (Array.isArray(this.VideoFiles) === false) return undefined -- cgit v1.2.3