2022-03-24 07:36:47 -05:00
|
|
|
import express from 'express'
|
|
|
|
import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer'
|
2022-05-05 07:12:57 -05:00
|
|
|
import { VideoStatsOverallQuery, VideoStatsTimeserieMetric, VideoStatsTimeserieQuery } from '@shared/models'
|
2022-03-24 07:36:47 -05:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
|
|
|
videoOverallStatsValidator,
|
|
|
|
videoRetentionStatsValidator,
|
|
|
|
videoTimeserieStatsValidator
|
|
|
|
} from '../../../middlewares'
|
|
|
|
|
|
|
|
const statsRouter = express.Router()
|
|
|
|
|
|
|
|
statsRouter.get('/:videoId/stats/overall',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoOverallStatsValidator),
|
|
|
|
asyncMiddleware(getOverallStats)
|
|
|
|
)
|
|
|
|
|
|
|
|
statsRouter.get('/:videoId/stats/timeseries/:metric',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoTimeserieStatsValidator),
|
|
|
|
asyncMiddleware(getTimeserieStats)
|
|
|
|
)
|
|
|
|
|
|
|
|
statsRouter.get('/:videoId/stats/retention',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoRetentionStatsValidator),
|
|
|
|
asyncMiddleware(getRetentionStats)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
statsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function getOverallStats (req: express.Request, res: express.Response) {
|
|
|
|
const video = res.locals.videoAll
|
2022-05-05 07:12:57 -05:00
|
|
|
const query = req.query as VideoStatsOverallQuery
|
2022-03-24 07:36:47 -05:00
|
|
|
|
2022-05-05 07:12:57 -05:00
|
|
|
const stats = await LocalVideoViewerModel.getOverallStats({
|
|
|
|
video,
|
|
|
|
startDate: query.startDate,
|
|
|
|
endDate: query.endDate
|
|
|
|
})
|
2022-03-24 07:36:47 -05:00
|
|
|
|
|
|
|
return res.json(stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getRetentionStats (req: express.Request, res: express.Response) {
|
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
const stats = await LocalVideoViewerModel.getRetentionStats(video)
|
|
|
|
|
|
|
|
return res.json(stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getTimeserieStats (req: express.Request, res: express.Response) {
|
|
|
|
const video = res.locals.videoAll
|
|
|
|
const metric = req.params.metric as VideoStatsTimeserieMetric
|
|
|
|
|
2022-04-07 03:53:35 -05:00
|
|
|
const query = req.query as VideoStatsTimeserieQuery
|
|
|
|
|
2022-03-24 07:36:47 -05:00
|
|
|
const stats = await LocalVideoViewerModel.getTimeserieStats({
|
|
|
|
video,
|
2022-04-07 03:53:35 -05:00
|
|
|
metric,
|
2022-05-06 07:23:02 -05:00
|
|
|
startDate: query.startDate ?? video.createdAt.toISOString(),
|
2022-04-07 03:53:35 -05:00
|
|
|
endDate: query.endDate ?? new Date().toISOString()
|
2022-03-24 07:36:47 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return res.json(stats)
|
|
|
|
}
|