ActivityPub-federated video streaming platform using P2P directly in your web browser
Feature/dynamic channel stats (#7750)
Sourav Sarkar2e08a14cb9f3
diff --git a/client/src/app/+my-library/+my-video-channels/my-video-channels.component.html b/client/src/app/+my-library/+my-video-channels/my-video-channels.component.htmlindex d8f07758c4..56589169a0 100644--- a/client/src/app/+my-library/+my-video-channels/my-video-channels.component.html+++ b/client/src/app/+my-library/+my-video-channels/my-video-channels.component.html@@ -15,6 +15,12 @@</div>}+ <div class="d-flex align-items-center">+ <label class="label-inline" for="stats-days" i18n>Stats</label>++ <my-select-options inputId="stats-days" [items]="statsDaysItems" [(ngModel)]="statsDays" (ngModelChange)="onStatsDaysChanged()"></my-select-options>+ </div>+<a class="peertube-create-button" routerLink="/manage/create"><my-global-icon iconName="add" aria-hidden="true"></my-global-icon><ng-container i18n>Create video channel</ng-container>@@ -27,7 +33,7 @@}<div class="video-channels" myInfiniteScroller (nearOfBottom)="onNearOfBottom()" [dataObservable]="onChannelDataSubject.asObservable()">- @for (videoChannel of videoChannels; track videoChannel; let i = $index) {+ @for (videoChannel of videoChannels; track videoChannel) {<div class="channel"><div class="channel-info"><my-actor-avatar [actor]="videoChannel" actorType="channel" [internalHref]="[ '/c', videoChannel.nameWithHost ]" size="120"></my-actor-avatar>@@ -60,6 +66,22 @@</div></div>+ @if (displayChart) {+ <div class="chart-container">+ <div myDeferLoading>+ <ng-template>+ @if (chartOptions && videoChannelsChartData[videoChannel.id]) {+ <p-chart+ width="100%" height="120px"+ type="bar" [data]="videoChannelsChartData[videoChannel.id]" [options]="chartOptions"+ [ariaLabel]="getChartAriaLabel(videoChannelsChartData[videoChannel.id])"+ ></p-chart>+ }+ </ng-template>+ </div>+ </div>+ }+<div class="buttons"><my-edit-button i18n-label label="Manage" [ptRouterLink]="[ '/my-library/video-channels/manage', videoChannel.nameWithHost ]"></my-edit-button>@@ -68,22 +90,6 @@}</div></div>-- @if (!isInSmallView) {- <div class="w-100 d-flex justify-content-end chart-container">- <div myDeferLoading>- <ng-template>- @if (chartOptions && videoChannelsChartData && videoChannelsChartData[i]) {- <p-chart- width="40vw" height="100px"- type="line" [data]="videoChannelsChartData[i]" [options]="chartOptions"- [ariaLabel]="getChartAriaLabel(videoChannelsChartData[i])"- ></p-chart>- }- </ng-template>- </div>- </div>- }</div>}</div>diff --git a/client/src/app/+my-library/+my-video-channels/my-video-channels.component.scss b/client/src/app/+my-library/+my-video-channels/my-video-channels.component.scssindex e602139a44..445c84d1ed 100644--- a/client/src/app/+my-library/+my-video-channels/my-video-channels.component.scss+++ b/client/src/app/+my-library/+my-video-channels/my-video-channels.component.scss@@ -18,14 +18,16 @@ input[type="text"] {.channel-info {display: flex;+ align-items: center;gap: 1rem;}.metadata {display: flex;flex-direction: column;- align-self: flex-start;min-width: 1px;++ @include margin-right(auto);}.video-channel-names {@@ -58,18 +60,20 @@ input[type="text"] {color: pvar(--fg);}+.chart-container {+ flex: 1 1 auto;+ min-width: 240px;+ max-width: 600px;++ // Sync this value with the template+ height: 120px;+}+.buttons {display: flex;flex-wrap: wrap;+ flex-shrink: 0;gap: 0.5rem;-- @include margin-left(auto);-}--.chart-container {- // Sync these values with the template- width: 40vw;- height: 100px;}@media screen and (max-width: $small-view) {@@ -78,17 +82,15 @@ input[type="text"] {align-items: center;text-align: center;padding-bottom: 1rem;- align-items: center;}.metadata {- align-self: normal;align-items: center;++ @include margin-right(unset);}.buttons {text-align: center;-- @include margin-left(0);}}diff --git a/client/src/app/+my-library/+my-video-channels/my-video-channels.component.ts b/client/src/app/+my-library/+my-video-channels/my-video-channels.component.tsindex 2f8bde5fb3..6726bfeb42 100644--- a/client/src/app/+my-library/+my-video-channels/my-video-channels.component.ts+++ b/client/src/app/+my-library/+my-video-channels/my-video-channels.component.ts@@ -4,6 +4,7 @@ import { ActivatedRoute, RouterLink } from '@angular/router'import {AuthService,ComponentPagination,+ ComponentPaginationLight,Notifier,PeerTubeRouterService,ScreenService,@@ -16,9 +17,17 @@ import { SelectOptionsComponent } from '@app/shared/shared-forms/select/select-oimport { CollaboratorStateComponent } from '@app/shared/shared-main/channel/collaborator-state.component'import { VideoChannel } from '@app/shared/shared-main/channel/video-channel.model'import { VideoChannelService } from '@app/shared/shared-main/channel/video-channel.service'-import { maxBy, minBy } from '@peertube/peertube-core-utils'+import { maxBy } from '@peertube/peertube-core-utils'+import {+ VIDEO_CHANNEL_STATS_DAYS_ALL_TIME,+ VIDEO_CHANNEL_STATS_DAYS_DEFAULT,+ VIDEO_CHANNEL_STATS_DAYS_OPTIONS,+ VideoChannelStatsDays,+ VideoChannelStatsGroupInterval,+ getVideoChannelStatsGroupInterval+} from '@peertube/peertube-models'import { SelectOptionsItem } from '@pt-types'-import { ChartData, ChartOptions, TooltipItem, TooltipModel } from 'chart.js'+import { ChartData, ChartOptions, TooltipItem } from 'chart.js'import { ChartModule } from 'primeng/chart'import { Subject, first, switchMap, tap } from 'rxjs'import { ActorAvatarComponent } from '../../shared/shared-actor-image/actor-avatar.component'@@ -31,7 +40,7 @@ import { DeferLoadingDirective } from '../../shared/shared-main/common/defer-loaimport { InfiniteScrollerDirective } from '../../shared/shared-main/common/infinite-scroller.directive'import { NumberFormatterPipe } from '../../shared/shared-main/common/number-formatter.pipe'-type CustomChartData = ChartData & { startDate: string, total: number }+type CustomChartData = ChartData & { startDate: string, total: number, tooltipTitles: string[] }type DisplayFilter = 'all' | 'owned'@Component({@@ -65,7 +74,7 @@ export class MyVideoChannelsComponent implements OnInit {videoChannels: VideoChannel[] = []- videoChannelsChartData: CustomChartData[]+ videoChannelsChartData: { [id: number]: CustomChartData } = {}chartOptions: ChartOptions@@ -85,10 +94,18 @@ export class MyVideoChannelsComponent implements OnInit {{ id: 'owned', label: $localize`Only channels owned by me` }]+ statsDays: VideoChannelStatsDays = VIDEO_CHANNEL_STATS_DAYS_DEFAULT+ statsDaysItems: SelectOptionsItem<VideoChannelStatsDays>[] = [+ { id: VIDEO_CHANNEL_STATS_DAYS_DEFAULT, label: $localize`Last 30 days` },+ { id: 90, label: $localize`Last 90 days` },+ { id: 365, label: $localize`Last year` },+ { id: VIDEO_CHANNEL_STATS_DAYS_ALL_TIME, label: $localize`All time` }+ ]+private pagesDone = new Set<number>()- get isInSmallView () {- return this.screenService.isInSmallView()+ get displayChart () {+ return !this.screenService.isInMediumView()}get user () {@@ -99,6 +116,11 @@ export class MyVideoChannelsComponent implements OnInit {if (this.route.snapshot.queryParamMap.get('displayFilter') === 'owned') {this.displayFilter = 'owned'}++ const statsDays = parseInt(this.route.snapshot.queryParamMap.get('statsDays'), 10)+ if (VIDEO_CHANNEL_STATS_DAYS_OPTIONS.includes(statsDays as VideoChannelStatsDays)) {+ this.statsDays = statsDays as VideoChannelStatsDays+ }}isOwned (channel: VideoChannel) {@@ -128,6 +150,7 @@ export class MyVideoChannelsComponent implements OnInit {private resetDataAndReload () {resetCurrentPage(this.pagination)this.videoChannels = []+ this.videoChannelsChartData = {}this.pagesDone.clear()this.loadMoreVideoChannels()@@ -157,17 +180,24 @@ export class MyVideoChannelsComponent implements OnInit {this.loadMoreVideoChannels()}- private loadMoreVideoChannels () {- if (this.pagesDone.has(this.pagination.currentPage)) return- this.pagesDone.add(this.pagination.currentPage)-- const channelBaseOptions = {+ private getChannelBaseOptions () {+ return {account: this.authService.getUser().account,search: this.search,componentPagination: this.pagination,includeCollaborations: this.displayFilter === 'all',sort: '-updatedAt'}+ }++ private loadMoreVideoChannels () {+ if (this.pagesDone.has(this.pagination.currentPage)) return+ this.pagesDone.add(this.pagination.currentPage)++ const channelBaseOptions = this.getChannelBaseOptions()++ // Snapshot the pagination so the stats request targets the same window as the list request+ const statsPagination = { ...this.pagination }const base = this.authService.userInformationLoaded.pipe(first())@@ -179,36 +209,89 @@ export class MyVideoChannelsComponent implements OnInit {this.pagination.totalItems = res.totalthis.onChannelDataSubject.next(res.data)- }),- switchMap(() => this.videoChannelService.listAccountChannels({ ...channelBaseOptions, withStats: true }))+ })).subscribe({+ // Only fetch stats for the page that was just loaded, not every channel loaded so far+ next: () => this.loadChannelsStats(statsPagination),++ error: err => this.notifier.handleError(err)+ })+ }++ onStatsDaysChanged () {+ this.peertubeRouter.silentNavigate([], {+ ...this.route.snapshot.queryParams,++ statsDays: this.statsDays === VIDEO_CHANNEL_STATS_DAYS_DEFAULT+ ? null+ : this.statsDays+ })++ if (this.videoChannels.length === 0) return++ // Refresh all channel stats, chunked by 100 (the max count accepted server side)+ const chunkSize = Math.min(this.videoChannels.length, 100)++ for (let start = 0; start < this.videoChannels.length; start += chunkSize) {+ this.loadChannelsStats({+ currentPage: (start / chunkSize) + 1,+ itemsPerPage: chunkSize+ })+ }+ }++ private loadChannelsStats (componentPagination: ComponentPaginationLight) {+ const channelBaseOptions = this.getChannelBaseOptions()+ const requestedStatsDays = this.statsDays++ this.videoChannelService.listAccountChannels({+ ...channelBaseOptions,+ withStats: true,+ statsDays: requestedStatsDays,+ componentPagination+ }).subscribe({next: res => {+ // statsDays changed again since this request was sent: a newer request will supersede it+ if (requestedStatsDays !== this.statsDays) return+for (const channelWithStats of res.data) {const channel = this.videoChannels.find(c => c.id === channelWithStats.id)+ if (!channel) continuechannel.viewsPerDay = channelWithStats.viewsPerDay+ channel.viewsGroupInterval = channelWithStats.viewsGroupIntervalchannel.videosCount = channelWithStats.videosCountchannel.totalViews = channelWithStats.totalViews}- this.videoChannelsChartData = this.videoChannels.map(v => ({- labels: v.viewsPerDay.map(day => day.date.toLocaleDateString()),- datasets: [- {- label: $localize`Views for the day`,- data: v.viewsPerDay.map(day => day.views),- fill: false,- borderColor: '#c6c6c6'- }- ],-- total: v.viewsPerDay.map(day => day.views)- .reduce((p, c) => p + c, 0),-- startDate: v.viewsPerDay.length !== 0- ? v.viewsPerDay[0].date.toLocaleDateString()- : ''- }))+ const barColor = getComputedStyle(document.documentElement).getPropertyValue('--border-primary')++ for (const v of this.videoChannels) {+ const viewsPerDay = v.viewsPerDay || []+ const groupInterval = v.viewsGroupInterval || getVideoChannelStatsGroupInterval(this.statsDays)++ this.videoChannelsChartData[v.id] = {+ labels: viewsPerDay.map(day => this.formatStatsAxisLabel(day.date, groupInterval)),+ tooltipTitles: viewsPerDay.map(day => this.formatStatsTooltipTitle(day.date, groupInterval)),+ datasets: [+ {+ label: $localize`Views`,+ data: viewsPerDay.map(day => day.views),+ backgroundColor: barColor,+ hoverBackgroundColor: barColor,+ maxBarThickness: 16,+ borderRadius: 2+ }+ ],++ total: viewsPerDay.map(day => day.views)+ .reduce((p, c) => p + c, 0),++ startDate: viewsPerDay.length !== 0+ ? this.formatStatsAxisLabel(viewsPerDay[0].date, groupInterval)+ : ''+ }+ }this.buildChartOptions()},@@ -219,11 +302,39 @@ export class MyVideoChannelsComponent implements OnInit {// ---------------------------------------------------------------------------+ private formatStatsAxisLabel (date: Date, groupInterval: VideoChannelStatsGroupInterval) {+ if (groupInterval === 'month') {+ return date.toLocaleDateString(undefined, { year: 'numeric', month: 'short' })+ }++ return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' })+ }++ private formatStatsTooltipTitle (date: Date, groupInterval: VideoChannelStatsGroupInterval) {+ if (groupInterval === 'month') {+ return date.toLocaleDateString(undefined, { year: 'numeric', month: 'short' })+ }++ if (groupInterval === 'week') {+ const weekStart = date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' })+ return $localize`Week of ${weekStart}`+ }++ return date.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' })+ }+private buildChartOptions () {- const channelsMinimumDailyViews = Math.min(...this.videoChannels.map(v => minBy(v.viewsPerDay, 'views').views))- const channelsMaximumDailyViews = Math.max(...this.videoChannels.map(v => maxBy(v.viewsPerDay, 'views').views))+ const channelsWithStats = this.videoChannels.filter(v => v.viewsPerDay?.length)+ if (channelsWithStats.length === 0) return++ const channelsMaximumDailyViews = Math.max(...channelsWithStats.map(v => maxBy(v.viewsPerDay, 'views').views))+ const styles = getComputedStyle(document.documentElement)+ const tickColor = styles.getPropertyValue('--fg-300')+ const gridColor = styles.getPropertyValue('--bg-secondary-350')this.chartOptions = {+ responsive: true,+ maintainAspectRatio: false,plugins: {legend: {display: false@@ -231,41 +342,78 @@ export class MyVideoChannelsComponent implements OnInit {tooltip: {mode: 'index',intersect: false,- external: function ({ tooltip }: { tooltip: TooltipModel<any> }) {- if (!tooltip) return-- // disable displaying the color box- tooltip.options.displayColors = false- },+ displayColors: false,callbacks: {- label: (tooltip: TooltipItem<any>) => `${tooltip.formattedValue} views`+ title: items => {+ const item = items[0]+ if (!item) return ''++ const tooltipTitles = (item.chart.data as CustomChartData).tooltipTitles+ return tooltipTitles?.[item.dataIndex] ?? item.label ?? ''+ },+ label: (tooltip: TooltipItem<any>) =>+ formatICU(+ $localize`${tooltip.raw} {value, plural, =1 {view} other {views}}`,+ { value: tooltip.raw as number }+ )}}},scales: {x: {- display: false+ display: true,+ border: {+ display: false+ },+ grid: {+ display: false+ },+ ticks: {+ autoSkip: true,+ maxTicksLimit: 4,+ maxRotation: 0,+ color: tickColor,+ font: {+ size: 11+ }+ }},y: {- display: false,- min: Math.max(0, channelsMinimumDailyViews - (3 * channelsMaximumDailyViews / 100)),- max: Math.max(1, channelsMaximumDailyViews)+ display: true,+ beginAtZero: true,+ suggestedMax: Math.max(1, channelsMaximumDailyViews),+ border: {+ display: false+ },+ grid: {+ color: gridColor+ },+ ticks: {+ maxTicksLimit: 3,+ color: tickColor,+ font: {+ size: 11+ },+ callback: value => {+ const n = Number(value)+ if (!Number.isFinite(n)) return value++ return Math.abs(n) >= 1000+ ? `${Math.round(n / 100) / 10}k`+ : n+ }+ }}},layout: {padding: {- left: 15,- right: 15,- top: 10,+ left: 0,+ right: 4,+ top: 4,bottom: 0}},- elements: {- point: {- radius: 0- }- },- hover: {+ interaction: {mode: 'index',intersect: false}diff --git a/client/src/app/core/wrappers/screen.service.ts b/client/src/app/core/wrappers/screen.service.tsindex 1facbf4b52..65e7c2b5ab 100644--- a/client/src/app/core/wrappers/screen.service.ts+++ b/client/src/app/core/wrappers/screen.service.ts@@ -12,6 +12,11 @@ export class ScreenService {this.refreshWindowInnerWidth()}+ isInMediumView () {+ // Keep it sync with SASS variable+ return this.getWindowInnerWidth() < 1000+ }+isInSmallView () {// Keep it sync with SASS variablereturn this.getWindowInnerWidth() < 800diff --git a/client/src/app/shared/shared-main/channel/video-channel.model.ts b/client/src/app/shared/shared-main/channel/video-channel.model.tsindex 445cd45406..7bb7ed5529 100644--- a/client/src/app/shared/shared-main/channel/video-channel.model.ts+++ b/client/src/app/shared/shared-main/channel/video-channel.model.ts@@ -1,6 +1,12 @@import { getOriginUrl } from '@app/helpers'import { maxBy } from '@peertube/peertube-core-utils'-import { ActorImage, Account as ServerAccount, VideoChannel as ServerVideoChannel, ViewsPerDate } from '@peertube/peertube-models'+import {+ ActorImage,+ Account as ServerAccount,+ VideoChannel as ServerVideoChannel,+ VideoChannelStatsGroupInterval,+ ViewsPerDate+} from '@peertube/peertube-models'import { Actor } from '../account/actor.model'export class VideoChannel extends Actor implements ServerVideoChannel {@@ -24,6 +30,7 @@ export class VideoChannel extends Actor implements ServerVideoChannel {videosCount?: numberviewsPerDay?: ViewsPerDate[]+ viewsGroupInterval?: VideoChannelStatsGroupIntervaltotalViews?: numberstatic GET_ACTOR_BANNER_URL (channel: Partial<Pick<ServerVideoChannel, 'banners'>>) {@@ -72,6 +79,8 @@ export class VideoChannel extends Actor implements ServerVideoChannel {this.viewsPerDay = hash.viewsPerDay.map(v => ({ ...v, date: new Date(v.date) }))}+ this.viewsGroupInterval = hash.viewsGroupInterval+if (hash.totalViews !== null && hash.totalViews !== undefined) {this.totalViews = hash.totalViews}diff --git a/client/src/app/shared/shared-main/channel/video-channel.service.ts b/client/src/app/shared/shared-main/channel/video-channel.service.tsindex 943455507f..4f578af3c7 100644--- a/client/src/app/shared/shared-main/channel/video-channel.service.ts+++ b/client/src/app/shared/shared-main/channel/video-channel.service.ts@@ -9,6 +9,7 @@ import {VideoChannelCollaborator,VideoChannelCreate,VideoChannel as VideoChannelServer,+ VideoChannelStatsDays,VideoChannelUpdate,VideosImportInChannelCreate} from '@peertube/peertube-models'@@ -54,11 +55,12 @@ export class VideoChannelService {account: Pick<Account, 'nameWithHost'>componentPagination?: ComponentPaginationLightwithStats?: boolean+ statsDays?: VideoChannelStatsDayssort?: stringsearch?: stringincludeCollaborations?: boolean}): Observable<ResultList<VideoChannel>> {- const { account, componentPagination, withStats = false, sort, search, includeCollaborations = false } = options+ const { account, componentPagination, withStats = false, statsDays, sort, search, includeCollaborations = false } = optionsconst defaultCount = Math.min(this.serverService.getHTMLConfig().videoChannels.maxPerUser, 100) // 100 is the max count on server side@@ -70,6 +72,7 @@ export class VideoChannelService {params = this.restService.addRestGetParams(params, pagination, sort)params = params.set('withStats', withStats + '')+ if (statsDays !== undefined) params = params.set('statsDays', statsDays + '')if (search) params = params.set('search', search)if (includeCollaborations) params = params.set('includeCollaborations', 'true')diff --git a/packages/models/src/videos/channel/video-channel.model.ts b/packages/models/src/videos/channel/video-channel.model.tsindex b73a300d2e..a14c84d780 100644--- a/packages/models/src/videos/channel/video-channel.model.ts+++ b/packages/models/src/videos/channel/video-channel.model.ts@@ -6,6 +6,33 @@ export type ViewsPerDate = {views: number}+// 0 means "all time" (no lower bound on the date range)+export const VIDEO_CHANNEL_STATS_DAYS_ALL_TIME = 0++export const VIDEO_CHANNEL_STATS_DAYS_DEFAULT = 30++export const VIDEO_CHANNEL_STATS_DAYS_OPTIONS = [ VIDEO_CHANNEL_STATS_DAYS_DEFAULT, 90, 365, VIDEO_CHANNEL_STATS_DAYS_ALL_TIME ] as const++export type VideoChannelStatsDays = typeof VIDEO_CHANNEL_STATS_DAYS_OPTIONS[number]++// Bucket size used for viewsPerDay points+export type VideoChannelStatsGroupInterval = 'day' | 'week' | 'month'++// ~2 months → daily; up to a bit over a year → weekly; longer → monthly+// Also used to build the SQL that computes viewsGroupInterval server side+export const VIDEO_CHANNEL_STATS_MONTH_GROUP_THRESHOLD_DAYS = 400+export const VIDEO_CHANNEL_STATS_WEEK_GROUP_THRESHOLD_DAYS = 60++// Fallback for when viewsGroupInterval is not available yet. Exact for a bounded range, best effort+// for "all time" where only the server knows the channel history+export function getVideoChannelStatsGroupInterval (statsDays: VideoChannelStatsDays): VideoChannelStatsGroupInterval {+ if (statsDays === VIDEO_CHANNEL_STATS_DAYS_ALL_TIME) return 'month'+ if (statsDays > VIDEO_CHANNEL_STATS_MONTH_GROUP_THRESHOLD_DAYS) return 'month'+ if (statsDays >= VIDEO_CHANNEL_STATS_WEEK_GROUP_THRESHOLD_DAYS) return 'week'++ return 'day'+}+export interface VideoChannel extends Actor {displayName: stringdescription: string@@ -18,7 +45,11 @@ export interface VideoChannel extends Actor {ownerAccount?: AccountvideosCount?: number- viewsPerDay?: ViewsPerDate[] // chronologically ordered+ // Chronologically ordered, always spans the whole requested statsDays range so channels can be compared to each other+ // Bucket size is viewsGroupInterval+ viewsPerDay?: ViewsPerDate[]+ viewsGroupInterval?: VideoChannelStatsGroupInterval+ // Lifetime views of all the channel videos, not bound to the selected statsDays rangetotalViews?: numberbanners: ActorImage[]diff --git a/packages/server-commands/src/videos/channels-command.ts b/packages/server-commands/src/videos/channels-command.tsindex 01f6a5a43f..c425c7bb22 100644--- a/packages/server-commands/src/videos/channels-command.ts+++ b/packages/server-commands/src/videos/channels-command.ts@@ -39,6 +39,7 @@ export class ChannelsCommand extends AbstractCommand {count?: numbersort?: stringwithStats?: boolean+ statsDays?: numbersearch?: stringincludeCollaborations?: boolean}@@ -50,7 +51,7 @@ export class ChannelsCommand extends AbstractCommand {...options,path,- query: { sort, ...pick(options, [ 'start', 'count', 'withStats', 'search', 'includeCollaborations' ]) },+ query: { sort, ...pick(options, [ 'start', 'count', 'withStats', 'statsDays', 'search', 'includeCollaborations' ]) },implicitToken: false,defaultExpectedStatus: HttpStatusCode.OK_200})diff --git a/packages/tests/src/api/check-params/channels.ts b/packages/tests/src/api/check-params/channels.tsindex dfa053541d..d2b8e69028 100644--- a/packages/tests/src/api/check-params/channels.ts+++ b/packages/tests/src/api/check-params/channels.ts@@ -127,6 +127,36 @@ describe('Test video channels API validator', function () {expectedStatus: HttpStatusCode.OK_200})})++ it('Should fail with an invalid statsDays value', async function () {+ await server.channels.listByAccount({+ accountName: 'fake',+ withStats: true,+ statsDays: 7,+ token: userInfo.accessToken,+ expectedStatus: HttpStatusCode.BAD_REQUEST_400+ })++ await makeGetRequest({+ url: server.url,+ path: '/api/v1/accounts/fake/video-channels',+ token: userInfo.accessToken,+ query: { withStats: true, statsDays: 'abc' },+ expectedStatus: HttpStatusCode.BAD_REQUEST_400+ })+ })++ it('Should succeed with valid statsDays values', async function () {+ for (const statsDays of [ 30, 90, 365, 0 ]) {+ await server.channels.listByAccount({+ accountName: 'fake',+ withStats: true,+ statsDays,+ token: userInfo.accessToken,+ expectedStatus: HttpStatusCode.OK_200+ })+ }+ })})describe('When adding a video channel', function () {diff --git a/packages/tests/src/api/videos/video-channels.ts b/packages/tests/src/api/videos/video-channels.tsindex 92428bfed6..4c8104a16f 100644--- a/packages/tests/src/api/videos/video-channels.ts+++ b/packages/tests/src/api/videos/video-channels.ts@@ -460,12 +460,57 @@ describe('Test video channels', function () {}})+ // Must run before any view is simulated+ it('Should aggregate channel views by statsDays range', async function () {+ {+ const { data } = await servers[0].channels.listByAccount({+ accountName,+ token: servers[0].accessToken,+ withStats: true,+ statsDays: 90+ })++ const lengths = new Set<number>()++ for (const channel of data) {+ expect(channel.totalViews).to.equal(0)++ // No videoStat history, so the group interval comes from the full 90 day range+ expect(channel.viewsGroupInterval).to.equal('week')++ expect(channel.viewsPerDay.length).to.be.at.least(12)+ expect(channel.viewsPerDay.length).to.be.below(16)++ lengths.add(channel.viewsPerDay.length)+ }++ // The series spans the requested range, not the channel history, so every channel has the same points+ expect(lengths.size).to.equal(1)+ }++ {+ const { data } = await servers[0].channels.listByAccount({+ accountName,+ token: servers[0].accessToken,+ withStats: true,+ statsDays: 0+ })++ for (const channel of data) {+ expect(channel.viewsGroupInterval).to.equal('day')+ expect(channel.viewsPerDay).to.have.length(1)+ }+ }+ })+it('Should report correct channel views per days', async function () {{const { data } = await servers[0].channels.listByAccount({ accountName, token: servers[0].accessToken, withStats: true })for (const channel of data) {expect(channel).to.haveOwnProperty('viewsPerDay')+ expect(channel).to.haveOwnProperty('viewsGroupInterval')+ expect(channel.viewsGroupInterval).to.equal('day')expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + todayfor (const v of channel.viewsPerDay) {@@ -506,6 +551,21 @@ describe('Test video channels', function () {const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)expect(channelWithView.totalViews).to.equal(2)}++ // totalViews is lifetime+ {+ for (const statsDays of [ 90, 365, 0 ]) {+ const { data } = await servers[0].channels.listByAccount({+ accountName,+ token: servers[0].accessToken,+ withStats: true,+ statsDays+ })++ const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)+ expect(channelWithView.totalViews).to.equal(2)+ }+ }})it('Should report correct videos count', async function () {diff --git a/server/core/controllers/api/accounts.ts b/server/core/controllers/api/accounts.tsindex 48fc88510e..b7c5a00b12 100644--- a/server/core/controllers/api/accounts.ts+++ b/server/core/controllers/api/accounts.ts@@ -165,6 +165,7 @@ async function listAccountChannels (req: express.Request, res: express.Response)count: req.query.count,sort: req.query.sort,withStats: req.query.withStats,+ statsDays: req.query.statsDays,includeCollaborations: req.query.includeCollaborations,search: req.query.search})diff --git a/server/core/middlewares/validators/videos/video-channels.ts b/server/core/middlewares/validators/videos/video-channels.tsindex ecb0e8e758..60b98e33e9 100644--- a/server/core/middlewares/validators/videos/video-channels.ts+++ b/server/core/middlewares/validators/videos/video-channels.ts@@ -1,11 +1,11 @@-import { HttpStatusCode, UserRight, VideosImportInChannelCreate } from '@peertube/peertube-models'+import { HttpStatusCode, UserRight, VIDEO_CHANNEL_STATS_DAYS_OPTIONS, VideosImportInChannelCreate } from '@peertube/peertube-models'import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc.js'import { CONFIG } from '@server/initializers/config.js'import { loadReservedActorName } from '@server/lib/local-actor.js'import { MChannelAccountDefault } from '@server/types/models/index.js'import express from 'express'import { body, param, query } from 'express-validator'-import { isBooleanValid, isIdValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc.js'+import { isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc.js'import {isVideoChannelDescriptionValid,isVideoChannelDisplayNameValid,@@ -115,6 +115,12 @@ export const listAccountChannelsValidator = [.optional().customSanitizer(toBooleanOrNull),+ query('statsDays')+ .optional()+ .customSanitizer(toIntOrNull)+ .isIn(VIDEO_CHANNEL_STATS_DAYS_OPTIONS)+ .withMessage(`Should have a valid statsDays value (${VIDEO_CHANNEL_STATS_DAYS_OPTIONS.join(', ')})`),+query('includeCollaborations').optional().customSanitizer(toBooleanOrNull),diff --git a/server/core/models/video/sql/channel/video-channel-list-query-builder.ts b/server/core/models/video/sql/channel/video-channel-list-query-builder.tsindex 622c77ace6..bb38eb3e29 100644--- a/server/core/models/video/sql/channel/video-channel-list-query-builder.ts+++ b/server/core/models/video/sql/channel/video-channel-list-query-builder.ts@@ -1,4 +1,10 @@-import { ActorImageType, VideoChannelCollaboratorState } from '@peertube/peertube-models'+import {+ ActorImageType,+ VIDEO_CHANNEL_STATS_DAYS_ALL_TIME,+ VIDEO_CHANNEL_STATS_MONTH_GROUP_THRESHOLD_DAYS,+ VIDEO_CHANNEL_STATS_WEEK_GROUP_THRESHOLD_DAYS,+ VideoChannelCollaboratorState+} from '@peertube/peertube-models'import { WEBSERVER } from '@server/initializers/constants.js'import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'import { buildServerIdsFollowedBy } from '@server/models/shared/index.js'@@ -30,6 +36,7 @@ export class VideoChannelListQueryBuilder extends AbstractListQuery {private builtAccountAvatarJoin = falseprivate builtChannelAvatarJoin = falseprivate builtChannelBannerJoin = false+ private builtChannelStatsJoin = falseconstructor (protected readonly sequelize: Sequelize,@@ -200,12 +207,89 @@ export class VideoChannelListQueryBuilder extends AbstractListQuery {this.builtChannelBannerJoin = true}+ private buildChannelStatsJoin () {+ if (this.builtChannelStatsJoin) return++ // On a bounded range, every channel of the page shares the same x axis and the same bucket size and can be compared to the others+ // "All time" has no boundary: it starts at the first recorded stat of the channel (a young channel keeps a fine bucket size)+ const seriesCTE = this.options.statsDaysPrior === VIDEO_CHANNEL_STATS_DAYS_ALL_TIME+ // dprint-ignore+ ? 'series AS ( ' ++ 'SELECT COALESCE(date_trunc(\'day\', MIN("videoStat"."startDate")), date_trunc(\'day\', now())) AS start_date ' ++ 'FROM "videoStat" INNER JOIN "video" ON "videoStat"."videoId" = "video"."id" ' ++ 'WHERE "video"."channelId" = "VideoChannelModel"."id"' ++ ')'+ : 'series AS ( ' ++ `SELECT date_trunc('day', now()) - make_interval(days => :statsDaysPrior) AS start_date` ++ ')'++ if (this.options.statsDaysPrior !== VIDEO_CHANNEL_STATS_DAYS_ALL_TIME) {+ this.replacements.statsDaysPrior = this.options.statsDaysPrior+ }++ this.replacements.statsMonthGroupThresholdDays = VIDEO_CHANNEL_STATS_MONTH_GROUP_THRESHOLD_DAYS+ this.replacements.statsWeekGroupThresholdDays = VIDEO_CHANNEL_STATS_WEEK_GROUP_THRESHOLD_DAYS++ // Computed once per channel row (instead of once per selected attribute) and shared by+ // viewsPerDay/viewsGroupInterval below via the CTEs+ this.join +=+ // dprint-ignore+ 'LEFT JOIN LATERAL ( ' ++ `WITH ${seriesCTE}, ` ++ 'series_group AS ( ' ++ // Keep in sync with getVideoChannelStatsGroupInterval, which mirrors this for the client fallback+ 'SELECT start_date, ' ++ 'CASE ' ++ 'WHEN EXTRACT(EPOCH FROM (date_trunc(\'day\', now()) - start_date)) / 86400.0 > :statsMonthGroupThresholdDays THEN \'month\' ' ++ 'WHEN EXTRACT(EPOCH FROM (date_trunc(\'day\', now()) - start_date)) / 86400.0 >= :statsWeekGroupThresholdDays THEN \'week\' ' ++ 'ELSE \'day\' ' ++ 'END AS grp ' ++ 'FROM series' ++ '), periods AS ( ' ++ 'SELECT gs.day AS day, series_group.grp AS grp, series_group.start_date AS start_date ' ++ 'FROM series_group, ' ++ 'LATERAL generate_series( ' ++ 'date_trunc(series_group.grp, series_group.start_date), ' ++ 'date_trunc(series_group.grp, now()), ' ++ 'CASE series_group.grp ' ++ 'WHEN \'day\' THEN interval \'1 day\' ' ++ 'WHEN \'week\' THEN interval \'1 week\' ' ++ 'ELSE interval \'1 month\' ' ++ 'END' ++ ') AS gs(day) ' ++ ') ' ++ 'SELECT ' ++ '(' ++ `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` ++ 'FROM ( ' ++ 'SELECT periods.day AS day, COALESCE(SUM("videoStat".views), 0) AS views ' ++ 'FROM periods ' ++ 'LEFT JOIN (' ++ '"videoStat" INNER JOIN "video" ON "videoStat"."videoId" = "video"."id" ' ++ 'AND "video"."channelId" = "VideoChannelModel"."id"' ++ ') ON date_trunc(periods.grp, "videoStat"."startDate") = periods.day ' ++ // The first (week/month) bucket floor can precede the range start, so without this+ // the first bar would also count views that happened before the requested range+ 'AND "videoStat"."startDate" >= periods.start_date ' ++ 'GROUP BY day ORDER BY day ' ++ ') t' ++ ') AS "viewsPerDay", ' ++ '(SELECT grp FROM series_group) AS "viewsGroupInterval"' ++ ') AS "ChannelStats" ON TRUE '++ this.builtChannelStatsJoin = true+ }+// ---------------------------------------------------------------------------protected buildQueryJoin () {this.buildChannelAvatarsJoin()this.buildAccountAvatarsJoin()this.buildChannelBannersJoin()++ if (this.options.statsDaysPrior !== undefined) {+ this.buildChannelStatsJoin()+ }}protected buildQueryAttributes () {@@ -217,34 +301,15 @@ export class VideoChannelListQueryBuilder extends AbstractListQuery {this.tableAttributes.getChannelBannerAttributes()]- if (this.options.statsDaysPrior) {- this.attributes.push(- `(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id") AS "videosCount"`- )-- this.attributes.push(- // dprint-ignore- '(' +- `SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +- 'FROM ( ' +- 'WITH days AS ( ' +- `SELECT generate_series(date_trunc('day', now()) - '${this.options.statsDaysPrior} day'::interval, ` +- `date_trunc('day', now()), '1 day'::interval) AS day ` +- ') ' +- 'SELECT days.day AS day, COALESCE(SUM("videoStat".views), 0) AS views ' +- 'FROM days ' +- 'LEFT JOIN (' +- '"videoStat" INNER JOIN "video" ON "videoStat"."videoId" = "video"."id" ' +- 'AND "video"."channelId" = "VideoChannelModel"."id"' +- `) ON date_trunc('day', "videoStat"."startDate") = date_trunc('day', days.day) ` +- 'GROUP BY day ORDER BY day ' +- ') t' +- ') AS "viewsPerDay"'- )-+ if (this.options.statsDaysPrior !== undefined) {this.attributes.push(+ `(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id") AS "videosCount"`,+ '"ChannelStats"."viewsPerDay" AS "viewsPerDay"',+ '"ChannelStats"."viewsGroupInterval" AS "viewsGroupInterval"',+ // Lifetime views: unlike viewsPerDay it is not bound to statsDaysPrior, and it uses the video+ // view counter so it also includes federated views and views older than the "videoStat" retention'(' +- 'SELECT COALESCE(SUM("video".views), 0) AS totalViews ' ++ 'SELECT COALESCE(SUM("video".views), 0) ' +'FROM "video" ' +'WHERE "video"."channelId" = "VideoChannelModel"."id"' +') AS "totalViews"'diff --git a/server/core/models/video/video-channel.ts b/server/core/models/video/video-channel.tsindex e85479724e..4fcc6dcc84 100644--- a/server/core/models/video/video-channel.ts+++ b/server/core/models/video/video-channel.ts@@ -1,4 +1,11 @@-import { ActivityPubActor, ActivityUrlObject, VideoChannel, VideoChannelSummary, VideoPrivacy } from '@peertube/peertube-models'+import {+ ActivityPubActor,+ ActivityUrlObject,+ VIDEO_CHANNEL_STATS_DAYS_DEFAULT,+ VideoChannel,+ VideoChannelSummary,+ VideoPrivacy+} from '@peertube/peertube-models'import { AttributesOnly } from '@peertube/peertube-typescript-utils'import { CONFIG } from '@server/initializers/config.js'import { getLocalActorPlayerSettingsActivityPubUrl } from '@server/lib/activitypub/url.js'@@ -401,10 +408,11 @@ export class VideoChannelModel extends SequelizeModel<VideoChannelModel> {static listByAccountForAPI (options: Pick<ListVideoChannelsOptions, 'accountId' | 'includeCollaborations' | 'search' | 'start' | 'count' | 'sort'> & {withStats?: boolean+ statsDays?: number}) {const listOptions = options.withStats- ? { ...options, statsDaysPrior: 30 }+ ? { ...options, statsDaysPrior: options.statsDays ?? VIDEO_CHANNEL_STATS_DAYS_DEFAULT }: optionsreturn this.listForApi(listOptions)@@ -549,6 +557,7 @@ export class VideoChannelModel extends SequelizeModel<VideoChannelModel> {toFormattedJSON (this: MChannelFormattable): VideoChannel {const viewsPerDayString = this.get('viewsPerDay') as stringconst videosCount = this.get('videosCount') as number+ const viewsGroupInterval = this.get('viewsGroupInterval') as VideoChannel['viewsGroupInterval']let viewsPerDay: { date: Date, views: number }[]@@ -580,6 +589,7 @@ export class VideoChannelModel extends SequelizeModel<VideoChannelModel> {videosCount,viewsPerDay,+ viewsGroupInterval,totalViews,avatars: actor.avatarsdiff --git a/support/doc/api/openapi.yaml b/support/doc/api/openapi.yamlindex bef85cd947..9e9832a8b7 100644--- a/support/doc/api/openapi.yaml+++ b/support/doc/api/openapi.yaml@@ -6195,9 +6195,24 @@ paths:- $ref: '#/components/parameters/name'- name: withStatsin: query- description: include daily view statistics for the last 30 days and total views (only if authenticated as the account user)+ description: include view statistics time series and total views (only if authenticated as the account user)schema:type: boolean+ - name: statsDays+ in: query+ description: >+ number of days of view statistics to include in `viewsPerDay` when `withStats` is true+ (only if authenticated as the account user).+ `0` means all time (since the channel's earliest recorded stat).+ `viewsPerDay` always spans the whole requested range so channels can be compared to each other.+ Its bucket size is reported in `viewsGroupInterval`: it is derived from the requested range,+ narrowed to the channel history so that a young channel on a long range is not aggregated+ into a single coarse bucket.+ `totalViews` is unaffected by this parameter and always reports lifetime views.+ schema:+ type: integer+ enum: [30, 90, 365, 0]+ default: 30- $ref: '#/components/parameters/start'- $ref: '#/components/parameters/count'- $ref: '#/components/parameters/search'@@ -13338,6 +13353,32 @@ components:$ref: '#/components/schemas/ActorImage'ownerAccount:$ref: '#/components/schemas/Account'+ videosCount:+ type: integer+ description: only included when `withStats` is true+ viewsPerDay:+ type: array+ description: >+ only included when `withStats` is true. Chronologically ordered, spans the whole `statsDays`+ range. Each point aggregates `viewsGroupInterval` worth of views, starting at `date`+ items:+ type: object+ properties:+ date:+ type: string+ format: date-time+ views:+ type: integer+ viewsGroupInterval:+ type: string+ description: bucket size of `viewsPerDay`, only included when `withStats` is true+ enum:+ - day+ - week+ - month+ totalViews:+ type: integer+ description: lifetime views of all the channel videos, only included when `withStats` is trueVideoChannelEdit:properties: