web/satellite: usage data converting implemented (#3673)
This commit is contained in:
parent
28a66747c9
commit
97fa000ca9
@ -36,6 +36,12 @@ export class ProjectUsageApiGql extends BaseGql implements UsageApi {
|
||||
|
||||
const response = await this.query(query, variables);
|
||||
|
||||
return response.data.project.usage;
|
||||
return this.fromJson(response);
|
||||
}
|
||||
|
||||
private fromJson(response: any): ProjectUsage {
|
||||
const usage = response.data.project.usage;
|
||||
|
||||
return new ProjectUsage(usage.storage, usage.egress, usage.objectCount, usage.since, usage.before);
|
||||
}
|
||||
}
|
||||
|
@ -29,11 +29,11 @@
|
||||
<div class="usage-report-container__main-area">
|
||||
<div class="usage-report-container__main-area__info-area">
|
||||
<div class="usage-report-container__main-area__info-area__item">
|
||||
<h1 class="usage-report-container__main-area__info-area__item__title">Storage, GBh</h1>
|
||||
<h1 class="usage-report-container__main-area__info-area__item__title">Storage, {{storageDataDimension}}*h</h1>
|
||||
<h2 class="usage-report-container__main-area__info-area__item__amount">{{storage}}</h2>
|
||||
</div>
|
||||
<div class="usage-report-container__main-area__info-area__item">
|
||||
<h1 class="usage-report-container__main-area__info-area__item__title">Egress, GB</h1>
|
||||
<h1 class="usage-report-container__main-area__info-area__item__title">Egress, {{egressDataDimension}}</h1>
|
||||
<h2 class="usage-report-container__main-area__info-area__item__amount">{{egress}}</h2>
|
||||
</div>
|
||||
<div class="usage-report-container__main-area__info-area__item">
|
||||
@ -104,20 +104,26 @@ export default class UsageReport extends Vue {
|
||||
return DateFormat.getUSDate(this.$store.state.usageModule.endDate, '/');
|
||||
}
|
||||
|
||||
// TODO: update bytes to GB
|
||||
public get storage(): string {
|
||||
return this.$store.state.usageModule.projectUsage.storage.toPrecision(5);
|
||||
return this.$store.state.usageModule.projectUsage.storage.formattedBytes;
|
||||
}
|
||||
|
||||
// TODO: update bytes to GB
|
||||
public get egress(): string {
|
||||
return this.$store.state.usageModule.projectUsage.egress.toPrecision(5);
|
||||
return this.$store.state.usageModule.projectUsage.egress.formattedBytes;
|
||||
}
|
||||
|
||||
public get objectsCount(): string {
|
||||
return this.$store.state.usageModule.projectUsage.objectCount.toPrecision(5);
|
||||
}
|
||||
|
||||
public get storageDataDimension(): string {
|
||||
return this.$store.state.usageModule.projectUsage.storage.label;
|
||||
}
|
||||
|
||||
public get egressDataDimension(): string {
|
||||
return this.$store.state.usageModule.projectUsage.egress.label;
|
||||
}
|
||||
|
||||
public async mounted(): Promise<void> {
|
||||
try {
|
||||
await this.$store.dispatch(PROJECT_USAGE_ACTIONS.FETCH_CURRENT_ROLLUP);
|
||||
|
@ -1,17 +1,19 @@
|
||||
// Copyright (C) 2019 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
import { Size } from '@/utils/bytesSize';
|
||||
|
||||
// ProjectUsage sums usage for given period.
|
||||
export class ProjectUsage {
|
||||
public storage: number;
|
||||
public egress: number;
|
||||
public storage: Size;
|
||||
public egress: Size;
|
||||
public objectCount: number;
|
||||
public since: Date;
|
||||
public before: Date;
|
||||
|
||||
public constructor(storage: number, egress: number, objectCount: number, since: Date, before: Date) {
|
||||
this.storage = storage;
|
||||
this.egress = egress;
|
||||
this.storage = new Size(storage);
|
||||
this.egress = new Size(egress);
|
||||
this.objectCount = objectCount;
|
||||
this.since = since;
|
||||
this.before = before;
|
||||
|
58
web/satellite/src/utils/bytesSize.ts
Normal file
58
web/satellite/src/utils/bytesSize.ts
Normal file
@ -0,0 +1,58 @@
|
||||
// Copyright (C) 2019 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
enum Memory {
|
||||
Bytes = 1e0,
|
||||
KB = 1e3,
|
||||
MB = 1e6,
|
||||
GB = 1e9,
|
||||
TB = 1e12,
|
||||
PB = 1e15,
|
||||
}
|
||||
|
||||
enum Dimensions {
|
||||
Bytes = 'Bytes',
|
||||
KB = 'KB',
|
||||
MB = 'MB',
|
||||
GB = 'GB',
|
||||
TB = 'TB',
|
||||
PB = 'PB',
|
||||
}
|
||||
|
||||
export class Size {
|
||||
private readonly precision: number = 4;
|
||||
public readonly bytes: number;
|
||||
public readonly formattedBytes: string;
|
||||
public readonly label: Dimensions;
|
||||
|
||||
public constructor(bytes: number) {
|
||||
const _bytes = Math.ceil(bytes);
|
||||
this.bytes = bytes;
|
||||
|
||||
switch (true) {
|
||||
case _bytes === 0:
|
||||
this.formattedBytes = (bytes / Memory.Bytes).toFixed(this.precision);
|
||||
this.label = Dimensions.Bytes;
|
||||
break;
|
||||
case _bytes < Memory.MB:
|
||||
this.formattedBytes = (bytes / Memory.KB).toFixed(this.precision);
|
||||
this.label = Dimensions.KB;
|
||||
break;
|
||||
case _bytes < Memory.GB:
|
||||
this.formattedBytes = (bytes / Memory.MB).toFixed(this.precision);
|
||||
this.label = Dimensions.MB;
|
||||
break;
|
||||
case _bytes < Memory.TB:
|
||||
this.formattedBytes = (bytes / Memory.GB).toFixed(this.precision);
|
||||
this.label = Dimensions.GB;
|
||||
break;
|
||||
case _bytes < Memory.PB:
|
||||
this.formattedBytes = (bytes / Memory.TB).toFixed(this.precision);
|
||||
this.label = Dimensions.TB;
|
||||
break;
|
||||
default:
|
||||
this.formattedBytes = (bytes / Memory.PB).toFixed(this.precision);
|
||||
this.label = Dimensions.PB;
|
||||
}
|
||||
}
|
||||
}
|
@ -42,8 +42,12 @@ describe('mutations', () => {
|
||||
it('fetch project usage', () => {
|
||||
store.commit(PROJECT_USAGE_MUTATIONS.SET_PROJECT_USAGE, testUsage);
|
||||
|
||||
expect(state.projectUsage.storage).toBe(2);
|
||||
expect(state.projectUsage.egress).toBe(3);
|
||||
expect(state.projectUsage.storage.bytes).toBe(2);
|
||||
expect(state.projectUsage.egress.bytes).toBe(3);
|
||||
expect(state.projectUsage.storage.formattedBytes).toBe('0.0020');
|
||||
expect(state.projectUsage.egress.formattedBytes).toBe('0.0030');
|
||||
expect(state.projectUsage.storage.label).toBe('KB');
|
||||
expect(state.projectUsage.egress.label).toBe('KB');
|
||||
expect(state.projectUsage.objectCount).toBe(4);
|
||||
expect(state.startDate.toDateString()).toBe(now.toDateString());
|
||||
expect(state.endDate.toDateString()).toBe(now.toDateString());
|
||||
@ -60,8 +64,12 @@ describe('mutations', () => {
|
||||
it('clear usage', () => {
|
||||
store.commit(PROJECT_USAGE_MUTATIONS.CLEAR);
|
||||
|
||||
expect(state.projectUsage.storage).toBe(0);
|
||||
expect(state.projectUsage.egress).toBe(0);
|
||||
expect(state.projectUsage.storage.bytes).toBe(0);
|
||||
expect(state.projectUsage.egress.bytes).toBe(0);
|
||||
expect(state.projectUsage.storage.formattedBytes).toBe('0.0000');
|
||||
expect(state.projectUsage.egress.formattedBytes).toBe('0.0000');
|
||||
expect(state.projectUsage.storage.label).toBe('Bytes');
|
||||
expect(state.projectUsage.egress.label).toBe('Bytes');
|
||||
expect(state.projectUsage.objectCount).toBe(0);
|
||||
expect(state.startDate.toDateString()).toBe(now.toDateString());
|
||||
expect(state.endDate.toDateString()).toBe(now.toDateString());
|
||||
@ -82,8 +90,12 @@ describe('actions', () => {
|
||||
|
||||
await store.dispatch(PROJECT_USAGE_ACTIONS.FETCH, dateRange);
|
||||
|
||||
expect(state.projectUsage.storage).toBe(2);
|
||||
expect(state.projectUsage.egress).toBe(3);
|
||||
expect(state.projectUsage.storage.bytes).toBe(2);
|
||||
expect(state.projectUsage.egress.bytes).toBe(3);
|
||||
expect(state.projectUsage.storage.formattedBytes).toBe('0.0020');
|
||||
expect(state.projectUsage.egress.formattedBytes).toBe('0.0030');
|
||||
expect(state.projectUsage.storage.label).toBe('KB');
|
||||
expect(state.projectUsage.egress.label).toBe('KB');
|
||||
expect(state.projectUsage.objectCount).toBe(4);
|
||||
expect(state.startDate.toDateString()).toBe(testDate1.toDateString());
|
||||
expect(state.endDate.toDateString()).toBe(testDate2.toDateString());
|
||||
@ -99,8 +111,12 @@ describe('actions', () => {
|
||||
|
||||
await store.dispatch(PROJECT_USAGE_ACTIONS.FETCH_CURRENT_ROLLUP);
|
||||
|
||||
expect(state.projectUsage.storage).toBe(2);
|
||||
expect(state.projectUsage.egress).toBe(3);
|
||||
expect(state.projectUsage.storage.bytes).toBe(2);
|
||||
expect(state.projectUsage.egress.bytes).toBe(3);
|
||||
expect(state.projectUsage.storage.formattedBytes).toBe('0.0020');
|
||||
expect(state.projectUsage.egress.formattedBytes).toBe('0.0030');
|
||||
expect(state.projectUsage.storage.label).toBe('KB');
|
||||
expect(state.projectUsage.egress.label).toBe('KB');
|
||||
expect(state.projectUsage.objectCount).toBe(4);
|
||||
expect(state.startDate.toDateString()).toBe(firstDate.toDateString());
|
||||
expect(state.endDate.toDateString()).toBe(now.toDateString());
|
||||
@ -118,8 +134,12 @@ describe('actions', () => {
|
||||
|
||||
await store.dispatch(PROJECT_USAGE_ACTIONS.FETCH_PREVIOUS_ROLLUP);
|
||||
|
||||
expect(state.projectUsage.storage).toBe(2);
|
||||
expect(state.projectUsage.egress).toBe(3);
|
||||
expect(state.projectUsage.storage.bytes).toBe(2);
|
||||
expect(state.projectUsage.egress.bytes).toBe(3);
|
||||
expect(state.projectUsage.storage.formattedBytes).toBe('0.0020');
|
||||
expect(state.projectUsage.egress.formattedBytes).toBe('0.0030');
|
||||
expect(state.projectUsage.storage.label).toBe('KB');
|
||||
expect(state.projectUsage.egress.label).toBe('KB');
|
||||
expect(state.projectUsage.objectCount).toBe(4);
|
||||
expect(state.startDate.toDateString()).toBe(firstDate.toDateString());
|
||||
expect(state.endDate.toDateString()).toBe(secondDate.toDateString());
|
||||
@ -128,8 +148,12 @@ describe('actions', () => {
|
||||
it('success clear usage', async () => {
|
||||
await store.dispatch(PROJECT_USAGE_ACTIONS.CLEAR);
|
||||
|
||||
expect(state.projectUsage.storage).toBe(0);
|
||||
expect(state.projectUsage.egress).toBe(0);
|
||||
expect(state.projectUsage.storage.bytes).toBe(0);
|
||||
expect(state.projectUsage.egress.bytes).toBe(0);
|
||||
expect(state.projectUsage.storage.formattedBytes).toBe('0.0000');
|
||||
expect(state.projectUsage.egress.formattedBytes).toBe('0.0000');
|
||||
expect(state.projectUsage.storage.label).toBe('Bytes');
|
||||
expect(state.projectUsage.egress.label).toBe('Bytes');
|
||||
expect(state.projectUsage.objectCount).toBe(0);
|
||||
expect(state.startDate.toDateString()).toBe(now.toDateString());
|
||||
expect(state.endDate.toDateString()).toBe(now.toDateString());
|
||||
@ -143,8 +167,12 @@ describe('actions', () => {
|
||||
await store.dispatch(PROJECT_USAGE_ACTIONS.FETCH_PREVIOUS_ROLLUP);
|
||||
expect(true).toBe(false);
|
||||
} catch (error) {
|
||||
expect(state.projectUsage.storage).toBe(0);
|
||||
expect(state.projectUsage.egress).toBe(0);
|
||||
expect(state.projectUsage.storage.bytes).toBe(0);
|
||||
expect(state.projectUsage.egress.bytes).toBe(0);
|
||||
expect(state.projectUsage.storage.formattedBytes).toBe('0.0000');
|
||||
expect(state.projectUsage.egress.formattedBytes).toBe('0.0000');
|
||||
expect(state.projectUsage.storage.label).toBe('Bytes');
|
||||
expect(state.projectUsage.egress.label).toBe('Bytes');
|
||||
expect(state.projectUsage.objectCount).toBe(0);
|
||||
expect(state.startDate.toDateString()).toBe(now.toDateString());
|
||||
expect(state.endDate.toDateString()).toBe(now.toDateString());
|
||||
|
Loading…
Reference in New Issue
Block a user