/** * Workflow Analytics — the Workflow Automation landing page. * * The module had no analytics at all. The list answered "did it run" with a * date and a lifetime counter, and the error log answered "what broke" one * incident at a time. Neither answers the question that actually matters for * unattended automation: *is this working*. A workflow that has run 24 times * and failed 24 times looks identical, on the list, to one that has never * missed. * * Every number here comes from rows the engine already wrote — execution * outcomes, step tallies, timestamps, and per-step output statuses. Nothing * new is instrumented; it was all being recorded and none of it was read. */ import { useState, useEffect, useCallback } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { ResponsiveContainer, BarChart, Bar, XAxis, YAxis, Tooltip, CartesianGrid, Legend, } from 'recharts'; import { apiGet } from '../../../utils/api'; import Card from '../../common/Card'; import Loader from '../../common/Loader'; import Notice from '../../common/Notice'; import EmptyState from '../../common/EmptyState'; import ArcGauge from '../../common/ArcGauge'; import { RankRow } from '../../common/RankBar'; import { formatDateTime } from '../../../utils/datetime'; import { Button } from '../../common/WpComponents'; const RANGES = [ 7, 30, 90 ]; /* * Recharts writes stroke and fill as SVG presentation attributes, where a * `var()` never resolves — so these are literals. Workflow Automation takes no * room light: it is house forest green, like the lobby, because it is the room * that operates all the others rather than one of them. * * The outcome ramp is the system's own status palette, not a decorative scale. * Success is leaf green, partial is the warning amber, failed is the error red, * and skipped is deliberately desaturated — a skipped run is not a failure, it * is the free-plan cap refusing to start one, and colouring it like damage * would blame the workflow for the plan. */ const OUTCOME = { success: '#2E7D32', partial: '#F9A825', failed: '#D32F2F', skipped: '#cbd8cc', }; const HOUSE = '#1B5E20'; const CHART_GRID = '#eaf2ea'; const CHART_TICK = '#5f7562'; const TOOLTIP_STYLE = { borderRadius: 8, border: '1px solid var(--aime-border-light, #e3ece4)', boxShadow: '0 8px 24px rgba(20, 45, 22, 0.12)', fontSize: 13, }; const WF_STATUS_COLORS = { active: '#2E7D32', paused: '#F9A825', draft: '#8fa893', }; const WF_STATUS_LABELS = { active: __( 'Active', 'ai-marketing-expert' ), paused: __( 'Paused', 'ai-marketing-expert' ), draft: __( 'Draft', 'ai-marketing-expert' ), }; const isoDay = ( date ) => { const y = date.getFullYear(); const m = String( date.getMonth() + 1 ).padStart( 2, '0' ); const d = String( date.getDate() ).padStart( 2, '0' ); return `${ y }-${ m }-${ d }`; }; /* * The endpoint groups by date and returns only days that had a run, so a * 30-day window with four busy days comes back as four points. Plotted raw, * three weeks of silence between two of them renders exactly as wide as a * single overnight gap and the axis lies about the shape of the work. * Scaffolding every day back in at zero is what makes the spacing true — and * for a schedule-driven module the *gaps* are the finding, so this matters more * here than anywhere else in the plugin. * * Rows outside the scaffold are kept rather than dropped: a timezone difference * between PHP's gmdate and the browser can put a real run one day either side * of the window, and silently losing it is worse than one extra tick. */ const buildSeries = ( days, rows ) => { const byDate = {}; const blank = () => ( { success: 0, partial: 0, failed: 0, skipped: 0 } ); const cursor = new Date(); cursor.setHours( 0, 0, 0, 0 ); for ( let i = days - 1; i >= 0; i-- ) { const day = new Date( cursor ); day.setDate( day.getDate() - i ); const key = isoDay( day ); byDate[ key ] = { date: key, ...blank() }; } ( rows || [] ).forEach( ( row ) => { const key = String( row.date || '' ).slice( 0, 10 ); if ( ! key ) { return; } if ( ! byDate[ key ] ) { byDate[ key ] = { date: key, ...blank() }; } if ( byDate[ key ][ row.status ] !== undefined ) { byDate[ key ][ row.status ] = Number( row.count ) || 0; } } ); return Object.values( byDate ) .sort( ( a, b ) => a.date.localeCompare( b.date ) ) .map( ( point ) => ( { ...point, label: point.date.slice( 5 ) } ) ); }; /** * Seconds as something a person reads at a glance. Runs here span a two-second * notification and a six-minute article generation, so a raw seconds column * would make the reader do the division on every row. * * @param {number} seconds Duration in seconds. * @return {string} Formatted duration. */ const formatDuration = ( seconds ) => { const total = Math.max( 0, Math.round( Number( seconds ) || 0 ) ); if ( total === 0 ) { return '—'; } if ( total < 60 ) { /* translators: %d: seconds. */ return sprintf( __( '%ds', 'ai-marketing-expert' ), total ); } const mins = Math.floor( total / 60 ); const secs = total % 60; if ( mins < 60 ) { return secs /* translators: 1: minutes, 2: seconds. */ ? sprintf( __( '%1$dm %2$ds', 'ai-marketing-expert' ), mins, secs ) /* translators: %d: minutes. */ : sprintf( __( '%dm', 'ai-marketing-expert' ), mins ); } /* translators: 1: hours, 2: minutes. */ return sprintf( __( '%1$dh %2$dm', 'ai-marketing-expert' ), Math.floor( mins / 60 ), mins % 60 ); }; const WorkflowAnalytics = ( { onNavigate } ) => { const [ loading, setLoading ] = useState( true ); const [ error, setError ] = useState( '' ); const [ stats, setStats ] = useState( null ); const [ days, setDays ] = useState( 30 ); const load = useCallback( async ( range ) => { setLoading( true ); try { const res = await apiGet( `/workflow-automation/analytics?days=${ range }` ); setStats( res ); setError( '' ); } catch ( e ) { setError( e?.message || __( 'Failed to load analytics.', 'ai-marketing-expert' ) ); } finally { setLoading( false ); } }, [] ); useEffect( () => { load( days ); }, [ load, days ] ); if ( loading && ! stats ) { return ; } const totals = stats?.totals || {}; const runsFinished = Number( totals.runs_finished || 0 ); const failedRuns = Number( totals.failed || 0 ); const partialRuns = Number( totals.partial || 0 ); const skippedRuns = Number( totals.skipped || 0 ); const successRate = Math.max( 0, Math.min( Number( totals.success_rate || 0 ), 100 ) ); const stepRate = Math.max( 0, Math.min( Number( totals.step_rate || 0 ), 100 ) ); const handsOff = Math.max( 0, Math.min( Number( totals.hands_off_rate || 0 ), 100 ) ); const stepsFailed = Number( totals.steps_failed || 0 ); const stepsSucceeded = Number( totals.steps_succeeded || 0 ); const manualRuns = Number( totals.manual_runs || 0 ); const unattendedRuns = Number( totals.unattended_runs || 0 ); const avgSeconds = Number( totals.avg_seconds || 0 ); const maxSeconds = Number( totals.max_seconds || 0 ); const idleActive = Number( totals.idle_active || 0 ); const wfStatus = stats?.workflow_status || {}; const totalWorkflows = Object.values( wfStatus ).reduce( ( sum, n ) => sum + Number( n || 0 ), 0 ); const failures = stats?.action_failures || []; const failurePeak = Math.max( 0, ...failures.map( ( f ) => Number( f.failures ) || 0 ) ); const workflows = stats?.workflows || []; const series = buildSeries( days, stats?.runs_by_day ); const seriesHasData = series.some( ( p ) => p.success > 0 || p.partial > 0 || p.failed > 0 || p.skipped > 0 ); // Roughly ten ticks whatever the range, so 90 days does not print 90 dates. const tickInterval = Math.max( 0, Math.ceil( series.length / 10 ) - 1 ); const usage = stats?.runs_usage || {}; const usageLimit = Number( usage.limit || 0 ); const usageUsed = Number( usage.used || 0 ); const usagePercent = usageLimit > 0 ? Math.min( 100, ( usageUsed / usageLimit ) * 100 ) : 0; /* * The one number on this page worth acting on today. A failure count leads * when there is one, because that is the thing to go and fix; with a clean * window the headline falls back to throughput, since a card that spends its * largest type on a zero has said nothing. */ const headlineValue = stepsFailed > 0 ? stepsFailed.toLocaleString() : stepsSucceeded.toLocaleString(); const headlineCaption = stepsFailed > 0 ? __( 'Steps failed in this window', 'ai-marketing-expert' ) : __( 'Steps completed, none failed', 'ai-marketing-expert' ); const rangeToggle = (
{ RANGES.map( ( option ) => ( ) ) }
); return (
{ error && setError( '' ) } /> }

