Skip to content

Commit

Permalink
Merge pull request AutomaApp#1151 from AprildreamMI/logs
Browse files Browse the repository at this point in the history
Fix the bug that "Get Log Data Block" can't get the latest logs,
  • Loading branch information
Kholid060 authored Feb 13, 2023
2 parents d1dac2a + dbe391a commit 79f3d12
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 26 deletions.
10 changes: 10 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@/*": ["src/*"],
"@business": ["business/dev/*"]
}
},
"include": ["src/**/*", "utils/**/*"]
}
9 changes: 1 addition & 8 deletions src/components/newtab/logs/LogsHistory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ import Papa from 'papaparse';
import objectPath from 'object-path';
import { countDuration, fileSaver } from '@/utils/helper';
import { getBlocks } from '@/utils/getSharedData';
import { dataExportTypes } from '@/utils/shared';
import { dataExportTypes, messageHasReferences } from '@/utils/shared';
import dayjs from '@/lib/dayjs';
const SharedCodemirror = defineAsyncComponent(() =>
Expand Down Expand Up @@ -363,13 +363,6 @@ const tabs = [
{ id: 'referenceData.prevBlockData', name: 'Previous block data' },
{ id: 'replacedValue', name: 'Replaced value' },
];
const messageHasReferences = [
'no-tab',
'invalid-active-tab',
'no-match-tab',
'invalid-body',
'element-not-found',
];
const { t, te } = useI18n();
Expand Down
9 changes: 9 additions & 0 deletions src/utils/dataExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ export default function (
const extractObj = (obj) => {
if (typeof obj !== 'object') return [obj];

// 需要处理深层对象 不然会返回:[object Object]
const kes = Object.keys(obj);
kes.forEach((key) => {
const itemValue = obj[key];
if (typeof itemValue === 'object') {
obj[key] = JSON.stringify(itemValue);
}
});

return Object.values(obj);
};

Expand Down
142 changes: 142 additions & 0 deletions src/utils/getTranslateLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { getBlocks } from '@/utils/getSharedData';
import dayjs from '@/lib/dayjs';
import vueI18n from '@/lib/vueI18n';
import { countDuration } from '@/utils/helper';
import { messageHasReferences } from '@/utils/shared';

const blocks = getBlocks();

/**
* 转换日志
* @param {*} log
* @returns
*/
function translateLog(log) {
const copyLog = { ...log };
const getTranslatation = (path, def) => {
const params = typeof path === 'string' ? { path } : path;

return vueI18n.global.te(params.path)
? vueI18n.global.t(params.path, params.params)
: def;
};

if (['finish', 'stop'].includes(log.type)) {
copyLog.name = vueI18n.global.t(`log.types.${log.type}`);
} else {
copyLog.name = getTranslatation(
`workflow.blocks.${log.name}.name`,
blocks[log.name].name
);
}

if (copyLog.message && messageHasReferences.includes(copyLog.message)) {
copyLog.messageId = `${copyLog.message}`;
}

copyLog.message = getTranslatation(
{ path: `log.messages.${log.message}`, params: log },
log.message
);

return copyLog;
}

function getDataSnapshot(propsCtxData, refData) {
if (!propsCtxData?.dataSnapshot) return;

const data = propsCtxData.dataSnapshot;
const getData = (key) => {
const currentData = refData[key];
if (typeof currentData !== 'string') return currentData;

return data[currentData] ?? {};
};

refData.loopData = getData('loopData');
refData.variables = getData('variables');
}

/**
* 获取日志
* @param {*} dataType 日志数据类型
* @param {*} translatedLog 转换后的日志
* @returns
*/
function getLogs(dataType, translatedLog, curStateCtxData) {
let data = dataType === 'plain-text' ? '' : [];
const getItemData = {
'plain-text': ([
timestamp,
duration,
status,
name,
description,
message,
ctxData,
]) => {
data += `${timestamp}(${countDuration(
0,
duration || 0
).trim()}) - ${status} - ${name} - ${description} - ${message} - ${JSON.stringify(
ctxData
)} \n`;
},
json: ([
timestamp,
duration,
status,
name,
description,
message,
ctxData,
]) => {
data.push({
timestamp,
duration: countDuration(0, duration || 0).trim(),
status,
name,
description,
message,
data: ctxData,
});
},
};
translatedLog.forEach((item, index) => {
let logData = curStateCtxData;
if (logData.ctxData) logData = logData.ctxData;

const itemData = logData[item.id] || null;
if (itemData) getDataSnapshot(curStateCtxData, itemData.referenceData);

getItemData[dataType](
[
dayjs(item.timestamp || Date.now()).format('YYYY-MM-DD HH:mm:ss'),
item.duration,
item.type.toUpperCase(),
item.name,
item.description || 'NULL',
item.message || 'NULL',
itemData,
],
index
);
});
return data;
}

/**
* 获取日志数据
* @param {*} curState 当前工作流状态
* @param {*} dataType 日志数据类型 plain-text 和 json
* @returns
*/
export default function (curState, dataType = 'plain-text') {
const { logs: curStateHistory, ctxData: curStateCtxData } = curState;
// 经过转换后的日志
const translatedLog = curStateHistory.map(translateLog);
// 获取日志
const logs = getLogs(dataType, translatedLog, curStateCtxData);
// 获取日志
return logs;
}
8 changes: 8 additions & 0 deletions src/utils/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -1736,3 +1736,11 @@ export const conditionBuilder = {
},
},
};

export const messageHasReferences = [
'no-tab',
'invalid-active-tab',
'no-match-tab',
'invalid-body',
'element-not-found',
];
6 changes: 5 additions & 1 deletion src/workflowEngine/WorkflowEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,11 @@ class WorkflowEngine {
tabIds: [],
currentBlock: [],
name: this.workflow.name,
logs: this.history.slice(-5),
logs: this.history,
ctxData: {
ctxData: this.historyCtxData,
dataSnapshot: this.refDataSnapshots,
},
startedTimestamp: this.startedTimestamp,
};

Expand Down
39 changes: 22 additions & 17 deletions src/workflowEngine/blocksHandler/handlerLogData.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import dbLogs from '@/db/logs';
import getTranslateLog from '@/utils/getTranslateLog';
import { workflowState } from '../index';

export async function logData({ id, data }) {
if (!data.workflowId) {
throw new Error('No workflow is selected');
}

const [workflowLog] = await dbLogs.items
.where('workflowId')
.equals(data.workflowId)
.reverse()
.sortBy('endedAt');
let workflowLogData = null;
// 工作流状态数组
const { states } = workflowState;
let logs = [];
if (states) {
// 转换为数组
const stateValues = Object.values(Object.fromEntries(states));
// 当前工作流状态
const curWorkflowState = stateValues.find(
(item) => item.workflowId === data.workflowId
)?.state;

if (workflowLog) {
workflowLogData = (
await dbLogs.logsData.where('logId').equals(workflowLog.id).first()
).data;
if (curWorkflowState) {
// 当前工作流最新日志
logs = getTranslateLog(curWorkflowState, 'json');

if (data.assignVariable) {
this.setVariable(data.variableName, workflowLogData);
}
if (data.saveData) {
this.addDataToColumn(data.dataColumn, workflowLogData);
if (data.assignVariable) {
this.setVariable(data.variableName, logs);
}
if (data.saveData) {
this.addDataToColumn(data.dataColumn, logs);
}
}
}

return {
data: workflowLogData,
data: logs,
nextBlockId: this.getBlockConnections(id),
};
}
Expand Down

0 comments on commit 79f3d12

Please sign in to comment.