From 384bdf1f5870924320118c1ab48243f7e44c3d82 Mon Sep 17 00:00:00 2001 From: NikolaiYurchenko Date: Fri, 31 Jan 2020 18:55:37 +0200 Subject: [PATCH] web/storagenode: code refactoring pt.1 Change-Id: I948261c61e7ed7f9703a85314d7a14ca9a59b16d --- web/storagenode/src/app/router/index.ts | 7 +- web/storagenode/src/app/store/index.ts | 10 +-- web/storagenode/src/app/store/modules/node.ts | 72 ++++++++------- web/storagenode/src/app/types/navigation.ts | 16 +--- web/storagenode/src/app/utils/chart.ts | 16 ++-- web/storagenode/src/app/utils/converter.ts | 2 +- .../src/app/views/DashboardArea.vue | 10 ++- web/storagenode/src/main.ts | 4 +- .../src/storagenode/api/storagenode.ts | 87 ++++++++++--------- web/storagenode/src/storagenode/dashboard.ts | 9 +- web/storagenode/src/storagenode/satellite.ts | 15 ++-- 11 files changed, 133 insertions(+), 115 deletions(-) diff --git a/web/storagenode/src/app/router/index.ts b/web/storagenode/src/app/router/index.ts index 8b6e5d16b..8e9b145ef 100644 --- a/web/storagenode/src/app/router/index.ts +++ b/web/storagenode/src/app/router/index.ts @@ -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; diff --git a/web/storagenode/src/app/store/index.ts b/web/storagenode/src/app/store/index.ts index d55ff2cdb..4fecea1cb 100644 --- a/web/storagenode/src/app/store/index.ts +++ b/web/storagenode/src/app/store/index.ts @@ -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; diff --git a/web/storagenode/src/app/store/modules/node.ts b/web/storagenode/src/app/store/modules/node.ts index 44646a542..58e8de1bc 100644 --- a/web/storagenode/src/app/store/modules/node.ts +++ b/web/storagenode/src/app/store/modules/node.ts @@ -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 { + [GET_NODE_INFO]: async function ({commit}: any): Promise { const response = await snoAPI.dashboard(); commit(NODE_MUTATIONS.POPULATE_STORE, response); }, - [NODE_ACTIONS.SELECT_SATELLITE]: async function ({commit}, id: any): Promise { - const response = id ? await snoAPI.satellite(id) : await snoAPI.satellites(); + [NODE_ACTIONS.SELECT_SATELLITE]: async function ({commit}, id?: string): Promise { + 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; } diff --git a/web/storagenode/src/app/types/navigation.ts b/web/storagenode/src/app/types/navigation.ts index 5bfb0fe44..a6243a48f 100644 --- a/web/storagenode/src/app/types/navigation.ts +++ b/web/storagenode/src/app/types/navigation.ts @@ -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; } } diff --git a/web/storagenode/src/app/utils/chart.ts b/web/storagenode/src/app/utils/chart.ts index 30a361c77..c2a6786ed 100644 --- a/web/storagenode/src/app/utils/chart.ts +++ b/web/storagenode/src/app/utils/chart.ts @@ -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 */ diff --git a/web/storagenode/src/app/utils/converter.ts b/web/storagenode/src/app/utils/converter.ts index 96f677c2d..ee086d0a5 100644 --- a/web/storagenode/src/app/utils/converter.ts +++ b/web/storagenode/src/app/utils/converter.ts @@ -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 */ diff --git a/web/storagenode/src/app/views/DashboardArea.vue b/web/storagenode/src/app/views/DashboardArea.vue index 24239aa4b..f9c6d6d92 100644 --- a/web/storagenode/src/app/views/DashboardArea.vue +++ b/web/storagenode/src/app/views/DashboardArea.vue @@ -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); + } } } diff --git a/web/storagenode/src/main.ts b/web/storagenode/src/main.ts index e594a1bee..8c3258820 100644 --- a/web/storagenode/src/main.ts +++ b/web/storagenode/src/main.ts @@ -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; diff --git a/web/storagenode/src/storagenode/api/storagenode.ts b/web/storagenode/src/storagenode/api/storagenode.ts index f725825de..8d071d151 100644 --- a/web/storagenode/src/storagenode/api/storagenode.ts +++ b/web/storagenode/src/storagenode/api/storagenode.ts @@ -40,7 +40,7 @@ export class SNOApi { public async dashboard(): Promise { 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 { - 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 { 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); } } diff --git a/web/storagenode/src/storagenode/dashboard.ts b/web/storagenode/src/storagenode/dashboard.ts index cfeb2b256..b15e886c7 100644 --- a/web/storagenode/src/storagenode/dashboard.ts +++ b/web/storagenode/src/storagenode/dashboard.ts @@ -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; } } diff --git a/web/storagenode/src/storagenode/satellite.ts b/web/storagenode/src/storagenode/satellite.ts index 9cdc5835e..55cc39779 100644 --- a/web/storagenode/src/storagenode/satellite.ts +++ b/web/storagenode/src/storagenode/satellite.ts @@ -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