Project

General

Profile

1
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
2
import {CountryUsageStat, UsageStat} from '../entities/usage-stat';
3
import {FormControl} from '@angular/forms';
4
import {Observable, Subscriber, Subscription} from 'rxjs';
5
import {UsageStatsService} from '../services/usage-stats.service';
6
import {map, startWith} from 'rxjs/operators';
7
import {countries} from '../services/countries';
8
import {DomSanitizer, Meta, SafeUrl, Title} from '@angular/platform-browser';
9
import {StringUtils} from '../openaireLibrary/utils/string-utils.class';
10
import {ActivatedRoute, Router} from '@angular/router';
11
import {NumberUtils} from '../openaireLibrary/utils/number-utils.class';
12
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
13
import {properties} from '../../environments/environment';
14
import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service';
15
import {SEOService} from '../openaireLibrary/sharedComponents/SEO/SEO.service';
16
import {SearchInputComponent} from '../openaireLibrary/sharedComponents/search-input/search-input.component';
17

    
18
@Component({
19
  selector: 'analytics',
20
  templateUrl: 'analytics.component.html',
21
  styleUrls: ['analytics.component.css'],
22
})
23
export class AnalyticsComponent implements OnInit, OnDestroy {
24
  public countryFb: FormControl;
25
  public countries: Observable<string[]>;
26
  public country: string;
27
  public loading: boolean = true;
28
  public iframeLoading: boolean = true;
29
  public display: UsageStat | CountryUsageStat;
30
  public showSearch: boolean = false;
31
  public state: number = 0;
32
  private timeouts: any[] = [];
33
  public years = 0;
34
  public charts: SafeUrl[];
35
  properties: EnvProperties = properties;
36
  description = "Track Countries Usage Activity. Worldwide Monthly Usage Events, Monthly Views & Downloads.";
37
  title = "OpenAIRE - UsageCounts | Analytics";
38
  subs: Subscription[] = [];
39
  searchSub: Subscription = null;
40
  @ViewChild('searchInput') searchInput: SearchInputComponent;
41
  
42
  constructor(private usageStatsService: UsageStatsService,
43
              private route: ActivatedRoute,
44
              private router: Router,
45
              private _title: Title, private _piwikService: PiwikService,
46
              private _meta: Meta, private seoService: SEOService,
47
              private sanitizer: DomSanitizer) {
48
  }
49
  
50
  ngOnInit() {
51
    this._title.setTitle(this.title);
52
    this._meta.updateTag({content: this.description}, "name='description'");
53
    this._meta.updateTag({content: this.description}, "property='og:description'");
54
    this._meta.updateTag({content: this.title}, "property='og:title'");
55
    this._title.setTitle(this.title);
56
    let url = this.properties.domain + this.properties.baseLink + this.router.url;
57
    this.seoService.createLinkForCanonicalURL(url, false);
58
    this._meta.updateTag({content: url}, "property='og:url'");
59
    if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
60
      this.subs.push(this._piwikService.trackView(this.properties, this.title).subscribe());
61
    }
62
    this.init();
63
    this.search(true);
64
    for(let i = 0; i < 3; i++) {
65
      this.charts = [
66
        this.getSafeUrl('https://docs.google.com/spreadsheets/d/e/2PACX-1vQ-HejU11H1dMKa2MeIvLevnH0jtJUvduan3WMTjgDYCeq6g_WBPFBbZvg83hLuMv7XNyDQ3vHZWNYc/pubchart?oid=919355937&amp;format=interactive'),
67
        this.getSafeUrl('https://docs.google.com/spreadsheets/d/e/2PACX-1vQ-HejU11H1dMKa2MeIvLevnH0jtJUvduan3WMTjgDYCeq6g_WBPFBbZvg83hLuMv7XNyDQ3vHZWNYc/pubchart?oid=1667183135&amp;format=interactive'),
68
        this.getSafeUrl('https://docs.google.com/spreadsheets/d/e/2PACX-1vQ-HejU11H1dMKa2MeIvLevnH0jtJUvduan3WMTjgDYCeq6g_WBPFBbZvg83hLuMv7XNyDQ3vHZWNYc/pubchart?oid=2070534117&amp;format=interactive')
69
      ]
70
    }
71
  }
