aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+stats/video/video-stats.service.ts
blob: e019c87f71e485e20a3ae2c33f1c7f3647648d3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { catchError } from 'rxjs'
import { environment } from 'src/environments/environment'
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { RestExtractor } from '@app/core'
import { VideoService } from '@app/shared/shared-main'
import { VideoStatsOverall, VideoStatsRetention, VideoStatsTimeserie, VideoStatsTimeserieMetric } from '@shared/models/videos'

@Injectable({
  providedIn: 'root'
})
export class VideoStatsService {
  static BASE_VIDEO_STATS_URL = environment.apiUrl + '/api/v1/videos/'

  constructor (
    private authHttp: HttpClient,
    private restExtractor: RestExtractor
  ) { }

  getOverallStats (options: {
    videoId: string
    startDate?: Date
    endDate?: Date
  }) {
    const { videoId, startDate, endDate } = options

    let params = new HttpParams()
    if (startDate) params = params.append('startDate', startDate.toISOString())
    if (endDate) params = params.append('endDate', endDate.toISOString())

    return this.authHttp.get<VideoStatsOverall>(VideoService.BASE_VIDEO_URL + '/' + videoId + '/stats/overall', { params })
      .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  getTimeserieStats (options: {
    videoId: string
    metric: VideoStatsTimeserieMetric
    startDate?: Date
    endDate?: Date
  }) {
    const { videoId, metric, startDate, endDate } = options

    let params = new HttpParams()
    if (startDate) params = params.append('startDate', startDate.toISOString())
    if (endDate) params = params.append('endDate', endDate.toISOString())

    return this.authHttp.get<VideoStatsTimeserie>(VideoService.BASE_VIDEO_URL + '/' + videoId + '/stats/timeseries/' + metric, { params })
      .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  getRetentionStats (videoId: string) {
    return this.authHttp.get<VideoStatsRetention>(VideoService.BASE_VIDEO_URL + '/' + videoId + '/stats/retention')
      .pipe(catchError(err => this.restExtractor.handleError(err)))
  }
}