1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! `hotspot-velocity` analysis — which files are *heating up*.
//!
//! Hotspots rank all-time churn; velocity asks the forward-looking
//! question: is a file's change rate **accelerating**? A file that
//! suddenly starts churning is becoming a hotspot before its all-time
//! count says so — an early-warning signal.
//!
//! For each file the analysis compares two windows ending at the latest
//! commit in the data:
//!
//! - **recent** — the last [`RECENT_DAYS`] days,
//! - **baseline** — the [`BASELINE_DAYS`] days *before* that.
//!
//! Both are normalised to revisions-per-week (the windows have different
//! lengths) and `acceleration = recent_per_week − baseline_per_week`.
//! Positive = heating up, negative = cooling down. Subtracting rates
//! (rather than a ratio) keeps brand-new files — baseline 0, recent high
//! — at the top where they belong, instead of dividing by zero.
//!
//! ## Anchoring
//!
//! "Now" defaults to `MAX(commits.date)`, NOT wall-clock today, so the
//! result is reproducible and survives back-testing (the same anchor
//! lesson `code-age` / `stale-code` learned). A repo whose last commit
//! was a year ago still reports its final-year velocity, not all-zeros.
//! `--age-time-now <date>` overrides the anchor with that calendar date's
//! end-of-day, re-cutting both windows so a back-test reproduces the
//! velocity the repo showed on that date.
use params;
use crateFactsDb;
use crate::;
/// Length of the "recent" window in days.
///
/// A ~monthly horizon is the shortest window that smooths week-to-week commit
/// noise (weekend lulls, single-PR bursts) while still being recent enough to
/// flag an acceleration early — before the all-time revision count catches up.
/// It matches the common "last 30 days" activity horizon reviewers reason in.
pub const RECENT_DAYS: u32 = 30;
/// Length of the "baseline" window (immediately preceding recent) in days.
///
/// A quarter — three times [`RECENT_DAYS`] — gives a stable estimate of the
/// file's "normal" change cadence to compare the recent rate against: long
/// enough to average out release-cycle and vacation gaps, short enough that a
/// file which cooled off long ago is not judged against ancient churn. 90 days
/// is also the project's prevailing recent-activity horizon
/// ([`DEFAULT_WINDOW_DAYS`](crate::constants::DEFAULT_WINDOW_DAYS),
/// [`DEFAULT_DEPARTED_THRESHOLD_DAYS`](crate::constants::DEFAULT_DEPARTED_THRESHOLD_DAYS)).
pub const BASELINE_DAYS: u32 = 90;
/// One hotspot-velocity finding.
// Two windows anchored at the reproducible "now": recent = last RECENT_DAYS,
// baseline = the BASELINE_DAYS before that. Rates are per-week so the
// unequal-length windows compare fairly. Only files touched in the recent
// window are reported (a file that went fully cold is stale-code's job);
// the `>= ?` floor drops one-off noise.
//
// The `anchor` CTE resolves "now" once: the leading positional param is the
// caller's `--age-time-now` end-of-day (or NULL), and `COALESCE(…, MAX(date))`
// falls back to the latest commit when the param is NULL — so the default
// path stays byte-identical to a bare `MAX(date)` while a back-test re-cuts
// both windows. `win` derives both window edges from that single value.
//
// SQL template. The day-window placeholders `{recent}` / `{baseline}` /
// `{boundary}` are resolved by `build_sql` from RECENT_DAYS / BASELINE_DAYS
// so those constants are the single source of truth (a naive literal `30`
// / `120` / `90` sprinkled through the SQL silently ignores the consts).
// `{boundary}` = RECENT_DAYS + BASELINE_DAYS, the baseline window's far edge.
const SQL_TEMPLATE: &str = "
WITH anchor AS (
SELECT COALESCE(CAST(? AS TIMESTAMP), {now_anchor}) AS now_ts
FROM commits
),
win AS (
SELECT
now_ts,
now_ts - INTERVAL '{recent} days' AS recent_start,
now_ts - INTERVAL '{boundary} days' AS baseline_start
FROM anchor
),
recent AS (
SELECT ch.path, COUNT(ch.rev) AS revs_recent
FROM changes ch
INNER JOIN commits c ON c.rev = ch.rev
CROSS JOIN win w
WHERE c.date > w.recent_start AND c.date <= w.now_ts
GROUP BY ch.path
),
baseline AS (
SELECT ch.path, COUNT(ch.rev) AS revs_baseline
FROM changes ch
INNER JOIN commits c ON c.rev = ch.rev
CROSS JOIN win w
WHERE c.date > w.baseline_start AND c.date <= w.recent_start
GROUP BY ch.path
)
SELECT
r.path,
r.revs_recent,
COALESCE(b.revs_baseline, 0) AS revs_baseline,
r.revs_recent * 7.0 / {recent}.0 AS recent_per_week,
COALESCE(b.revs_baseline, 0) * 7.0 / {baseline}.0 AS baseline_per_week,
(r.revs_recent * 7.0 / {recent}.0)
- (COALESCE(b.revs_baseline, 0) * 7.0 / {baseline}.0) AS acceleration
FROM recent r
LEFT JOIN baseline b ON r.path = b.path
WHERE (r.revs_recent + COALESCE(b.revs_baseline, 0)) >= ?
ORDER BY acceleration DESC, revs_recent DESC, path ASC
LIMIT ?
";
/// Resolve the day-window placeholders in [`SQL_TEMPLATE`] from the
/// [`RECENT_DAYS`] / [`BASELINE_DAYS`] constants (the single source of
/// truth). `{boundary}` is the baseline window's far edge,
/// `RECENT_DAYS + BASELINE_DAYS` days back from the anchor.
/// Run the `hotspot-velocity` analysis. Returns files ranked by change
/// acceleration (heating up first).
///
/// # Errors
///
/// Returns [`crate::CodeLoreError::Analysis`] on `DuckDB` query errors.