Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Add get alerts API and fix some tests (#77)
Browse files Browse the repository at this point in the history
* update and sync swagger

* fix
  • Loading branch information
yiyione committed Jun 4, 2021
1 parent f91e92f commit a23bac9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
27 changes: 27 additions & 0 deletions src/api/v2/clients/alertClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { IPAICluster, IPAIClusterInfo } from '@api/v2';
import { Util } from '@pai/commom/util';

import { OpenPAIBaseClient } from './baseClient';

/**
* OpenPAI Api client.
*/
export class AlertClient extends OpenPAIBaseClient {
constructor(cluster: IPAICluster) {
super(cluster);
}

/**
* Get OpenPAI cluster info.
*/
public async getAlerts(): Promise<IPAIClusterInfo> {
const url: string = Util.fixUrl(
`${this.cluster.rest_server_uri}/api/v2/alerts`,
this.cluster.https
);
return await this.httpClient.get(url);
}
}
46 changes: 40 additions & 6 deletions src/api/v2/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ info:
Version 2.2.0: add get task status api; add jobAttempId filter to job status api and extend job detail schema
Version 2.2.1: a user can add/delete tags to/from his/her own jobs
version 2.2.2: add get task logs api
version 2.2.3: set .jobStatus.appId nullable in get jobAttempt schema
version 2.2.4: support sorting by completionTime in get the list of jobs
version 2.2.5: add alert related api
license:
name: MIT License
url: "https://github.com/microsoft/pai/blob/master/LICENSE"
version: 2.2.0
version: 2.2.5
externalDocs:
description: Find out more about OpenPAI
url: "https://github.com/microsoft/pai"
Expand All @@ -39,6 +42,8 @@ tags:
description: API endpoint for storage
- name: job history
description: API endpoint for job history
- name: alert
description: API endpoint for alert
- name: kubernetes
description: API endpoint for kubernetes info
paths:
Expand Down Expand Up @@ -515,7 +520,6 @@ paths:
summary: Get a user's data.
description: >-
Get a user's data.
Only allowed in basic mode
operationId: getUser
security:
- bearerAuth: []
Expand Down Expand Up @@ -725,6 +729,8 @@ paths:
description: description
externalName: externalName
extension: {}
"401":
$ref: "#/components/responses/UnauthorizedUserError"
"500":
$ref: "#/components/responses/UnknownError"
post:
Expand Down Expand Up @@ -930,6 +936,8 @@ paths:
clusterAdmin: false
"401":
$ref: "#/components/responses/UnauthorizedUserError"
"403":
$ref: "#/components/responses/ForbiddenUserError"
"500":
$ref: "#/components/responses/UnknownError"
/api/v2/virtual-clusters:
Expand Down Expand Up @@ -1153,7 +1161,10 @@ paths:
type: number
- name: order
in: query
description: 'order of job list. It follows the format <field>,<ASC|DESC>, default value is "submissionTime,DESC". Available fields include: jobName, submissionTime, username, vc, retries, totalTaskNumber, totalGpuNumber, state'
description: 'order of job list.
It follows the format <field>,<ASC|DESC>, default value is "submissionTime,DESC".
Available fields include: jobName, submissionTime, username, vc, retries, totalTaskNumber, totalGpuNumber, state, completionTime.
CompletionTime maybe null for some jobs, these jobs will be returned at the end of the list when sorting by ASC order & at the beginning when sorting by DESC order.'
schema:
type: string
- name: withTotalCount
Expand Down Expand Up @@ -1233,7 +1244,7 @@ paths:
name: task role name
taskStatuses:
- taskIndex: 0
taskUid: 421
taskUid: e0235258-0c4b-11eb-b396-96ba8c8049b4
taskState: SUCCEEDED
attemptId: 0
attemptState: SUCCEEDED
Expand Down Expand Up @@ -1288,7 +1299,7 @@ paths:
name: task role name
taskStatuses:
- taskIndex: 0
taskUid: 421
taskUid: e0235258-0c4b-11eb-b396-96ba8c8049b4
taskState: SUCCEEDED
attemptId: 0
attemptState: SUCCEEDED
Expand Down Expand Up @@ -1849,6 +1860,28 @@ paths:
$ref: "#/components/responses/UnauthorizedUserError"
"403":
$ref: "#/components/responses/ForbiddenUserError"
/api/v2/alerts:
get:
tags:
- alert
summary: Get related alerts.
description: Get related alerts. For admin users, return all the firing alerts; Return only the alerts related to the user's jobs for normal users.
operationId: getAlerts
security:
- bearerAuth: []
responses:
"200":
description: Succeeded
content:
application/json:
example:
- Please refer to Prometheus Alerts API:
- >-
https://prometheus.io/docs/prometheus/latest/querying/api/#alerts
"401":
$ref: "#/components/responses/UnauthorizedUserError"
"500":
$ref: "#/components/responses/UnknownError"
/api/v2/kubernetes/pods:
get:
tags:
Expand Down Expand Up @@ -2293,6 +2326,7 @@ components:
type: string
appId:
type: string
nullable: true
description: unique id for the job
appCreatedTime:
type: integer
Expand Down Expand Up @@ -2426,7 +2460,7 @@ components:
type: integer
description: task index
taskUid:
type: integer
type: string
taskState:
type: string
description: task state
Expand Down
23 changes: 16 additions & 7 deletions tests/common/apiTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,15 @@ export const ApiDefaultTestCases: {[key: string]: IApiTestCase} = {
}
}],
after: [ updateTestJobExecutionType('STOP') ]
},
'get /api/v2/alerts': {
tests: [
{
// Skip the test temporarily because the alerts came from prometheus.
description: 'Skip',
customizedTest: 'skipTest'
}
]
}
};

