Skip to content

Commit

Permalink
added job history query api method
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Nov 7, 2020
1 parent 62971ad commit a8758ce
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 61 deletions.
40 changes: 24 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cronicle-client",
"version": "1.0.1",
"version": "1.1.0",
"description": "Light Cronicle node client with full TypeScript support",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -63,6 +63,9 @@
"timezone-mock": "^1.0.5",
"ts-node": "^8.1.0",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
"typescript": "^3.7.2"
},
"dependencies": {
"request-promise": "^4.2.6"
}
}
6 changes: 5 additions & 1 deletion src/CronicleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ICreateEventRequest,
ICreateEventResponse, IDeleteEventRequest, IErrorResponse,
IGetEventRequest,
IGetEventResponse,
IGetEventResponse, IGetHistoryRequest, IGetHistoryResponse,
IGetJobStatusRequest,
IGetJobStatusResponse, IGetScheduleRequest, IGetScheduleResponse,
IRunEventRequest,
Expand Down Expand Up @@ -125,6 +125,10 @@ export class CronicleClient<Categories extends string = BaseCategories,
return this._executeRequest('get_schedule', HttpMethods.GET, req);
}

public getHistory(req?: IGetHistoryRequest): Promise<IGetHistoryResponse<any, Plugins, Targets, Categories>> {
return this._executeRequest('get_history', HttpMethods.GET, req);
}

