]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+stats/video/video-stats.service.ts
Fix menu dropdowns
[github/Chocobozzz/PeerTube.git] / client / src / app / +stats / video / video-stats.service.ts
1 import { catchError } from 'rxjs'
2 import { environment } from 'src/environments/environment'
3 import { HttpClient, HttpParams } from '@angular/common/http'
4 import { Injectable } from '@angular/core'
5 import { RestExtractor } from '@app/core'
6 import { VideoService } from '@app/shared/shared-main'
7 import { VideoStatsOverall, VideoStatsRetention, VideoStatsTimeserie, VideoStatsTimeserieMetric } from '@shared/models/videos'
8
9 @Injectable({
10 providedIn: 'root'
11 })
12 export class VideoStatsService {
13 static BASE_VIDEO_STATS_URL = environment.apiUrl + '/api/v1/videos/'
14
15 constructor (
16 private authHttp: HttpClient,
17 private restExtractor: RestExtractor
18 ) { }
19
20 getOverallStats (options: {
21 videoId: string
22 startDate?: Date
23 endDate?: Date
24 }) {
25 const { videoId, startDate, endDate } = options
26
27 let params = new HttpParams()
28 if (startDate) params = params.append('startDate', startDate.toISOString())
29 if (endDate) params = params.append('endDate', endDate.toISOString())
30
31 return this.authHttp.get<VideoStatsOverall>(VideoService.BASE_VIDEO_URL + '/' + videoId + '/stats/overall', { params })
32 .pipe(catchError(err => this.restExtractor.handleError(err)))
33 }
34
35 getTimeserieStats (options: {
36 videoId: string
37 metric: VideoStatsTimeserieMetric
38 startDate?: Date
39 endDate?: Date
40 }) {
41 const { videoId, metric, startDate, endDate } = options
42
43 let params = new HttpParams()
44 if (startDate) params = params.append('startDate', startDate.toISOString())
45 if (endDate) params = params.append('endDate', endDate.toISOString())
46
47 return this.authHttp.get<VideoStatsTimeserie>(VideoService.BASE_VIDEO_URL + '/' + videoId + '/stats/timeseries/' + metric, { params })
48 .pipe(catchError(err => this.restExtractor.handleError(err)))
49 }
50
51 getRetentionStats (videoId: string) {
52 return this.authHttp.get<VideoStatsRetention>(VideoService.BASE_VIDEO_URL + '/' + videoId + '/stats/retention')
53 .pipe(catchError(err => this.restExtractor.handleError(err)))
54 }
55 }