1
- import { defaults , omit } from "lodash" ;
1
+ import { defaults , omit , range } from "lodash" ;
2
2
import {
3
3
DataFrame ,
4
4
DataQueryRequest ,
@@ -21,6 +21,7 @@ import {
21
21
GCStatsRequestData ,
22
22
GCVariable ,
23
23
GCVariableQuery ,
24
+ Paginator ,
24
25
} from "./types" ;
25
26
import {
26
27
createGetterSample ,
@@ -67,25 +68,15 @@ export class DataSource extends DataSourceApi<GCQuery, GCDataSourceOptions> {
67
68
selector === GCVariable . Resource ||
68
69
selector === GCVariable . Vhost
69
70
) {
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 ( ) ;
81
72
82
73
switch ( selector ) {
83
74
case GCVariable . Vhost :
84
- return getValueVariable ( data . map ( ( item ) => item . cname ) ) ;
75
+ return getValueVariable ( cdnResource . map ( ( item ) => item . cname ) ) ;
85
76
case GCVariable . Resource :
86
- return getValueVariable ( data . map ( ( item ) => item . id ) ) ;
77
+ return getValueVariable ( cdnResource . map ( ( item ) => item . id ) ) ;
87
78
case GCVariable . Client :
88
- return getValueVariable ( data . map ( ( item ) => item . client ) ) ;
79
+ return getValueVariable ( cdnResource . map ( ( item ) => item . client ) ) ;
89
80
}
90
81
} else if ( selector === GCVariable . Region ) {
91
82
return getValueVariable ( regions ) ;
@@ -96,6 +87,48 @@ export class DataSource extends DataSourceApi<GCQuery, GCDataSourceOptions> {
96
87
return [ ] ;
97
88
}
98
89
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
+
99
132
prepareTargets ( targets : GCQuery [ ] ) : GCQuery [ ] {
100
133
return targets . map ( ( query ) => defaults ( query , defaultQuery ) ) ;
101
134
}
0 commit comments