storj/web/satellite/src/api/usage.ts

46 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-04-05 12:24:34 +01:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
import apollo from '@/utils/apollo';
2019-04-05 12:24:34 +01:00
import gql from 'graphql-tag';
import { RequestResponse } from '@/types/response';
2019-04-05 12:24:34 +01:00
// fetchProjectUsage retrieves total project usage for a given period
2019-08-05 16:43:52 +01:00
export async function fetchProjectUsage(projectId: string, since: Date, before: Date): Promise<RequestResponse<ProjectUsage>> {
let result: RequestResponse<ProjectUsage> = new RequestResponse<ProjectUsage>();
2019-04-05 12:24:34 +01:00
let response: any = await apollo.query(
{
query: gql(`
2019-08-05 16:43:52 +01:00
query($projectId: String!, $since: DateTime!, $before: DateTime!) {
project(id: $projectId) {
usage(since: $since, before: $before) {
2019-04-05 12:24:34 +01:00
storage,
egress,
objectCount,
2019-04-05 12:24:34 +01:00
since,
before
}
}
}`
),
2019-08-05 16:43:52 +01:00
variables: {
projectId: projectId,
since: since.toISOString(),
before: before.toISOString()
},
2019-04-05 12:24:34 +01:00
fetchPolicy: 'no-cache',
errorPolicy: 'all'
}
);
if (response.errors) {
result.errorMessage = response.errors[0].message;
} else {
result.isSuccess = true;
result.data = response.data.project.usage;
}
return result;
}