private _executeRequest<T extends IBasicResponse>(operation: string, httpMethod: HttpMethods,
bodyOrQuery?: IBodyOrQuery): Promise<T> {
return Promise.resolve(request(this._buildRequest(operation, httpMethod, bodyOrQuery)))
Expand Down
69 changes: 37 additions & 32 deletions src/dataTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,52 @@ export interface ITiming {
years?: number[];
}

export interface IJob<Plugin extends keyof Plugins,
export interface IJobHistory<Plugin extends keyof Plugins,
Plugins,
Targets extends string,
Categories extends string> {
hostname: string;
id: string;
code?: NumberedBoolean;
event: string;
category: Categories;
plugin: Plugin;
hostname: string;
time_start: number;
elapsed: number;
cpu?: {
min: number;
max: number;
total: number;
count: number;
current: number;
};
mem?: {
min: number;
max: number;
total: number;
count: number;
current: number;
};
perf?: {
scale?: number;
counters: { [k: string]: number; };
perf: { [k: string]: number; };
};
log_file_size: number;
event_title: string;
plugin_title: string;
category_title: string;
}

export interface IJob<Plugin extends keyof Plugins,
Plugins,
Targets extends string,
Categories extends string> extends IJobHistory<Plugin, Plugins, Targets, Categories> {
source: string;
log_file: string;
log_file_size: number;
time_end: number;
elapsed: number;
pid: number;
now: number;
time_start: number;
plugin: Plugin;
category: Categories;
timeout: number;
retries: number;
retry_delay: number;
Expand All @@ -170,37 +200,12 @@ export interface IJob<Plugin extends keyof Plugins,
notify_success: string;
timezone: string;
params: Plugins[Plugin];
id: string;
event_title: string;
plugin_title: string;
category_title: string;
nice_target: string;
command: string;
type: string;
description?: string;
complete?: NumberedBoolean;
code?: NumberedBoolean;
progress?: number;
cpu?: {
min: number;
max: number;
total: number;
count: number;
current: number;
};
mem?: {
min: number;
max: number;
total: number;
count: number;
current: number;
};
perf?: {
scale?: number;
counters: { [k: string]: number; };
perf: { [k: string]: number; };
};

}

export interface ICreatedEvent<Plugin extends keyof Plugins,
Expand Down
33 changes: 23 additions & 10 deletions src/requestResponseTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { ICreatedEvent, IEvent, IJob } from './dataTypes';
import {ICreatedEvent, IEvent, IJob, IJobHistory} from './dataTypes';
import { NumberedBoolean, RecursivePartial } from './helperTypes';

export interface ICreateEventRequest<Plugin extends keyof Plugins,
Expand Down Expand Up @@ -65,11 +65,14 @@ export type IUpdateJobRequest =
export type IDeleteEventRequest = IIdRequest;
export type IAbortJobRequest = IIdRequest;

export interface IGetScheduleRequest {
export interface IPaginatedRequest {
offset?: number;
limit?: number;
}

export type IGetScheduleRequest = IPaginatedRequest;
export type IGetHistoryRequest = IPaginatedRequest;

export interface IBasicResponse {
code: 0 | number | string;
}
Expand All @@ -82,18 +85,28 @@ export interface ICreateEventResponse extends IBasicResponse {
id: string;
}

export interface IPaginatedResponse {
list: {
page_size: number;
first_page: number;
last_page: number;
length: number;
type: 'list'
};
}

export interface IGetScheduleResponse<Plugin extends keyof Plugins,
Plugins,
Targets extends string,
Categories extends string> extends IBasicResponse {
Categories extends string> extends IBasicResponse, IPaginatedResponse {
rows: Array<ICreatedEvent<Plugin, Plugins, Targets, Categories>>;
list: {
page_size: number;
first_page: number;
last_page: number;
length: number;
type: 'list'
};
}

export interface IGetHistoryResponse<Plugin extends keyof Plugins,
Plugins,
Targets extends string,
Categories extends string> extends IBasicResponse, IPaginatedResponse {
rows: Array<IJobHistory<Plugin, Plugins, Targets, Categories>>;
}

export interface IRunEventResponse extends IBasicResponse {
Expand Down
99 changes: 99 additions & 0 deletions test/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,105 @@ limit=${limit}&offset=${offset}`,

});

describe('get history', () => {

it('should get history with no params', () => {
const client = new cronicleClientStubbed({masterUrl, apiKey});
const response = {code: 0};
requestStub.resolves(response);
return client.getHistory()
.then((resp) => {
expect(requestStub.firstCall.args[0]).to.eql({
body: undefined,
headers: {
'X-API-Key': apiKey,
},
json: true,
method: 'GET',
url: `${masterUrl}/api/app/get_history/${defaultVersion}`,
});
expect(resp).to.eq(response);
});
});

it('should get history with offset', () => {
const client = new cronicleClientStubbed({masterUrl, apiKey});
const response = {code: 0};
requestStub.resolves(response);
const offset = 1;
return client.getHistory({offset})
.then((resp) => {
expect(requestStub.firstCall.args[0]).to.eql({
body: undefined,
headers: {
'X-API-Key': apiKey,
},
json: true,
method: 'GET',
url: `${masterUrl}/api/app/get_history/${defaultVersion}?offset=${offset}`,
});
expect(resp).to.eq(response);
});
});

it('should get history with limit', () => {
const client = new cronicleClientStubbed({masterUrl, apiKey});
const response = {code: 0};
requestStub.resolves(response);
const limit = 1;
return client.getHistory({limit})
.then((resp) => {
expect(requestStub.firstCall.args[0]).to.eql({
body: undefined,
headers: {
'X-API-Key': apiKey,
},
json: true,
method: 'GET',
url: `${masterUrl}/api/app/get_history/${defaultVersion}?limit=${limit}`,
});
expect(resp).to.eq(response);
});
});

it('should get history with limit and offset', () => {
const client = new cronicleClientStubbed({masterUrl, apiKey});
const response = {code: 0};
requestStub.resolves(response);
const limit = 1;
const offset = 1;
return client.getHistory({limit, offset})
.then((resp) => {
expect(requestStub.firstCall.args[0]).to.eql({
body: undefined,
headers: {
'X-API-Key': apiKey,
},
json: true,
method: 'GET',
url: `${masterUrl}/api/app/get_history/${defaultVersion}?\
limit=${limit}&offset=${offset}`,
});
expect(resp).to.eq(response);
});
});

it('should get history with error', (done) => {
const client = new cronicleClientStubbed({masterUrl, apiKey});
const code = 'myCode';
const description = 'mydDescription';
requestStub.resolves({code, description});
client.getHistory()
.catch((error) => {
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql(code);
expect(error.message).to.eql(description);
done();
});
});

});

describe('run event', () => {

it('should run event with id', () => {
Expand Down

0 comments on commit a8758ce

Please sign in to comment.