Remove unused code and update promise types in report pages

This commit is contained in:
luke-hagar-sp
2024-01-23 19:57:10 -06:00
parent c305caa200
commit 94e81ec368
6 changed files with 193 additions and 23 deletions

View File

@@ -1,6 +1,6 @@
import { createConfiguration } from '$lib/sailpoint/sdk.js';
import { getToken } from '$lib/utils/oauth.js';
import { SearchApi, type Search, Paginator } from 'sailpoint-api-client';
import { SearchApi, type Search, Paginator, type IdentityDocument } from 'sailpoint-api-client';
export const load = async ({ cookies }) => {
const search: Search = {
@@ -16,7 +16,13 @@ export const load = async ({ cookies }) => {
const config = createConfiguration(session.baseUrl, idnSession.access_token);
const api = new SearchApi(config);
const reportData = (await Paginator.paginateSearchApi(api, search, 100, 20000)).data;
const reportResp = Paginator.paginateSearchApi(api, search, 100, 20000);
const reportData = new Promise<IdentityDocument[]>((resolve) => {
reportResp.then((response) => {
resolve(response.data);
});
});
return { reportData };
};

View File

@@ -1,6 +1,6 @@
import { createConfiguration } from '$lib/sailpoint/sdk.js';
import { getToken } from '$lib/utils/oauth.js';
import { SearchApi, type Search, Paginator } from 'sailpoint-api-client';
import { Paginator, SearchApi, type IdentityDocument, type Search } from 'sailpoint-api-client';
export const load = async ({ cookies }) => {
const search: Search = {
@@ -16,7 +16,13 @@ export const load = async ({ cookies }) => {
const config = createConfiguration(session.baseUrl, idnSession.access_token);
const api = new SearchApi(config);
const reportData = (await Paginator.paginateSearchApi(api, search, 100, 20000)).data;
const searchResp = Paginator.paginateSearchApi(api, search, 100, 20000);
const reportData = new Promise<IdentityDocument[]>((resolve) => {
searchResp.then((response) => {
resolve(response.data);
});
});
return { reportData };
};

View File

@@ -0,0 +1,28 @@
import { createConfiguration } from '$lib/sailpoint/sdk';
import { getSession, getToken } from '$lib/utils/oauth';
import { Paginator, SearchApi, type Search, type EventDocument } from 'sailpoint-api-client';
export const load = async ({ cookies }) => {
const session = await getSession(cookies);
const idnSession = await getToken(cookies);
const config = createConfiguration(session.baseUrl, idnSession.access_token);
const api = new SearchApi(config);
const search: Search = {
indices: ['events'],
query: {
query: `name: "Create Account Failed" AND created: [now-90d TO now]`
},
sort: ['created']
};
const searchResp = Paginator.paginateSearchApi(api, search, 100, 20000);
const errorEvents = new Promise<EventDocument[]>((resolve) => {
searchResp.then((response) => {
resolve(response.data);
});
});
return { errorEvents };
};

View File

@@ -1,10 +1,150 @@
import type { SourceEvents } from '$lib/Types.js';
import type { Source } from 'sailpoint-api-client';
import { getFilters, getLimit, getPage, getSorters } from '$lib/Utils.js';
import { createConfiguration } from '$lib/sailpoint/sdk.js';
import { getToken } from '$lib/utils/oauth.js';
import {
SearchApi,
SourcesApi,
type EventDocument,
type Search,
type SourcesApiListSourcesRequest,
type Source
} from 'sailpoint-api-client';
export const load = async ({ fetch }) => {
const aggData: { sources: Source[]; events: SourceEvents[] } = (
await fetch('/api/sailpoint/sourceAggEvents')
).json();
export const load = async ({ cookies, url }) => {
const session = JSON.parse(cookies.get('session')!);
const idnSession = await getToken(cookies);
return { aggData };
const config = createConfiguration(session.baseUrl, idnSession.access_token);
const sourceApi = new SourcesApi(config);
const searchApi = new SearchApi(config);
const page = getPage(url);
const filters = getFilters(url);
const limit = getLimit(url);
const sorters = getSorters(url);
const requestParams: SourcesApiListSourcesRequest = {
filters,
offset: Number(page) * Number(limit),
limit: Number(limit),
sorters,
count: true
};
const apiResponse = sourceApi.listSources(requestParams);
const sources = new Promise<Source[]>((resolve) => {
apiResponse
.then((response) => {
resolve(response.data);
})
.catch((err) => {
throw err;
});
});
const totalCount = new Promise<number>((resolve) => {
apiResponse
.then((response) => {
resolve(response.headers['x-total-count']);
})
.catch((err) => {
throw err;
});
});
type SourceEvents = {
accounts: { started: EventDocument | undefined; passed: EventDocument | undefined };
entitlements: { started: EventDocument | undefined; passed: EventDocument | undefined };
};
const eventNames: string[] = [
'Aggregate Source Account Passed',
'Aggregate Source Account Started',
'Aggregate Source Entitlement Passed',
'Aggregate Source Entitlement Started'
];
const eventsMap = new Promise<Map<string, SourceEvents>>((resolve) => {
sources.then(async (sources) => {
const sourceEventsMap = new Map<string, SourceEvents>();
for (const source of sources) {
const allEvents: EventDocument[] = [];
const promises: Promise<EventDocument[]>[] = [];
for (const event of eventNames) {
const search: Search = {
indices: ['events'],
query: {
query: `target.name: "${source.name}" AND name:"${event}"`
},
sort: ['created']
};
promises.push(
searchApi
.searchPost({
search
})
.then((response) => {
return response.data;
})
.catch((err) => {
throw err;
})
);
}
await Promise.allSettled(promises).then((results) => {
for (const event of results) {
if (event.status == 'fulfilled' && event.value.length > 0) {
allEvents.push(event.value[0]);
}
}
const sourceEvents: SourceEvents = {
accounts: { started: undefined, passed: undefined },
entitlements: { started: undefined, passed: undefined }
};
for (const event of allEvents) {
if (event.attributes!.sourceName === source.name) {
switch (event.technicalName) {
case 'SOURCE_ACCOUNT_AGGREGATE_STARTED':
if (!sourceEvents.accounts.started) {
sourceEvents.accounts.started = event || undefined;
}
break;
case 'SOURCE_ACCOUNT_AGGREGATE_PASSED':
if (!sourceEvents.accounts.passed) {
sourceEvents.accounts.passed = event || undefined;
}
break;
case 'SOURCE_ENTITLEMENT_AGGREGATE_STARTED':
if (!sourceEvents.entitlements.started) {
sourceEvents.entitlements.started = event || undefined;
}
break;
case 'SOURCE_ENTITLEMENT_AGGREGATE_PASSED':
if (!sourceEvents.entitlements.passed) {
sourceEvents.entitlements.passed = event || undefined;
}
break;
default:
break;
}
}
}
sourceEventsMap.set(source.name, sourceEvents);
});
}
resolve(sourceEventsMap);
});
});
return { sources, eventsMap, totalCount, params: { page, limit, filters, sorters } };
};

View File

@@ -1,10 +0,0 @@
export const load = async ({ fetch }) => {
const response = fetch('/api/sailpoint/sources', {
method: 'GET',
headers: {
'content-type': 'application/json',
},
});
return { response };
};

View File

@@ -25,10 +25,10 @@ export const load = async ({ cookies, url }) => {
const apiResponse = api.listSources(requestParams);
const sources = new Promise((resolve) => {
const sources = new Promise<Source[]>((resolve) => {
apiResponse
.then((response) => {
resolve(response.data as Source[]);
resolve(response.data);
})
.catch((err) => {
throw err;