Skip to content

Commit 0127e49

Browse files
committed
feat(server-core): Support for scheduledRefreshTimeZones as function, passing securityContext
1 parent 4613628 commit 0127e49

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ class ApiGateway {
456456

457457
app.get('/cubejs-system/v1/pre-aggregations/timezones', systemMiddlewares, systemAsyncHandler(async (req, res) => {
458458
this.resToResultFn(res)({
459-
timezones: this.scheduledRefreshTimeZones || []
459+
timezones: this.scheduledRefreshTimeZones ? this.scheduledRefreshTimeZones(req.context) : []
460460
});
461461
}));
462462

@@ -630,7 +630,7 @@ class ApiGateway {
630630
context,
631631
normalizeQueryPreAggregations(
632632
{
633-
timezones: this.scheduledRefreshTimeZones,
633+
timezones: this.scheduledRefreshTimeZones ? this.scheduledRefreshTimeZones(context) : [],
634634
preAggregations: preAggregations.map(p => ({
635635
id: p.id,
636636
cacheOnly,
@@ -654,7 +654,7 @@ class ApiGateway {
654654
try {
655655
query = normalizeQueryPreAggregations(
656656
this.parseQueryParam(query),
657-
{ timezones: this.scheduledRefreshTimeZones }
657+
{ timezones: this.scheduledRefreshTimeZones ? this.scheduledRefreshTimeZones(context) : [] }
658658
);
659659
const orchestratorApi = await this.getAdapterApi(context);
660660
const compilerApi = await this.getCompilerApi(context);

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,24 @@ type UserBackgroundContext = {
3434
authInfo?: any;
3535
};
3636

37+
type RequestContext = {
38+
// @deprecated Renamed to securityContext, please use securityContext.
39+
authInfo?: any;
40+
securityContext: any;
41+
requestId: string;
42+
};
43+
3744
/**
38-
* Function that should provides a logic of scheduled returning of
45+
* Function that should provide a logic of scheduled returning of
3946
* the user background context. Used as a part of a main
4047
* configuration object of the Gateway to provide extendability to
4148
* this logic.
4249
*/
4350
type ScheduledRefreshContextsFn =
4451
() => Promise<UserBackgroundContext[]>;
4552

53+
type ScheduledRefreshTimeZonesFn = (context: RequestContext) => string[] | Promise<string[]>;
54+
4655
/**
4756
* Gateway configuration options interface.
4857
*/
@@ -52,7 +61,7 @@ interface ApiGatewayOptions {
5261
dataSourceStorage: any;
5362
refreshScheduler: any;
5463
scheduledRefreshContexts?: ScheduledRefreshContextsFn;
55-
scheduledRefreshTimeZones?: String[];
64+
scheduledRefreshTimeZones?: ScheduledRefreshTimeZonesFn;
5665
basePath: string;
5766
extendContext?: ExtendContextFn;
5867
jwt?: JWTOptions;

packages/cubejs-api-gateway/test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ describe('API Gateway', () => {
849849
playgroundAuthSecret,
850850
refreshScheduler: () => new RefreshSchedulerMock(),
851851
scheduledRefreshContexts: () => Promise.resolve(scheduledRefreshContextsFactory()),
852-
scheduledRefreshTimeZones: scheduledRefreshTimeZonesFactory()
852+
scheduledRefreshTimeZones: scheduledRefreshTimeZonesFactory
853853
}
854854
);
855855
const token = generateAuthToken({ uid: 5, scope }, {}, playgroundAuthSecret);

packages/cubejs-server-core/src/core/server.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,23 @@ import LRUCache from 'lru-cache';
55
import isDocker from 'is-docker';
66
import pLimit from 'p-limit';
77

8-
import { ApiGateway, ApiGatewayOptions, UserBackgroundContext } from '@cubejs-backend/api-gateway';
8+
import {
9+
ApiGateway,
10+
ApiGatewayOptions,
11+
UserBackgroundContext
12+
} from '@cubejs-backend/api-gateway';
913
import {
1014
CancelableInterval,
11-
createCancelableInterval, formatDuration, getAnonymousId,
12-
getEnv, assertDataSource, getRealType, internalExceptions, track, FileRepository, SchemaFileRepository,
15+
createCancelableInterval,
16+
formatDuration,
17+
getAnonymousId,
18+
getEnv,
19+
assertDataSource,
20+
getRealType,
21+
internalExceptions,
22+
track,
23+
FileRepository,
24+
SchemaFileRepository,
1325
} from '@cubejs-backend/shared';
1426

1527
import type { Application as ExpressApplication } from 'express';
@@ -46,8 +58,15 @@ import type {
4658
DriverContext,
4759
LoggerFn,
4860
DriverConfig,
61+
ScheduledRefreshTimeZonesFn,
62+
} from './types';
63+
import {
64+
ContextToOrchestratorIdFn,
65+
ContextAcceptanceResult,
66+
ContextAcceptanceResultHttp,
67+
ContextAcceptanceResultWs,
68+
ContextAcceptor
4969
} from './types';
50-
import { ContextToOrchestratorIdFn, ContextAcceptanceResult, ContextAcceptanceResultHttp, ContextAcceptanceResultWs, ContextAcceptor } from './types';
5170

5271
const { version } = require('../../../package.json');
5372

@@ -119,6 +138,8 @@ export class CubejsServerCore {
119138

120139
protected readonly preAggregationsSchema: PreAggregationsSchemaFn;
121140

141+
protected readonly scheduledRefreshTimeZones: ScheduledRefreshTimeZonesFn;
142+
122143
protected readonly orchestratorOptions: OrchestratorOptionsFn;
123144

124145
public logger: LoggerFn;
@@ -173,6 +194,7 @@ export class CubejsServerCore {
173194
this.contextToExternalDbType = wrapToFnIfNeeded(this.options.externalDbType);
174195
this.preAggregationsSchema = wrapToFnIfNeeded(this.options.preAggregationsSchema);
175196
this.orchestratorOptions = wrapToFnIfNeeded(this.options.orchestratorOptions);
197+
this.scheduledRefreshTimeZones = wrapToFnIfNeeded(this.options.scheduledRefreshTimeZones || []);
176198

177199
this.compilerCache = new LRUCache<string, CompilerApi>({
178200
max: this.options.compilerCacheSize || 250,
@@ -453,7 +475,7 @@ export class CubejsServerCore {
453475
jwt: this.options.jwt,
454476
refreshScheduler: this.getRefreshScheduler.bind(this),
455477
scheduledRefreshContexts: this.options.scheduledRefreshContexts,
456-
scheduledRefreshTimeZones: this.options.scheduledRefreshTimeZones,
478+
scheduledRefreshTimeZones: this.scheduledRefreshTimeZones,
457479
serverCoreVersion: this.coreServerVersion,
458480
contextToApiScopes: this.options.contextToApiScopes,
459481
gatewayPort: this.options.gatewayPort,
@@ -700,7 +722,7 @@ export class CubejsServerCore {
700722
}
701723

702724
/**
703-
* @internal Please dont use this method directly, use refreshTimer
725+
* @internal Please don't use this method directly, use refreshTimer
704726
*/
705727
public handleScheduledRefreshInterval = async (options) => {
706728
const allContexts = await this.options.scheduledRefreshContexts();
@@ -730,7 +752,10 @@ export class CubejsServerCore {
730752
concurrency: this.options.scheduledRefreshConcurrency,
731753
};
732754

733-
if (this.options.scheduledRefreshTimeZones) {
755+
const timezonesFromSecurityContext = await this.scheduledRefreshTimeZones(context);
756+
if (timezonesFromSecurityContext.length > 0) {
757+
queryingOptions.timezones = timezonesFromSecurityContext;
758+
} else if (this.options.scheduledRefreshTimeZones) {
734759
queryingOptions.timezones = this.options.scheduledRefreshTimeZones;
735760
}
736761

@@ -746,7 +771,7 @@ export class CubejsServerCore {
746771
}
747772

748773
/**
749-
* @internal Please dont use this method directly, use refreshTimer
774+
* @internal Please don't use this method directly, use refreshTimer
750775
*/
751776
public async runScheduledRefresh(context: UserBackgroundContext | null, queryingOptions?: ScheduledRefreshOptions) {
752777
return this.getRefreshScheduler().runScheduledRefresh(

packages/cubejs-server-core/src/core/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ export type OrchestratorOptionsFn = (context: RequestContext) => OrchestratorOpt
128128

129129
export type PreAggregationsSchemaFn = (context: RequestContext) => string | Promise<string>;
130130

131+
export type ScheduledRefreshTimeZonesFn = (context: RequestContext) => string[] | Promise<string[]>;
132+
133+
/**
134+
* Function that should provide a logic of scheduled returning of
135+
* the user background context. Used as a part of a main
136+
* configuration object of the Gateway to provide extendability to
137+
* this logic.
138+
*/
139+
export type ScheduledRefreshContextsFn = () => Promise<UserBackgroundContext[]>;
140+
131141
// internal
132142
export type DriverOptions = {
133143
dataSource?: string,
@@ -194,7 +204,7 @@ export interface CreateOptions {
194204
schemaVersion?: (context: RequestContext) => string | Promise<string>;
195205
extendContext?: ExtendContextFn;
196206
scheduledRefreshTimer?: boolean | number;
197-
scheduledRefreshTimeZones?: string[];
207+
scheduledRefreshTimeZones?: string[] | ScheduledRefreshTimeZonesFn;
198208
scheduledRefreshContexts?: () => Promise<UserBackgroundContext[]>;
199209
scheduledRefreshConcurrency?: number;
200210
scheduledRefreshBatchSize?: number;

0 commit comments

Comments
 (0)