Skip to content

Commit 3dcdb5e

Browse files
authored
Refactoring/aipaidao renew api calls (#73)
* replace deprecated api calls for plugin fetching * - remove Create New field, - Component selector from "app" to "", since this is how the components are named - resolved some TS warnings * - resolved all TS warnings in plugin-editor.component.ts * - allow leading underscore in variable names * - added policy details such as objectives and parameters * - hidden the edit buttons * fixes in query.controller.ts - query function - tag function - address function * fixes in query.controller.ts - query function - tag function - address function * console debug error in tag * adjusted the query.service.ts to the modern response shape of the api; fixed lots of issues reported by tslint * switched default to SkillsAndDistance due to outdated default * tslint fixes * removed commented code * added todo; cleanup; error info * clean up
1 parent dabe64b commit 3dcdb5e

22 files changed

+829
-724
lines changed

workbench/backend/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ module.exports = {
2020
'@typescript-eslint/interface-name-prefix': 'off',
2121
'@typescript-eslint/explicit-function-return-type': 'off',
2222
'@typescript-eslint/no-explicit-any': 'off',
23+
'@typescript-eslint/camelcase': 'off',
2324
},
2425
};

workbench/backend/package-lock.json

Lines changed: 475 additions & 475 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workbench/backend/src/app/booking/booking.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class BookingController {
9696
.json({ activityId: e.message });
9797
}
9898

99-
let axiosError: AxiosError = e?.error;
99+
const axiosError: AxiosError = e?.error;
100100
return res
101101
.status(axiosError?.response?.status || 500)
102102
.json(axiosError?.response?.data);

