Skip to content
Draft
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
77 changes: 70 additions & 7 deletions packages/cli/src/modules/insights/insights-collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { OnLifecycleEvent, type WorkflowExecuteAfterContext } from '@n8n/decorat
import { Service } from '@n8n/di';
import { In } from '@n8n/typeorm';
import { DateTime } from 'luxon';
import { UnexpectedError, type ExecutionStatus, type WorkflowExecuteMode } from 'n8n-workflow';
import {
UnexpectedError,
type ExecutionStatus,
type IRun,
type WorkflowExecuteMode,
} from 'n8n-workflow';

import { InsightsMetadata } from '@/modules/insights/database/entities/insights-metadata';
import { InsightsRaw } from '@/modules/insights/database/entities/insights-raw';
Expand Down Expand Up @@ -156,12 +161,15 @@ export class InsightsCollectionService {
}

// time saved event
if (status === 'success' && ctx.workflow.settings?.timeSavedPerExecution) {
this.bufferedInsights.add({
...commonWorkflowData,
type: 'time_saved_min',
value: ctx.workflow.settings.timeSavedPerExecution,
});
if (status === 'success') {
const finalTimeSaved = this.calculateTimeSaved(ctx);
if (finalTimeSaved !== undefined) {
this.bufferedInsights.add({
...commonWorkflowData,
type: 'time_saved_min',
value: finalTimeSaved,
});
}
}

if (!this.isAsynchronouslySavingInsights) {
Expand Down Expand Up @@ -279,4 +287,59 @@ export class InsightsCollectionService {
this.flushesInProgress.add(flushPromise);
await flushPromise;
}

/**
* Calculate the final time saved value by extracting SavedTime node metadata
* and combining it with workflow settings based on the node's behavior.
*/
private calculateTimeSaved(ctx: WorkflowExecuteAfterContext): number | undefined {
console.log('calculateTimeSaved');
const workflowTimeSaved = ctx.workflow.settings?.timeSavedPerExecution;

// Extract time saved from SavedTime nodes
const nodeTimeSaved = this.extractTimeSavedFromNodes(ctx.runData);

console.log('nodeTimeSaved', nodeTimeSaved);
if (nodeTimeSaved === undefined) {
// No SavedTime nodes, use workflow setting
return workflowTimeSaved;
}

console.log('nodeTimeSaved', nodeTimeSaved);

return nodeTimeSaved;
}

/**
* Extract and sum time saved from all SavedTime nodes in the workflow execution.
* Returns undefined if no SavedTime nodes were executed.
*/
private extractTimeSavedFromNodes(runData: IRun): number | undefined {
let totalMinutes = 0;
let hasAnyNode = false;

const resultData = runData.data.resultData.runData;
if (!resultData) {
return undefined;
}

// Iterate through all node metadata
for (const nodeName in resultData) {
const taskData = resultData[nodeName];

// Each node can have multiple run indexes
for (const taskDataEntry of taskData) {
if (taskDataEntry?.metadata?.timeSaved) {
hasAnyNode = true;
totalMinutes += taskDataEntry?.metadata?.timeSaved.minutes;
}
}
}

if (!hasAnyNode) {
return undefined;
}

return totalMinutes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ import IconLucideTags from '~icons/lucide/tags';
import IconLucideTerminal from '~icons/lucide/terminal';
import IconLucideThumbsDown from '~icons/lucide/thumbs-down';
import IconLucideThumbsUp from '~icons/lucide/thumbs-up';
import IconLucideTimer from '~icons/lucide/timer';
import IconLucideToggleRight from '~icons/lucide/toggle-right';
import IconLucideTrash2 from '~icons/lucide/trash-2';
import IconLucideTreePine from '~icons/lucide/tree-pine';
Expand Down Expand Up @@ -407,6 +408,7 @@ export const deprecatedIconSet = {
tasks: IconLucideListChecks,
terminal: IconLucideTerminal,
'th-large': IconLucideGrid2x2,
timer: IconLucideTimer,
thumbtack: IconLucidePin,
'thumbs-down': IconLucideThumbsDown,
'thumbs-up': IconLucideThumbsUp,
Expand Down
11 changes: 11 additions & 0 deletions packages/frontend/@n8n/i18n/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2916,6 +2916,17 @@
"workflowSettings.timeSavedPerExecution": "Estimated time saved",
"workflowSettings.timeSavedPerExecution.hint": "Minutes per production execution",
"workflowSettings.timeSavedPerExecution.tooltip": "Total time savings are summarised in the Overview page.",
"workflowSettings.timeSavedPerExecution.tab.fixed": "Fixed",
"workflowSettings.timeSavedPerExecution.tab.conditional": "Conditional",
"workflowSettings.timeSavedPerExecution.noNodesDetected": "No time saved nodes detected",
"workflowSettings.timeSavedPerExecution.noNodesDetected.hint": "Add {link} saved nodes to calculate time saved dynamically",
"workflowSettings.timeSavedPerExecution.noNodesDetected.link": "one or more time",
"workflowSettings.timeSavedPerExecution.nodesDetected": "Active - {count} time saved nodes currently setup",
"workflowSettings.timeSavedPerExecution.nodesDetected.hint": "Time saved is calculated dynamically based on each execution",
"workflowSettings.timeSavedPerExecution.nodesDetected.addMore": "Add more time saved nodes",
"workflowSettings.timeSavedPerExecution.fixedTabWarning": "There are one or more {link} calculating time saved on this workflows dynamically. {action} if you want to set a fixed time.",
"workflowSettings.timeSavedPerExecution.fixedTabWarning.link": "time saved nodes",
"workflowSettings.timeSavedPerExecution.fixedTabWarning.action": "Remove those nodes",
"workflowSettings.availableInMCP": "Available in MCP",
"workflowSettings.availableInMCP.tooltip": "Make this workflow visible to AI Agents through n8n MCP",
"workflowSettings.toggleMCP.error.title": "Error updating MCP settings",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ const hasErrorWorkflow = computed(() => {
return !!props.workflow.settings?.errorWorkflow;
});

const hasSavedTimeNodes = computed(() => {
if (!props.workflow?.nodes) return false;
return props.workflow.nodes.some((node) => node.type === 'n8n-nodes-base.savedTime');
});

const hasTimeSaved = computed(() => {
return props.workflow.settings?.timeSavedPerExecution !== undefined;
return props.workflow.settings?.timeSavedPerExecution !== undefined || hasSavedTimeNodes.value;
});

const isActivationModalOpen = computed(() => {
Expand Down
Loading
Loading