Expand All @@ -1096,7 +1105,7 @@ class CustomizedTestsClass {
): Promise<void> {
const client: TokenClient = new TokenClient({
token: unauthorizedToken,
https: true,
https: clustersJson[0].https,
rest_server_uri: clustersJson[0].rest_server_uri
});

Expand All @@ -1112,7 +1121,7 @@ class CustomizedTestsClass {
): Promise<void> {
const client: AuthnClient = new AuthnClient({
token: operationResults!.beforeResults![0].token,
https: true,
https: clustersJson[0].https,
rest_server_uri: clustersJson[0].rest_server_uri
});

Expand All @@ -1125,7 +1134,7 @@ class CustomizedTestsClass {
): Promise<void> {
const client: AuthnClient = new AuthnClient({
token: unauthorizedToken,
https: true,
https: clustersJson[0].https,
rest_server_uri: clustersJson[0].rest_server_uri
});

Expand Down Expand Up @@ -1250,7 +1259,7 @@ class CustomizedTestsClass {
): Promise<void> {
const client: UserClient = new UserClient({
token: applicationToken,
https: true,
https: clustersJson[0].https,
rest_server_uri: clustersJson[0].rest_server_uri
});

Expand All @@ -1269,7 +1278,7 @@ class CustomizedTestsClass {

const client: UserClient = new UserClient({
token: info.token,
https: true,
https: clustersJson[0].https,
rest_server_uri: clustersJson[0].rest_server_uri
});

Expand All @@ -1286,7 +1295,7 @@ class CustomizedTestsClass {
private async createNonadminUserAndToken(): Promise<ILoginInfo> {
const openPAIClient: OpenPAIClient = new OpenPAIClient({
token: clustersJson[0].token,
https: true,
https: clustersJson[0].https,
rest_server_uri: clustersJson[0].rest_server_uri
});

Expand All @@ -1310,7 +1319,7 @@ class CustomizedTestsClass {
private async deleteNonadminUserAndToken(info: ILoginInfo): Promise<void> {
const openPAIClient: OpenPAIClient = new OpenPAIClient({
token: clustersJson[0].token,
https: true,
https: clustersJson[0].https,
rest_server_uri: clustersJson[0].rest_server_uri
});

Expand Down
1 change: 1 addition & 0 deletions tests/common/apiTestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class ApiTestRunner {
const valid: boolean = this.ajvInstance.validate(operation.response.schema, res) as boolean;
if (!valid) {
console.log(this.ajvInstance.errors);
console.log(JSON.stringify(res));
}
expect(valid, 'response should be valid.').to.be.true();
}
Expand Down

0 comments on commit a23bac9

Please sign in to comment.