Skip to content

Commit 1fb66af

Browse files
authored
Merge pull request #6 from G-Core/get-all-cdn-resources
Get all CDN resources by a paginator
2 parents d0f52b3 + 759cf4d commit 1fb66af

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

src/datasource.ts

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaults, omit } from "lodash";
1+
import { defaults, omit, range } from "lodash";
22
import {
33
DataFrame,
44
DataQueryRequest,
@@ -21,6 +21,7 @@ import {
2121
GCStatsRequestData,
2222
GCVariable,
2323
GCVariableQuery,
24+
Paginator,
2425
} from "./types";
2526
import {
2627
createGetterSample,
@@ -67,25 +68,15 @@ export class DataSource extends DataSourceApi<GCQuery, GCDataSourceOptions> {
6768
selector === GCVariable.Resource ||
6869
selector === GCVariable.Vhost
6970
) {
70-
const {
71-
data,
72-
}: {
73-
data: GCCdnResource[];
74-
} = await getBackendSrv().datasourceRequest({
75-
method: "GET",
76-
url: `${this.url}/resources`,
77-
responseType: "json",
78-
showErrorAlert: true,
79-
params: { fields: "id,cname,client", deleted: true },
80-
});
71+
const cdnResource: GCCdnResource[] = await this.getAllGCCdnResources();
8172

8273
switch (selector) {
8374
case GCVariable.Vhost:
84-
return getValueVariable(data.map((item) => item.cname));
75+
return getValueVariable(cdnResource.map((item) => item.cname));
8576
case GCVariable.Resource:
86-
return getValueVariable(data.map((item) => item.id));
77+
return getValueVariable(cdnResource.map((item) => item.id));
8778
case GCVariable.Client:
88-
return getValueVariable(data.map((item) => item.client));
79+
return getValueVariable(cdnResource.map((item) => item.client));
8980
}
9081
} else if (selector === GCVariable.Region) {
9182
return getValueVariable(regions);
@@ -96,6 +87,48 @@ export class DataSource extends DataSourceApi<GCQuery, GCDataSourceOptions> {
9687
return [];
9788
}
9889

90+
private async getAllGCCdnResources(): Promise<GCCdnResource[]> {
91+
const getGCCdnResources = (
92+
limit: number,
93+
offset = 0
94+
): Promise<{ data: Paginator<GCCdnResource> }> =>
95+
getBackendSrv().datasourceRequest({
96+
method: "GET",
97+
url: `${this.url}/resources`,
98+
responseType: "json",
99+
showErrorAlert: true,
100+
params: {
101+
fields: "id,cname,client",
102+
deleted: true,
103+
limit,
104+
offset,
105+
},
106+
});
107+
108+
const limit = 1000;
109+
110+
const firstChunk = await getGCCdnResources(limit);
111+
112+
const cdnResourcesCount = firstChunk.data.count;
113+
114+
if (cdnResourcesCount <= limit) {
115+
return firstChunk.data.results;
116+
} else {
117+
const restChunkRequests = range(
118+
limit,
119+
cdnResourcesCount,
120+
limit
121+
).map((offset) => getGCCdnResources(limit, offset));
122+
123+
const restChunks = await Promise.all(restChunkRequests);
124+
125+
return restChunks.reduce(
126+
(acc, current) => acc.concat(current.data.results),
127+
firstChunk.data.results
128+
);
129+
}
130+
}
131+
99132
prepareTargets(targets: GCQuery[]): GCQuery[] {
100133
return targets.map((query) => defaults(query, defaultQuery));
101134
}

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,10 @@ export interface GCResponseStats {
122122
dc?: string;
123123
resource?: number;
124124
}
125+
126+
export interface Paginator<T> {
127+
count: number;
128+
next: string;
129+
previous: string;
130+
results: T[];
131+
}

0 commit comments

Comments
 (0)