Skip to content

Commit

Permalink
solution: sierra stat API
Browse files Browse the repository at this point in the history
  • Loading branch information
kanazirsky committed Jun 26, 2024
1 parent 35cc856 commit 7c95817
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api-definitions
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export {
} from './typesMarket';
export * as token from './typesToken';
export * as transaction from './typesTransaction';
export * as sierra from './typesSierra';
export {
Headers, AuthDetails, JwtSignature,
AuthenticationListener, AuthenticationStatus,
Expand Down
133 changes: 133 additions & 0 deletions packages/core/src/typesSierra.ts
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()),
};
};
}

}
11 changes: 8 additions & 3 deletions packages/web/src/EmeraldApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {SecretToken} from "@emeraldpay/api";
import {WebChannel} from "./channel";
import {CredentialsContext, emeraldCredentials} from "./credentials";
import {BlockchainClient} from "./wrapped/BlockchainClient";
import {InsightsClient} from "./wrapped/InsightsClient";
import {MarketClient} from "./wrapped/MarketClient";
import {CredentialsContext, emeraldCredentials} from "./credentials";
import {SecretToken} from "@emeraldpay/api";
import {SierraStatClient} from "./wrapped/SierraStatClient";

export class EmeraldApi {
private readonly hostname: string;
Expand All @@ -18,7 +19,7 @@ export class EmeraldApi {

static devApi(token?: SecretToken | undefined, credentials?: CredentialsContext): EmeraldApi {
// a dev token with access only from the internal network
let devToken = token ?? 'emrld_8ntrHbZN67DF8TWKgCMO1I9nSaMG0cpoMhj3GP';
const devToken = token ?? 'emrld_8ntrHbZN67DF8TWKgCMO1I9nSaMG0cpoMhj3GP';
return new EmeraldApi('https://api.emeraldpay.dev', devToken, credentials);
}

Expand All @@ -41,4 +42,8 @@ export class EmeraldApi {
return new MarketClient(this.hostname, this.channel, this.credentials);
}

get sierraStat(): SierraStatClient {
return new SierraStatClient(this.hostname, this.channel, this.credentials);
}

}
58 changes: 58 additions & 0 deletions packages/web/src/__integration-tests__/sierra.test.ts
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();
});
});
});
10 changes: 9 additions & 1 deletion packages/web/src/wrapped/Factory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { MessageFactory } from "@emeraldpay/api";
import * as auth_pb from "../generated/auth_pb";
import * as blockchain_pb from "../generated/blockchain_pb";
import * as common_pb from "../generated/common_pb";
import * as market_pb from "../generated/market_pb";
import * as auth_pb from "../generated/auth_pb";
import * as sierra_stat_message_pb from "../generated/sierra.stat.message_pb";

export const classFactory: MessageFactory = (id: string) => {
if (id == "common_pb.Chain") {
Expand Down Expand Up @@ -65,6 +66,13 @@ export const classFactory: MessageFactory = (id: string) => {
if (id == "market_pb.Pair") {
return new market_pb.Pair();
}
// Sierra
if (id == "sierra_stat_message_pb.GetRequestCountRequest") {
return new sierra_stat_message_pb.GetRequestCountRequest();
}
if (id == "sierra_stat_message_pb.GetTokenStatRequest") {
return new sierra_stat_message_pb.GetTokenStatRequest();
}

throw Error("Unsupported type: " + id)
};
38 changes: 38 additions & 0 deletions packages/web/src/wrapped/SierraStatClient.ts
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);
}



}

0 comments on commit 7c95817

Please sign in to comment.