Project

General

Profile

1
import { Component, OnInit } from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import { MetricsInfo, PiwikInfo } from '../../domain/typeScriptClasses';
4
import { PiwikService } from '../../services/piwik.service';
5
import { RepositoryService } from '../../services/repository.service';
6
import { loadingMetrics, loadingMetricsError } from '../../domain/shared-messages';
7
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
8
import {AuthenticationService} from "../../services/authentication.service";
9

    
10
@Component ({
11
  selector: 'metrics-show',
12
  templateUrl: 'metrics-show.component.html'
13
})
14

    
15
export class MetricsShowComponent implements OnInit {
16
  errorMessage: string;
17
  loadingMessage: string;
18

    
19
  repoId: string;
20
  piwik: PiwikInfo;
21
  repoMetrics: MetricsInfo;
22
  pageViews = '--';
23
  totalViews = '--';
24
  totalDownloads = '--';
25
  viewsUrl: SafeResourceUrl;
26
  downloadsUrl: SafeResourceUrl;
27

    
28
  constructor(
29
    private route: ActivatedRoute,
30
    private router: Router,
31
    private sanitizer: DomSanitizer,
32
    private piwikService: PiwikService,
33
    private repoService: RepositoryService,
34
    private authService: AuthenticationService) {}
35

    
36
  ngOnInit() {
37
    this.repoId = this.route.snapshot.paramMap.get('id');
38
    setTimeout(()=> {
39
      this.getPiwik();
40
    },1000);
41
  }
42

    
43

    
44
  getPiwik() {
45
    this.loadingMessage = loadingMetrics;
46
    this.piwikService.getPiwikInfo(this.repoId).subscribe(
47
      piwik => this.piwik = piwik,
48
      error => {
49
        this.loadingMessage = '';
50
        this.errorMessage = loadingMetricsError;
51
        console.log(error);
52
      },
53
      () => {
54
        this.loadingMessage = '';
55
        this.errorMessage = '';
56
        // TODO: UNCOMMENT
57
        /*if ( this.authService.getUserEmail() !== this.piwik.requestorEmail ) {
58
          this.router.navigateByUrl('/403-forbidden', { skipLocationChange: true });
59
        } else {*/
60
          this.getMetrics();
61
        /*}*/
62
      }
63
    );
64
  }
65

    
66
  getMetrics() {
67
    this.loadingMessage = loadingMetrics;
68
    this.repoService.getMetricsInfoForRepository(this.repoId).subscribe(
69
      metrics => {
70
        this.repoMetrics = metrics;
71
        if (this.repoMetrics.metricsNumbers.pageviews)
72
          this.pageViews = this.repoMetrics.metricsNumbers.pageviews;
73
        if (this.repoMetrics.metricsNumbers.total_views)
74
          this.totalViews = this.repoMetrics.metricsNumbers.total_views;
75
        if (this.repoMetrics.metricsNumbers.total_downloads)
76
          this.totalDownloads = this.repoMetrics.metricsNumbers.total_downloads;
77
        this.getViewsUrl();
78
        this.getDownloadsUrl();
79
      },
80
      error => {
81
        this.loadingMessage = '';
82
        this.errorMessage = loadingMetricsError;
83
        console.log(error);
84
      },
85
      () => {
86
        this.loadingMessage = '';
87
        this.errorMessage = '';
88
      }
89
    );
90
  }
91

    
92

    
93
  getViewsUrl () {
94
    this.viewsUrl = this.sanitizer.bypassSecurityTrustResourceUrl(
95
      `${this.repoMetrics.diagramsBaseURL}merge.php?com=query
96
      &data=[{"query":"dtsrcRepoViews","dtsrcName":"${this.piwik.openaireId}",
97
      "table":"","fields":[{"fld":"sum","agg":"sum","type":"chart","yaxis":1,"c":false}],
98
      "xaxis":{"name":"month","agg":"sum"},"group":"","color":"","type":"chart","size":30,
99
      "sort":"xaxis","xStyle":{"r":-30,"s":"0","l":"-","ft":"-","wt":"-"},"title":"","subtitle":"",
100
      "xaxistitle":"","yaxisheaders":["Monthly views"],"generalxaxis":"","theme":0,"in":[]}]
101
      &info_types=["spline"]&stacking=&steps=false&fontFamily=Courier&spacing=[5,0,0,0]
102
      &style=[{"color":"rgba(0, 0, 0, 1)","size":"18"},{"color":"rgba(0, 0, 0, 1)","size":"18"},
103
      {"color":"000000","size":""},{"color":"000000","size":""}]&backgroundColor=rgba(255,255,255,1)
104
      &colors[]=rgba(124,181, 236, 1)&colors[]=rgba(67, 67, 72, 1)&colors[]=rgba(144, 237, 125,1)
105
      &colors[]=rgba(247, 163, 92, 1)&colors[]=rgba(128, 133, 233,1)&colors[]=rgba(241, 92, 128, 1)
106
      &colors[]=rgba(228, 211, 84,1)&colors[]=rgba(43, 144, 143, 1)&colors[]=rgba(244, 91, 91,1)
107
      &colors[]=rgba(145, 232, 225,1)&xlinew=0&ylinew=1&legends=true&tooltips=true&persistent=false`
108
    );
109
  }
110

    
111
  getDownloadsUrl () {
112
    this.downloadsUrl = this.sanitizer.bypassSecurityTrustResourceUrl(
113
      `${this.repoMetrics.diagramsBaseURL}merge.php?com=query
114
      &data=[{"query":"dtsrcRepoDownloads","dtsrcName":"${this.piwik.openaireId}",
115
      "table":"","fields":[{"fld":"sum","agg":"sum","type":"chart","yaxis":1,"c":false}],
116
      "xaxis":{"name":"month","agg":"sum"},"group":"","color":"","type":"chart","size":30,
117
      "sort":"xaxis","xStyle":{"r":-30,"s":"0","l":"-","ft":"-","wt":"-"},"title":"","subtitle":"",
118
      "xaxistitle":"","yaxisheaders":["Monthly downloads"],"generalxaxis":"","theme":0,"in":[]}]
119
      &info_types=["spline"]&stacking=&steps=false&fontFamily=Courier&spacing=[5,0,0,0]
120
      &style=[{"color":"rgba(0, 0, 0, 1)","size":"18"},{"color":"rgba(0, 0, 0,1)","size":"18"},
121
      {"color":"000000","size":""},{"color":"000000","size":""}]&backgroundColor=rgba(255,255,255,1)
122
      &colors[]=rgba(124, 181, 236, 1)&colors[]=rgba(67, 67, 72, 1)&colors[]=rgba(144, 237, 125,1)
123
      &colors[]=rgba(247, 163, 92, 1)&colors[]=rgba(128, 133, 233,1)&colors[]=rgba(241, 92, 128, 1)
124
      &colors[]=rgba(228, 211, 84,1)&colors[]=rgba(43, 144, 143, 1)&colors[]=rgba(244, 91, 91,1)
125
      &colors[]=rgba(145, 232, 225,1)&xlinew=0&ylinew=1&legends=true&tooltips=true&persistent=false`
126
    );
127
  }
128

    
129
}
(6-6/16)