event service

This commit is contained in:
Kyle Spearrin 2019-07-03 09:53:55 -04:00
parent e15e0eebbd
commit cfec7c4815
4 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,6 @@
import { EventType } from '../enums/eventType';
export abstract class EventService {
collect: (eventType: EventType, cipherId?: string, uploadImmediately?: boolean) => Promise<any>;
uploadEvents: () => Promise<any>;
}

View File

@ -0,0 +1,6 @@
import { EventType } from '../../enums/eventType';
export class EventData {
type: EventType;
cipherId: string;
}

View File

@ -21,6 +21,7 @@ export class ConstantsService {
static readonly pinProtectedKey: string = 'pinProtectedKey'; static readonly pinProtectedKey: string = 'pinProtectedKey';
static readonly protectedPin: string = 'protectedPin'; static readonly protectedPin: string = 'protectedPin';
static readonly clearClipboardKey: string = 'clearClipboardKey'; static readonly clearClipboardKey: string = 'clearClipboardKey';
static readonly eventCollectionKey: string = 'eventCollection';
readonly environmentUrlsKey: string = ConstantsService.environmentUrlsKey; readonly environmentUrlsKey: string = ConstantsService.environmentUrlsKey;
readonly disableGaKey: string = ConstantsService.disableGaKey; readonly disableGaKey: string = ConstantsService.disableGaKey;
@ -43,4 +44,5 @@ export class ConstantsService {
readonly pinProtectedKey: string = ConstantsService.pinProtectedKey; readonly pinProtectedKey: string = ConstantsService.pinProtectedKey;
readonly protectedPin: string = ConstantsService.protectedPin; readonly protectedPin: string = ConstantsService.protectedPin;
readonly clearClipboardKey: string = ConstantsService.clearClipboardKey; readonly clearClipboardKey: string = ConstantsService.clearClipboardKey;
readonly eventCollectionKey: string = ConstantsService.eventCollectionKey;
} }

View File

@ -0,0 +1,78 @@
import { EventType } from '../enums/eventType';
import { EventData } from '../models/data/eventData';
import { EventRequest } from '../models/request/eventRequest';
import { ApiService } from '../abstractions/api.service';
import { CipherService } from '../abstractions/cipher.service';
import { EventService as EventServiceAbstraction } from '../abstractions/event.service';
import { StorageService } from '../abstractions/storage.service';
import { UserService } from '../abstractions/user.service';
import { ConstantsService } from './constants.service';
export class EventService implements EventServiceAbstraction {
private inited = false;
constructor(private storageService: StorageService, private apiService: ApiService,
private userService: UserService, private cipherService: CipherService) { }
init(checkOnInterval: boolean) {
if (this.inited) {
return;
}
this.inited = true;
if (checkOnInterval) {
this.uploadEvents();
setInterval(() => this.uploadEvents(), 60 * 1000); // check every 60 seconds
}
}
async collect(eventType: EventType, cipherId: string = null, uploadImmediately = false): Promise<any> {
const organizations = await this.userService.getAllOrganizations();
if (organizations == null) {
return;
}
const orgIds = new Set<string>(organizations.filter((o) => o.useEvents).map((o) => o.id));
if (orgIds.size === 0) {
return;
}
if (cipherId != null) {
const cipher = await this.cipherService.get(cipherId);
if (cipher == null || cipher.organizationId == null || !orgIds.has(cipher.organizationId)) {
return;
}
}
let eventCollection = await this.storageService.get<EventData[]>(ConstantsService.eventCollectionKey);
if (eventCollection == null) {
eventCollection = [];
}
const event = new EventData();
event.type = eventType;
event.cipherId = cipherId;
eventCollection.push(event);
await this.storageService.save(ConstantsService.eventCollectionKey, eventCollection);
if (uploadImmediately) {
await this.uploadEvents();
}
}
async uploadEvents(): Promise<any> {
const eventCollection = await this.storageService.get<EventData[]>(ConstantsService.eventCollectionKey);
if (eventCollection == null || eventCollection.length === 0) {
return;
}
const request = eventCollection.map((e) => {
const req = new EventRequest();
req.type = e.type;
req.cipherId = e.cipherId;
return req;
});
try {
await this.apiService.postEventsCollectMany(request);
await this.storageService.remove(ConstantsService.eventCollectionKey);
} catch { }
}
}