web/multinode: bandwidth api and store module created
Change-Id: I5a280f836d73ca9c34bab7dd5371bf1dce5a3974
This commit is contained in:
parent
0ca7583282
commit
cc5de4288b
58
web/multinode/src/api/bandwidth.ts
Normal file
58
web/multinode/src/api/bandwidth.ts
Normal file
@ -0,0 +1,58 @@
|
||||
// Copyright (C) 2021 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
import { APIClient } from '@/api/index';
|
||||
import { BandwidthRollup, BandwidthTraffic, Egress, Ingress } from '@/bandwidth';
|
||||
|
||||
/**
|
||||
* Client for bandwidth controller of MND api.
|
||||
*/
|
||||
export class BandwidthClient extends APIClient {
|
||||
private readonly ROOT_PATH: string = '/api/v0/bandwidth';
|
||||
|
||||
/**
|
||||
* Returns bandwidth information for selected node and satellite in any.
|
||||
*
|
||||
* @throws {@link BadRequestError}
|
||||
* This exception is thrown if the input is not a valid.
|
||||
*
|
||||
* @throws {@link UnauthorizedError}
|
||||
* Thrown if the auth cookie is missing or invalid.
|
||||
*
|
||||
* @throws {@link InternalError}
|
||||
* Thrown if something goes wrong on server side.
|
||||
*/
|
||||
public async fetch(satelliteId: string | null, nodeId: string | null): Promise<BandwidthTraffic> {
|
||||
let path = `${this.ROOT_PATH}`;
|
||||
|
||||
if (satelliteId) {
|
||||
path += `/satellite/${satelliteId}`;
|
||||
}
|
||||
|
||||
if (nodeId) {
|
||||
path += `/${nodeId}`;
|
||||
}
|
||||
|
||||
const response = await this.http.get(path);
|
||||
|
||||
if (!response.ok) {
|
||||
await this.handleError(response);
|
||||
}
|
||||
|
||||
const traffic = await response.json();
|
||||
|
||||
return new BandwidthTraffic(
|
||||
traffic.bandwidthDaily.map(daily => {
|
||||
return new BandwidthRollup(
|
||||
new Egress(daily.egress.repair, daily.egress.audit, daily.egress.usage),
|
||||
new Ingress(daily.ingress.repair, daily.ingress.usage),
|
||||
daily.delete,
|
||||
new Date(daily.intervalStart),
|
||||
);
|
||||
}),
|
||||
traffic.bandwidthSummary,
|
||||
traffic.egressSummary,
|
||||
traffic.ingressSummary,
|
||||
);
|
||||
}
|
||||
}
|
63
web/multinode/src/app/store/bandwidth.ts
Normal file
63
web/multinode/src/app/store/bandwidth.ts
Normal file
@ -0,0 +1,63 @@
|
||||
// Copyright (C) 2021 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
import { ActionContext, ActionTree, GetterTree, Module, MutationTree } from 'vuex';
|
||||
|
||||
import { RootState } from '@/app/store/index';
|
||||
import { BandwidthTraffic } from '@/bandwidth';
|
||||
import { Bandwidth } from '@/bandwidth/service';
|
||||
|
||||
/**
|
||||
* BandwidthState is a representation of bandwidth egress and ingress.
|
||||
*/
|
||||
export class BandwidthState {
|
||||
public traffic: BandwidthTraffic = new BandwidthTraffic();
|
||||
}
|
||||
|
||||
/**
|
||||
* BandwidthModule is a part of a global store that encapsulates all bandwidth related logic.
|
||||
*/
|
||||
export class BandwidthModule implements Module<BandwidthState, RootState> {
|
||||
public readonly namespaced: boolean;
|
||||
public readonly state: BandwidthState;
|
||||
public readonly getters?: GetterTree<BandwidthState, RootState>;
|
||||
public readonly actions: ActionTree<BandwidthState, RootState>;
|
||||
public readonly mutations: MutationTree<BandwidthState>;
|
||||
|
||||
private readonly bandwidth: Bandwidth;
|
||||
|
||||
public constructor(bandwidth: Bandwidth) {
|
||||
this.bandwidth = bandwidth;
|
||||
|
||||
this.namespaced = true;
|
||||
this.state = new BandwidthState();
|
||||
this.mutations = {
|
||||
populate: this.populate,
|
||||
};
|
||||
this.actions = {
|
||||
fetch: this.fetch.bind(this),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* populate mutation will set bandwidth state.
|
||||
* @param state - state of the module.
|
||||
* @param traffic - representation of bandwidth egress and ingress.
|
||||
*/
|
||||
public populate(state: BandwidthState, traffic: BandwidthTraffic): void {
|
||||
state.traffic = traffic;
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch action loads bandwidth information.
|
||||
* @param ctx - context of the Vuex action.
|
||||
*/
|
||||
public async fetch(ctx: ActionContext<BandwidthState, RootState>): Promise<void> {
|
||||
const selectedSatelliteId = ctx.rootState.nodes.selectedSatellite ? ctx.rootState.nodes.selectedSatellite.id : null;
|
||||
const selectedNodeId = ctx.rootState.nodes.selectedNode ? ctx.rootState.nodes.selectedNode.id : null;
|
||||
|
||||
const traffic = await this.bandwidth.fetch(selectedSatelliteId, selectedNodeId);
|
||||
|
||||
ctx.commit('populate', traffic);
|
||||
}
|
||||
}
|
47
web/multinode/src/bandwidth/index.ts
Normal file
47
web/multinode/src/bandwidth/index.ts
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright (C) 2021 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
/**
|
||||
* Represents by day and total bandwidth.
|
||||
*/
|
||||
export class BandwidthTraffic {
|
||||
public constructor(
|
||||
public bandwidthDaily: BandwidthRollup[] = [],
|
||||
public bandwidthSummary: number = 0,
|
||||
public egressSummary: number = 0,
|
||||
public ingressSummary: number = 0,
|
||||
) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents by day bandwidth.
|
||||
*/
|
||||
export class BandwidthRollup {
|
||||
public constructor(
|
||||
public egress: Egress = new Egress(),
|
||||
public ingress: Ingress = new Ingress(),
|
||||
public deletes: number = 0,
|
||||
public intervalStart: Date = new Date(),
|
||||
) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores info about storage node egress usage.
|
||||
*/
|
||||
export class Egress {
|
||||
public constructor(
|
||||
public repair: number = 0,
|
||||
public audit: number = 0,
|
||||
public usage: number = 0,
|
||||
) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores info about storage node ingress usage.
|
||||
*/
|
||||
export class Ingress {
|
||||
public constructor(
|
||||
public repair: number = 0,
|
||||
public usage: number = 0,
|
||||
) {}
|
||||
}
|
32
web/multinode/src/bandwidth/service.ts
Normal file
32
web/multinode/src/bandwidth/service.ts
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright (C) 2021 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
import { BandwidthClient } from '@/api/bandwidth';
|
||||
import { BandwidthTraffic } from '@/bandwidth/index';
|
||||
|
||||
/**
|
||||
* exposes all bandwidth related logic
|
||||
*/
|
||||
export class Bandwidth {
|
||||
private readonly bandwidth: BandwidthClient;
|
||||
|
||||
public constructor(bandwidth: BandwidthClient) {
|
||||
this.bandwidth = bandwidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns bandwidth for selected satellite and node if any.
|
||||
*
|
||||
* @throws {@link BadRequestError}
|
||||
* This exception is thrown if the input is not a valid.
|
||||
*
|
||||
* @throws {@link UnauthorizedError}
|
||||
* Thrown if the auth cookie is missing or invalid.
|
||||
*
|
||||
* @throws {@link InternalError}
|
||||
* Thrown if something goes wrong on server side.
|
||||
*/
|
||||
public async fetch(satelliteId: string | null, nodeId: string | null): Promise<BandwidthTraffic> {
|
||||
return await this.bandwidth.fetch(satelliteId, nodeId);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user