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
    this.getPiwik();
39
    let body = document.getElementsByTagName('body')[0];
40
    body.classList.remove("top_bar_active");   //remove the class
41
    body.classList.remove("page_heading_active");
42
    body.classList.remove("landing");
43
    body.classList.add("dashboard");
44
  }
45

    
46

    
47
  getPiwik() {
48
    this.loadingMessage = loadingMetrics;
49
    this.piwikService.getPiwikInfo(this.repoId).subscribe(
50
      piwik => this.piwik = piwik,
51
      error => {
52
        this.loadingMessage = '';
53
        this.errorMessage = loadingMetricsError;
54
        console.log(error);
55
      },
56
      () => {
57
        this.loadingMessage = '';
58
        this.errorMessage = '';
59
        this.getMetrics();
60
      }
61
    );
62
  }
63

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

    
93

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

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

    
130
}
(6-6/17)