Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace convertConnectionType with another function in registry(QL) #1493

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {getAppEndpointsConfig} from '../shared/endpoints';
import {appEnv} from './app-env';
import {getOpensourceLayoutConfig} from './components/layout/opensource-layout-config';
import authZitadel from './middlewares/auth-zitadel';
import {convertConnectionType} from './modes/charts/plugins/ql/utils/connection';
import {getConnectorToQlConnectionTypeMap} from './modes/charts/plugins/ql/utils/connection';
import initOpensourceApp from './modes/opensource/app';
import {nodekit} from './nodekit';
import {registry} from './registry';
import {registerAppPlugins} from './registry/utils/register-app-plugins';

registry.registerGetLayoutConfig(getOpensourceLayoutConfig);
registry.registerConvertConnectorTypeToQLConnectionType(convertConnectionType);
registry.setupQLConnectionTypeMap(getConnectorToQlConnectionTypeMap());

registerAppPlugins();

Expand Down
6 changes: 5 additions & 1 deletion src/server/modes/charts/plugins/ql/module/js/build-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {mapQlConfigToLatestVersion} from '../../../../../../../shared/modules/co
import prepareSingleResult from '../../../datalens/js/helpers/misc/prepare-single-result';
import {extractColorPalettesFromData} from '../../../helpers/color-palettes';
import {getFieldList} from '../../../helpers/misc';
import type {QLConnectionTypeMap} from '../../utils/connection';
import {
doesQueryContainOrderBy,
getColumnsAndRows,
Expand All @@ -46,10 +47,12 @@ type BuildGraphArgs = {
ChartEditor: IChartEditor;
features: FeatureConfig;
palettes: Record<string, Palette>;
qlConnectionTypeMap: QLConnectionTypeMap;
};

// eslint-disable-next-line complexity
export function buildGraph({shared, ChartEditor, features, palettes}: BuildGraphArgs) {
export function buildGraph(args: BuildGraphArgs) {
const {shared, ChartEditor, features, palettes, qlConnectionTypeMap} = args;
const data = ChartEditor.getLoadedData();

log('LOADED DATA:', data);
Expand All @@ -67,6 +70,7 @@ export function buildGraph({shared, ChartEditor, features, palettes}: BuildGraph
queries: config.queries,
connectionType: config.connection.type,
data: loadedData,
qlConnectionTypeMap,
});

if (
Expand Down
3 changes: 2 additions & 1 deletion src/server/modes/charts/plugins/ql/module/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default ({shared, ChartEditor}: {shared: QlConfig; ChartEditor: IChartEdi
const features = getServerFeatures(app.nodekit.ctx);
const {getAvailablePalettesMap} = registry.common.functions.getAll();
const palettes = getAvailablePalettesMap();
const qlConnectionTypeMap = registry.getQLConnectionTypeMap();

return buildGraph({shared, ChartEditor, features, palettes});
return buildGraph({shared, ChartEditor, features, palettes, qlConnectionTypeMap});
};
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const prepareDefaultDate = (date: string) => {
};

export function buildSources(args: BuildSourcesArgs) {
const {shared, ChartEditor, palettes} = args;
const {shared, ChartEditor, palettes, qlConnectionTypeMap} = args;
const config = mapQlConfigToLatestVersion(shared, {i18n: ChartEditor.getTranslation});

const urlParams = ChartEditor.getParams();
Expand Down Expand Up @@ -190,6 +190,7 @@ export function buildSources(args: BuildSourcesArgs) {

params: localParams,
paramsDescription: localParamsDescription,
qlConnectionTypeMap,
});

sources[`ql_${i}`] = source;
Expand All @@ -208,6 +209,7 @@ export function buildSources(args: BuildSourcesArgs) {

params,
paramsDescription: config.params,
qlConnectionTypeMap,
}),
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/modes/charts/plugins/ql/module/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {buildSources} from './build-sources';
export default function ({shared, ChartEditor}: {shared: QlConfig; ChartEditor: IChartEditor}) {
const {getAvailablePalettesMap} = registry.common.functions.getAll();
const palettes = getAvailablePalettesMap();
const qlConnectionTypeMap = registry.getQLConnectionTypeMap();

return buildSources({shared, ChartEditor, palettes});
return buildSources({shared, ChartEditor, palettes, qlConnectionTypeMap});
}
2 changes: 2 additions & 0 deletions src/server/modes/charts/plugins/ql/module/url/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type {IChartEditor, Palette, QlConfig} from '../../../../../../../shared';
import type {QLConnectionTypeMap} from '../../utils/connection';

export type BuildSourcesArgs = {
shared: QlConfig;
ChartEditor: IChartEditor;
palettes: Record<string, Palette>;
qlConnectionTypeMap: QLConnectionTypeMap;
};
10 changes: 9 additions & 1 deletion src/server/modes/charts/plugins/ql/ql-chart-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ import type {WizardWorker} from '../../../../components/charts-engine/components
import type {RunnerHandler, RunnerHandlerProps} from '../../../../components/charts-engine/runners';
import {runChart} from '../../../../components/charts-engine/runners/chart';
import {runWorkerChart} from '../../../../components/charts-engine/runners/worker';
import {registry} from '../../../../registry';

import type {QLAdditionalData} from './types';

let wizardWorkersPool: Pool | null = null;
async function getQlWorker(): Promise<Proxy<WizardWorker>> {
if (wizardWorkersPool === null) {
const scriptPath = path.resolve(__dirname, './worker');
wizardWorkersPool = workerpool.pool(scriptPath);
const additionalData: QLAdditionalData = {
qlConnectionTypeMap: registry.getQLConnectionTypeMap(),
};
wizardWorkersPool = workerpool.pool(scriptPath, {
workerThreadOpts: {workerData: additionalData},
});
}

return wizardWorkersPool.proxy<WizardWorker>();
Expand Down
5 changes: 5 additions & 0 deletions src/server/modes/charts/plugins/ql/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type {QLConnectionTypeMap} from './utils/connection';

export type QLAdditionalData = {
qlConnectionTypeMap: QLConnectionTypeMap;
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type {QLConfigQuery} from '../../../../../../../shared';
import {ConnectorType} from '../../../../../../../shared';
import {convertConnectionType} from '../connection';
import {buildSource, doesQueryContainOrderBy, iterateThroughVisibleQueries} from '../misc-helpers';

const MOCK_ID = 'MOCK_ID';

const mockedBuildSourceArgsSet = {
const commonBuildSourceArgsSet = {
id: MOCK_ID,
connectionType: 'postgres',
qlConnectionTypeMap: {postgres: ConnectorType.Postgres},
};

const mockedBuildSourceArgsSet = {
...commonBuildSourceArgsSet,
query: 'select built_year, iznos from public.sample where built_year in {{years}} limit 10',
params: {years: '1995'},
paramsDescription: [
Expand All @@ -25,8 +31,7 @@ const expectedBuildSourceResultSet = {
};

const mockedBuildSourceArgsSingle = {
id: MOCK_ID,
connectionType: 'postgres',
...commonBuildSourceArgsSet,
query: 'select built_year, iznos from public.sample where built_year = {{years}} limit 10',
params: {years: '1995'},
paramsDescription: [
Expand All @@ -45,8 +50,7 @@ const expectedBuildSourceResultSingle = {
};

const mockedBuildSourceArgsPrewrapped = {
id: MOCK_ID,
connectionType: 'postgres',
...commonBuildSourceArgsSet,
query: 'select built_year, iznos from public.sample where built_year in ({{years}}) limit 10',
params: {years: '1995'},
paramsDescription: [
Expand Down
13 changes: 11 additions & 2 deletions src/server/modes/charts/plugins/ql/utils/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@ export const CONNECTOR_TYPE_TO_QL_CONNECTION_TYPE_MAP: Record<string, ConnectorT
[ConnectorType.MonitoringExt]: DATALENS_QL_CONNECTION_TYPES.MONITORING,
};

export function convertConnectionType(connectionType: string) {
const mappedConnectionType = CONNECTOR_TYPE_TO_QL_CONNECTION_TYPE_MAP[connectionType];
export function getConnectorToQlConnectionTypeMap() {
return CONNECTOR_TYPE_TO_QL_CONNECTION_TYPE_MAP;
}

export type QLConnectionTypeMap = ReturnType<typeof getConnectorToQlConnectionTypeMap>;

export function convertConnectionType(
connectionTypeMap: QLConnectionTypeMap,
connectionType: string,
) {
const mappedConnectionType = connectionTypeMap[connectionType];

if (!mappedConnectionType) {
throw new Error('Unsupported connection type');
Expand Down
39 changes: 26 additions & 13 deletions src/server/modes/charts/plugins/ql/utils/misc-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import type {
QlConfigResultEntryMetadataDataColumnOrGroup,
QlConfigResultEntryMetadataDataGroup,
} from '../../../../../../shared/types/config/ql';
import {registry} from '../../../../../registry';

import type {QLConnectionTypeMap} from './connection';
import {convertConnectionType} from './connection';
import {LINEAR_VISUALIZATIONS, LOG_INFO, LOG_TIMING, QUERY_TITLE} from './constants';

export {
Expand Down Expand Up @@ -389,17 +390,18 @@ export function buildSource({
query,
params,
paramsDescription,
qlConnectionTypeMap,
}: {
id: string;
connectionType: string;
query: string;
params: StringParams;
paramsDescription: QlConfigParam[];
qlConnectionTypeMap: QLConnectionTypeMap;
}) {
const convertConnectionType = registry.getConvertConnectorTypeToQLConnectionType();
let sqlQuery = query;

const datalensQLConnectionType = convertConnectionType(connectionType);
const datalensQLConnectionType = convertConnectionType(qlConnectionTypeMap, connectionType);

const QLRequestParams: Record<string, QlConfigRequestParam | QlConfigRequestParam[]> = {};
if (
Expand Down Expand Up @@ -506,16 +508,16 @@ export function getRows(data: QLResult, field = 'sql'): string[][] {
return rows;
}

export function getColumns(
data: QLResult,
connectionType: string,
field = 'sql',
): QlConfigResultEntryMetadataDataColumn[] | null {
export function getColumns(args: {
data: QLResult;
connectionType: string;
field?: string;
qlConnectionTypeMap: QLConnectionTypeMap;
}): QlConfigResultEntryMetadataDataColumn[] | null {
const {data, connectionType, field = 'sql', qlConnectionTypeMap} = args;
const row = data[field].find((entry: QLResultEntry) => entry.event === 'metadata');

const convertConnectionType = registry.getConvertConnectorTypeToQLConnectionType();

const datalensQLConnectionType = convertConnectionType(connectionType);
const datalensQLConnectionType = convertConnectionType(qlConnectionTypeMap, connectionType);

if (row) {
const metadataRow = row as QLResultEntryMetadata;
Expand Down Expand Up @@ -553,12 +555,14 @@ export function getColumnsAndRows({
queries,
connectionType,
data,
qlConnectionTypeMap,
}: {
chartType: QLChartType;
ChartEditor: IChartEditor;
queries: QLConfigQuery[];
connectionType: string;
data: {[key: string]: any};
qlConnectionTypeMap: QLConnectionTypeMap;
}) {
let columns: QlConfigResultEntryMetadataDataColumn[] | null = [];

Expand All @@ -572,7 +576,12 @@ export function getColumnsAndRows({
let localColumns: QlConfigResultEntryMetadataDataColumn[] = [];

try {
const parsedColumns = getColumns(data, connectionType, `ql_${i}`);
const parsedColumns = getColumns({
data,
connectionType,
field: `ql_${i}`,
qlConnectionTypeMap,
});

if (parsedColumns === null) {
return;
Expand Down Expand Up @@ -661,7 +670,11 @@ export function getColumnsAndRows({
}
} else {
try {
columns = getColumns(data, connectionType || ConnectorType.Clickhouse);
columns = getColumns({
data,
connectionType: connectionType || ConnectorType.Clickhouse,
qlConnectionTypeMap,
});

if (columns !== null) {
rows = getRows(data);
Expand Down
13 changes: 13 additions & 0 deletions src/server/modes/charts/plugins/ql/worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {workerData} from 'worker_threads';

import workerPool from 'workerpool';

import type {QlConfig, QlExtendedConfig} from '../../../../../shared';
Expand All @@ -17,8 +19,15 @@ import {getChartApiContext} from '../../../../components/charts-engine/component
import {createI18nInstance} from '../../../../utils/language';

import qlModule from './module/private-module';
import type {QLAdditionalData} from './types';
import {identifyParams} from './utils/identify-params';

function getQLAdditionalData(): QLAdditionalData {
return {
qlConnectionTypeMap: workerData?.qlConnectionTypeMap ?? {},
};
}

const worker: WizardWorker = {
buildParams: async (args: BuildParamsArgs) => {
const {shared, userLang} = args;
Expand Down Expand Up @@ -48,11 +57,13 @@ const worker: WizardWorker = {
const console = new Console({});
qlModule.setConsole(console);

const {qlConnectionTypeMap} = getQLAdditionalData();
return {
exports: qlModule.buildSources({
shared: shared as QlConfig,
ChartEditor: context.ChartEditor,
palettes,
qlConnectionTypeMap,
}),
runtimeMetadata: context.__runtimeMetadata,
logs: console.getLogs(),
Expand Down Expand Up @@ -144,11 +155,13 @@ const worker: WizardWorker = {
const console = new Console({});
qlModule.setConsole(console);

const {qlConnectionTypeMap} = getQLAdditionalData();
const result = qlModule.buildGraph({
shared: shared as QlConfig,
ChartEditor: context.ChartEditor,
palettes,
features,
qlConnectionTypeMap,
});

return {
Expand Down
19 changes: 8 additions & 11 deletions src/server/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import getGatewayControllers from '@gravity-ui/gateway';
import type {AppContext} from '@gravity-ui/nodekit';

import type {ChartsEngine} from '../components/charts-engine';
import {convertConnectionType} from '../modes/charts/plugins/ql/utils/connection';
import type {QLConnectionTypeMap} from '../modes/charts/plugins/ql/utils/connection';
import {getConnectorToQlConnectionTypeMap} from '../modes/charts/plugins/ql/utils/connection';
import type {GetLayoutConfig} from '../types/app-layout';
import type {ConvertConnectorTypeToQLConnectionType} from '../types/connections';
import type {XlsxConverterFn} from '../types/xlsxConverter';

import commonRegistry from './units/common';
Expand All @@ -24,7 +24,7 @@ let gateway: ReturnType<typeof wrapperGetGatewayControllers>;
let getLayoutConfig: GetLayoutConfig | undefined;
let yfmPlugins: MarkdownItPluginCb[];
let getXlsxConverter: XlsxConverterFn | undefined;
let convertConnectorTypeToQLConnectionType: ConvertConnectorTypeToQLConnectionType | undefined;
let qLConnectionTypeMap: QLConnectionTypeMap | undefined;

export const registry = {
common: commonRegistry,
Expand Down Expand Up @@ -109,15 +109,12 @@ export const registry = {
getXlsxConverter() {
return getXlsxConverter;
},
registerConvertConnectorTypeToQLConnectionType(fn: ConvertConnectorTypeToQLConnectionType) {
if (!convertConnectorTypeToQLConnectionType) {
convertConnectorTypeToQLConnectionType = fn;
setupQLConnectionTypeMap(map: QLConnectionTypeMap) {
if (!qLConnectionTypeMap) {
qLConnectionTypeMap = map;
}
},
getConvertConnectorTypeToQLConnectionType() {
if (!convertConnectorTypeToQLConnectionType) {
return convertConnectionType;
}
return convertConnectorTypeToQLConnectionType;
getQLConnectionTypeMap() {
return qLConnectionTypeMap ?? getConnectorToQlConnectionTypeMap();
},
};
Loading