-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor tenant app logs functionality (#29)
- Loading branch information
Showing
10 changed files
with
151 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { IManagedObject } from '@c8y/client'; | ||
|
||
export interface ILastUpdated { | ||
date: { | ||
$date: string; | ||
}; | ||
offset: number; | ||
} | ||
|
||
export interface IInstanceDetails { | ||
lastUpdated: ILastUpdated; | ||
memoryInBytes: number; | ||
scheduled: boolean; | ||
restarts: number; | ||
cpuInMillis: number; | ||
} | ||
|
||
export interface IApplicationStatus { | ||
lastUpdated: ILastUpdated; | ||
instances: { | ||
[instanceName: string]: IInstanceDetails; | ||
}; | ||
details: { | ||
desired: number; | ||
aggregatedResources: { | ||
memory: string; | ||
cpu: string; | ||
}; | ||
active: number; | ||
restarts: number; | ||
}; | ||
status: string; | ||
} | ||
|
||
export interface IApplicationManagedObjectAdditions { | ||
c8y_Status: IApplicationStatus; | ||
applicationOwner: string; | ||
c8y_Subscriptions: { | ||
[tenantId: string]: IApplicationStatus; | ||
}; | ||
applicationId: string; | ||
name: string; | ||
c8y_SupportedLogs?: string[]; | ||
} | ||
|
||
export type IApplicationManagedObject = IManagedObject & IApplicationManagedObjectAdditions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { IApplication } from '@c8y/client'; | ||
import { IApplicationManagedObject } from '@models/application-mo'; | ||
import { FakeMicroserviceService } from '@services/fake-microservice.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class AppsOfTenantService { | ||
private promiseMap = new Map<string, Promise<IApplication[]>>(); | ||
private promiseMapAppStatus = new Map<string, Promise<IApplicationManagedObject[]>>(); | ||
constructor(private credService: FakeMicroserviceService) {} | ||
|
||
async getCachedAppsOfTenant(tenantId: string): Promise<IApplication[]> { | ||
let cachedResponse = this.promiseMap.get(tenantId); | ||
async getCachedAppsSupportingLogs(tenantId: string): Promise<IApplicationManagedObject[]> { | ||
let cachedResponse = this.promiseMapAppStatus.get(tenantId); | ||
if (!cachedResponse) { | ||
this.promiseMap.clear(); | ||
cachedResponse = this.getAllAppsOfTenant(tenantId); | ||
this.promiseMap.set(tenantId, cachedResponse); | ||
this.promiseMapAppStatus.clear(); | ||
cachedResponse = this.getAppsSupportingLogs(tenantId); | ||
this.promiseMapAppStatus.set(tenantId, cachedResponse); | ||
} | ||
const apps = await cachedResponse; | ||
return apps; | ||
} | ||
|
||
private async getAllAppsOfTenant(tenantId: string): Promise<IApplication[]> { | ||
const credentials = await this.credService.prepareCachedDummyMicroserviceForAllSubtenants(); | ||
const clients = await this.credService.createClients(credentials); | ||
const client = clients.find((tmp) => tmp.core.tenant === tenantId); | ||
|
||
if (!client) { | ||
throw Error(`No client for tenant: ${tenantId} available.`); | ||
} | ||
const apps = new Array<IApplication>(); | ||
let response = await client.application.list({ pageSize: 100 }); | ||
private async getAppsSupportingLogs(tenantId: string): Promise<IApplicationManagedObject[]> { | ||
const client = await this.credService.getClientForTenant(tenantId); | ||
const apps = new Array<IApplicationManagedObject>(); | ||
let response = await client.inventory.list({ | ||
pageSize: 2000, | ||
query: `type eq 'c8y_Application_*' and has(c8y_SupportedLogs)` | ||
}); | ||
while (response.data.length) { | ||
apps.push(...response.data); | ||
apps.push(...(response.data as IApplicationManagedObject[])); | ||
if (response.data.length < response.paging.pageSize) { | ||
break; | ||
} | ||
response = await response.paging.next(); | ||
} | ||
return apps; | ||
const filteredApps = apps.filter( | ||
(app) => app.c8y_SupportedLogs && Array.isArray(app.c8y_SupportedLogs) && app.c8y_SupportedLogs.includes('syslog') | ||
); | ||
return filteredApps; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.