Skip to content

Commit 2318eb4

Browse files
committed
improving paging for ops metrics
1 parent df4daee commit 2318eb4

File tree

3 files changed

+159
-15
lines changed

3 files changed

+159
-15
lines changed

src/services/keystone/batch-service.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,65 @@ export class BatchService {
6161
return result['data'][query].length == 0 ? [] : result['data'][query];
6262
}
6363

64+
public async listAllPages(
65+
query: any,
66+
fields: string[],
67+
where: BatchWhereClause = undefined
68+
) {
69+
const records = [];
70+
71+
const pageSize = 50;
72+
const first = pageSize;
73+
let skip = 0;
74+
let more = true;
75+
76+
logger.debug('[listAllPages] : %s', query);
77+
do {
78+
logger.debug('[listAllPages] : %d', skip);
79+
let queryString;
80+
if (where) {
81+
queryString = `query(${where.query}) {
82+
${query}(where: ${where.clause}, first: ${first}, skip: ${skip}) {
83+
id, ${fields.join(',')}
84+
}
85+
}`;
86+
} else {
87+
queryString = `query {
88+
${query}(first: ${first}, skip: ${skip}) {
89+
id, ${fields.join(',')}
90+
}
91+
}`;
92+
}
93+
logger.debug('[listAllPages] %s', queryString);
94+
95+
const result = await this.context.executeGraphQL({
96+
query: queryString,
97+
variables: where ? where.variables : {},
98+
});
99+
100+
if ('errors' in result) {
101+
logger.error('[listAll] RESULT %j', result);
102+
return null;
103+
}
104+
105+
more = result['data'][query].length > 0;
106+
107+
skip += pageSize;
108+
109+
logger.debug(
110+
'[listAllPages] RESULT COUNT %d',
111+
result['data'][query].length
112+
);
113+
records.push(
114+
result['data'][query].length == 0 ? [] : result['data'][query]
115+
);
116+
} while (more);
117+
118+
logger.info('[listAllPages] TOTAL COUNT %d', records.length);
119+
120+
return records;
121+
}
122+
64123
public async list(
65124
query: any,
66125
refKey: string,

src/services/report/ops-metrics.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -525,21 +525,15 @@ async function getAllConsumers(ctx: any) {
525525
const batch = new BatchService(ctx);
526526

527527
// Limiting to 1000 is not great! We should really recurse until we get to the end!
528-
const allConsumers = await batch.listAll(
529-
'allServiceAccesses',
530-
[
531-
'namespace',
532-
'active',
533-
'consumerType',
534-
'consumer { username }',
535-
'application { name, owner { name }}',
536-
'productEnvironment { namespace, name, flow, product { name, namespace, dataset { title } } }',
537-
'createdAt',
538-
],
539-
undefined,
540-
0,
541-
1000
542-
);
528+
const allConsumers = await batch.listAllPages('allServiceAccesses', [
529+
'namespace',
530+
'active',
531+
'consumerType',
532+
'consumer { username }',
533+
'application { name, owner { name }}',
534+
'productEnvironment { namespace, name, flow, product { name, namespace, dataset { title } } }',
535+
'createdAt',
536+
]);
543537

544538
return allConsumers;
545539
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Wire up directly with Keycloak and use the Services
3+
To run:
4+
npm run ts-build
5+
npm run ts-watch
6+
node dist/test/integrated/batchworker/paging.js
7+
*/
8+
9+
import InitKeystone from '../keystonejs/init';
10+
import {
11+
getRecords,
12+
parseJsonString,
13+
transformAllRefID,
14+
removeEmpty,
15+
removeKeys,
16+
syncRecords,
17+
} from '../../../batch/feed-worker';
18+
import { o } from '../util';
19+
import { BatchService } from '../../../services/keystone/batch-service';
20+
import { newEnvironmentID, newProductID } from '../../../services/identifiers';
21+
22+
(async () => {
23+
const keystone = await InitKeystone();
24+
console.log('K = ' + keystone);
25+
26+
const ns = 'platform';
27+
const skipAccessControl = false;
28+
29+
const identity = {
30+
id: null,
31+
username: 'sample_username',
32+
namespace: ns,
33+
roles: JSON.stringify(['api-owner']),
34+
scopes: [],
35+
userId: null,
36+
} as any;
37+
38+
const ctx = keystone.createContext({
39+
skipAccessControl,
40+
authentication: { item: identity },
41+
});
42+
43+
if (false) {
44+
const json = {
45+
name: 'Refactor Time Test2',
46+
namespace: ns,
47+
environments: [
48+
{
49+
name: 'stage',
50+
appId: '0A021EB0',
51+
//services: [] as any,
52+
//services: ['a-service-for-refactortime'],
53+
// services: ['a-service-for-refactortime', 'a-service-for-aps-moh-proto'],
54+
},
55+
] as any,
56+
};
57+
const res = await syncRecords(ctx, 'Product', null, json);
58+
o(res);
59+
}
60+
if (false) {
61+
for (let i = 0; i < 1000; i++) {
62+
const appId = newProductID();
63+
console.log(appId);
64+
const json = {
65+
name: 'Refactor Time Test 4',
66+
appId: appId,
67+
namespace: ns,
68+
environments: [
69+
{
70+
name: 'stage',
71+
appId: newEnvironmentID(),
72+
services: [] as any,
73+
},
74+
],
75+
};
76+
const res = await syncRecords(ctx, 'Product', appId, json);
77+
o(res);
78+
}
79+
}
80+
81+
const batchService = new BatchService(ctx);
82+
83+
const res = await batchService.listAllPages('allProducts', [
84+
'name',
85+
'namespace',
86+
'dataset { title }',
87+
'createdAt',
88+
]);
89+
//console.log(res);
90+
await keystone.disconnect();
91+
})();

0 commit comments

Comments
 (0)