workbench/backend/src/app/job-slots/job-slots.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class JobSlotsController {
1616
return res.json(data);
1717
} catch ({ error }) {
1818

19-
let axiosError: AxiosError = error;
19+
const axiosError: AxiosError = error;
2020
return res
2121
.status(parseInt(axiosError?.code || '500'))
2222
.json(axiosError?.response?.data);

workbench/backend/src/app/plugin/ai-data-api.dao.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,27 @@ export class AiDataAPIDAO {
5353
private request<T>(config: AxiosRequestConfig) {
5454
const requestStart: Date = new Date();
5555
return this.http.request<T>(config).pipe(
56-
tap(response => {
57-
try {
58-
const elapsedMilliseconds: number = new Date().getTime() - requestStart.getTime();
59-
console.debug(
60-
`[AiDataAPIDAO:${config.method}] url: [${config.url}] response: [${JSON.stringify(response ? response.status : null)}], time: [${elapsedMilliseconds}]`,
61-
);
62-
} catch {
63-
console.debug(`[AiDataAPIDAO:${config.method}] url: [${config.url}] response[UNPROCESSIBLE]`);
64-
}
65-
}),
66-
catchError((error: AxiosError) => {
67-
console.error('AiDataAPIDAO', error);
68-
return throwError({ error });
69-
})
56+
tap(response => {
57+
try {
58+
const elapsedMilliseconds: number = new Date().getTime() - requestStart.getTime();
59+
console.debug(
60+
`[AiDataAPIDAO:${config.method}] url: [${config.url}] response: [${JSON.stringify(response ? response.status : null)}], time: [${elapsedMilliseconds}]`,
61+
);
62+
} catch {
63+
console.debug(`[AiDataAPIDAO:${config.method}] url: [${config.url}] response[UNPROCESSIBLE]`);
64+
}
65+
}),
66+
catchError((error: AxiosError) => {
67+
console.error('AiDataAPIDAO', error);
68+
return throwError({ error });
69+
})
7070
);
7171
}
7272

7373
getAll(ctx: Context) {
7474
return this.request<PluginDto[]>({
7575
method: 'GET',
76-
url: `${this.resolveHost(ctx.cloudHost)}/cloud-ai-data-service/api/autoscheduler/v1/optimization-plugins`,
76+
url: `${this.resolveHost(ctx.cloudHost)}/cloud-ai-policy-designer/api/optimization/v1/policies`,
7777
headers: this.getHeaders(ctx),
7878
params: this.getParams(ctx),
7979
responseType: 'json',
@@ -93,7 +93,7 @@ export class AiDataAPIDAO {
9393
getByName(ctx: Context, name: string) {
9494
return this.request<PluginDto[]>({
9595
method: 'GET',
96-
url: `${this.resolveHost(ctx.cloudHost)}/cloud-ai-data-service/api/autoscheduler/v1/optimization-plugins/by-name/${name}`,
96+
url: `${this.resolveHost(ctx.cloudHost)}/cloud-ai-policy-designer/api/optimization/v1/policies/by-name/${name}`,
9797
headers: this.getHeaders(ctx),
9898
params: this.getParams(ctx),
9999
responseType: 'json',

workbench/backend/src/app/query/query.controller.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,44 @@ import { AddressDTO, TagDTO } from '../../common/dto-models';
44
import { FsmAPIClientFactory } from '../../common/fsm-api-client.factory';
55
import { DTOName } from 'fsm-sdk/release/core/dto-name.model';
66

7+
interface QueryResponse<T> {
8+
data: T[];
9+
}
10+
711
@Controller('api/query')
812
export class QueryController {
913

1014
constructor(private factory: FsmAPIClientFactory) { }
1115

1216
@Post()
1317
async query<T>(@Context() ctx: Context, @Body() { query }: { query: string }): Promise<T> {
14-
return await this.factory.fromContext(ctx).query(query, Object.keys(this.factory.ALL_DTO_VERSIONS) as DTOName[])
15-
.then(x => x.data)
16-
.catch(e => { console.error(query); throw e; return undefined as any }) as T;
18+
19+
20+
const coreApiClient = this.factory.fromContext(ctx);
21+
const all_dto_versions = this.factory.ALL_DTO_VERSIONS
22+
delete all_dto_versions["CrowdExecutionRecord"] // TODO remove this line after fsm-sdk has been updated
23+
24+
25+
const dto_names = Object.keys(all_dto_versions) as DTOName[]
26+
return await coreApiClient.query(query, dto_names)
27+
.then(x =>{
28+
return x})
29+
.catch(e => {
30+
throw e;
31+
return undefined as any }) as T;
1732
}
1833

1934
@Get('tags')
2035
async listTags(@Context() ctx: Context) {
2136

22-
const data: { tag: TagDTO }[] = await this.query(ctx, { query: `SELECT tag FROM Tag tag` });
23-
24-
const work = data.map(({ tag }) => {
37+
const data: QueryResponse<{ tag: TagDTO }> = await this.query(ctx, { query: `SELECT tag FROM Tag tag` });
38+
const work = data.data.map(({ tag }) => {
2539
return this.query(ctx, { query: `SELECT it.tag, it.person FROM Skill it WHERE it.tag = '${tag.id}' LIMIT 500` })
26-
.then((resp: { it: { person: string } }[]) => ({ ...tag, persons: resp.map(({ it }) => it.person) }))
27-
.catch(error => ({ ...tag, persons: [] }));
40+
.then((resp: QueryResponse<{ it: { person: string } }>) => ({ ...tag, persons: resp.data.map(({ it }) => it.person) }))
41+
.catch(error => {
42+
console.error("List of Tags could not be collected due to: ", error)
43+
return { ...tag, persons: [] };
44+
});
2845
});
2946

3047
return await Promise.all(work);
@@ -38,8 +55,8 @@ export class QueryController {
3855
@Get('address')
3956
async listAddress(@Context() ctx: Context) {
4057
const query = `SELECT address FROM Address address LIMIT 2500`;
41-
const list: { address: AddressDTO }[] = await this.query(ctx, { query });
42-
return list.map(x => x.address);
58+
const list: QueryResponse<{ address: AddressDTO }> = await this.query(ctx, { query });
59+
return list.data.map(x => x.address);
4360
}
4461

4562
}

workbench/backend/src/app/re-optimize/re-optimize.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class ReOptimizeController {
1818
const { data } = await this.dao.reOptimize('sync', ctx, body).toPromise();
1919
return res.json(data);
2020
} catch (e) {
21-
let axiosError: AxiosError = e?.error;
21+
const axiosError: AxiosError = e?.error;
2222
return res
2323
.status(axiosError?.response?.status || 500)
2424
.json(axiosError?.response?.data);
@@ -30,7 +30,7 @@ export class ReOptimizeController {
3030
const { data } = await this.dao.reOptimize('async', ctx, body).toPromise();
3131
return res.json(data);
3232
} catch (e) {
33-
let axiosError: AxiosError = e?.error;
33+
const axiosError: AxiosError = e?.error;
3434
return res
3535
.status(axiosError?.response?.status || 500)
3636
.json(axiosError?.response?.data);

workbench/backend/src/common/error.filter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export class ErrorFilter implements ExceptionFilter {
2222
let message = 'Error';
2323
let inner: {} = {};
2424

25-
const _inner = (error as any).error as undefined | AxiosError;
25+
26+
27+
const _inner = (error as any).error as undefined | Record<any, any>;
2628

2729
if (!!_inner) {
2830
inner = typeof _inner.toJSON === 'function' ? _inner.toJSON() : {};

workbench/backend/src/common/fsm-api-client.factory.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ import { Context } from '../ctx.decorator';
66
@Injectable()
77
export class FsmAPIClientFactory {
88

9+
public ALL_DTO_VERSIONS: { [name: string]: number } = ALL_DTO_VERSIONS;
10+
911
public fromContext(ctx: Context) {
10-
return new CoreAPIClient({
12+
const result = new CoreAPIClient({
1113
debug: configService.useVerboseLogs(),
1214
clientIdentifier: ctx.clientId,
1315
clientVersion: ctx.clientVersion,
14-
clientSecret: 'none',
16+
clientSecret: ctx.authToken,
1517
authAccountName: ctx.account,
1618
authCompany: ctx.company,
17-
authUserName: ctx.user
18-
}).setToken({
19+
authUserName: ctx.user,
20+
authGrantType: 'client_credentials',
21+
});
22+
result.setToken({
23+
// eslint-disable-next-line @typescript-eslint/camelcase
1924
access_token: ctx.authToken.split(' ')[1],
2025
token_type: ctx.authToken.split(' ')[0],
2126
expires_in: 9999,
@@ -28,8 +33,10 @@ export class FsmAPIClientFactory {
2833
companies: [{ name: ctx.company, id: parseInt(ctx.companyId), strictEncryptionPolicy: false, description: '' }],
2934
authorities: [],
3035
cluster_url: `https://${ctx.cloudHost}`
31-
})
36+
});
37+
38+
return result;
3239
}
3340

34-
public ALL_DTO_VERSIONS: { [name: string]: number } = ALL_DTO_VERSIONS;
35-
}
41+
42+
}

workbench/frontend/src/app/common/components/plugin-editor/plugin-editor.component.html

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,6 @@
1313
</mat-select>
1414
</mat-form-field>
1515

16-
17-
<button mat-button [disabled]="form.invalid" (click)="save()" color="primary" class="inline-btn">
18-
<mat-icon>save</mat-icon>
19-
</button>
20-
21-
<button mat-button [disabled]="selectedPlugin.value === 'create new (unsaved)'" (click)="delete()" color="primary"
22-
class="inline-btn">
23-
<mat-icon>delete</mat-icon>
24-
</button>
25-
26-
<button mat-button [disabled]="(selectedPlugin.value !== 'create new (unsaved)') || (disableEditor$ | async)"
27-
(click)="createNewFromTemplate()" color="primary" class="inline-btn">
28-
<mat-icon>insert_drive_file</mat-icon>
29-
</button>
30-
31-
3216
</div>
3317
</section>
3418

@@ -38,14 +22,50 @@
3822
<mat-progress-bar *ngIf="!!(isLoading$ | async)" style="height: 5px;" mode="indeterminate"></mat-progress-bar>
3923
</div>
4024

41-
<input type="text" formControlName="description" style="border:0" />
25+
<p>{{ form.value.description }}</p>
26+
27+
<mat-card>
28+
<mat-card-content>
29+
<h2>Policy Objective Details</h2>
30+
<table>
31+
<tr>
32+
<td><strong>Description:</strong></td>
33+
<td>{{ form.value.objective?.description }}</td>
34+
</tr>
35+
<tr>
36+
<td><strong>Is Objective Parent:</strong></td>
37+
<td>{{ form.value.objective?.isObjectiveParent ? 'Yes' : 'No' }}</td>
38+
</tr>
39+
<tr>
40+
<td><strong>Is Rule Parent:</strong></td>
41+
<td>{{ form.value.objective?.isRuleParent ? 'Yes' : 'No' }}</td>
42+
</tr>
43+
<tr>
44+
<td><strong>Object Type:</strong></td>
45+
<td>{{ form.value.objective?.objectType }}</td>
46+
</tr>
47+
<tr>
48+
<td><strong>Origin:</strong></td>
49+
<td>{{ form.value.objective?.origin }}</td>
50+
</tr>
51+
</table>
52+
<h3>Parameters:</h3>
53+
<table class="parameters">
54+
<tr>
55+
<th>Description</th>
56+
<th>Value</th>
57+
</tr>
58+
<tr *ngFor="let parameter of form.value.objective?.parameters; let i = index">
59+
<td>{{ parameter?.description }}</td>
60+
<td><pre>{{ parameter?.value | json }}</pre></td>
61+
</tr>
62+
</table>
63+
</mat-card-content>
64+
</mat-card>
4265

43-
<ngx-monaco-editor #editorInstance class="monaco-editor" [options]="editorOptions" (onInit)="onEditorInit($event)"
44-
formControlName="pluginCode" style="min-height: 700px;">
45-
</ngx-monaco-editor>
4666

4767
</form>
4868

49-
<!--
69+
<!--
5070
<pre>{{ form.value | json }}</pre>
51-
-->
71+
-->

0 commit comments

Comments
 (0)