fix(core): Fixes issue with workflow lastUpdated field (#5015)

Fixed issue causing workflow updated field to be affected by statistics data
This commit is contained in:
freya
2023-01-05 12:16:40 +00:00
committed by GitHub
parent 7954025eb2
commit 59004fe7bb
16 changed files with 263 additions and 28 deletions

View File

@@ -1,8 +1,9 @@
import type { INode, IRun, IWorkflowBase } from 'n8n-workflow';
import { INode, IRun, IWorkflowBase } from 'n8n-workflow';
import * as Db from '@/Db';
import { InternalHooksManager } from '@/InternalHooksManager';
import { StatisticsNames } from '@/databases/entities/WorkflowStatistics';
import { getWorkflowOwner } from '@/UserManagement/UserManagementHelper';
import { QueryFailedError } from 'typeorm';
export async function workflowExecutionCompleted(
workflowData: IWorkflowBase,
@@ -47,7 +48,10 @@ export async function workflowExecutionCompleted(
// Send the metrics
await InternalHooksManager.getInstance().onFirstProductionWorkflowSuccess(metrics);
} catch (error) {
// Do we just assume it's a conflict error? If there is any other sort of error in the DB it should trigger here too
if (!(error instanceof QueryFailedError)) {
throw error;
}
await Db.collections.WorkflowStatistics.update(
{ workflowId, name },
{ count: () => 'count + 1', latestEvent: new Date() },
@@ -56,16 +60,24 @@ export async function workflowExecutionCompleted(
}
export async function nodeFetchedData(workflowId: string, node: INode): Promise<void> {
// Update only if necessary
const response = await Db.collections.Workflow.update(
{ id: workflowId, dataLoaded: false },
{ dataLoaded: true },
);
// Try to insert the data loaded statistic
try {
await Db.collections.WorkflowStatistics.insert({
workflowId,
name: StatisticsNames.dataLoaded,
count: 1,
latestEvent: new Date(),
});
} catch (error) {
// if it's a duplicate key error then that's fine, otherwise throw the error
if (!(error instanceof QueryFailedError)) {
throw error;
}
// If it is a query failed error, we return
return;
}
// If response.affected is 1 then we know this was the first time data was loaded into the workflow; do posthog event here
if (!response.affected) return;
// Compile the metrics
// Compile the metrics since this was a new data loaded event
const owner = await getWorkflowOwner(workflowId);
let metrics = {
user_id: owner.id,