import {Component, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core';
import {SearchResearchResultsService} from '../../services/searchResearchResults.service';
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
import {RefineFieldResultsService} from '../../services/refineFieldResults.service';
import {EnvProperties} from '../../utils/properties/env-properties';
import {properties} from '../../../../environments/environment';
import {NumberSize, NumberUtils} from '../../utils/number-utils.class';
import {BehaviorSubject, Observable, Subscription, zip} from 'rxjs';
import {RouterHelper} from "../../utils/routerHelper.class";
import {OpenaireEntities} from "../../utils/properties/searchFields";
import {SearchOrganizationsService} from '../../services/searchOrganizations.service';
export interface Numbers {
publicationsSize?: NumberSize;
datasetsSize?: NumberSize;
datasetsLinkedSize?: NumberSize;
softwareLinkedSize?: NumberSize;
softwareSize?: NumberSize;
otherSize?: NumberSize;
fundersSize?: NumberSize;
mergedFundersSize?: NumberSize;
projectsSize?: NumberSize;
datasourcesSize?: NumberSize;
organizationsSize?: NumberSize;
}
@Component({
selector: 'numbers',
template: `
A comprehensive and open dataset of research information covering
{{numbers.publicationsSize.number|number}}{{numbers.publicationsSize.size}} {{openaireEntities.PUBLICATIONS.toLowerCase()}}
,
{{numbers.datasetsSize.number|number}}{{numbers.datasetsSize.size}} {{openaireEntities.DATASETS.toLowerCase()}}
,
{{numbers.softwareSize.number|number}}{{numbers.softwareSize.size}} {{openaireEntities.SOFTWARE.toLowerCase()}} items
,
from
{{numbers.datasourcesSize.number|number}}{{numbers.datasourcesSize.size}} {{openaireEntities.DATASOURCES.toLowerCase()}}
, linked to
{{numbers.projectsSize.number|number}}{{numbers.projectsSize.size}} grants
and
{{numbers.organizationsSize.number|number}}{{numbers.organizationsSize.size}} {{openaireEntities.ORGANIZATIONS.toLowerCase()}}.
All linked together through citations and semantics.
`,
})
export class NumbersComponent implements OnInit, OnDestroy {
/** Add a value if you want to apply refine query*/
@Input() refineValue = null;
/** True: Default initialization
* False: Call init method to initialize numbers */
@Input() defaultInit = true;
/** When numbers have been initialized this emitter will emitted */
@Output() results: EventEmitter = new EventEmitter();
showPublications: boolean = true;
showDatasets: boolean = true;
showSoftware: boolean = true;
showOrp: boolean = true;
showOrganizations: boolean = true;
showProjects: boolean = true;
showDataProviders: boolean = true;
numbersLimit: number = 100;
public properties: EnvProperties = properties;
public openaireEntities = OpenaireEntities;
public routerHelper: RouterHelper = new RouterHelper();
public numbers: Numbers = {};
private emptySubject: BehaviorSubject = new BehaviorSubject(0);
private subs: any[] = [];
constructor(private searchResearchResultsService: SearchResearchResultsService,
private searchDataprovidersService: SearchDataprovidersService,
private refineFieldResultsService: RefineFieldResultsService,
private _searchOrganizationsService: SearchOrganizationsService) {
this.emptySubject = new BehaviorSubject(0);
}
ngOnInit() {
if (this.defaultInit) {
this.init();
}
}
init(getDatasetsLinked = false, getSoftwareLinked = false, getPublications = true, getDatasets = true,
getSoftware = true, getOther = true, getProjects = true, getDataProviders = true, getOrganizations = true, refineValue: string = null) {
if (refineValue) {
this.refineValue = refineValue;
}
let refineParams = (this.refineValue) ? ('&fq=' + this.refineValue) : null;
let mergedFundersSet = new Set();
this.subs.push(zip(
(getPublications) ? this.searchResearchResultsService.numOfSearchResults('publication', '', this.properties, refineParams) : this.empty,
(getDatasets) ? this.searchResearchResultsService.numOfSearchResults('dataset', '', this.properties, refineParams) : this.empty,
(getDatasetsLinked) ? this.searchResearchResultsService.numOfSearchResultsLinkedToPub("dataset", this.properties) : this.empty,
(getSoftware) ? this.searchResearchResultsService.numOfSearchResults('software', '', this.properties, refineParams) : this.empty,
(getSoftwareLinked) ? this.searchResearchResultsService.numOfSearchResultsLinkedToPub("software", this.properties) : this.empty,
(getOther) ? this.searchResearchResultsService.numOfSearchResults('other', '', this.properties, refineParams) : this.empty,
(getProjects) ? this.refineFieldResultsService.getAllRefineFieldResultsByFieldName('funder', 'project', this.properties, refineParams) : this.empty,
(getDataProviders) ? this.searchDataprovidersService.numOfSearchDataproviders('', this.properties, refineParams) : this.empty,
(getOrganizations) ? this._searchOrganizationsService.numOfSearchOrganizations2('', this.properties, refineParams) : this.empty,
(getPublications && getDatasets && getSoftware && getOther) ? this.refineFieldResultsService.getAllRefineFieldResultsByFieldName('relfunder', 'result', this.properties, refineParams) : this.empty,
).subscribe((data: any[]) => {
if (data[0] && data[0] > 0) {
this.numbers.publicationsSize = NumberUtils.roundNumber(data[0]);
}
if (data[1] && data[1] > 0) {
this.numbers.datasetsSize = NumberUtils.roundNumber(data[1]);
}
if (data[2] && data[2] > 0) {
this.numbers.datasetsLinkedSize = NumberUtils.roundNumber(data[2]);
}
if (data[3] && data[3] > 0) {
this.numbers.softwareSize = NumberUtils.roundNumber(data[3]);
}
if (data[4] && data[4] > 0) {
this.numbers.softwareLinkedSize = NumberUtils.roundNumber(data[4]);
}
if (data[5] && data[5] > 0) {
this.numbers.otherSize = NumberUtils.roundNumber(data[5]);
}
if (data[6][0] && data[6][0] > 0) {
this.numbers.projectsSize = NumberUtils.roundNumber(data[6][0]);
}
if (data[6][1] && data[6][1].length > 0 && data[6][1][0].filterId == 'funder' && data[6][1][0].values) {
this.numbers.fundersSize = NumberUtils.roundNumber(data[6][1][0].values.length);
let queriedFunders = data[6][1][0].values;
queriedFunders.forEach(queriedFunder => {
if(+queriedFunder.number > 1) {
if (!mergedFundersSet.has(queriedFunder.id)) {
mergedFundersSet.add(queriedFunder.id);
}
}
});
}
if (data[7] && data[7] > 0) {
this.numbers.datasourcesSize = NumberUtils.roundNumber(data[7]);
}
if (data[8] && data[8] > 0) {
this.numbers.organizationsSize = NumberUtils.roundNumber(data[8]);
}
if (data[9][1] && data[9][1].length > 0 && data[9][1][0].filterId == 'relfunder' && data[9][1][0].values) {
let queriedFunders = data[9][1][0].values;
queriedFunders.forEach(queriedFunder => {
if (!mergedFundersSet.has(queriedFunder.id)) {
mergedFundersSet.add(queriedFunder.id);
}
});
}
this.numbers.mergedFundersSize = NumberUtils.roundNumber(mergedFundersSet.size);
this.results.emit(this.numbers);
}, err => {
this.handleError('Error getting numbers', err);
}));
}
ngOnDestroy() {
this.subs.forEach(sub => {
if (sub instanceof Subscription) {
sub.unsubscribe();
}
});
}
get empty(): Observable {
return this.emptySubject.asObservable();
}
private handleError(message: string, error) {
console.error('Numbers: ' + message, error);
}
public get showContentWithNumbers() {
if (this.numbers && (this.hasPublications || this.hasDatasets || this.hasSoftware || this.hasDatasources || this.hasProjects || this.hasOrganizations)) {
return true;
}
}
public get hasPublications() {
return this.showPublications && this.numbers.publicationsSize && this.numbers.publicationsSize.count >= this.numbersLimit;
}
public get hasDatasets() {
return this.showDatasets && this.numbers.datasetsSize && this.numbers.datasetsSize.count >= this.numbersLimit;
}
public get hasSoftware() {
return this.showSoftware && this.numbers.softwareSize && this.numbers.softwareSize.count >= this.numbersLimit;
}
public get hasDatasources() {
return this.showDataProviders && this.numbers.datasourcesSize && this.numbers.datasourcesSize.count >= this.numbersLimit;
}
public get hasProjects() {
return this.showProjects && this.numbers.projectsSize && this.numbers.projectsSize.count >= this.numbersLimit;
}
public get hasOrganizations() {
return this.showOrganizations && this.numbers.organizationsSize && this.numbers.organizationsSize.count >= this.numbersLimit;
}
}