2018-07-07 05:08:10 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
2018-07-10 15:19:29 +02:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
2018-07-07 05:08:10 +02:00
|
|
|
|
2018-07-09 18:22:32 +02:00
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiService } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { ExportService } from 'jslib-common/abstractions/export.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
|
|
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
2018-07-07 05:08:10 +02:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { EventResponse } from 'jslib-common/models/response/eventResponse';
|
|
|
|
import { ListResponse } from 'jslib-common/models/response/listResponse';
|
|
|
|
import { EventView } from 'jslib-common/models/view/eventView';
|
2018-07-06 16:21:08 +02:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { EventService } from '../../services/event.service';
|
2018-07-09 17:47:57 +02:00
|
|
|
|
2018-07-06 16:21:08 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-org-events',
|
|
|
|
templateUrl: 'events.component.html',
|
|
|
|
})
|
2018-07-07 05:08:10 +02:00
|
|
|
export class EventsComponent implements OnInit {
|
|
|
|
loading = true;
|
|
|
|
loaded = false;
|
|
|
|
organizationId: string;
|
2021-05-14 01:39:53 +02:00
|
|
|
events: EventView[];
|
2018-07-07 05:08:10 +02:00
|
|
|
start: string;
|
|
|
|
end: string;
|
2021-06-02 14:21:57 +02:00
|
|
|
dirtyDates: boolean = true;
|
2018-07-07 05:08:10 +02:00
|
|
|
continuationToken: string;
|
|
|
|
refreshPromise: Promise<any>;
|
2021-05-14 01:39:53 +02:00
|
|
|
exportPromise: Promise<any>;
|
2018-07-07 05:08:10 +02:00
|
|
|
morePromise: Promise<any>;
|
|
|
|
|
2018-07-09 18:22:32 +02:00
|
|
|
private orgUsersUserIdMap = new Map<string, any>();
|
|
|
|
private orgUsersIdMap = new Map<string, any>();
|
|
|
|
|
2021-05-14 01:39:53 +02:00
|
|
|
constructor(private apiService: ApiService, private route: ActivatedRoute, private eventService: EventService,
|
|
|
|
private i18nService: I18nService, private toasterService: ToasterService, private userService: UserService,
|
|
|
|
private exportService: ExportService, private platformUtilsService: PlatformUtilsService,
|
2018-07-10 15:19:29 +02:00
|
|
|
private router: Router) { }
|
2018-07-07 05:08:10 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
2021-02-03 18:41:33 +01:00
|
|
|
this.route.parent.parent.params.subscribe(async params => {
|
2018-07-07 05:08:10 +02:00
|
|
|
this.organizationId = params.organizationId;
|
2018-07-10 15:19:29 +02:00
|
|
|
const organization = await this.userService.getOrganization(this.organizationId);
|
|
|
|
if (organization == null || !organization.useEvents) {
|
|
|
|
this.router.navigate(['/organizations', this.organizationId]);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-07 05:08:10 +02:00
|
|
|
const defaultDates = this.eventService.getDefaultDateFilters();
|
|
|
|
this.start = defaultDates[0];
|
|
|
|
this.end = defaultDates[1];
|
|
|
|
await this.load();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2018-07-09 18:22:32 +02:00
|
|
|
const response = await this.apiService.getOrganizationUsers(this.organizationId);
|
2021-02-03 18:41:33 +01:00
|
|
|
response.data.forEach(u => {
|
2018-07-09 18:22:32 +02:00
|
|
|
const name = u.name == null || u.name.trim() === '' ? u.email : u.name;
|
|
|
|
this.orgUsersIdMap.set(u.id, { name: name, email: u.email });
|
|
|
|
this.orgUsersUserIdMap.set(u.userId, { name: name, email: u.email });
|
|
|
|
});
|
2018-07-07 05:08:10 +02:00
|
|
|
await this.loadEvents(true);
|
|
|
|
this.loaded = true;
|
|
|
|
}
|
|
|
|
|
2021-05-14 01:39:53 +02:00
|
|
|
async exportEvents() {
|
2021-06-02 14:21:57 +02:00
|
|
|
if (this.appApiPromiseUnfulfilled() || this.dirtyDates) {
|
2021-05-14 01:39:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loading = true;
|
2021-06-02 14:21:57 +02:00
|
|
|
|
|
|
|
const dates = this.parseDates();
|
|
|
|
if (dates == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-14 01:39:53 +02:00
|
|
|
try {
|
2021-06-02 14:21:57 +02:00
|
|
|
this.exportPromise = this.export(dates[0], dates[1]);
|
|
|
|
|
2021-05-14 01:39:53 +02:00
|
|
|
await this.exportPromise;
|
|
|
|
} catch { }
|
|
|
|
|
|
|
|
this.exportPromise = null;
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
|
2018-07-07 05:08:10 +02:00
|
|
|
async loadEvents(clearExisting: boolean) {
|
2021-05-14 01:39:53 +02:00
|
|
|
if (this.appApiPromiseUnfulfilled()) {
|
2018-07-07 05:08:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-02 14:21:57 +02:00
|
|
|
const dates = this.parseDates();
|
|
|
|
if (dates == null) {
|
2018-07-07 05:08:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loading = true;
|
2021-06-02 14:21:57 +02:00
|
|
|
let events: EventView[] = [];
|
2018-07-07 05:08:10 +02:00
|
|
|
try {
|
2021-06-02 14:21:57 +02:00
|
|
|
const promise = this.loadAndParseEvents(dates[0], dates[1], clearExisting ? null : this.continuationToken);
|
2018-07-07 05:08:10 +02:00
|
|
|
if (clearExisting) {
|
|
|
|
this.refreshPromise = promise;
|
|
|
|
} else {
|
|
|
|
this.morePromise = promise;
|
|
|
|
}
|
2021-06-02 14:21:57 +02:00
|
|
|
const result = await promise;
|
|
|
|
this.continuationToken = result.continuationToken;
|
|
|
|
events = result.events;
|
2018-07-07 05:08:10 +02:00
|
|
|
} catch { }
|
2018-07-09 17:47:57 +02:00
|
|
|
|
2021-06-02 14:21:57 +02:00
|
|
|
if (!clearExisting && this.events != null && this.events.length > 0) {
|
|
|
|
this.events = this.events.concat(events);
|
|
|
|
} else {
|
|
|
|
this.events = events;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dirtyDates = false;
|
|
|
|
this.loading = false;
|
|
|
|
this.morePromise = null;
|
|
|
|
this.refreshPromise = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async export(start: string, end: string) {
|
|
|
|
let continuationToken = this.continuationToken;
|
|
|
|
let events = [].concat(this.events);
|
|
|
|
|
|
|
|
while (continuationToken != null) {
|
|
|
|
const result = await this.loadAndParseEvents(start, end, continuationToken);
|
|
|
|
continuationToken = result.continuationToken;
|
|
|
|
events = events.concat(result.events);
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await this.exportService.getEventExport(events);
|
|
|
|
const fileName = this.exportService.getFileName('org-events', 'csv');
|
|
|
|
this.platformUtilsService.saveFile(window, data, { type: 'text/plain' }, fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async loadAndParseEvents(startDate: string, endDate: string, continuationToken: string) {
|
|
|
|
const response = await this.apiService.getEventsOrganization(this.organizationId, startDate, endDate,
|
|
|
|
continuationToken);
|
|
|
|
|
2021-05-14 01:39:53 +02:00
|
|
|
const events = await Promise.all(response.data.map(async r => {
|
2018-07-09 17:47:57 +02:00
|
|
|
const userId = r.actingUserId == null ? r.userId : r.actingUserId;
|
2021-05-14 01:39:53 +02:00
|
|
|
const eventInfo = await this.eventService.getEventInfo(r);
|
2018-07-09 18:22:32 +02:00
|
|
|
const user = userId != null && this.orgUsersUserIdMap.has(userId) ?
|
|
|
|
this.orgUsersUserIdMap.get(userId) : null;
|
2021-05-14 01:39:53 +02:00
|
|
|
return new EventView({
|
2018-07-09 17:47:57 +02:00
|
|
|
message: eventInfo.message,
|
2021-05-14 01:39:53 +02:00
|
|
|
humanReadableMessage: eventInfo.humanReadableMessage,
|
2018-07-09 17:47:57 +02:00
|
|
|
appIcon: eventInfo.appIcon,
|
|
|
|
appName: eventInfo.appName,
|
|
|
|
userId: userId,
|
2018-07-09 18:22:32 +02:00
|
|
|
userName: user != null ? user.name : this.i18nService.t('unknown'),
|
|
|
|
userEmail: user != null ? user.email : '',
|
2018-07-09 17:47:57 +02:00
|
|
|
date: r.date,
|
|
|
|
ip: r.ipAddress,
|
2018-07-10 14:39:05 +02:00
|
|
|
type: r.type,
|
2021-05-14 01:39:53 +02:00
|
|
|
});
|
|
|
|
}));
|
2021-06-02 14:21:57 +02:00
|
|
|
return { continuationToken: response.continuationToken, events: events };
|
|
|
|
}
|
2018-07-09 17:47:57 +02:00
|
|
|
|
2021-06-02 14:21:57 +02:00
|
|
|
private parseDates() {
|
|
|
|
let dates: string[] = null;
|
|
|
|
try {
|
|
|
|
dates = this.eventService.formatDateFilters(this.start, this.end);
|
|
|
|
} catch (e) {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('invalidDateRange'));
|
|
|
|
return null;
|
2018-07-09 17:47:57 +02:00
|
|
|
}
|
2021-06-02 14:21:57 +02:00
|
|
|
return dates;
|
2018-07-07 05:08:10 +02:00
|
|
|
}
|
2021-05-14 01:39:53 +02:00
|
|
|
|
|
|
|
private appApiPromiseUnfulfilled() {
|
|
|
|
return this.refreshPromise != null || this.morePromise != null || this.exportPromise != null;
|
|
|
|
}
|
2018-07-07 05:08:10 +02:00
|
|
|
}
|