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
        /*if ( this.authService.getUserEmail() !== this.piwik.requestorEmail ) {
57
          this.router.navigateByUrl('/403-forbidden', { skipLocationChange: true });
58
        } else {*/
59
          this.getMetrics();
60
        /*}*/
61
      }
62
    );
63
  }
64

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

    
91

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

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

    
128
}
(6-6/16)