import { Component, ElementRef, Injector, OnInit, ViewChild, ViewEncapsulation } from '@angular/core'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppComponentBase } from '@shared/common/app-component-base'; import { HostDashboardData, HostDashboardServiceProxy, ChartDateInterval } from '@shared/service-proxies/service-proxies'; import * as moment from 'moment'; import * as _ from 'lodash'; import { Table } from 'primeng/components/table/table'; @Component({ templateUrl: './host-dashboard.component.html', styleUrls: ['./host-dashboard.component.less'], encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()] }) export class HostDashboardComponent extends AppComponentBase implements OnInit { @ViewChild('EditionStatisticsChart', { static: true }) editionStatisticsChart: ElementRef; @ViewChild('IncomeStatisticsChart', { static: true }) incomeStatisticsChart: ElementRef; @ViewChild('RecentTenantsTable', { static: true }) recentTenantsTable: Table; @ViewChild('ExpiringTenantsTable', { static: true }) expiringTenantsTable: Table; loading = false; loadingIncomeStatistics = false; isInitialized: boolean; hostDashboardData: HostDashboardData; currency = '$'; appIncomeStatisticsDateInterval = ChartDateInterval; selectedIncomeStatisticsDateInterval: number; editionStatisticsHasData = false; incomeStatisticsHasData = false; incomeStatisticsData: any = []; editionStatisticsData: any = []; selectedDateRange: moment.Moment[] = [moment().add(-7, 'days').startOf('day'), moment().endOf('day')]; expiringTenantsData = []; recentTenantsData = []; constructor( injector: Injector, private _hostDashboardService: HostDashboardServiceProxy ) { super(injector); } init(): void { this.selectedIncomeStatisticsDateInterval = ChartDateInterval.Daily; this.getDashboardStatisticsData(); } ngOnInit(): void { this.init(); } getDashboardStatisticsData(): void { if (!this.selectedDateRange || !this.selectedDateRange[0] || !this.selectedDateRange[1]) { return; } this.loading = true; this._hostDashboardService .getDashboardStatisticsData( this.selectedIncomeStatisticsDateInterval, this.selectedDateRange[0], this.selectedDateRange[1].endOf('day') ) .subscribe(result => { this.hostDashboardData = result; this.incomeStatisticsData = this.normalizeIncomeStatisticsData(result.incomeStatistics); this.incomeStatisticsHasData = _.filter(this.incomeStatisticsData[0].series, data => data.value > 0).length > 0; this.editionStatisticsData = this.normalizeEditionStatisticsData(result.editionStatistics); this.editionStatisticsHasData = _.filter(this.editionStatisticsData, data => data.value > 0).length > 0; this.recentTenantsData = result.recentTenants; this.expiringTenantsData = result.expiringTenants; this.loading = false; }); } /* * Edition statistics pie chart */ normalizeEditionStatisticsData(data): Array { const chartData = new Array(data.length); for (let i = 0; i < data.length; i++) { chartData[i] = { name: data[i].label, value: data[i].value }; } return chartData; } /* * Income statistics line chart */ normalizeIncomeStatisticsData(data): any { const chartData = []; for (let i = 0; i < data.length; i++) { chartData.push({ 'name': moment(moment(data[i].date).utc().valueOf()).format('L'), 'value': data[i].amount }); } return [{ name: '', series: chartData }]; } incomeStatisticsDateIntervalChange(interval: number) { this.selectedIncomeStatisticsDateInterval = interval; this.refreshIncomeStatisticsData(); } refreshIncomeStatisticsData(): void { this.loadingIncomeStatistics = true; this._hostDashboardService.getIncomeStatistics( this.selectedIncomeStatisticsDateInterval, moment(this.selectedDateRange[0]), moment(this.selectedDateRange[1])) .subscribe(result => { this.incomeStatisticsData = this.normalizeIncomeStatisticsData(result.incomeStatistics); this.incomeStatisticsHasData = _.filter(this.incomeStatisticsData[0].series, data => data.value > 0).length > 0; this.loadingIncomeStatistics = false; }); } gotoAllRecentTenants(): void { window.open(abp.appPath + 'app/admin/tenants?' + 'creationDateStart=' + encodeURIComponent(this.hostDashboardData.tenantCreationStartDate.format())); } gotoAllExpiringTenants(): void { const url = abp.appPath + 'app/admin/tenants?' + 'subscriptionEndDateStart=' + encodeURIComponent(this.hostDashboardData.subscriptionEndDateStart.format()) + '&' + 'subscriptionEndDateEnd=' + encodeURIComponent(this.hostDashboardData.subscriptionEndDateEnd.format()); window.open(url); } }