Skip to content

Commit 56658ee

Browse files
authored
Make dash data optional if possible (#2257)
1 parent a4cb3fe commit 56658ee

File tree

26 files changed

+908
-681
lines changed

26 files changed

+908
-681
lines changed

api/server/plugins.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
export {
2-
configuredDashApiPlugin,
3-
dashApiValidation,
4-
} from '../../src/server/modes/charts/plugins/dash-api';
1+
export {configuredDashApiPlugin} from '../../src/server/modes/charts/plugins/dash-api';
2+
export {dashApiValidation} from '../../src/server/modes/charts/plugins/data-api-json-schema';
3+
54
export {plugin as ql} from '../../src/server/modes/charts/plugins/ql';
65
export {configurableRequestWithDatasetPlugin} from '../../src/server/modes/charts/plugins/request-with-dataset';
76
export {plugin as loginsBlacklist} from '../../src/server/modes/charts/plugins/logins-blacklist';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Feature} from '../../../../shared';
2+
import {createFeatureConfig} from '../utils';
3+
4+
export default createFeatureConfig({
5+
name: Feature.DashServerMigrationEnable,
6+
state: {
7+
development: true,
8+
production: false,
9+
},
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Feature} from '../../../../shared';
2+
import {createFeatureConfig} from '../utils';
3+
4+
export default createFeatureConfig({
5+
name: Feature.DashServerValidationEnable,
6+
state: {
7+
development: true,
8+
production: false,
9+
},
10+
});

src/server/components/sdk/dash.ts

+35-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import assign from 'lodash/assign';
66
import intersection from 'lodash/intersection';
77

88
import type {ServerI18n} from '../../../i18n/types';
9-
import {DASH_CURRENT_SCHEME_VERSION} from '../../../shared/constants';
9+
import {DASH_CURRENT_SCHEME_VERSION, DASH_DATA_REQUIRED_FIELDS} from '../../../shared/constants';
10+
import {DashSchemeConverter} from '../../../shared/modules';
1011
import type {
1112
CreateEntryRequest,
1213
DashData,
@@ -22,7 +23,9 @@ import {
2223
DashTabItemType,
2324
EntryScope,
2425
EntryUpdateMode,
26+
Feature,
2527
} from '../../../shared/types';
28+
import {isEnabledServerFeature} from '../../../shared/utils';
2629

2730
import US from './us';
2831

@@ -69,11 +72,15 @@ function gatherLinks(data: DashData) {
6972
);
7073
}
7174

72-
function assignData(I18n: ServerI18n, requestData: DashData) {
75+
function setDefaultData(I18n: ServerI18n, requestData: DashData, initialData?: DashData) {
7376
const i18n = I18n.keyset('dash.tabs-dialog.edit');
77+
78+
if (initialData) {
79+
return assign({}, initialData, requestData);
80+
}
81+
7482
const salt = Math.random().toString();
7583
const hashids = new Hashids(salt);
76-
7784
const data: DashData = {
7885
salt,
7986
counter: 2,
@@ -102,6 +109,9 @@ function assignData(I18n: ServerI18n, requestData: DashData) {
102109
return assign(data, requestData);
103110
}
104111

112+
const needSetDefaultData = (data: DashData) =>
113+
DASH_DATA_REQUIRED_FIELDS.every((fieldName) => fieldName in data);
114+
105115
function validateData(data: DashData) {
106116
const allTabsIds: Set<string> = new Set();
107117
const allItemsIds: Set<string> = new Set();
@@ -193,8 +203,15 @@ class Dash {
193203
scope: EntryScope.Dash,
194204
type: '',
195205
};
196-
} else {
197-
usData.data = assignData(I18n, usData.data);
206+
} else if (needSetDefaultData(usData.data)) {
207+
usData.data = setDefaultData(I18n, usData.data);
208+
}
209+
210+
const isServerMigrationEnabled = Boolean(
211+
isEnabledServerFeature(ctx, Feature.DashServerMigrationEnable),
212+
);
213+
if (isServerMigrationEnabled && DashSchemeConverter.isUpdateNeeded(usData.data)) {
214+
usData.data = await DashSchemeConverter.update(usData.data);
198215
}
199216

200217
usData.links = gatherLinks(usData.data);
@@ -242,6 +259,13 @@ class Dash {
242259
ctx,
243260
)) as DashEntry;
244261

262+
const isServerMigrationEnabled = Boolean(
263+
isEnabledServerFeature(ctx, Feature.DashServerMigrationEnable),
264+
);
265+
if (isServerMigrationEnabled && DashSchemeConverter.isUpdateNeeded(result.data)) {
266+
result.data = await DashSchemeConverter.update(result.data);
267+
}
268+
245269
ctx.log('SDK_DASH_READ_SUCCESS', US.getLoggedEntry(result));
246270

247271
return result;
@@ -265,8 +289,13 @@ class Dash {
265289

266290
const needDataSend = !(mode === EntryUpdateMode.Publish && data.revId);
267291
if (needDataSend) {
268-
usData.data = assignData(I18n, usData.data);
292+
if (needSetDefaultData(usData.data)) {
293+
const initialData = await Dash.read(entryId, null, headers, ctx);
294+
usData.data = setDefaultData(I18n, usData.data, initialData.data);
295+
}
296+
269297
usData.links = gatherLinks(usData.data);
298+
270299
validateData(usData.data);
271300
}
272301

0 commit comments

Comments
 (0)