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.subs.push(this.route.fragment.subscribe(fragment => {
63
      setTimeout(() => {
64
        this.goTo(fragment);
65
      }, 100);
66
    }));
67
    this.init();
68
    this.search(true);
69
    for(let i = 0; i < 3; i++) {
70
      this.charts = [
71
        this.getSafeUrl('https://docs.google.com/spreadsheets/d/e/2PACX-1vQ-HejU11H1dMKa2MeIvLevnH0jtJUvduan3WMTjgDYCeq6g_WBPFBbZvg83hLuMv7XNyDQ3vHZWNYc/pubchart?oid=919355937&amp;format=interactive'),
72
        this.getSafeUrl('https://docs.google.com/spreadsheets/d/e/2PACX-1vQ-HejU11H1dMKa2MeIvLevnH0jtJUvduan3WMTjgDYCeq6g_WBPFBbZvg83hLuMv7XNyDQ3vHZWNYc/pubchart?oid=1667183135&amp;format=interactive'),
73
        this.getSafeUrl('https://docs.google.com/spreadsheets/d/e/2PACX-1vQ-HejU11H1dMKa2MeIvLevnH0jtJUvduan3WMTjgDYCeq6g_WBPFBbZvg83hLuMv7XNyDQ3vHZWNYc/pubchart?oid=2070534117&amp;format=interactive')
74
      ]
75
    }
76
  }
77
  
78
  public ngOnDestroy() {
79
    this.subs.forEach(subscription => {
80
      if (subscription instanceof Subscriber) {
81
        subscription.unsubscribe();
82
      }
83
    });
84
    this.clearTimeouts();
85
  }
86
  
87
  private init() {
88
    this.countryFb = new FormControl(null);
89
    this.countries = this.countryFb.valueChanges
90
      .pipe(
91
        startWith(''),
92
        map(value => this._filter(value))
93
      );
94
  }
95
  
96
  changeYear(i: number) {
97
    if(!this.iframeLoading) {
98
      this.iframeLoading = true;
99
      this.years = i;
100
    }
101
  }
102
  
103
  onLoad() {
104
    this.iframeLoading = false;
105
  }
106
  
107
  roundUsageStat(stats: UsageStat) {
108
    if(stats) {
109
      stats.repositories = NumberUtils.roundNumber(Number.parseInt(stats.repositories));
110
      stats.total_downloads = NumberUtils.roundNumber(Number.parseInt(stats.total_downloads));
111
      stats.total_views = NumberUtils.roundNumber(Number.parseInt(stats.total_views));
112
    }
113
    return stats;
114
  }
115
  
116
  roundCountryUsageStat(stats: CountryUsageStat) {
117
    if(stats) {
118
      stats.total_repos = NumberUtils.roundNumber(Number.parseInt(stats.total_repos));
119
      stats.downloads = NumberUtils.roundNumber(Number.parseInt(stats.downloads));
120
      stats.views = NumberUtils.roundNumber(Number.parseInt(stats.views));
121
    }
122
    return stats;
123
  }
124
  
125
  public search(show: boolean = false) {
126
    this.country = this.countryFb.value;
127
    this.showSearch = show;
128
    this.loading = true;
129
    this.clearTimeouts();
130
    if (this.country && this.country.length > 0) {
131
      this.country = StringUtils.capitalize(this.country);
132
      this.searchSub = this.usageStatsService.getCountryMetrics(this.country).subscribe(stats => {
133
        this.display = this.roundCountryUsageStat(stats);
134
        this.loading = false;
135
        if (this.display) {
136
          this.state = 1;
137
          this.animation();
138
        }
139
      }, error => {
140
        this.display = null;
141
        this.loading = false;
142
      });
143
    } else {
144
      this.usageStatsService.getAllMetrics().subscribe(stats => {
145
        this.display = this.roundUsageStat(stats);
146
        this.loading = false;
147
        if (this.display) {
148
          this.state = 1;
149
          this.animation();
150
        }
151
      }, error => {
152
        this.display = null;
153
        this.loading = false;
154
      });
155
    }
156
  }
157
  
158
  private animation() {
159
    this.timeouts.push(setTimeout(() => {
160
      if (this.state != 3) {
161
        this.animation();
162
      }
163
      this.state++;
164
    }, 800));
165
  }
166
  
167
  private _filter(value: string): string[] {
168
    const filterValue = value.toLowerCase();
169
    return countries.map(value => value.name).filter(option => filterValue && option.toLowerCase().includes(filterValue));
170
  }
171
  
172
  clearTimeouts() {
173
    this.timeouts.forEach(timeout => {
174
      clearTimeout(timeout);
175
    });
176
    this.state = 0;
177
    if(this.searchSub instanceof Subscription) {
178
      this.searchSub.unsubscribe();
179
    }
180
  }
181
  
182
  reset() {
183
    this.clearTimeouts();
184
    if (this.state == 0) {
185
      this.init();
186
      this.searchInput.reset();
187
      this.search(true);
188
    }
189
  }
190
  
191
  goTo(id: string) {
192
    const yOffset = -100;
193
    const element = document.getElementById(id);
194
    if(element) {
195
      const y = element.getBoundingClientRect().top + window.pageYOffset + yOffset;
196
      window.scrollTo({top: y, behavior: 'smooth'});
197
    }
198
  }
199
  
200
  public getSafeUrl(url: string): SafeUrl {
201
    return this.sanitizer.bypassSecurityTrustResourceUrl(url);
202
  }
203
}
(3-3/4)