id: storage-days-until-full-forecast
namespace: company.team
inputs:
- id: full_threshold_pct
type: INT
displayName: Full at (%)
description: The usage level treated as full. Most volumes degrade well before 100%.
defaults: 90
- id: critical_days
type: INT
displayName: Critical within (days)
defaults: 7
- id: warning_days
type: INT
displayName: Warning within (days)
defaults: 30
- id: min_r2
type: FLOAT
displayName: Minimum trend fit (R squared)
description: Below this the growth is too irregular for a straight-line
forecast, so the volume is reported as UNPREDICTABLE instead of getting a
date.
defaults: 0.7
- id: fit_window_days
type: INT
displayName: Fit window (days)
description: Only the most recent days are used for the trend, so last month's
cleanup does not hide this week's growth.
defaults: 14
- id: notify_slack
type: BOOL
displayName: Notify Slack
description: Post a failure alert to Slack. Needs the SLACK_WEBHOOK_URL secret.
defaults: false
tasks:
- id: forecast
type: io.kestra.plugin.jdbc.duckdb.Queries
description: >
Load 30 days of hourly usage samples per volume, fit a linear trend per
volume on the fit window with DuckDB regression aggregates, then project
the date each volume reaches the full threshold and classify it.
fetchType: FETCH
outputFiles:
- forecast
sql: |
SELECT setseed(0.73);
-- Demo samples: hourly used percent per volume. Replace with read_csv or a Prometheus export.
CREATE TABLE samples AS
WITH volumes(volume, start_pct, growth_per_day, noise, cleanup_day, pattern) AS (VALUES
('pg-primary-data', 58.0, 0.95, 0.4, NULL, 'steady'),
('kafka-broker-1-logs', 20.0, 1.70, 1.2, NULL, 'steady'),
('elastic-hot-02', 52.0, 0.00, 0.6, NULL, 'flat'),
('build-cache', 30.0, 2.40, 1.0, 22, 'cleanup'),
('clickhouse-shard-3', 38.0, 0.25, 0.3, NULL, 'steady'),
('object-staging', 40.0, 0.40, 14.0, NULL, 'spiky'),
('wal-archive', 40.0, 0.30, 0.2, 26, 'accelerating'),
('prometheus-tsdb', 30.0, 0.60, 0.5, NULL, 'steady'))
SELECT volume,
TIMESTAMP '2026-09-05 00:00' + to_hours(h) AS ts,
least(100, greatest(0,
start_pct
+ growth_per_day * h / 24
+ CASE WHEN pattern = 'cleanup' AND h / 24 >= cleanup_day THEN -35 ELSE 0 END
+ CASE WHEN pattern = 'accelerating' AND h / 24 >= cleanup_day THEN 2.6 * (h / 24 - cleanup_day) ELSE 0 END
+ (random() - 0.5) * 2 * noise)) AS used_pct
FROM volumes, range(0, 30 * 24) t(h);
CREATE TABLE forecast AS
WITH recent AS (
SELECT volume, epoch(ts) / 86400.0 AS day, used_pct
FROM samples
WHERE ts >= (SELECT max(ts) FROM samples) - to_days({{ inputs.fit_window_days }})
), fit AS (
SELECT volume,
regr_slope(used_pct, day) AS pct_per_day,
regr_intercept(used_pct, day) AS intercept,
regr_r2(used_pct, day) AS r2,
max(day) AS last_day,
arg_max(used_pct, day) AS current_pct
FROM recent GROUP BY volume
)
SELECT volume,
round(current_pct, 1) AS current_pct,
round(pct_per_day, 2) AS pct_per_day,
round(r2, 2) AS r2,
CASE WHEN pct_per_day > 0.05 AND r2 >= {{ inputs.min_r2 }} THEN round(({{ inputs.full_threshold_pct }} - (intercept + pct_per_day * last_day)) / pct_per_day, 1) END AS days_until_full,
CASE
WHEN current_pct >= {{ inputs.full_threshold_pct }} THEN 'FULL'
WHEN r2 < {{ inputs.min_r2 }} AND pct_per_day > 0.05 THEN 'UNPREDICTABLE'
WHEN pct_per_day <= 0.05 THEN 'OK'
WHEN ({{ inputs.full_threshold_pct }} - (intercept + pct_per_day * last_day)) / pct_per_day <= {{ inputs.critical_days }} THEN 'CRITICAL'
WHEN ({{ inputs.full_threshold_pct }} - (intercept + pct_per_day * last_day)) / pct_per_day <= {{ inputs.warning_days }} THEN 'WARNING'
ELSE 'OK'
END AS status,
CASE WHEN pct_per_day > 0.05 AND r2 >= {{ inputs.min_r2 }} THEN strftime(to_timestamp((last_day + ({{ inputs.full_threshold_pct }} - (intercept + pct_per_day * last_day)) / pct_per_day) * 86400), '%Y-%m-%d') END AS full_on
FROM fit;
COPY (SELECT * FROM forecast ORDER BY days_until_full NULLS LAST) TO '{{ outputFiles.forecast }}' (HEADER, DELIMITER ',');
SELECT * FROM forecast ORDER BY CASE status WHEN 'FULL' THEN 0 WHEN 'CRITICAL' THEN 1 WHEN 'WARNING' THEN 2 WHEN 'UNPREDICTABLE' THEN 3 ELSE 4 END, days_until_full NULLS LAST;
- id: summary
type: io.kestra.plugin.core.output.OutputValues
values:
rows: "{{ (outputs.forecast.outputs | last).rows | toJson }}"
urgent: "{{ (outputs.forecast.outputs | last).rows | jq('map(select(.status ==
\"FULL\" or .status == \"CRITICAL\")) | map(.volume + \" \" + .status +
\" in \" + ((.days_until_full // 0) | tostring) + \" days (\" +
(.full_on // \"now\") + \")\") | join(\"; \")') | first }}"
warnings: "{{ (outputs.forecast.outputs | last).rows | jq('map(select(.status ==
\"WARNING\")) | map(.volume + \" full on \" + .full_on) | join(\"; \")')
| first }}"
table: "{{ (outputs.forecast.outputs | last).rows | jq('map(.status + \" \" +
.volume + \" \" + (.current_pct | tostring) + \"% now, \" +
(.pct_per_day | tostring) + \"%/day, R2 \" + (.r2 | tostring) + (if
.full_on then \", full on \" + .full_on + \" (\" + (.days_until_full |
tostring) + \" days)\" else \"\" end)) | join(\"\\n\")') | first }}"
unpredictable: "{{ (outputs.forecast.outputs | last).rows |
jq('map(select(.status == \"UNPREDICTABLE\")) | map(.volume + \" (R2 \"
+ (.r2 | tostring) + \")\") | join(\"; \")') | first }}"
- id: log_forecast
type: io.kestra.plugin.core.log.Log
message: "Storage forecast, full at {{ inputs.full_threshold_pct }}%, trend over
the last {{ inputs.fit_window_days }} days.
{{ outputs.summary.values.table }}"
- id: page_if_urgent
type: io.kestra.plugin.core.flow.If
description: Anything full or critical ends the run as FAILED, so on-call is
paged through the errors block. Warnings are only reported.
condition: "{{ outputs.summary.values.urgent != '' }}"
then:
- id: publish_forecast
type: io.kestra.plugin.core.namespace.UploadFiles
namespace: "{{ flow.namespace }}"
filesMap:
capacity/storage-forecast.csv: "{{ outputs.forecast.outputFiles.forecast }}"
- id: page
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Storage will be full soon: {{ outputs.summary.values.urgent }}.
Warnings: {{ outputs.summary.values.warnings }}"
else:
- id: log_ok
type: io.kestra.plugin.core.log.Log
message: "Nothing full within {{ inputs.critical_days }} days. Warnings: {{
outputs.summary.values.warnings == '' ? 'none' :
outputs.summary.values.warnings }}. Unpredictable: {{
outputs.summary.values.unpredictable == '' ? 'none' :
outputs.summary.values.unpredictable }}."
errors:
- id: alert_flow_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
runIf: '{{ inputs.notify_slack }}'
description: Pages Slack when a volume is full or critical, and when the
forecast itself could not run.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{{ {'text': 'Storage days-until-full forecast failed (execution ' ~ execution.id ~ '). ' ~ (outputs.summary.values.urgent ?? 'The forecast did not complete, so the volume state is unknown. Check the logs.')} | toJson }}
triggers:
- id: every_6_hours
type: io.kestra.plugin.core.trigger.Schedule
disabled: true
cron: "0 */6 * * *"