{ __( 'Workflow Automation', 'ai-marketing-expert' ) }

{ rangeToggle }
{ /* * Three gauges, each a bounded rate with a real full. Reliability leads * because it is the module's whole promise; step completion sits beside * it because the two disagree in the useful case — a run that died on * its first step and a run that lost one step of eight both count as a * single failure at run level, and only the step rate tells them apart. */ }
{ __( 'Reliability', 'ai-marketing-expert' ) } { runsFinished > 0 ? `${ Math.round( successRate ) }%` : '—' } { __( 'Clean runs', 'ai-marketing-expert' ) }
{ __( 'Runs', 'ai-marketing-expert' ) } { runsFinished.toLocaleString() }
{ __( 'Failed', 'ai-marketing-expert' ) } { failedRuns.toLocaleString() }
{ __( 'Steps', 'ai-marketing-expert' ) } { stepsSucceeded + stepsFailed > 0 ? `${ Math.round( stepRate ) }%` : '—' } { __( 'Steps completed', 'ai-marketing-expert' ) }

{ partialRuns > 0 ? sprintf( /* translators: %s: number of partly failed runs. */ __( '%s runs finished with some steps missing.', 'ai-marketing-expert' ), partialRuns.toLocaleString() ) : __( 'Every run that started got all the way through its steps.', 'ai-marketing-expert' ) }

