Skip to content

Commit 77e8432

Browse files
authored
feat(native): Cube.py - support contextToAppId/contextToOrchestratorId (#6725)
1 parent 806db35 commit 77e8432

File tree

9 files changed

+152
-151
lines changed

9 files changed

+152
-151
lines changed

packages/cubejs-api-gateway/src/gateway.ts

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ import {
3838
PreAggJob,
3939
PreAggJobStatusItem,
4040
PreAggJobStatusObject,
41-
PreAggJobStatusResponse, SqlApiRequest,
41+
PreAggJobStatusResponse,
42+
SqlApiRequest,
4243
} from './types/request';
4344
import {
4445
CheckAuthInternalOptions,
@@ -139,8 +140,8 @@ class ApiGateway {
139140

140141
public constructor(
141142
protected readonly apiSecret: string,
142-
protected readonly compilerApi: any,
143-
protected readonly adapterApi: any,
143+
protected readonly compilerApi: (ctx: RequestContext) => Promise<any>,
144+
protected readonly adapterApi: (ctx: RequestContext) => Promise<any>,
144145
protected readonly logger: any,
145146
protected readonly options: ApiGatewayOptions,
146147
) {
@@ -214,7 +215,7 @@ class ApiGateway {
214215
},
215216
],
216217
async (req, res) => {
217-
const compilerApi = this.getCompilerApi(req.context);
218+
const compilerApi = await this.getCompilerApi(req.context);
218219
let schema = compilerApi.getGraphQLSchema();
219220
if (!schema) {
220221
let metaConfig = await compilerApi.metaConfig({
@@ -342,15 +343,13 @@ class ApiGateway {
342343
}
343344
},
344345
],
345-
(req: Request, res: Response) => {
346+
async (req: Request, res: Response) => {
346347
const { transformedQuery, references } = req.body;
347-
const canUsePreAggregationForTransformedQuery =
348-
this
349-
.compilerApi(req.context)
350-
.canUsePreAggregationForTransformedQuery(
351-
transformedQuery,
352-
references,
353-
);
348+
const compilerApi = await this.getCompilerApi(req.context as RequestContext);
349+
const canUsePreAggregationForTransformedQuery = compilerApi.canUsePreAggregationForTransformedQuery(
350+
transformedQuery,
351+
references,
352+
);
354353
res.json({ canUsePreAggregationForTransformedQuery });
355354
}
356355
);
@@ -511,7 +510,8 @@ class ApiGateway {
511510

512511
try {
513512
await this.assertApiScope('meta', context.securityContext);
514-
const metaConfig = await this.getCompilerApi(context).metaConfig({
513+
const compilerApi = await this.getCompilerApi(context);
514+
const metaConfig = await compilerApi.metaConfig({
515515
requestId: context.requestId,
516516
});
517517
const cubes = this.filterVisibleItemsInMeta(context, metaConfig).map(cube => cube.config);
@@ -531,7 +531,8 @@ class ApiGateway {
531531

532532
try {
533533
await this.assertApiScope('meta', context.securityContext);
534-
const metaConfigExtended = await this.getCompilerApi(context).metaConfigExtended({
534+
const compilerApi = await this.getCompilerApi(context);
535+
const metaConfigExtended = await compilerApi.metaConfigExtended({
535536
requestId: context.requestId,
536537
});
537538
const { metaConfig, cubeDefinitions } = metaConfigExtended;
@@ -566,7 +567,7 @@ class ApiGateway {
566567
public async getPreAggregations({ cacheOnly, context, res }: { cacheOnly?: boolean, context: RequestContext, res: ResponseResultFn }) {
567568
const requestStarted = new Date();
568569
try {
569-
const compilerApi = this.getCompilerApi(context);
570+
const compilerApi = await this.getCompilerApi(context);
570571
const preAggregations = await compilerApi.preAggregations();
571572

572573
const preAggregationPartitions = await this.refreshScheduler()
@@ -600,8 +601,8 @@ class ApiGateway {
600601
this.parseQueryParam(query),
601602
{ timezones: this.scheduledRefreshTimeZones }
602603
);
603-
const orchestratorApi = this.getAdapterApi(context);
604-
const compilerApi = this.getCompilerApi(context);
604+
const orchestratorApi = await this.getAdapterApi(context);
605+
const compilerApi = await this.getCompilerApi(context);
605606

606607
const preAggregationPartitions = await this.refreshScheduler()
607608
.preAggregationPartitions(
@@ -661,7 +662,7 @@ class ApiGateway {
661662
query = normalizeQueryPreAggregationPreview(this.parseQueryParam(query));
662663
const { preAggregationId, versionEntry, timezone } = query;
663664

664-
const orchestratorApi = this.getAdapterApi(context);
665+
const orchestratorApi = await this.getAdapterApi(context);
665666

666667
const preAggregationPartitions = await this.refreshScheduler()
667668
.preAggregationPartitions(
@@ -863,7 +864,7 @@ class ApiGateway {
863864
context: RequestContext,
864865
selector: PreAggsSelector
865866
): Promise<string[]> {
866-
const compiler = this.getCompilerApi(context);
867+
const compiler = await this.getCompilerApi(context);
867868
const { timezones } = selector;
868869
const preaggs = await compiler.preAggregations({
869870
dataSources: selector.dataSources,
@@ -909,8 +910,8 @@ class ApiGateway {
909910
const promise: Promise<(PreAggJobStatusItem | undefined)[]> = Promise.all(
910911
selector.map(async (selected, i) => {
911912
const ctx = { ...context, ...selected.context };
912-
const orchestrator = this.getAdapterApi(ctx);
913-
const compiler = this.getCompilerApi(ctx);
913+
const orchestrator = await this.getAdapterApi(ctx);
914+
const compiler = await this.getCompilerApi(ctx);
914915
const sel: PreAggsSelector = {
915916
cubes: [selected.preagg.split('.')[0]],
916917
preAggregations: [selected.preagg],
@@ -1068,7 +1069,7 @@ class ApiGateway {
10681069
) {
10691070
const requestStarted = new Date();
10701071
try {
1071-
const orchestratorApi = this.getAdapterApi(context);
1072+
const orchestratorApi = await this.getAdapterApi(context);
10721073
res({
10731074
result: await orchestratorApi.getPreAggregationQueueStates()
10741075
});
@@ -1085,7 +1086,7 @@ class ApiGateway {
10851086
const requestStarted = new Date();
10861087
try {
10871088
const { queryKeys, dataSource } = normalizeQueryCancelPreAggregations(this.parseQueryParam(query));
1088-
const orchestratorApi = this.getAdapterApi(context);
1089+
const orchestratorApi = await this.getAdapterApi(context);
10891090
res({
10901091
result: await orchestratorApi.cancelPreAggregationQueriesFromQueue(queryKeys, dataSource)
10911092
});
@@ -1179,7 +1180,7 @@ class ApiGateway {
11791180
const [queryType, normalizedQueries] = await this.getNormalizedQueries(query, context);
11801181

11811182
const sqlQueries = await Promise.all<any>(
1182-
normalizedQueries.map((normalizedQuery) => this.getCompilerApi(context).getSql(
1183+
normalizedQueries.map(async (normalizedQuery) => (await this.getCompilerApi(context)).getSql(
11831184
this.coerceForSqlQuery({ ...normalizedQuery, memberToAlias }, context),
11841185
{
11851186
includeDebugInfo: getEnv('devMode') || context.signedWithPlaygroundAuthSecret,
@@ -1207,7 +1208,7 @@ class ApiGateway {
12071208
const requestStarted = new Date();
12081209

12091210
try {
1210-
const compilerApi = this.getCompilerApi(context);
1211+
const compilerApi = await this.getCompilerApi(context);
12111212
const query = {
12121213
requestId: context.requestId,
12131214
};
@@ -1293,7 +1294,7 @@ class ApiGateway {
12931294
const [queryType, normalizedQueries] = await this.getNormalizedQueries(query, context);
12941295

12951296
const sqlQueries = await Promise.all<any>(
1296-
normalizedQueries.map((normalizedQuery) => this.getCompilerApi(context).getSql(
1297+
normalizedQueries.map(async (normalizedQuery) => (await this.getCompilerApi(context)).getSql(
12971298
this.coerceForSqlQuery(normalizedQuery, context),
12981299
{
12991300
includeDebugInfo: getEnv('devMode') || context.signedWithPlaygroundAuthSecret
@@ -1330,7 +1331,7 @@ class ApiGateway {
13301331
normalizedQueries.map(
13311332
async (normalizedQuery, index) => {
13321333
const loadRequestSQLStarted = new Date();
1333-
const sqlQuery = await this.getCompilerApi(context)
1334+
const sqlQuery = await (await this.getCompilerApi(context))
13341335
.getSql(
13351336
this.coerceForSqlQuery(normalizedQuery, context)
13361337
);
@@ -1390,8 +1391,7 @@ class ApiGateway {
13901391
}
13911392
const [response, total] = await Promise.all(
13921393
queries.map(async (query) => {
1393-
const res = await this
1394-
.getAdapterApi(context)
1394+
const res = await (await this.getAdapterApi(context))
13951395
.executeQuery(query);
13961396
return res;
13971397
})
@@ -1495,7 +1495,7 @@ class ApiGateway {
14951495
originalQuery: query,
14961496
normalizedQuery: normalizedQueries[0],
14971497
streamingQuery: q,
1498-
stream: await this.getAdapterApi(context).streamQuery(q),
1498+
stream: await (await this.getAdapterApi(context)).streamQuery(q),
14991499
};
15001500
return _stream;
15011501
} catch (err: any) {
@@ -1546,10 +1546,10 @@ class ApiGateway {
15461546
const [queryType, normalizedQueries] =
15471547
await this.getNormalizedQueries(query, context);
15481548

1549-
let metaConfigResult = await this
1550-
.getCompilerApi(context).metaConfig({
1551-
requestId: context.requestId
1552-
});
1549+
let metaConfigResult = await (await this
1550+
.getCompilerApi(context)).metaConfig({
1551+
requestId: context.requestId
1552+
});
15531553

15541554
metaConfigResult = this.filterVisibleItemsInMeta(context, metaConfigResult);
15551555

@@ -1664,10 +1664,10 @@ class ApiGateway {
16641664
const [queryType, normalizedQueries] =
16651665
await this.getNormalizedQueries(query, context);
16661666

1667-
let metaConfigResult = await this
1668-
.getCompilerApi(context).metaConfig({
1669-
requestId: context.requestId
1670-
});
1667+
const compilerApi = await this.getCompilerApi(context);
1668+
let metaConfigResult = await compilerApi.metaConfig({
1669+
requestId: context.requestId
1670+
});
16711671

16721672
metaConfigResult = this.filterVisibleItemsInMeta(context, metaConfigResult);
16731673

@@ -1690,8 +1690,11 @@ class ApiGateway {
16901690
persistent: true,
16911691
forceNoCache: true,
16921692
};
1693+
1694+
const adapterApi = await this.getAdapterApi(context);
1695+
16931696
return {
1694-
stream: await this.getAdapterApi(context).streamQuery(q),
1697+
stream: await adapterApi.streamQuery(q),
16951698
};
16961699
};
16971700

@@ -1711,9 +1714,8 @@ class ApiGateway {
17111714
if (request.streaming) {
17121715
results = [await streamResponse(finalQuery)];
17131716
} else {
1714-
const response = await this
1715-
.getAdapterApi(context)
1716-
.executeQuery(finalQuery);
1717+
const adapterApi = await this.getAdapterApi(context);
1718+
const response = await adapterApi.executeQuery(finalQuery);
17171719

17181720
const annotation = prepareAnnotation(
17191721
metaConfigResult, normalizedQueries[0]
@@ -1791,19 +1793,19 @@ class ApiGateway {
17911793
}
17921794
}
17931795

1794-
public subscribeQueueEvents({ context, signedWithPlaygroundAuthSecret, connectionId, res }) {
1796+
public async subscribeQueueEvents({ context, signedWithPlaygroundAuthSecret, connectionId, res }) {
17951797
if (this.enforceSecurityChecks && !signedWithPlaygroundAuthSecret) {
17961798
throw new CubejsHandlerError(
17971799
403,
17981800
'Forbidden',
17991801
'Only for signed with playground auth secret'
18001802
);
18011803
}
1802-
return this.getAdapterApi(context).subscribeQueueEvents(connectionId, res);
1804+
return (await this.getAdapterApi(context)).subscribeQueueEvents(connectionId, res);
18031805
}
18041806

1805-
public unSubscribeQueueEvents({ context, connectionId }) {
1806-
return this.getAdapterApi(context).unSubscribeQueueEvents(connectionId);
1807+
public async unSubscribeQueueEvents({ context, connectionId }) {
1808+
return (await this.getAdapterApi(context)).unSubscribeQueueEvents(connectionId);
18071809
}
18081810

18091811
public async subscribe({
@@ -1866,20 +1868,12 @@ class ApiGateway {
18661868
return query as Query | Query[];
18671869
}
18681870

1869-
protected getCompilerApi(context) {
1870-
if (typeof this.compilerApi === 'function') {
1871-
return this.compilerApi(context);
1872-
}
1873-
1874-
return this.compilerApi;
1871+
protected async getCompilerApi(context: RequestContext) {
1872+
return this.compilerApi(context);
18751873
}
18761874

1877-
protected getAdapterApi(context) {
1878-
if (typeof this.adapterApi === 'function') {
1879-
return this.adapterApi(context);
1880-
}
1881-
1882-
return this.adapterApi;
1875+
protected async getAdapterApi(context: RequestContext) {
1876+
return this.adapterApi(context);
18831877
}
18841878

18851879
public async contextByReq(req: Request, securityContext, requestId: string): Promise<ExtendedRequestContext> {
@@ -2386,7 +2380,7 @@ class ApiGateway {
23862380
let health: 'HEALTH' | 'DOWN' = 'HEALTH';
23872381

23882382
if (this.standalone) {
2389-
const orchestratorApi = await this.adapterApi({});
2383+
const orchestratorApi = await this.adapterApi({} as any);
23902384

23912385
try {
23922386
// todo: test other data sources

0 commit comments

Comments
 (0)