web/storagenode: code refactoring pt.1

Change-Id: I948261c61e7ed7f9703a85314d7a14ca9a59b16d
This commit is contained in:
NikolaiYurchenko 2020-01-31 18:55:37 +02:00 committed by Nikolay Yurchenko
parent 91a480f5a0
commit 384bdf1f58
11 changed files with 133 additions and 115 deletions

View File

@ -15,7 +15,10 @@ export abstract class RouteConfig {
public static Notifications = new NavigationLink('/notifications', 'Notifications');
}
const router = new Router({
/**
* Router describes location mapping with components.
*/
export const router = new Router({
mode: 'history',
routes: [
{
@ -30,5 +33,3 @@ const router = new Router({
},
]
});
export default router;

View File

@ -9,12 +9,12 @@ import { node } from './modules/node';
Vue.use(Vuex);
// storage node store (vuex)
const store = new Vuex.Store({
/**
* storage node store (vuex)
*/
export const store = new Vuex.Store({
modules: {
node,
appStateModule,
}
},
});
export default store;

View File

@ -4,11 +4,13 @@
import { Duration, millisecondsInSecond, secondsInMinute } from '@/app/utils/duration';
import { SNOApi } from '@/storagenode/api/storagenode';
import { Dashboard, SatelliteInfo } from '@/storagenode/dashboard';
import { BandwidthUsed, EgressUsed, IngressUsed, Satellite, Stamp } from '@/storagenode/satellite';
import { BandwidthUsed, EgressUsed, IngressUsed, Satellite, Satellites, Stamp } from '@/storagenode/satellite';
export const NODE_MUTATIONS = {
POPULATE_STORE: 'POPULATE_STORE',
SELECT_SATELLITE: 'SELECT_SATELLITE',
SELECT_ALL_SATELLITES: 'SELECT_ALL_SATELLITES',
SET_DAILY_DATA: 'SET_DAILY_DATA',
};
export const NODE_ACTIONS = {
@ -22,6 +24,8 @@ export const StatusOffline = 'Offline';
const {
POPULATE_STORE,
SELECT_SATELLITE,
SELECT_ALL_SATELLITES,
SET_DAILY_DATA,
} = NODE_MUTATIONS;
const {
@ -94,7 +98,7 @@ export const node = {
return satellite.disqualified;
});
state.satellites = nodeInfo.satellites ? nodeInfo.satellites : [];
state.satellites = nodeInfo.satellites || [];
state.info.status = StatusOffline;
@ -108,31 +112,30 @@ export const node = {
}
},
[SELECT_SATELLITE](state: any, satelliteInfo: Satellite): void {
if (satelliteInfo.id) {
state.satellites.forEach(satellite => {
if (satelliteInfo.id === satellite.id) {
const audit = calculateSuccessRatio(
satelliteInfo.audit.successCount,
satelliteInfo.audit.totalCount
);
const selectedSatellite = state.satellites.find(satellite => satelliteInfo.id === satellite.id);
const uptime = calculateSuccessRatio(
satelliteInfo.uptime.successCount,
satelliteInfo.uptime.totalCount
);
state.selectedSatellite = satellite;
state.checks.audit = audit;
state.checks.uptime = uptime;
}
return;
});
}
else {
state.selectedSatellite = allSatellites;
if (!selectedSatellite) {
return;
}
const audit = calculateSuccessRatio(
satelliteInfo.audit.successCount,
satelliteInfo.audit.totalCount
);
const uptime = calculateSuccessRatio(
satelliteInfo.uptime.successCount,
satelliteInfo.uptime.totalCount,
);
state.selectedSatellite = selectedSatellite;
state.checks.audit = audit;
state.checks.uptime = uptime;
},
[SELECT_ALL_SATELLITES](state: any): void {
state.selectedSatellite = allSatellites;
},
[SET_DAILY_DATA](state: any, satelliteInfo: Satellite): void {
state.bandwidthChartData = satelliteInfo.bandwidthDaily;
state.egressChartData = satelliteInfo.egressDaily;
state.ingressChartData = satelliteInfo.ingressDaily;
@ -144,15 +147,22 @@ export const node = {
},
},
actions: {
[GET_NODE_INFO]: async function ({commit}: any): Promise<any> {
[GET_NODE_INFO]: async function ({commit}: any): Promise<void> {
const response = await snoAPI.dashboard();
commit(NODE_MUTATIONS.POPULATE_STORE, response);
},
[NODE_ACTIONS.SELECT_SATELLITE]: async function ({commit}, id: any): Promise<any> {
const response = id ? await snoAPI.satellite(id) : await snoAPI.satellites();
[NODE_ACTIONS.SELECT_SATELLITE]: async function ({commit}, id?: string): Promise<void> {
let response: Satellite | Satellites;
if (id) {
response = await snoAPI.satellite(id);
commit(NODE_MUTATIONS.SELECT_SATELLITE, response);
} else {
response = await snoAPI.satellites();
commit(NODE_MUTATIONS.SELECT_ALL_SATELLITES, response);
}
commit(NODE_MUTATIONS.SELECT_SATELLITE, response);
commit(NODE_MUTATIONS.SET_DAILY_DATA, response);
},
},
};
@ -163,9 +173,5 @@ export const node = {
* @param totalCount - holds total amount of attempts for reputation metric
*/
function calculateSuccessRatio(successCount: number, totalCount: number) : number {
if (totalCount === 0) {
return 100;
}
return successCount / totalCount * 100;
return totalCount === 0 ? 100 : successCount / totalCount * 100;
}

View File

@ -5,19 +5,11 @@
* NavigationLink class holds info for NavigationLink entity.
*/
export class NavigationLink {
private _path: string;
private _name: string;
public readonly path: string;
public readonly name: string;
public constructor(path: string, name: string) {
this._path = path;
this._name = name;
}
public get path(): string {
return this._path;
}
public get name(): string {
return this._name;
this.path = path;
this.name = name;
}
}

View File

@ -5,11 +5,11 @@ import { GB, KB, MB, PB, TB } from '@/app/utils/converter';
import { BandwidthUsed, Stamp } from '@/storagenode/satellite';
/**
* Used to display correct and convenient data on chart
* Used to display correct and convenient data on chart.
*/
export class ChartUtils {
/**
* Brings chart data to a more compact form
* Brings chart data to a more compact form.
* @param data - holds array of chart data in numeric form
* @returns data - numeric array of normalized data
*/
@ -36,14 +36,14 @@ export class ChartUtils {
}
/**
* gets chart data dimension depending on data size
* gets chart data dimension depending on data size.
* @param data - holds array of chart data in numeric form
* @returns dataDimension - string of data dimension
*/
public static getChartDataDimension(data: number[]): string {
const maxBytes = Math.ceil(Math.max(...data));
let dataDimension: string = '';
let dataDimension: string;
switch (true) {
case maxBytes < MB:
dataDimension = 'KB';
@ -65,7 +65,7 @@ export class ChartUtils {
}
/**
* Used to display correct number of days on chart's labels
* Used to display correct number of days on chart's labels.
*
* @returns daysDisplayed - array of days converted to a string by using the current or specified locale
*/
@ -87,13 +87,13 @@ export class ChartUtils {
}
/**
* Adds missing bandwidth usage for bandwidth chart data for each day of month
* Adds missing bandwidth usage for bandwidth chart data for each day of month.
* @param fetchedData - array of data that is spread over missing bandwidth usage for each day of the month
* @returns bandwidthChartData - array of filled data
*/
public static populateEmptyBandwidth(fetchedData: BandwidthUsed[]): BandwidthUsed[] {
const bandwidthChartData: BandwidthUsed[] = new Array(new Date().getDate());
const data: BandwidthUsed[] = fetchedData ? fetchedData : [];
const data: BandwidthUsed[] = fetchedData || [];
if (data.length === 0) {
return bandwidthChartData;
@ -122,7 +122,7 @@ export class ChartUtils {
}
/**
* Adds missing stamps for storage chart data for each day of month
* Adds missing stamps for storage chart data for each day of month.
* @param fetchedData - array of data that is spread over missing stamps for each day of the month
* @returns storageChartData - array of filled data
*/

View File

@ -8,7 +8,7 @@ export const TB = 1e12;
export const PB = 1e15;
/**
* Used to format amount from bytes to more compact unit
* Used to format amount from bytes to more compact unit.
* @param bytes - holds amount of bytes
* @returns bytes - amount of formatted bytes with unit name
*/

View File

@ -28,9 +28,13 @@ const {
},
})
export default class Dashboard extends Vue {
public async mounted() {
await this.$store.dispatch(GET_NODE_INFO);
await this.$store.dispatch(SELECT_SATELLITE, null);
public mounted() {
try {
this.$store.dispatch(GET_NODE_INFO);
this.$store.dispatch(SELECT_SATELLITE, null);
} catch (error) {
console.error(error);
}
}
}
</script>

View File

@ -5,8 +5,8 @@ import Vue, { VNode } from 'vue';
import { DirectiveBinding } from 'vue/types/options';
import App from './app/App.vue';
import router from './app/router';
import store from './app/store';
import { router } from './app/router';
import { store } from './app/store';
Vue.config.productionTip = false;

View File

@ -40,7 +40,7 @@ export class SNOApi {
public async dashboard(): Promise<Dashboard> {
const json = (await (await httpGet('/api/dashboard')).json() as any).data;
const satellitesJson = json.satellites ? json.satellites : [];
const satellitesJson = json.satellites || [];
const satellites: SatelliteInfo[] = satellitesJson.map((satellite: any) => {
const disqualified: Date | null = satellite.disqualified ? new Date(satellite.disqualified) : null;
@ -49,7 +49,6 @@ export class SNOApi {
});
const diskSpace: DiskSpaceInfo = new DiskSpaceInfo(json.diskSpace.used, json.diskSpace.available);
const bandwidth: BandwidthInfo = new BandwidthInfo(json.bandwidth.used, json.bandwidth.available);
return new Dashboard(json.nodeID, json.wallet, satellites, diskSpace, bandwidth,
@ -61,35 +60,11 @@ export class SNOApi {
* @returns satellite - new satellite instance filled with data from json
*/
public async satellite(id: string): Promise<Satellite> {
const url = '/api/satellite/' + id;
const url = `/api/satellite/${id}`;
const json = (await (await httpGet(url)).json() as any).data;
const storageDailyJson = json.storageDaily ? json.storageDaily : [];
const bandwidthDailyJson = json.bandwidthDaily ? json.bandwidthDaily : [];
const storageDaily: Stamp[] = storageDailyJson.map((stamp: any) => {
return new Stamp(stamp.atRestTotal, new Date(stamp.intervalStart));
});
const bandwidthDaily: BandwidthUsed[] = bandwidthDailyJson.map((bandwidth: any) => {
const egress = new Egress(bandwidth.egress.audit, bandwidth.egress.repair, bandwidth.egress.usage);
const ingress = new Ingress(bandwidth.ingress.repair, bandwidth.ingress.usage);
return new BandwidthUsed(egress, ingress, new Date(bandwidth.intervalStart));
});
const egressDaily: EgressUsed[] = bandwidthDailyJson.map((bandwidth: any) => {
const egress = new Egress(bandwidth.egress.audit, bandwidth.egress.repair, bandwidth.egress.usage);
return new EgressUsed(egress, new Date(bandwidth.intervalStart));
});
const ingressDaily: IngressUsed[] = bandwidthDailyJson.map((bandwidth: any) => {
const ingress = new Ingress(bandwidth.ingress.repair, bandwidth.ingress.usage);
return new IngressUsed(ingress, new Date(bandwidth.intervalStart));
});
const satelliteByDayInfo = new SatelliteByDayInfo(json);
const audit: Metric = new Metric(json.audit.totalCount, json.audit.successCount, json.audit.alpha,
json.audit.beta, json.audit.score);
@ -97,9 +72,19 @@ export class SNOApi {
const uptime: Metric = new Metric(json.uptime.totalCount, json.uptime.successCount, json.uptime.alpha,
json.uptime.beta, json.uptime.score);
return new Satellite(json.id, storageDaily, bandwidthDaily, egressDaily, ingressDaily,
json.storageSummary, json.bandwidthSummary, json.egressSummary, json.ingressSummary,
audit, uptime);
return new Satellite(
json.id,
satelliteByDayInfo.storageDaily,
satelliteByDayInfo.bandwidthDaily,
satelliteByDayInfo.egressDaily,
satelliteByDayInfo.ingressDaily,
json.storageSummary,
json.bandwidthSummary,
json.egressSummary,
json.ingressSummary,
audit,
uptime
);
}
/**
@ -109,33 +94,55 @@ export class SNOApi {
public async satellites(): Promise<Satellites> {
const json = (await (await httpGet('/api/satellites')).json() as any).data;
const storageDailyJson = json.storageDaily ? json.storageDaily : [];
const bandwidthDailyJson = json.bandwidthDaily ? json.bandwidthDaily : [];
const satelliteByDayInfo = new SatelliteByDayInfo(json);
const storageDaily: Stamp[] = storageDailyJson.map((stamp: any) => {
return new Satellites(
satelliteByDayInfo.storageDaily,
satelliteByDayInfo.bandwidthDaily,
satelliteByDayInfo.egressDaily,
satelliteByDayInfo.ingressDaily,
json.storageSummary,
json.bandwidthSummary,
json.egressSummary,
json.ingressSummary
);
}
}
/**
* SatelliteByDayInfo holds by day bandwidth metrics.
*/
class SatelliteByDayInfo {
public storageDaily: Stamp[];
public bandwidthDaily: BandwidthUsed[];
public egressDaily: EgressUsed[];
public ingressDaily: IngressUsed[];
public constructor(json) {
const storageDailyJson = json.storageDaily || [];
const bandwidthDailyJson = json.bandwidthDaily || [];
this.storageDaily = storageDailyJson.map((stamp: any) => {
return new Stamp(stamp.atRestTotal, new Date(stamp.intervalStart));
});
const bandwidthDaily: BandwidthUsed[] = bandwidthDailyJson.map((bandwidth: any) => {
this.bandwidthDaily = bandwidthDailyJson.map((bandwidth: any) => {
const egress = new Egress(bandwidth.egress.audit, bandwidth.egress.repair, bandwidth.egress.usage);
const ingress = new Ingress(bandwidth.ingress.repair, bandwidth.ingress.usage);
return new BandwidthUsed(egress, ingress, new Date(bandwidth.intervalStart));
});
const egressDaily: EgressUsed[] = bandwidthDailyJson.map((bandwidth: any) => {
this.egressDaily = bandwidthDailyJson.map((bandwidth: any) => {
const egress = new Egress(bandwidth.egress.audit, bandwidth.egress.repair, bandwidth.egress.usage);
return new EgressUsed(egress, new Date(bandwidth.intervalStart));
});
const ingressDaily: IngressUsed[] = bandwidthDailyJson.map((bandwidth: any) => {
this.ingressDaily = bandwidthDailyJson.map((bandwidth: any) => {
const ingress = new Ingress(bandwidth.ingress.repair, bandwidth.ingress.usage);
return new IngressUsed(ingress, new Date(bandwidth.intervalStart));
});
return new Satellites(storageDaily, bandwidthDaily, egressDaily, ingressDaily,
json.storageSummary, json.bandwidthSummary, json.egressSummary, json.ingressSummary);
}
}

View File

@ -15,7 +15,8 @@ export class Dashboard {
public startedAt: Date,
public version: string,
public allowedVersion: string,
public isUpToDate: boolean) {}
public isUpToDate: boolean,
) {}
}
/**
@ -37,7 +38,8 @@ export class DiskSpaceInfo {
public constructor(
public used: number,
public available: number) {
public available: number,
) {
this.remaining = available - used;
}
}
@ -50,7 +52,8 @@ export class BandwidthInfo {
public constructor(
public used: number,
public available: number) {
public available: number,
) {
this.remaining = available - used;
}
}

View File

@ -16,7 +16,8 @@ export class Satellite {
public egressSummary: number,
public ingressSummary: number,
public audit: Metric,
public uptime: Metric) {}
public uptime: Metric,
) {}
}
/**
@ -53,7 +54,8 @@ export class Metric {
public successCount: number,
public alpha: number,
public beta: number,
public score: number) {}
public score: number,
) {}
}
/**
@ -63,7 +65,8 @@ export class Egress {
public constructor(
public audit: number,
public repair: number,
public usage: number) {}
public usage: number,
) {}
}
/**
@ -72,7 +75,8 @@ export class Egress {
export class Ingress {
public constructor(
public repair: number,
public usage: number) {}
public usage: number,
) {}
}
/**
@ -82,7 +86,8 @@ export class BandwidthUsed {
public constructor(
public egress: Egress,
public ingress: Ingress,
public intervalStart: Date) {}
public intervalStart: Date,
) {}
/**
* Used to summarize all bandwidth usage data