]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-video-channels/my-account-video-channels.component.ts
Use inner join and document code for viewr stats for channels
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-video-channels / my-account-video-channels.component.ts
CommitLineData
08c1efbe 1import { Component, OnInit } from '@angular/core'
f8b2c1b4 2import { Notifier } from '@app/core'
08c1efbe
C
3import { AuthService } from '../../core/auth'
4import { ConfirmService } from '../../core/confirm'
5import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
6import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
8165d00a 7import { ScreenService } from '@app/shared/misc/screen.service'
08c1efbe 8import { User } from '@app/shared'
db400f44 9import { flatMap } from 'rxjs/operators'
b1d40cff 10import { I18n } from '@ngx-translate/i18n-polyfill'
3d527ba1
RK
11import { min, minBy, max, maxBy } from 'lodash-es'
12import { ChartData } from 'chart.js'
08c1efbe
C
13
14@Component({
15 selector: 'my-account-video-channels',
16 templateUrl: './my-account-video-channels.component.html',
17 styleUrls: [ './my-account-video-channels.component.scss' ]
18})
6a478b11 19export class MyAccountVideoChannelsComponent implements OnInit {
08c1efbe 20 videoChannels: VideoChannel[] = []
3d527ba1 21 videoChannelsChartData: ChartData[]
8165d00a
RK
22 videoChannelsMinimumDailyViews = 0
23 videoChannelsMaximumDailyViews: number
08c1efbe
C
24
25 private user: User
26
27 constructor (
28 private authService: AuthService,
f8b2c1b4 29 private notifier: Notifier,
08c1efbe 30 private confirmService: ConfirmService,
b1d40cff 31 private videoChannelService: VideoChannelService,
8165d00a 32 private screenService: ScreenService,
b1d40cff 33 private i18n: I18n
08c1efbe
C
34 ) {}
35
36 ngOnInit () {
37 this.user = this.authService.getUser()
38
39 this.loadVideoChannels()
40 }
41
8165d00a
RK
42 get isInSmallView () {
43 return this.screenService.isInSmallView()
44 }
45
46 get chartOptions () {
47 return {
48 legend: {
49 display: false
50 },
51 scales: {
52 xAxes: [{
53 display: false
54 }],
55 yAxes: [{
56 display: false,
57 ticks: {
58 min: Math.max(0, this.videoChannelsMinimumDailyViews - (3 * this.videoChannelsMaximumDailyViews / 100)),
59 max: this.videoChannelsMaximumDailyViews
60 }
747c5628 61 }]
8165d00a
RK
62 },
63 layout: {
64 padding: {
65 left: 15,
66 right: 15,
67 top: 10,
68 bottom: 0
69 }
70 },
71 elements: {
747c5628 72 point: {
8165d00a
RK
73 radius: 0
74 }
75 },
76 tooltips: {
77 mode: 'index',
78 intersect: false,
79 custom: function (tooltip: any) {
747c5628
RK
80 if (!tooltip) return
81 // disable displaying the color box
82 tooltip.displayColors = false
8165d00a
RK
83 },
84 callbacks: {
747c5628 85 label: (tooltip: any, data: any) => `${tooltip.value} views`
8165d00a
RK
86 }
87 },
88 hover: {
89 mode: 'index',
90 intersect: false
91 }
92 }
93 }
94
08c1efbe
C
95 async deleteVideoChannel (videoChannel: VideoChannel) {
96 const res = await this.confirmService.confirmWithInput(
b1d40cff 97 this.i18n(
df93a9be
C
98 // tslint:disable
99 'Do you really want to delete {{channelDisplayName}}? It will delete all videos uploaded in this channel, and you will not be able to create another channel with the same name ({{channelName}})!',
b4593cd7
C
100 { channelDisplayName: videoChannel.displayName, channelName: videoChannel.name }
101 ),
102 this.i18n(
103 'Please type the display name of the video channel ({{displayName}}) to confirm',
104 { displayName: videoChannel.displayName }
b1d40cff 105 ),
08c1efbe 106 videoChannel.displayName,
b1d40cff 107 this.i18n('Delete')
08c1efbe
C
108 )
109 if (res === false) return
110
111 this.videoChannelService.removeVideoChannel(videoChannel)
112 .subscribe(
f8b2c1b4 113 () => {
08c1efbe 114 this.loadVideoChannels()
f8b2c1b4 115 this.notifier.success(
25acef90 116 this.i18n('Video channel {{videoChannelName}} deleted.', { videoChannelName: videoChannel.displayName })
b1d40cff 117 )
08c1efbe
C
118 },
119
f8b2c1b4 120 error => this.notifier.error(error.message)
08c1efbe
C
121 )
122 }
123
124 private loadVideoChannels () {
125 this.authService.userInformationLoaded
747c5628 126 .pipe(flatMap(() => this.videoChannelService.listAccountVideoChannels(this.user.account, null, true)))
8165d00a
RK
127 .subscribe(res => {
128 this.videoChannels = res.data
3d527ba1
RK
129
130 // chart data
131 this.videoChannelsChartData = this.videoChannels.map(v => ({
8165d00a
RK
132 labels: v.viewsPerDay.map(day => day.date.toLocaleDateString()),
133 datasets: [
134 {
135 label: this.i18n('Views for the day'),
136 data: v.viewsPerDay.map(day => day.views),
137 fill: false,
138 borderColor: "#c6c6c6"
139 }
140 ]
3d527ba1
RK
141 } as ChartData))
142
143 // chart options that depend on chart data:
144 // we don't want to skew values and have min at 0, so we define what the floor/ceiling is here
145 this.videoChannelsMinimumDailyViews = min(
146 this.videoChannels.map(v => minBy( // compute local minimum daily views for each channel, by their "views" attribute
147 v.viewsPerDay,
148 day => day.views
149 ).views) // the object returned is a ViewPerDate, so we still need to get the views attribute
150 )
151 this.videoChannelsMaximumDailyViews = max(
152 this.videoChannels.map(v => maxBy( // compute local maximum daily views for each channel, by their "views" attribute
153 v.viewsPerDay,
154 day => day.views
155 ).views) // the object returned is a ViewPerDate, so we still need to get the views attribute
156 )
8165d00a 157 })
08c1efbe
C
158 }
159}