0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 04:02:30 +08:00
discourse/plugins/automation/lib/discourse_automation
Régis Hanol 527a13566f
DEV: Add usage metrics for workflows, automations, and data explorer (#41437)
Adds basic usage metrics for **workflows**, **automations**, and **data
explorer** so we can measure adoption across all sites, following the
pattern established for Kanban (dev topic `t/185911`).

Each plugin registers stats via the freeform `stat_type` API, so the
values flow automatically into our site statistics pipeline. No
infrastructure changes are needed.

## Metrics

For each feature: `total` (running count), `created`, `edited`,
`executed` (distinct objects run), and `executions` (total run count) —
each windowed over `last_day / 7_days / 30_days / previous_30_days`,
with a lifetime `count` on `total` and `executions`. Resulting columns:

| | Workflows | Automations | Data Explorer |
|---|---|---|---|
| total | `workflows_total_count` | `automations_total_count` |
`de_queries_total_count` |
| created | `workflows_created_*` | `automations_created_*` |
`de_queries_created_*` |
| edited | `workflows_edited_*` | `automations_edited_*` |
`de_queries_edited_*` |
| executed | `workflows_executed_*` | `automations_executed_*` |
`de_queries_executed_*` |
| executions | `workflows_executions_*` | `automations_executions_*` |
`de_executions_*` |

## How executions are counted

Automations already have a daily rollup (`discourse_automation_stats`),
so they reuse it.

Workflows and Data Explorer get a small daily rollup table each
(`discourse_workflows_execution_stats`, `data_explorer_query_stats`)
recording `total_runs` per object per day. This deliberately avoids two
traps:

- **Workflow executions are purged** after
`workflow_executions_retention_days` (default 30), and the purged rows
carry heavy payloads (full node-graph snapshot + step I/O). Counting
from a lightweight rollup keeps `previous_30_days`/lifetime metrics
valid without retaining those payloads, and decouples the metrics from a
per-site retention setting.
- **Data Explorer has no run log** — only a single `last_run_at`. The
rollup gives us real execution counts and a durable `previous_30_days`
window.

Workflow runs are recorded via an `after_create` on `Execution` (one row
per execution, rate-limited executions excluded). Data Explorer runs go
through a new `Query#record_run!` used by the three run sites.

## Data Explorer `updated_at` fix

Query runs previously did `query.update!(last_run_at:)`, which bumped
`updated_at` on every run and made "edited" indistinguishable from
"executed". `record_run!` now uses `update_columns`, so `updated_at`
reflects genuine edits again. Default (unpersisted) queries are still
persisted on first run as before, and the `DeleteHiddenQueries` job is
unaffected (it also gates on `last_run_at`).

## Core change

`register_stat` deduplicated by name only, so the three plugins couldn't
all register generic names like `total`/`executions` — whichever
activated first won and the rest were silently dropped. The identity
check now includes `stat_type`, so a name can be reused across stat
types (columns stay unique because they are prefixed with the stat
type). Kanban avoided this only because its names happened to be
globally unique.

## Notes for review

- **Single `de` stat_type** (yielding `de_queries_*` and
`de_executions_*`) rather than splitting into
`de_queries`/`de_executions` — happy to change if you'd rather query by
two separate `stat_type_freeform` values. cc measurement folks.
- `structure.sql` and model annotations were hand-updated (local box is
on PG17, which can't run the temp-DB dump/annotate tasks); CI will
verify.

## Tests

- New specs for all three `Statistics` modules and both rollup models
(`.log`, the `after_create`/`record_run!` hooks, the `updated_at`
behavior).
- New core spec covering same-name/different-`stat_type` registration.
2026-07-13 11:41:58 +02:00
..
scripts FEATURE: Anonymous shadow sessions can outlive suspension of the master account (#40156) 2026-05-20 12:07:56 +08:00
triggers FIX: Automation Plugin - Monthly recurring triggers shifting dates and skipping months (BYDAY to BYMONTHDAY) (#40747) 2026-06-16 11:46:43 -04:00
engine.rb DEV: Clean up scope resolution operators in plugins (#34979) 2025-09-30 14:36:34 +02:00
event_handlers.rb DEV: Add automatically closed topics event trigger and adapt the automation plugin's auto tag_topic_script accordingly (#39235) 2026-04-14 14:50:36 -05:00
logger.rb FEATURE: Track automation errors and improve logging (#36938) 2026-01-19 18:50:13 +01:00
plugin_instance_extension.rb
post_extension.rb DEV: Enable Style/RedundantSelf rubocop rule (#40098) 2026-05-19 19:27:45 +02:00
scriptable.rb DEV: Enable Style/RedundantParentheses rubocop rule (#40095) 2026-05-19 15:48:09 +02:00
scripts.rb FEATURE: Introduce set_topic_timer automation script (#36660) 2025-12-23 09:46:52 +00:00
stalled_topic_finder.rb
statistics.rb DEV: Add usage metrics for workflows, automations, and data explorer (#41437) 2026-07-13 11:41:58 +02:00
triggerable.rb FIX: Improve automation on/off toggle (#33482) 2025-08-14 10:16:20 +03:00
triggers.rb FEATURE: New automation for emailing specific users when a post is flagged (#36580) 2025-12-11 10:31:13 +08:00