72
  
73
  public ngOnDestroy() {
74
    this.subs.forEach(subscription => {
75
      if (subscription instanceof Subscriber) {
76
        subscription.unsubscribe();
77
      }
78
    });
79
    this.clearTimeouts();
80
  }
81
  
82
  private init() {
83
    this.countryFb = new FormControl(null);
84
    this.countries = this.countryFb.valueChanges
85
      .pipe(
86
        startWith(''),
87
        map(value => this._filter(value))
88
      );
89
  }
90
  
91
  changeYear(i: number) {
92
    if(!this.iframeLoading) {
93
      this.iframeLoading = true;
94
      this.years = i;
95
    }
96
  }
97
  
98
  onLoad() {
99
    this.iframeLoading = false;
100
  }
101
  
102
  roundUsageStat(stats: UsageStat) {
103
    if(stats) {
104
      stats.repositories = NumberUtils.roundNumber(Number.parseInt(stats.repositories));
105
      stats.total_downloads = NumberUtils.roundNumber(Number.parseInt(stats.total_downloads));
106
      stats.total_views = NumberUtils.roundNumber(Number.parseInt(stats.total_views));
107
    }
108
    return stats;
109
  }
110
  
111
  roundCountryUsageStat(stats: CountryUsageStat) {
112
    if(stats) {
113
      stats.total_repos = NumberUtils.roundNumber(Number.parseInt(stats.total_repos));
114
      stats.downloads = NumberUtils.roundNumber(Number.parseInt(stats.downloads));
115
      stats.views = NumberUtils.roundNumber(Number.parseInt(stats.views));
116
    }
117
    return stats;
118
  }
119
  
120
  public search(show: boolean = false) {
121
    this.country = this.countryFb.value;
122
    this.showSearch = show;
123
    this.loading = true;
124
    this.clearTimeouts();
125
    if (this.country && this.country.length > 0) {
126
      this.country = StringUtils.capitalize(this.country);
127
      this.searchSub = this.usageStatsService.getCountryMetrics(this.country).subscribe(stats => {
128
        this.display = this.roundCountryUsageStat(stats);
129
        this.loading = false;
130
        if (this.display) {
131
          this.state = 1;
132
          this.animation();
133
        }
134
      }, error => {
135
        this.display = null;
136
        this.loading = false;
137
      });
138
    } else {
139
      this.usageStatsService.getAllMetrics().subscribe(stats => {
140
        this.display = this.roundUsageStat(stats);
141
        this.loading = false;
142
        if (this.display) {
143
          this.state = 1;
144
          this.animation();
145
        }
146
      }, error => {
147
        this.display = null;
148
        this.loading = false;
149
      });
150
    }
151
  }
152
  
153
  private animation() {
154
    this.timeouts.push(setTimeout(() => {
155
      if (this.state != 3) {
156
        this.animation();
157
      }
158
      this.state++;
159
    }, 800));
160
  }
161
  
162
  private _filter(value: string): string[] {
163
    const filterValue = value.toLowerCase();
164
    return countries.map(value => value.name).filter(option => filterValue && option.toLowerCase().includes(filterValue));
165
  }
166
  
167
  clearTimeouts() {
168
    this.timeouts.forEach(timeout => {
169
      clearTimeout(timeout);
170
    });
171
    this.state = 0;
172
    if(this.searchSub instanceof Subscription) {
173
      this.searchSub.unsubscribe();
174
    }
175
  }
176
  
177
  reset() {
178
    this.clearTimeouts();
179
    if (this.state == 0) {
180
      this.init();
181
      this.searchInput.reset();
182
      this.search(true);
183
    }
184
  }
185
  
186
  public getSafeUrl(url: string): SafeUrl {
187
    return this.sanitizer.bypassSecurityTrustResourceUrl(url);
188
  }
189
}
(3-3/4)