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 {HelperFunctions} from "../../utils/HelperFunctions.class";
export interface Numbers {
publicationsSize?: NumberSize;
datasetsSize?: NumberSize;
datasetsLinkedSize?: NumberSize;
softwareLinkedSize?: NumberSize;
softwareSize?: NumberSize;
otherSize?: NumberSize;
fundersSize?: NumberSize;
projectsSize?: NumberSize;
datasourcesSize?: NumberSize;
}
type Entity = 'publication' | 'dataset' | 'software' | 'other' | 'project' | 'datasource';
interface Link {
link: string,
params?: any
}
@Component({
selector: 'numbers',
template: `
`,
})
export class NumbersComponent implements OnInit, OnDestroy {
@Input() colorClass = 'portal-color';
@Input() backgroundClass = null;
/** 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;
/** Add an external link for numbers link */
@Input() externalLink;
/** When numbers have been initialized this emitter will emitted */
@Output() results: EventEmitter = new EventEmitter();
public properties: EnvProperties = properties;
public routerHelper: RouterHelper = new RouterHelper();
public numbers: Numbers = {};
public loading: boolean = true;
public links: Map = new Map();
private params: Map = new Map();
private emptySubject: BehaviorSubject = new BehaviorSubject(0);
private subs: any[] = [];
constructor(private searchResearchResultsService: SearchResearchResultsService,
private searchDataprovidersService: SearchDataprovidersService,
private refineFieldResultsService: RefineFieldResultsService) {
this.links = new Map();
this.params = new Map();
this.emptySubject = new BehaviorSubject(0);
}
ngOnInit() {
if (this.defaultInit) {
this.init();
}
this.setLinks();
}
setParams() {
this.params.set('publication', {type: 'publications'});
this.params.set('dataset', {type: 'datasets'});
this.params.set('software', {type: 'software'});
this.params.set('other', {type: 'other'});
this.params.set('project', {});
this.params.set('datasource', {});
if (this.refineValue) {
this.params.forEach((value) => {
value['fq'] = this.refineValue;
});
}
}
setLinks() {
this.setParams();
if (this.externalLink) {
this.links.set('publication', {
link: this.externalLink + properties.searchLinkToResults +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('publication')), HelperFunctions.getValues(this.params.get('publication')))
});
this.links.set('dataset', {
link: this.externalLink + properties.searchLinkToResults +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('dataset')), HelperFunctions.getValues(this.params.get('dataset')))
});
this.links.set('software', {
link: this.externalLink + properties.searchLinkToResults +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('software')), HelperFunctions.getValues(this.params.get('software')))
});
this.links.set('other', {
link: this.externalLink + properties.searchLinkToResults +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('other')), HelperFunctions.getValues(this.params.get('other')))
});
this.links.set('project', {
link: this.externalLink + properties.searchLinkToProjects +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('project')), HelperFunctions.getValues(this.params.get('project')))
});
this.links.set('datasource', {
link: this.externalLink + properties.searchLinkToDataProviders +
this.routerHelper.createQueryParamsString(Object.keys(this.params.get('datasource')), HelperFunctions.getValues(this.params.get('datasource')))
});
} else {
this.links.set('publication', {
link: properties.searchLinkToResults,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('publication')), HelperFunctions.getValues(this.params.get('publication')))
});
this.links.set('dataset', {
link: properties.searchLinkToResults,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('dataset')), HelperFunctions.getValues(this.params.get('dataset')))
});
this.links.set('software', {
link: properties.searchLinkToResults,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('software')), HelperFunctions.getValues(this.params.get('software')))
});
this.links.set('other', {
link: properties.searchLinkToResults,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('other')), HelperFunctions.getValues(this.params.get('other')))
});
this.links.set('project', {
link: properties.searchLinkToProjects,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('project')), HelperFunctions.getValues(this.params.get('project')))
});
this.links.set('datasource', {
link: properties.searchLinkToDataProviders,
params: this.routerHelper.createQueryParams(Object.keys(this.params.get('datasource')), HelperFunctions.getValues(this.params.get('datasource')))
});
}
}
init(getDatasetsLinked = false, getSoftwareLinked = false, getPublications = true, getDatasets = true,
getSoftware = true, getOther = true, getProjects = true, getDataProviders = true, refineValue: string = null) {
this.loading = true;
if (refineValue) {
this.refineValue = refineValue;
}
let refineParams = (this.refineValue) ? ('&fq=' + this.refineValue) : null;
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.getRefineFieldsResultsByEntityName(['funder'], 'project', this.properties, refineParams) : this.empty,
(getDataProviders) ? this.searchDataprovidersService.numOfSearchDataproviders('', 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);
}
if (data[7] && data[7] > 0) {
this.numbers.datasourcesSize = NumberUtils.roundNumber(data[7]);
}
this.results.emit(this.numbers);
this.loading = false;
}, err => {
this.handleError('Error getting numbers', err);
this.loading = false;
}));
}
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);
}
}