{ /* * The module's actual value proposition, and nothing else on the page * measures it: automation you have to press a button for is not * automation. A low number here with a high reliability score means * the workflows work and nobody has switched them on. */ } { __( 'Hands off', 'ai-marketing-expert' ) } { runsFinished > 0 ? `${ Math.round( handsOff ) }%` : '—' } { __( 'Ran on their own', 'ai-marketing-expert' ) }

{ runsFinished > 0 ? sprintf( /* translators: 1: unattended runs, 2: manual runs. */ __( '%1$s fired from a schedule or trigger, %2$s from Run now.', 'ai-marketing-expert' ), unattendedRuns.toLocaleString(), manualRuns.toLocaleString() ) : __( 'No finished runs in this window yet.', 'ai-marketing-expert' ) }

{ seriesHasData ? ( { /* * Stacked, not grouped. A day's runs are one quantity split * by how they ended, so the bar's full height has to stay * readable as "runs that day" — grouping would make the eye * add four columns to recover a number it should just see. * Stacked bottom-up in order of severity so the failures sit * on the baseline, where they are compared against a * straight edge instead of a ragged one. */ } ) : ( 0 ? __( 'No workflow finished a run in the selected range. Widen it above, or check that your workflows are active.', 'ai-marketing-expert' ) : __( 'Create a workflow and its runs will chart here.', 'ai-marketing-expert' ) } /> ) } { seriesHasData && avgSeconds > 0 && (

{ sprintf( /* translators: 1: average run duration, 2: slowest run duration. */ __( 'Runs take %1$s on average; the slowest took %2$s.', 'ai-marketing-expert' ), formatDuration( avgSeconds ), formatDuration( maxSeconds ) ) }

) }
{ /* * Which action breaks, not which run broke — the error log already * lists incidents one at a time, and a list of incidents never adds * up to a cause. Ranked by failure count with the rate beside it, * because five failures out of nine hundred attempts and two out of * two are different problems and only one of them is the action's * fault. */ }
{ headlineValue } { headlineCaption }
{ failures.length > 0 ? ( <>
    { failures.map( ( f ) => ( 0 ? ( ( Number( f.failures ) || 0 ) / failurePeak ) * 100 : 0 } color={ OUTCOME.failed } suffix={ sprintf( /* translators: %s: failure rate as a percentage. */ __( '%s%%', 'ai-marketing-expert' ), Math.round( Number( f.failure_rate ) || 0 ) ) } /> ) ) }

{ __( 'Failures per action, with the share of its attempts that failed.', 'ai-marketing-expert' ) } { ' ' }

) : (

{ stepsSucceeded > 0 ? __( 'No step failed in this window. Nothing here needs your attention.', 'ai-marketing-expert' ) : __( 'No steps have run in this window, so there is nothing to diagnose yet.', 'ai-marketing-expert' ) }

) }
{ /* * Full width, because it is the only place on the page that names a * workflow — everything above is the module in aggregate, and an * aggregate never tells you which one to open. */ } { workflows.length > 0 ? ( <>
{ workflows.map( ( wf ) => ( onNavigate( 'history', { id: wf.id } ) } > { /* * A bar, not another percentage column. Nine rows of * "94.2%" is nine numbers to read in sequence; nine * bars on a shared baseline is one shape, and the * odd one out is visible before it is read. */ } ) ) }
{ __( 'Workflow', 'ai-marketing-expert' ) } { __( 'Status', 'ai-marketing-expert' ) } { __( 'Runs', 'ai-marketing-expert' ) } { __( 'Clean', 'ai-marketing-expert' ) } { __( 'Avg time', 'ai-marketing-expert' ) } { __( 'Last run', 'ai-marketing-expert' ) }
{ wf.name || __( '(untitled)', 'ai-marketing-expert' ) } { WF_STATUS_LABELS[ wf.status ] || wf.status } { Number( wf.runs || 0 ).toLocaleString() } 0 ? OUTCOME.failed : OUTCOME.success, } } /> { `${ Math.round( Number( wf.success_rate ) || 0 ) }%` } { formatDuration( wf.avg_seconds ) } { formatDateTime( wf.last_run_at ) }
{ idleActive > 0 && (

{ sprintf( /* translators: %s: number of active workflows with no runs. */ __( '%s active workflows did not run at all in this window — check their schedule or trigger.', 'ai-marketing-expert' ), idleActive.toLocaleString() ) }

) } ) : ( 0 ? sprintf( /* translators: %s: number of workflows. */ __( 'You have %s workflows, and none of them finished a run in the selected range. A workflow only fires while it is active.', 'ai-marketing-expert' ), totalWorkflows.toLocaleString() ) : __( 'Build a workflow and its run history will be measured here.', 'ai-marketing-expert' ) } /> ) }
); }; export default WorkflowAnalytics;