-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35cc856
commit 7c95817
Showing
7 changed files
with
248 additions
and
5 deletions.
There are no files selected for viewing
Submodule api-definitions
updated
2 files
+62 −0 | proto/sierra.stat.message.proto | |
+9 −0 | proto/sierra.stat.proto |
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,133 @@ | ||
import * as sierra_stat_message_pb from './generated/sierra.stat.message_pb'; | ||
import { DataMapper } from './Publisher'; | ||
import { MessageFactory } from './typesConvert'; | ||
|
||
export enum Granularity { | ||
UNSPECIFIED = 0, | ||
SECOND = 1, | ||
MINUTE = 2, | ||
HOUR = 3, | ||
DAY = 4, | ||
WEEK = 5, | ||
MONTH = 6, | ||
} | ||
|
||
export enum GroupBy { | ||
UNSPECIFIED = 0, | ||
SERVICE = 1, | ||
PROJECT = 2, | ||
} | ||
export interface GetRequestCountRequest { | ||
orgId: string; | ||
timestampFrom?: Date; | ||
timestampTo?: Date; | ||
projectIds?: string[]; | ||
services?: string[]; | ||
granularity?: Granularity; | ||
GroupBy?: GroupBy; | ||
} | ||
|
||
export interface RequestCount { | ||
timestamp: Date; | ||
count: number; | ||
} | ||
|
||
export interface GroupService { | ||
service: string; | ||
} | ||
|
||
export interface GroupProjectId { | ||
projectId: string; | ||
} | ||
|
||
export interface GroupRequestCount { | ||
group: GroupService | GroupProjectId; | ||
counts: RequestCount[]; | ||
} | ||
|
||
export interface GetTokenStatRequest { | ||
orgId: string; | ||
tokenIds?: string[]; | ||
} | ||
|
||
export interface TokenStat { | ||
tokenId: string; | ||
lastTimestamp: Date; | ||
} | ||
|
||
export class ConvertSierra { | ||
private readonly factory: MessageFactory; | ||
|
||
constructor(factory: MessageFactory) { | ||
this.factory = factory; | ||
} | ||
|
||
public getRequestCountRequest(request: GetRequestCountRequest): sierra_stat_message_pb.GetRequestCountRequest { | ||
const result: sierra_stat_message_pb.GetRequestCountRequest = this.factory('sierra_stat_message_pb.GetRequestCountRequest'); | ||
result.setOrgId(request.orgId) | ||
if (request.timestampFrom) { | ||
result.setTimestampFrom(request.timestampFrom.getTime()); | ||
} | ||
if (request.timestampTo) { | ||
result.setTimestampTo(request.timestampTo.getTime()) | ||
} | ||
if (request.projectIds) { | ||
result.setProjectIdsList(request.projectIds) | ||
} | ||
if (request.services) { | ||
result.setServicesList(request.services) | ||
} | ||
if (request.granularity) { | ||
result.setGranularity(request.granularity.valueOf()) | ||
} | ||
if (request.GroupBy) { | ||
result.setGroupBy(request.GroupBy.valueOf()); | ||
} | ||
return result; | ||
} | ||
|
||
public requestCount(): DataMapper<sierra_stat_message_pb.RequestCount, RequestCount> { | ||
return (response) => ({ | ||
timestamp: new Date(response.getTimestamp()), | ||
count: response.getCount(), | ||
}); | ||
} | ||
|
||
public groupRequestCount(): DataMapper<sierra_stat_message_pb.GroupRequestCount, GroupRequestCount> { | ||
return (response) => { | ||
let group: GroupService | GroupProjectId; | ||
if (response.hasService()) { | ||
group = { | ||
service: response.getService(), | ||
}; | ||
} else if (response.hasProjectId()) { | ||
group = { | ||
projectId: response.getProjectId(), | ||
}; | ||
} | ||
return { | ||
group: group, | ||
counts: response.getCountsList().map(this.requestCount()), | ||
}; | ||
}; | ||
} | ||
|
||
public getTokenStatRequest(request: GetTokenStatRequest): sierra_stat_message_pb.GetTokenStatRequest { | ||
const result: sierra_stat_message_pb.GetTokenStatRequest = this.factory('sierra_stat_message_pb.GetTokenStatRequest'); | ||
result.setOrgId(request.orgId); | ||
if (request.tokenIds) { | ||
result.setTokenIdList(request.tokenIds); | ||
} | ||
return result; | ||
} | ||
|
||
public tokenStat(): DataMapper<sierra_stat_message_pb.TokenStat, TokenStat> { | ||
return (response) => { | ||
return { | ||
tokenId: response.getTokenId(), | ||
lastTimestamp: new Date(response.getLastTimestamp()), | ||
}; | ||
}; | ||
} | ||
|
||
} |
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,58 @@ | ||
import {EmeraldApi} from "../EmeraldApi"; | ||
|
||
jest.setTimeout(35000); | ||
|
||
describe("SierraStatClient", () => { | ||
let api: EmeraldApi; | ||
|
||
beforeAll(() => { | ||
// ORIGIN is set in jest.config.js | ||
api = EmeraldApi.devApi(); | ||
}); | ||
|
||
test('getRequestCount', (done) => { | ||
const client = api.sierraStat; | ||
|
||
const call = client.getRequestCount({ | ||
orgId: "test-org", | ||
}) | ||
call | ||
.onData((data) => { | ||
console.log("requestCount", data); | ||
call.cancel(); | ||
done(); | ||
}) | ||
.onError((error) => { | ||
call.cancel(); | ||
console.log("cancel: ", error.message); | ||
done(error); | ||
}) | ||
.finally(() => { | ||
console.log("stream ended"); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('getTokenStat', (done) => { | ||
const client = api.sierraStat; | ||
|
||
const call = client.getTokenStat({ | ||
orgId: "test-org", | ||
}) | ||
call | ||
.onData((data) => { | ||
console.log("tokenStat", data); | ||
call.cancel(); | ||
done(); | ||
}) | ||
.onError((error) => { | ||
call.cancel(); | ||
console.log("cancel: ", error.message); | ||
done(error); | ||
}) | ||
.finally(() => { | ||
console.log("stream ended"); | ||
done(); | ||
}); | ||
}); | ||
}); |
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,38 @@ | ||
import {Publisher, readOnce, sierra} from "@emeraldpay/api"; | ||
import {WebChannel, callStream} from "../channel"; | ||
import {CredentialsContext} from "../credentials"; | ||
import * as sierra_rpc from '../generated/Sierra.statServiceClientPb'; | ||
import {classFactory} from "./Factory"; | ||
|
||
export class SierraStatClient { | ||
readonly client: sierra_rpc.StatClient; | ||
readonly channel: WebChannel; | ||
readonly retries: number; | ||
|
||
private readonly convert = new sierra.ConvertSierra(classFactory); | ||
|
||
constructor(hostname: string, channel: WebChannel, credentials: CredentialsContext, retries = 3) { | ||
this.client = new sierra_rpc.StatClient(hostname, null, credentials.options); | ||
this.channel = channel; | ||
this.retries = retries; | ||
} | ||
|
||
public getRequestCount(request: sierra.GetRequestCountRequest): Publisher<sierra.GroupRequestCount> { | ||
const req = this.convert.getRequestCountRequest(request); | ||
const mapper = this.convert.groupRequestCount(); | ||
|
||
const call = callStream(this.client.getRequestCount.bind(this.client), mapper); | ||
return readOnce(this.channel, call, req, this.retries); | ||
} | ||
|
||
public getTokenStat(request: sierra.GetTokenStatRequest): Publisher<sierra.TokenStat> { | ||
const req = this.convert.getTokenStatRequest(request); | ||
const mapper = this.convert.tokenStat(); | ||
|
||
const call = callStream(this.client.getTokenStat.bind(this.client), mapper); | ||
return readOnce(this.channel, call, req, this.retries); | ||
} | ||
|
||
|
||
|
||
} |