1
|
import {Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
2
|
import {CountryUsageStat, UsageStat} from '../entities/usage-stat';
|
3
|
import {FormControl, FormGroup} 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
|
|
17
|
@Component({
|
18
|
selector: 'analytics',
|
19
|
templateUrl: 'analytics.component.html',
|
20
|
styleUrls: ['analytics.component.css'],
|
21
|
})
|
22
|
export class AnalyticsComponent implements OnInit, OnDestroy {
|
23
|
public countryFb: FormGroup;
|
24
|
public countries: Observable<string[]>;
|
25
|
public country: string;
|
26
|
public loading: boolean = true;
|
27
|
public iframeLoading: boolean = true;
|
28
|
public display: UsageStat | CountryUsageStat;
|
29
|
public showSearch: boolean = false;
|
30
|
public state: number = 0;
|
31
|
private timeouts: any[] = [];
|
32
|
public years = 0;
|
33
|
public charts: SafeUrl[];
|
34
|
@ViewChild('input') input: ElementRef;
|
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
|
|
41
|
constructor(private usageStatsService: UsageStatsService,
|
42
|
private route: ActivatedRoute,
|
43
|
private router: Router,
|
44
|
private _title: Title, private _piwikService: PiwikService,
|
45
|
private _meta: Meta, private seoService: SEOService,
|
46
|
private sanitizer: DomSanitizer) {
|
47
|
}
|
48
|
|
49
|
ngOnInit() {
|
50
|
this._title.setTitle(this.title);
|
51
|
this._meta.updateTag({content: this.description}, "name='description'");
|
52
|
this._meta.updateTag({content: this.description}, "property='og:description'");
|
53
|
this._meta.updateTag({content: this.title}, "property='og:title'");
|
54
|
this._title.setTitle(this.title);
|
55
|
let url = this.properties.domain + this.properties.baseLink + this.router.url;
|
56
|
this.seoService.createLinkForCanonicalURL(url, false);
|
57
|
this._meta.updateTag({content: url}, "property='og:url'");
|
58
|
if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
|
59
|
this.subs.push(this._piwikService.trackView(this.properties, this.title).subscribe());
|
60
|
}
|
61
|
this.subs.push(this.route.fragment.subscribe(fragment => {
|
62
|
setTimeout(() => {
|
63
|
this.goTo(fragment);
|
64
|
}, 100);
|
65
|
}));
|
66
|
this.init();
|
67
|
this.search(true);
|
68
|
for(let i = 0; i < 3; i++) {
|
69
|
this.charts = [
|
70
|
this.getSafeUrl('https://docs.google.com/spreadsheets/d/e/2PACX-1vQ-HejU11H1dMKa2MeIvLevnH0jtJUvduan3WMTjgDYCeq6g_WBPFBbZvg83hLuMv7XNyDQ3vHZWNYc/pubchart?oid=919355937&format=interactive'),
|
71
|
this.getSafeUrl('https://docs.google.com/spreadsheets/d/e/2PACX-1vQ-HejU11H1dMKa2MeIvLevnH0jtJUvduan3WMTjgDYCeq6g_WBPFBbZvg83hLuMv7XNyDQ3vHZWNYc/pubchart?oid=1667183135&format=interactive'),
|
72
|
this.getSafeUrl('https://docs.google.com/spreadsheets/d/e/2PACX-1vQ-HejU11H1dMKa2MeIvLevnH0jtJUvduan3WMTjgDYCeq6g_WBPFBbZvg83hLuMv7XNyDQ3vHZWNYc/pubchart?oid=2070534117&format=interactive')
|
73
|
]
|
74
|
}
|
75
|
}
|
76
|
|
77
|
public ngOnDestroy() {
|
78
|
this.subs.forEach(subscription => {
|
79
|
if (subscription instanceof Subscriber) {
|
80
|
subscription.unsubscribe();
|
81
|
}
|
82
|
});
|
83
|
this.clearTimeouts();
|
84
|
}
|
85
|
|
86
|
private init() {
|
87
|
this.countryFb = new FormGroup({
|
88
|
country: new FormControl(null)
|
89
|
});
|
90
|
this.countries = this.countryFb.get('country').valueChanges
|
91
|
.pipe(
|
92
|
startWith(''),
|
93
|
map(value => this._filter(value))
|
94
|
);
|
95
|
}
|
96
|
|
97
|
changeYear(i: number) {
|
98
|
if(!this.iframeLoading) {
|
99
|
this.iframeLoading = true;
|
100
|
this.years = i;
|
101
|
}
|
102
|
}
|
103
|
|
104
|
onLoad() {
|
105
|
this.iframeLoading = false;
|
106
|
}
|
107
|
|
108
|
roundUsageStat(stats: UsageStat) {
|
109
|
if(stats) {
|
110
|
stats.repositories = NumberUtils.roundNumber(Number.parseInt(stats.repositories));
|
111
|
stats.total_downloads = NumberUtils.roundNumber(Number.parseInt(stats.total_downloads));
|
112
|
stats.total_views = NumberUtils.roundNumber(Number.parseInt(stats.total_views));
|
113
|
}
|
114
|
return stats;
|
115
|
}
|
116
|
|
117
|
roundCountryUsageStat(stats: CountryUsageStat) {
|
118
|
if(stats) {
|
119
|
stats.total_repos = NumberUtils.roundNumber(Number.parseInt(stats.total_repos));
|
120
|
stats.downloads = NumberUtils.roundNumber(Number.parseInt(stats.downloads));
|
121
|
stats.views = NumberUtils.roundNumber(Number.parseInt(stats.views));
|
122
|
}
|
123
|
return stats;
|
124
|
}
|
125
|
|
126
|
public search(show: boolean = false) {
|
127
|
this.country = this.countryFb.value.country;
|
128
|
this.showSearch = show;
|
129
|
this.loading = true;
|
130
|
this.clearTimeouts();
|
131
|
if (this.country && this.country.length > 0) {
|
132
|
this.country = StringUtils.capitalize(this.country);
|
133
|
this.searchSub = this.usageStatsService.getCountryMetrics(this.country).subscribe(stats => {
|
134
|
this.display = this.roundCountryUsageStat(stats);
|
135
|
this.loading = false;
|
136
|
if (this.display) {
|
137
|
this.state = 1;
|
138
|
this.animation();
|
139
|
}
|
140
|
}, error => {
|
141
|
this.display = null;
|
142
|
this.loading = false;
|
143
|
});
|
144
|
} else {
|
145
|
this.usageStatsService.getAllMetrics().subscribe(stats => {
|
146
|
this.display = this.roundUsageStat(stats);
|
147
|
this.loading = false;
|
148
|
if (this.display) {
|
149
|
this.state = 1;
|
150
|
this.animation();
|
151
|
}
|
152
|
}, error => {
|
153
|
this.display = null;
|
154
|
this.loading = false;
|
155
|
});
|
156
|
}
|
157
|
}
|
158
|
|
159
|
private animation() {
|
160
|
this.timeouts.push(setTimeout(() => {
|
161
|
if (this.state != 3) {
|
162
|
this.animation();
|
163
|
}
|
164
|
this.state++;
|
165
|
}, 800));
|
166
|
}
|
167
|
|
168
|
private _filter(value: string): string[] {
|
169
|
const filterValue = value.toLowerCase();
|
170
|
return countries.map(value => value.name).filter(option => filterValue && option.toLowerCase().includes(filterValue));
|
171
|
}
|
172
|
|
173
|
toggle() {
|
174
|
this.showSearch = !this.showSearch;
|
175
|
if (this.showSearch) {
|
176
|
setTimeout(() => { // this will make the execution after the above boolean has changed
|
177
|
this.input.nativeElement.focus();
|
178
|
}, 0);
|
179
|
}
|
180
|
}
|
181
|
|
182
|
closeSearch() {
|
183
|
this.showSearch = false;
|
184
|
}
|
185
|
|
186
|
clearTimeouts() {
|
187
|
this.timeouts.forEach(timeout => {
|
188
|
clearTimeout(timeout);
|
189
|
});
|
190
|
this.state = 0;
|
191
|
if(this.searchSub instanceof Subscription) {
|
192
|
this.searchSub.unsubscribe();
|
193
|
}
|
194
|
}
|
195
|
|
196
|
reset() {
|
197
|
this.clearTimeouts();
|
198
|
if (this.state == 0) {
|
199
|
this.init();
|
200
|
this.showSearch = true;
|
201
|
setTimeout(() => { // this will make the execution after the above boolean has changed
|
202
|
this.input.nativeElement.focus();
|
203
|
}, 0);
|
204
|
this.search();
|
205
|
}
|
206
|
}
|
207
|
|
208
|
goTo(id: string) {
|
209
|
const yOffset = -100;
|
210
|
const element = document.getElementById(id);
|
211
|
if(element) {
|
212
|
const y = element.getBoundingClientRect().top + window.pageYOffset + yOffset;
|
213
|
window.scrollTo({top: y, behavior: 'smooth'});
|
214
|
}
|
215
|
}
|
216
|
|
217
|
public getSafeUrl(url: string): SafeUrl {
|
218
|
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
|
219
|
}
|
220
|
}
|