PromucFlow_constructor/app/client/src/sagas/EvaluationsSaga.ts

394 lines
11 KiB
TypeScript
Raw Normal View History

import {
actionChannel,
call,
fork,
put,
select,
take,
} from "redux-saga/effects";
import {
EvaluationReduxAction,
ReduxAction,
ReduxActionErrorTypes,
ReduxActionTypes,
ReduxActionWithoutPayload,
} from "constants/ReduxActionConstants";
2021-01-14 14:37:21 +00:00
import { getUnevaluatedDataTree } from "selectors/dataTreeSelectors";
import WidgetFactory, { WidgetTypeConfigMap } from "../utils/WidgetFactory";
2021-02-22 05:00:16 +00:00
import { GracefulWorkerService } from "utils/WorkerUtil";
import Worker from "worker-loader!../workers/evaluation.worker";
import {
EVAL_WORKER_ACTIONS,
EvalError,
EvalErrorTypes,
2021-02-22 05:00:16 +00:00
} from "utils/DynamicBindingUtils";
import log from "loglevel";
2021-02-22 05:00:16 +00:00
import { WidgetProps } from "widgets/BaseWidget";
import PerformanceTracker, {
PerformanceTransactionName,
} from "../utils/PerformanceTracker";
2020-11-24 07:01:37 +00:00
import { Variant } from "components/ads/common";
import { Toaster } from "components/ads/Toast";
import * as Sentry from "@sentry/react";
2020-12-30 13:26:44 +00:00
import { Action } from "redux";
import _ from "lodash";
import {
createMessage,
ERROR_EVAL_ERROR_GENERIC,
ERROR_EVAL_TRIGGER,
} from "constants/messages";
2021-04-23 13:50:55 +00:00
import AppsmithConsole from "utils/AppsmithConsole";
import LOG_TYPE from "entities/AppsmithConsole/logtype";
import AnalyticsUtil from "utils/AnalyticsUtil";
let widgetTypeConfigMap: WidgetTypeConfigMap;
const worker = new GracefulWorkerService(Worker);
const evalErrorHandler = (errors: EvalError[]) => {
2020-12-07 05:45:14 +00:00
if (!errors) return;
2020-12-24 04:32:25 +00:00
errors.forEach((error) => {
2021-02-22 05:00:16 +00:00
switch (error.type) {
case EvalErrorTypes.DEPENDENCY_ERROR: {
if (error.context) {
// Add more info about node for the toast
const { entityType, node } = error.context;
Toaster.show({
text: `${error.message} Node was: ${node}`,
variant: Variant.danger,
});
AppsmithConsole.error({
text: `${error.message} Node was: ${node}`,
});
// Send the generic error message to sentry for better grouping
Sentry.captureException(new Error(error.message), {
tags: {
node,
entityType,
},
// Level is warning because it could be a user error
level: Sentry.Severity.Warning,
});
// Log an analytics event for cyclical dep errors
AnalyticsUtil.logEvent("CYCLICAL_DEPENDENCY_ERROR", {
node,
entityType,
// Level is warning because it could be a user error
level: Sentry.Severity.Warning,
});
}
2021-02-22 05:00:16 +00:00
break;
}
case EvalErrorTypes.EVAL_TREE_ERROR: {
Toaster.show({
text: createMessage(ERROR_EVAL_ERROR_GENERIC),
2021-02-22 05:00:16 +00:00
variant: Variant.danger,
});
break;
}
case EvalErrorTypes.BAD_UNEVAL_TREE_ERROR: {
Sentry.captureException(error);
break;
}
case EvalErrorTypes.EVAL_TRIGGER_ERROR: {
log.debug(error);
2021-02-22 05:00:16 +00:00
Toaster.show({
text: createMessage(ERROR_EVAL_TRIGGER, error.message),
2021-02-22 05:00:16 +00:00
variant: Variant.danger,
showDebugButton: true,
});
AppsmithConsole.error({
text: createMessage(ERROR_EVAL_TRIGGER, error.message),
2021-02-22 05:00:16 +00:00
});
break;
}
case EvalErrorTypes.EVAL_ERROR: {
log.debug(error);
AppsmithConsole.error({
logType: LOG_TYPE.EVAL_ERROR,
text: `The value at ${error.context?.source.propertyPath} is invalid`,
message: error.message,
source: error.context?.source,
});
break;
}
2021-04-23 13:50:55 +00:00
case EvalErrorTypes.WIDGET_PROPERTY_VALIDATION_ERROR: {
AppsmithConsole.error({
logType: LOG_TYPE.WIDGET_PROPERTY_VALIDATION_ERROR,
text: `The value at ${error.context?.source.propertyPath} is invalid`,
message: error.message,
source: error.context?.source,
state: error.context?.state,
2021-04-23 13:50:55 +00:00
});
break;
}
2021-02-22 05:00:16 +00:00
default: {
Sentry.captureException(error);
log.debug(error);
2021-02-22 05:00:16 +00:00
}
}
});
};
function* postEvalActionDispatcher(
actions: Array<ReduxAction<unknown> | ReduxActionWithoutPayload>,
) {
for (const action of actions) {
yield put(action);
}
}
function* evaluateTreeSaga(
postEvalActions?: Array<ReduxAction<unknown> | ReduxActionWithoutPayload>,
) {
2021-03-31 07:40:59 +00:00
const unevalTree = yield select(getUnevaluatedDataTree);
log.debug({ unevalTree });
PerformanceTracker.startAsyncTracking(
PerformanceTransactionName.DATA_TREE_EVALUATION,
);
const workerResponse = yield call(
worker.request,
EVAL_WORKER_ACTIONS.EVAL_TREE,
{
unevalTree,
widgetTypeConfigMap,
},
);
const { dataTree, dependencies, errors, logs } = workerResponse;
log.debug({ dataTree: dataTree });
logs.forEach((evalLog: any) => log.debug(evalLog));
evalErrorHandler(errors);
2021-03-31 07:40:59 +00:00
PerformanceTracker.stopAsyncTracking(
PerformanceTransactionName.DATA_TREE_EVALUATION,
);
PerformanceTracker.startAsyncTracking(
PerformanceTransactionName.SET_EVALUATED_TREE,
);
yield put({
type: ReduxActionTypes.SET_EVALUATED_TREE,
payload: dataTree,
});
PerformanceTracker.stopAsyncTracking(
2021-03-31 07:40:59 +00:00
PerformanceTransactionName.SET_EVALUATED_TREE,
);
yield put({
2021-01-14 14:37:21 +00:00
type: ReduxActionTypes.SET_EVALUATION_INVERSE_DEPENDENCY_MAP,
payload: { inverseDependencyMap: dependencies },
});
if (postEvalActions && postEvalActions.length) {
yield call(postEvalActionDispatcher, postEvalActions);
}
}
2021-01-14 14:37:21 +00:00
export function* evaluateActionBindings(
bindings: string[],
executionParams: Record<string, any> | string = {},
2020-12-14 18:48:13 +00:00
) {
const workerResponse = yield call(
worker.request,
2021-01-14 14:37:21 +00:00
EVAL_WORKER_ACTIONS.EVAL_ACTION_BINDINGS,
{
2021-01-14 14:37:21 +00:00
bindings,
executionParams,
},
);
2021-01-14 14:37:21 +00:00
const { errors, values } = workerResponse;
evalErrorHandler(errors);
2021-01-14 14:37:21 +00:00
return values;
}
export function* evaluateDynamicTrigger(
dynamicTrigger: string,
2020-11-20 09:30:50 +00:00
callbackData?: Array<any>,
) {
const unEvalTree = yield select(getUnevaluatedDataTree);
const workerResponse = yield call(
worker.request,
EVAL_WORKER_ACTIONS.EVAL_TRIGGER,
{ dataTree: unEvalTree, dynamicTrigger, callbackData },
);
const { errors, triggers } = workerResponse;
evalErrorHandler(errors);
return triggers;
}
export function* clearEvalCache() {
yield call(worker.request, EVAL_WORKER_ACTIONS.CLEAR_CACHE);
return true;
}
export function* clearEvalPropertyCache(propertyPath: string) {
yield call(worker.request, EVAL_WORKER_ACTIONS.CLEAR_PROPERTY_CACHE, {
propertyPath,
});
}
/**
* clears all cache keys of a widget
*
* @param widgetName
*/
export function* clearEvalPropertyCacheOfWidget(widgetName: string) {
yield call(
worker.request,
EVAL_WORKER_ACTIONS.CLEAR_PROPERTY_CACHE_OF_WIDGET,
{
widgetName,
},
);
}
export function* validateProperty(
property: string,
value: any,
props: WidgetProps,
) {
const unevalTree = yield select(getUnevaluatedDataTree);
const validation = unevalTree[props.widgetName].validationPaths[property];
return yield call(worker.request, EVAL_WORKER_ACTIONS.VALIDATE_PROPERTY, {
property,
value,
props,
validation,
});
}
2020-12-30 13:26:44 +00:00
const FIRST_EVAL_REDUX_ACTIONS = [
// Pages
ReduxActionTypes.FETCH_PAGE_SUCCESS,
ReduxActionTypes.FETCH_PUBLISHED_PAGE_SUCCESS,
];
const EVALUATE_REDUX_ACTIONS = [
2020-12-30 13:26:44 +00:00
...FIRST_EVAL_REDUX_ACTIONS,
// Actions
ReduxActionTypes.FETCH_ACTIONS_SUCCESS,
ReduxActionTypes.FETCH_PLUGIN_FORM_CONFIGS_SUCCESS,
ReduxActionTypes.FETCH_ACTIONS_VIEW_MODE_SUCCESS,
ReduxActionErrorTypes.FETCH_ACTIONS_ERROR,
ReduxActionErrorTypes.FETCH_ACTIONS_VIEW_MODE_ERROR,
ReduxActionTypes.FETCH_ACTIONS_FOR_PAGE_SUCCESS,
ReduxActionTypes.SUBMIT_CURL_FORM_SUCCESS,
ReduxActionTypes.CREATE_ACTION_SUCCESS,
ReduxActionTypes.UPDATE_ACTION_PROPERTY,
ReduxActionTypes.DELETE_ACTION_SUCCESS,
ReduxActionTypes.COPY_ACTION_SUCCESS,
ReduxActionTypes.MOVE_ACTION_SUCCESS,
ReduxActionTypes.RUN_ACTION_SUCCESS,
ReduxActionErrorTypes.RUN_ACTION_ERROR,
ReduxActionTypes.EXECUTE_API_ACTION_SUCCESS,
ReduxActionErrorTypes.EXECUTE_ACTION_ERROR,
// App Data
ReduxActionTypes.SET_APP_MODE,
ReduxActionTypes.FETCH_USER_DETAILS_SUCCESS,
2021-03-24 05:09:47 +00:00
ReduxActionTypes.UPDATE_APP_PERSISTENT_STORE,
ReduxActionTypes.UPDATE_APP_TRANSIENT_STORE,
// Widgets
ReduxActionTypes.UPDATE_LAYOUT,
ReduxActionTypes.UPDATE_WIDGET_PROPERTY,
ReduxActionTypes.UPDATE_WIDGET_NAME_SUCCESS,
// Widget Meta
ReduxActionTypes.SET_META_PROP,
ReduxActionTypes.RESET_WIDGET_META,
// Batches
ReduxActionTypes.BATCH_UPDATES_SUCCESS,
];
const shouldProcessAction = (action: ReduxAction<unknown>) => {
// debugger;
if (
action.type === ReduxActionTypes.BATCH_UPDATES_SUCCESS &&
Array.isArray(action.payload)
) {
const batchedActionTypes = action.payload.map(
(batchedAction) => batchedAction.type,
);
return (
_.intersection(EVALUATE_REDUX_ACTIONS, batchedActionTypes).length > 0
);
}
return true;
};
2020-12-30 13:26:44 +00:00
function evalQueueBuffer() {
let canTake = false;
2020-12-30 13:26:44 +00:00
let postEvalActions: any = [];
const take = () => {
if (canTake) {
2020-12-30 13:26:44 +00:00
const resp = postEvalActions;
postEvalActions = [];
canTake = false;
return { postEvalActions: resp, type: "BUFFERED_ACTION" };
2020-12-30 13:26:44 +00:00
}
};
const flush = () => {
if (canTake) {
2020-12-30 13:26:44 +00:00
return [take() as Action];
}
return [];
};
const put = (action: EvaluationReduxAction<unknown | unknown[]>) => {
if (!shouldProcessAction(action)) {
return;
}
canTake = true;
2020-12-30 13:26:44 +00:00
// TODO: If the action is the same as before, we can send only one and ignore duplicates.
if (action.postEvalActions) {
postEvalActions.push(...action.postEvalActions);
}
};
return {
take,
put,
isEmpty: () => {
return !canTake;
2020-12-30 13:26:44 +00:00
},
flush,
};
}
function* evaluationChangeListenerSaga() {
// Explicitly shutdown old worker if present
yield call(worker.shutdown);
yield call(worker.start);
widgetTypeConfigMap = WidgetFactory.getWidgetTypeConfigMap();
const initAction = yield take(FIRST_EVAL_REDUX_ACTIONS);
yield fork(evaluateTreeSaga, initAction.postEvalActions);
2020-12-30 13:26:44 +00:00
const evtActionChannel = yield actionChannel(
EVALUATE_REDUX_ACTIONS,
evalQueueBuffer(),
);
while (true) {
const action: EvaluationReduxAction<unknown | unknown[]> = yield take(
evtActionChannel,
);
if (shouldProcessAction(action)) {
yield call(evaluateTreeSaga, action.postEvalActions);
}
}
// TODO(hetu) need an action to stop listening and evaluate (exit app)
}
export default function* evaluationSagaListeners() {
2020-12-30 13:26:44 +00:00
yield take(ReduxActionTypes.START_EVALUATION);
while (true) {
try {
yield call(evaluationChangeListenerSaga);
} catch (e) {
log.error(e);
Sentry.captureException(e);
}
}
}