metering 0.25.0

German energy metering quantities: DST-correct Europe/Berlin and 06:00-Gastag calendar arithmetic, exact decimal values, and the market's own code lists. Zero I/O, no async, no clock.
Documentation
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
//! Plausibilisierung — the content rules a validated [`Series`] is checked
//! against, and the [`Report`] they produce.
//!
//! ## Rules
//!
//! | [`Rule`] | Severity | What it catches | Basis |
//! |---|---|---|---|
//! | `GAP` | error | a slot of the declared period with no value — head, tail or interior | MeteringCode 2006 A7.1.2.1/A7.1.2.2 |
//! | `NON_BILLABLE` | error | a value flagged `Faulty` or `Unknown` | A7.1.2.3 (status information) |
//! | `NEGATIVE` | error | a negative quantity on a MaKo channel | Codeliste OBIS 2.5c § 2.1 |
//! | `CAPACITY` | error | average power above the plant's capacity | A7.2 (Grenzleistung der Anlage) |
//! | `FUTURE` | blocking | a slot that has not ended at the caller's `now` | — |
//! | `LAENGSVERGLEICH` | blocking | Σ load curve ≠ register advance beyond a tolerance | A7.2 (Längsvergleich) |
//! | `OUTSIDE_PERIOD` | warning | data outside the declared period | — |
//! | `ZERO_RUN` | warning | a run of zeros at least as long as the configured duration | A7.2 (Prüfung auf Nullwerte) |
//! | `STALE` | warning | a non-zero value frozen for a window of slots | pvanalytics `stale_values_diff` |
//! | `SPIKE` | warning | a value far from its neighbours (Hampel, MAD floor) | A7.2 (relative Messwerte) |
//! | `SEASONAL` | warning | a value far from the same slot on comparable days | A7.2 (relative Messwerte) |
//!
//! [`substitute`](crate::vee::substitute::substitute) repairs the slots an
//! **error** covers, never a **blocking** finding. The [`Grade`] follows from
//! the findings: `A` none, `B` warnings only, `C` errors, `F` anything blocking.
//!
//! MeteringCode 2006 Anlage 7 (superseded by VDE-AR-N 4400) names the checks,
//! not their limits; the limits are [`Rules`] parameters with presets per [`Sparte`].
//!
//! ```rust
//! use metering::prelude::*;
//! use metering::vee::validation::{Grade, Rule};
//! use time::{Duration, macros::{date, datetime}};
//!
//! let day = DayBoundary::Strom.day(date!(2026 - 06 - 01)).unwrap();
//! let slots = (0..96)
//!     .filter(|i| *i != 40) // one quarter-hour never arrived
//!     .map(|i| MeterInterval::quarter_hour(day.start() + Duration::minutes(15 * i), dec!(1.2), QualityFlag::Measured))
//!     .collect::<Result<Vec<_>, _>>()?;
//! let series = Series::new(Resolution::QUARTER_HOUR, DayBoundary::Strom, slots)?;
//!
//! let report = validate(&series, &Rules::strom(day, datetime!(2026-07-01 0:00 UTC), None));
//! assert_eq!(report.grade(), Grade::C);
//! assert_eq!(report.by_rule(Rule::Gap).count(), 1);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use rust_decimal::{Decimal, dec};
use time::{Duration, OffsetDateTime};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::precision::{PERCENT_DP, PERCENT_STRATEGY};
use crate::series::Series;
use crate::series::interval::{MeterInterval, Sparte};
use crate::series::reading::MeterReading;
use crate::time::calendar::{DayKind, Period};
use crate::time::holiday::Bundesland;
use crate::vee::{Data, hampel, is_actual, references, slots};

/// A content rule — see the [module table](self#rules).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Rule {
    /// A slot of the declared period with no value.
    Gap,
    /// A value whose quality flag is not billable.
    NonBillable,
    /// A negative quantity.
    Negative,
    /// Average power above the plant capacity.
    Capacity,
    /// A slot that had not ended at `now`.
    Future,
    /// Σ load curve against the register advance.
    Laengsvergleich,
    /// Data outside the declared period.
    OutsidePeriod,
    /// A run of zeros.
    ZeroRun,
    /// A frozen non-zero value.
    Stale,
    /// A value far from its neighbours.
    Spike,
    /// A value far from the same slot on comparable days.
    Seasonal,
}

impl Rule {
    /// Every rule, in declaration order.
    pub const ALL: [Self; 11] = [
        Self::Gap,
        Self::NonBillable,
        Self::Negative,
        Self::Capacity,
        Self::Future,
        Self::Laengsvergleich,
        Self::OutsidePeriod,
        Self::ZeroRun,
        Self::Stale,
        Self::Spike,
        Self::Seasonal,
    ];

    /// Stable code; the `serde` tag and [`FromStr`](std::str::FromStr) input.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Gap => "GAP",
            Self::NonBillable => "NON_BILLABLE",
            Self::Negative => "NEGATIVE",
            Self::Capacity => "CAPACITY",
            Self::Future => "FUTURE",
            Self::Laengsvergleich => "LAENGSVERGLEICH",
            Self::OutsidePeriod => "OUTSIDE_PERIOD",
            Self::ZeroRun => "ZERO_RUN",
            Self::Stale => "STALE",
            Self::Spike => "SPIKE",
            Self::Seasonal => "SEASONAL",
        }
    }

    /// How serious a finding of this rule is; fixed per rule.
    #[must_use]
    pub const fn severity(self) -> Severity {
        match self {
            Self::Gap | Self::NonBillable | Self::Negative | Self::Capacity => Severity::Error,
            Self::Future | Self::Laengsvergleich => Severity::Blocking,
            Self::OutsidePeriod | Self::ZeroRun | Self::Stale | Self::Spike | Self::Seasonal => {
                Severity::Warning
            }
        }
    }
}

/// How serious a finding is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Severity {
    /// Worth a look; the values stay billable.
    Warning,
    /// The slots it covers must not be billed as they are; a substitute repairs them.
    Error,
    /// Not repairable by a substitute: the series as a whole needs clarifying.
    Blocking,
}

impl Severity {
    /// Every severity, least to most serious.
    pub const ALL: [Self; 3] = [Self::Warning, Self::Error, Self::Blocking];

    /// Stable code; the `serde` tag and [`FromStr`](std::str::FromStr) input.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Warning => "WARNING",
            Self::Error => "ERROR",
            Self::Blocking => "BLOCKING",
        }
    }
}

/// The verdict over a [`Report`] — a pure function of its findings.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Grade {
    /// No finding.
    A,
    /// Warnings only: bill it and note it.
    B,
    /// Errors a substitute can repair.
    C,
    /// A blocking finding.
    F,
}

impl Grade {
    /// Every grade, best first.
    pub const ALL: [Self; 4] = [Self::A, Self::B, Self::C, Self::F];

    /// Stable code; the `serde` tag and [`FromStr`](std::str::FromStr) input.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::A => "A",
            Self::B => "B",
            Self::C => "C",
            Self::F => "F",
        }
    }

    /// The grade of a set of findings.
    #[must_use]
    pub fn of<'a>(findings: impl IntoIterator<Item = &'a Finding>) -> Self {
        match findings.into_iter().map(|f| f.rule.severity()).max() {
            None => Self::A,
            Some(Severity::Warning) => Self::B,
            Some(Severity::Error) => Self::C,
            Some(Severity::Blocking) => Self::F,
        }
    }
}

crate::ids::codes::string_codes! {
    Rule;
    Severity;
    Grade;
}

/// One finding, over the half-open span `[from, to)` it concerns.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub struct Finding {
    /// The rule that fired.
    pub rule: Rule,
    /// First instant concerned (UTC).
    #[cfg_attr(feature = "serde", serde(with = "crate::wire::rfc3339"))]
    pub from: OffsetDateTime,
    /// End of the span concerned (UTC, exclusive).
    #[cfg_attr(feature = "serde", serde(with = "crate::wire::rfc3339"))]
    pub to: OffsetDateTime,
    /// The observed quantity: the slot's value, or Σ load curve for
    /// `LAENGSVERGLEICH`.
    #[cfg_attr(feature = "serde", serde(with = "crate::wire::decimal_option"))]
    pub value: Option<Decimal>,
    /// What it was compared with: the median, the capacity in kW, the
    /// register advance, or the count of missing slots for `GAP`.
    #[cfg_attr(feature = "serde", serde(with = "crate::wire::decimal_option"))]
    pub reference: Option<Decimal>,
    /// A sentence for a human.
    pub message: String,
}

impl Finding {
    fn new(rule: Rule, from: OffsetDateTime, to: OffsetDateTime, message: String) -> Self {
        Self {
            rule,
            from,
            to,
            value: None,
            reference: None,
            message,
        }
    }

    fn data(mut self, value: Option<Decimal>, reference: Option<Decimal>) -> Self {
        self.value = value;
        self.reference = reference;
        self
    }

    /// The finding's severity — [`Rule::severity`].
    #[must_use]
    pub const fn severity(&self) -> Severity {
        self.rule.severity()
    }
}

/// How much of a period a series covers, both as durations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub struct Coverage {
    /// Time covered by intervals, clipped to the period.
    pub covered: Duration,
    /// The period's length.
    pub expected: Duration,
}

impl Coverage {
    /// Covered over expected, in percent, rounded to [`PERCENT_DP`]. `None` for an empty period.
    #[must_use]
    pub fn pct(&self) -> Option<Decimal> {
        let expected = self.expected.whole_seconds();
        if expected <= 0 {
            return None;
        }
        Some(
            (Decimal::from(self.covered.whole_seconds()) * Decimal::ONE_HUNDRED
                / Decimal::from(expected))
            .round_dp_with_strategy(PERCENT_DP, PERCENT_STRATEGY),
        )
    }

    /// `true` when every instant of the period is covered.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.covered == self.expected
    }
}

/// Each interval `include` accepts, clipped to `period` and summed (a [`Series`] has no overlaps).
pub(crate) fn coverage(
    series: &Series,
    period: &Period,
    include: impl Fn(&MeterInterval) -> bool,
) -> Coverage {
    let covered = series
        .iter()
        .filter(|iv| include(iv))
        .map(|iv| {
            let from = iv.from().max(period.start());
            let to = iv.to().min(period.end());
            (to - from).max(Duration::ZERO)
        })
        .fold(Duration::ZERO, |sum, d| sum + d);
    Coverage {
        covered,
        expected: period.duration(),
    }
}

/// What [`validate`] found.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub struct Report {
    /// The period the series was declared to cover.
    pub period: Period,
    /// The findings, by start instant, then rule.
    pub findings: Vec<Finding>,
    /// The rules that ran; [`skipped`](Self::skipped) names the rest.
    pub evaluated: Vec<Rule>,
    /// Delivered intervals (any quality) against the period.
    pub coverage: Coverage,
    /// Fingerprint of the validated series: interval count and span.
    pub(crate) len: usize,
    #[cfg_attr(feature = "serde", serde(with = "crate::wire::rfc3339_option"))]
    pub(crate) first: Option<OffsetDateTime>,
    #[cfg_attr(feature = "serde", serde(with = "crate::wire::rfc3339_option"))]
    pub(crate) end: Option<OffsetDateTime>,
}

impl Report {
    /// `true` when this report was produced for `series`.
    pub(crate) fn describes(&self, series: &Series) -> bool {
        self.len == series.len() && self.first.zip(self.end) == series.span()
    }

    /// `A` none, `B` warnings only, `C` errors, `F` blocking — see [`Grade::of`].
    #[must_use]
    pub fn grade(&self) -> Grade {
        Grade::of(&self.findings)
    }

    /// The rules that did not run — switched off, or stopped by the data.
    #[must_use]
    pub fn skipped(&self) -> Vec<Rule> {
        Rule::ALL
            .into_iter()
            .filter(|r| !self.evaluated.contains(r))
            .collect()
    }

    /// The findings of one rule.
    pub fn by_rule(&self, rule: Rule) -> impl Iterator<Item = &Finding> {
        self.findings.iter().filter(move |f| f.rule == rule)
    }

    /// The findings at or above `severity`.
    pub fn at_least(&self, severity: Severity) -> impl Iterator<Item = &Finding> {
        self.findings
            .iter()
            .filter(move |f| f.severity() >= severity)
    }

    /// `true` when nothing was found.
    #[must_use]
    pub fn is_clean(&self) -> bool {
        self.findings.is_empty()
    }
}

/// The rule set [`validate`] applies: a preset per [`Sparte`], the facts only
/// the caller knows, and setters per rule.
///
/// The facts are constructor arguments: the declared `period` (head and tail
/// gaps, coverage), `now` (this crate reads no clock) and the plant's
/// `capacity_kw` (`None` switches `CAPACITY` off, listed in [`Report::skipped`]).
///
/// | Preset | zero run | stale | spike half-window | Hampel `t` | MAD floor |
/// |---|---|---|---|---|---|
/// | Strom | 1 h | 6 values, exact | 12 | 6 | 0.01 |
/// | Gas | 48 h | off | 12 | 6 | 0.01 |
/// | Wärme, Wasser | 720 h | off | 12 | 6 | 0.05, 0.001 |
///
/// No source states these numbers; each is a setter. The floor is in channel units per slot.
#[derive(Debug, Clone, PartialEq)]
pub struct Rules {
    period: Period,
    now: OffsetDateTime,
    capacity_kw: Option<Decimal>,
    negative: bool,
    zero_run: Option<Duration>,
    stale: Option<(usize, Decimal)>,
    spike: Option<usize>,
    threshold: Decimal,
    floor: Decimal,
    seasonal: Option<(Bundesland, usize)>,
    laengsvergleich: Option<(MeterReading, MeterReading, Decimal)>,
    history: Option<Series>,
}

impl Rules {
    /// The preset for `sparte`.
    #[must_use]
    pub fn new(
        sparte: Sparte,
        period: Period,
        now: OffsetDateTime,
        capacity_kw: Option<Decimal>,
    ) -> Self {
        let (zero_hours, stale, floor) = match sparte {
            Sparte::Strom => (1, Some((6, Decimal::ZERO)), dec!(0.01)),
            Sparte::Gas => (48, None, dec!(0.01)),
            Sparte::Waerme => (720, None, dec!(0.05)),
            Sparte::Wasser => (720, None, dec!(0.001)),
        };
        Self {
            period,
            now,
            capacity_kw,
            negative: true,
            zero_run: Some(Duration::hours(zero_hours)),
            stale,
            spike: Some(12),
            threshold: dec!(6),
            floor,
            seasonal: None,
            laengsvergleich: None,
            history: None,
        }
    }

    /// The electricity preset.
    #[must_use]
    pub fn strom(period: Period, now: OffsetDateTime, capacity_kw: Option<Decimal>) -> Self {
        Self::new(Sparte::Strom, period, now, capacity_kw)
    }

    /// The gas preset.
    #[must_use]
    pub fn gas(period: Period, now: OffsetDateTime, capacity_kw: Option<Decimal>) -> Self {
        Self::new(Sparte::Gas, period, now, capacity_kw)
    }

    /// The declared period.
    #[must_use]
    pub const fn period(&self) -> Period {
        self.period
    }

    /// Accept negative values — a Korrekturenergiemenge or a derived signed
    /// series (Codeliste OBIS 2.5c § 2.1 allows them only there).
    #[must_use]
    pub const fn without_negative(mut self) -> Self {
        self.negative = false;
        self
    }

    /// Report zero runs at least `min` long.
    #[must_use]
    pub const fn zero_run(mut self, min: Duration) -> Self {
        self.zero_run = Some(min);
        self
    }

    /// Do not report zero runs.
    #[must_use]
    pub const fn without_zero_run(mut self) -> Self {
        self.zero_run = None;
        self
    }

    /// Report a non-zero value repeated for at least `window` adjacent slots, each
    /// within absolute `tolerance` of the first (pvanalytics `stale_values_diff`).
    #[must_use]
    pub const fn stale(mut self, window: usize, tolerance: Decimal) -> Self {
        self.stale = Some((window, tolerance));
        self
    }

    /// Do not report stale values.
    #[must_use]
    pub const fn without_stale(mut self) -> Self {
        self.stale = None;
        self
    }

    /// Judge each value against `half_window` neighbours either side.
    #[must_use]
    pub const fn spike(mut self, half_window: usize) -> Self {
        self.spike = Some(half_window);
        self
    }

    /// Do not report spikes.
    #[must_use]
    pub const fn without_spike(mut self) -> Self {
        self.spike = None;
        self
    }

    /// The Hampel threshold `t`, in robust sigma, for `SPIKE` and `SEASONAL`.
    #[must_use]
    pub const fn threshold(mut self, t: Decimal) -> Self {
        self.threshold = t;
        self
    }

    /// The floor on the robust sigma, in channel units per slot.
    #[must_use]
    pub const fn floor(mut self, floor: Decimal) -> Self {
        self.floor = floor;
        self
    }

    /// Compare each value with the same slot on the previous `days` days of the same
    /// day class in `land` (holidays count as Sundays); needs that much history.
    #[must_use]
    pub const fn seasonal(mut self, land: Bundesland, days: usize) -> Self {
        self.seasonal = Some((land, days));
        self
    }

    /// Prior data for `SEASONAL`; never itself validated.
    #[must_use]
    pub fn history(mut self, prior: Series) -> Self {
        self.history = Some(prior);
        self
    }

    /// The Längsvergleich (MeteringCode 2006 A7.2): Σ load curve over
    /// `[start.at, end.at)` against `end.value − start.value`, as a relative
    /// `tolerance` (`0.007` = 0,7 %). Elexon BSCP502 § 4.1.5 recommends at
    /// most ±0,7 % for a weekly and ±5 % for a daily check; the caller picks.
    #[must_use]
    pub fn laengsvergleich(
        mut self,
        start: MeterReading,
        end: MeterReading,
        tolerance: Decimal,
    ) -> Self {
        self.laengsvergleich = Some((start, end, tolerance));
        self
    }
}

/// Check `series` against `rules`.
///
/// Runs are of adjacent intervals (`next.from == previous.to`), so a gap ends every run.
#[must_use]
pub fn validate(series: &Series, rules: &Rules) -> Report {
    let mut findings = Vec::new();
    let mut evaluated = vec![
        Rule::Gap,
        Rule::NonBillable,
        Rule::Future,
        Rule::OutsidePeriod,
    ];
    gaps(series, rules, &mut findings);
    per_interval(series, rules, &mut findings, &mut evaluated);
    if let Some(min) = rules.zero_run {
        evaluated.push(Rule::ZeroRun);
        runs(
            series,
            |a, _| a.is_zero(),
            |len, d| d >= min && len > 0,
            Rule::ZeroRun,
            &mut findings,
        );
    }
    if let Some((window, tol)) = rules.stale {
        evaluated.push(Rule::Stale);
        runs(
            series,
            |v, anchor| !v.is_zero() && v.checked_sub(anchor).is_some_and(|d| d.abs() <= tol),
            |len, _| len >= window.max(2),
            Rule::Stale,
            &mut findings,
        );
    }
    if spikes(series, rules, &mut findings) {
        evaluated.push(Rule::Spike);
    }
    if seasonal(series, rules, &mut findings) {
        evaluated.push(Rule::Seasonal);
    }
    if laengsvergleich(series, rules, &mut findings) {
        evaluated.push(Rule::Laengsvergleich);
    }
    findings.sort_by_key(|f| (f.from, f.rule));
    evaluated.sort_unstable();
    Report {
        period: rules.period,
        findings,
        evaluated,
        coverage: coverage(series, &rules.period, |_| true),
        len: series.len(),
        first: series.span().map(|(f, _)| f),
        end: series.span().map(|(_, e)| e),
    }
}

/// `GAP`: every slot of the period with no interval, as maximal runs.
fn gaps(series: &Series, rules: &Rules, out: &mut Vec<Finding>) {
    let p = rules.period;
    let mut run: Option<(OffsetDateTime, OffsetDateTime, u32)> = None;
    let mut close = |run: Option<(OffsetDateTime, OffsetDateTime, u32)>| {
        if let Some((from, to, n)) = run {
            out.push(
                Finding::new(Rule::Gap, from, to, gap_message(series, from, to, n))
                    .data(None, Some(Decimal::from(n))),
            );
        }
    };
    for slot in slots(series.boundary(), series.resolution(), p.start(), p.end()) {
        if series.get(slot.start()).is_some() {
            close(run.take());
        } else {
            run = Some(match run {
                Some((from, _, n)) => (from, slot.end(), n + 1),
                None => (slot.start(), slot.end(), 1),
            });
        }
    }
    close(run);
}

fn gap_message(series: &Series, from: OffsetDateTime, to: OffsetDateTime, n: u32) -> String {
    let span = series.span();
    let place = match span {
        None => "the series is empty",
        Some((first, _)) if to <= first => "before the first value",
        Some((_, last)) if from >= last => "after the last value",
        Some(_) => "between two values",
    };
    // The fall-back day repeats local 02:00–03:00: [t − 1 h, t + 1 h) in UTC.
    let boundary = series.boundary();
    let mut dst = String::new();
    let (Some(mut day), Some(last)) = (
        boundary.day_of(from),
        boundary.day_of(to - Duration::nanoseconds(1)),
    ) else {
        return format!("{n} slot(s) missing from {from} to {to}, {place}");
    };
    while day <= last {
        if let Some(period) = boundary.day(day)
            && period.kind() == DayKind::LongDay
            && let Some(t) = period.transition()
            && from < t + Duration::hours(1)
            && to > t - Duration::hours(1)
        {
            dst = format!("; it includes the repeated hour 02:00–03:00 of {day}");
        }
        let Some(next) = day.next_day() else { break };
        day = next;
    }
    format!("{n} slot(s) missing from {from} to {to}, {place}{dst}")
}

/// `NON_BILLABLE`, `FUTURE` and `OUTSIDE_PERIOD` as runs; `NEGATIVE` and
/// `CAPACITY` per slot, with the value.
fn per_interval(series: &Series, rules: &Rules, out: &mut Vec<Finding>, evaluated: &mut Vec<Rule>) {
    if rules.negative {
        evaluated.push(Rule::Negative);
    }
    let capacity = rules
        .capacity_kw
        .filter(|_| series.unit().is_none_or(|u| u.is_energy()));
    if capacity.is_some() {
        evaluated.push(Rule::Capacity);
    }
    let p = rules.period;
    let mut grouped: Vec<(Rule, OffsetDateTime, OffsetDateTime)> = Vec::new();
    let mut push = |rule: Rule, iv: &MeterInterval| {
        if let Some(group) = grouped.iter_mut().rev().find(|(r, ..)| *r == rule)
            && group.2 == iv.from()
        {
            group.2 = iv.to();
            return;
        }
        grouped.push((rule, iv.from(), iv.to()));
    };
    for iv in series {
        if !iv.quality().is_billable() {
            push(Rule::NonBillable, iv);
        }
        if iv.to() > rules.now {
            push(Rule::Future, iv);
        }
        if iv.from() < p.start() || iv.to() > p.end() {
            push(Rule::OutsidePeriod, iv);
        }
        if rules.negative && iv.value() < Decimal::ZERO {
            out.push(
                Finding::new(
                    Rule::Negative,
                    iv.from(),
                    iv.to(),
                    format!("negative quantity {} at {}", iv.value(), iv.from()),
                )
                .data(Some(iv.value()), None),
            );
        }
        if let Some(cap) = capacity
            && let Some(kw) = iv.demand_kw()
            && kw > cap
        {
            out.push(
                Finding::new(
                    Rule::Capacity,
                    iv.from(),
                    iv.to(),
                    format!(
                        "average power {kw} kW at {} exceeds the capacity {cap} kW",
                        iv.from()
                    ),
                )
                .data(Some(iv.value()), Some(cap)),
            );
        }
    }
    for (rule, from, to) in grouped {
        let message = match rule {
            Rule::NonBillable => format!("non-billable quality from {from} to {to}"),
            Rule::Future => format!("slots from {from} to {to} end after now ({})", rules.now),
            _ => format!("data from {from} to {to} lies outside the declared period"),
        };
        out.push(Finding::new(rule, from, to, message));
    }
}

/// Maximal runs of adjacent intervals whose value `member(value, first)`
/// accepts; a run that `report(len, duration)` accepts is one finding.
fn runs(
    series: &Series,
    member: impl Fn(Decimal, Decimal) -> bool,
    report: impl Fn(usize, Duration) -> bool,
    rule: Rule,
    out: &mut Vec<Finding>,
) {
    let mut run: Option<(OffsetDateTime, OffsetDateTime, Decimal, usize)> = None;
    let mut close = |run: Option<(OffsetDateTime, OffsetDateTime, Decimal, usize)>| {
        if let Some((from, to, first, len)) = run
            && report(len, to - from)
        {
            let what = if rule == Rule::ZeroRun {
                "zero"
            } else {
                "an unchanged value"
            };
            out.push(
                Finding::new(
                    rule,
                    from,
                    to,
                    format!("{len} adjacent slots of {what} from {from} to {to}"),
                )
                .data(Some(first), None),
            );
        }
    };
    for iv in series {
        run = match run {
            Some((from, to, first, len)) if to == iv.from() && member(iv.value(), first) => {
                Some((from, iv.to(), first, len + 1))
            }
            previous => {
                close(previous);
                member(iv.value(), iv.value()).then(|| (iv.from(), iv.to(), iv.value(), 1))
            }
        };
    }
    close(run);
}

/// `SPIKE`: the Hampel test over `half_window` billable neighbours either side.
/// `false` when off or when the series is shorter than one window.
fn spikes(series: &Series, rules: &Rules, out: &mut Vec<Finding>) -> bool {
    let Some(k) = rules.spike.filter(|k| *k > 0) else {
        return false;
    };
    let billable: Vec<&MeterInterval> = series
        .iter()
        .filter(|iv| iv.quality().is_billable())
        .collect();
    if billable.len() <= 2 * k {
        return false;
    }
    let values: Vec<Decimal> = billable.iter().map(|iv| iv.value()).collect();
    for (i, iv) in billable.iter().enumerate() {
        let mut window = values[i.saturating_sub(k)..(i + k + 1).min(values.len())].to_vec();
        if let Some(median) = hampel(iv.value(), &mut window, rules.threshold, rules.floor) {
            out.push(
                Finding::new(
                    Rule::Spike,
                    iv.from(),
                    iv.to(),
                    format!(
                        "{} at {} is far from its neighbours' median {median}",
                        iv.value(),
                        iv.from()
                    ),
                )
                .data(Some(iv.value()), Some(median)),
            );
        }
    }
    true
}

/// `SEASONAL`: the Hampel test against the same slot on comparable days.
/// `true` when at least one value had a full set of references.
fn seasonal(series: &Series, rules: &Rules, out: &mut Vec<Finding>) -> bool {
    let Some((land, days)) = rules.seasonal.filter(|(_, d)| *d > 0) else {
        return false;
    };
    let data = Data {
        series,
        prior: rules.history.as_ref(),
    };
    let mut ran = false;
    for iv in series.iter().filter(|iv| iv.quality().is_billable()) {
        let refs = references(&data, iv.from(), iv.from(), land, days, |r| {
            is_actual(r.quality())
        });
        if refs.len() < days {
            continue;
        }
        ran = true;
        let mut window: Vec<Decimal> = refs.iter().map(|(_, v)| *v).collect();
        if let Some(median) = hampel(iv.value(), &mut window, rules.threshold, rules.floor) {
            out.push(
                Finding::new(
                    Rule::Seasonal,
                    iv.from(),
                    iv.to(),
                    format!(
                        "{} at {} is far from the median {median} of the same slot on {days} comparable days",
                        iv.value(),
                        iv.from()
                    ),
                )
                .data(Some(iv.value()), Some(median)),
            );
        }
    }
    ran
}

/// `LAENGSVERGLEICH`. Evaluated only when every slot of the reading interval
/// is present and billable — a gap explains a shortfall and is its own finding.
fn laengsvergleich(series: &Series, rules: &Rules, out: &mut Vec<Finding>) -> bool {
    let Some((start, end, tolerance)) = &rules.laengsvergleich else {
        return false;
    };
    let (from, to) = (start.at, end.at);
    if to <= from {
        return false;
    }
    let mut sum = Decimal::ZERO;
    let mut last_end = from;
    for slot in slots(series.boundary(), series.resolution(), from, to) {
        let Some(iv) = series.get(slot.start()) else {
            return false;
        };
        if slot.start() < from || !iv.quality().is_billable() {
            return false;
        }
        let Some(s) = sum.checked_add(iv.value()) else {
            return false;
        };
        sum = s;
        last_end = slot.end();
    }
    if last_end != to {
        return false;
    }
    let Some(advance) = end.value.checked_sub(start.value) else {
        return false;
    };
    let Some(deviation) = sum.checked_sub(advance).map(|d| d.abs()) else {
        return false;
    };
    let allowed = advance
        .abs()
        .checked_mul(*tolerance)
        .unwrap_or(Decimal::MAX);
    if deviation > allowed {
        out.push(
            Finding::new(
                Rule::Laengsvergleich,
                from,
                to,
                format!(
                    "the load curve sums to {sum} from {from} to {to}, the register advanced by \
                     {advance}; the tolerance is {tolerance} of the advance"
                ),
            )
            .data(Some(sum), Some(advance)),
        );
    }
    true
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::series::interval::QualityFlag;
    use crate::time::calendar::DayBoundary;
    use crate::time::resolution::Resolution;
    use time::macros::{date, datetime};

    const NOW: OffsetDateTime = datetime!(2027-01-01 0:00 UTC);

    fn day(d: time::Date) -> Period {
        DayBoundary::Strom.day(d).unwrap()
    }

    /// Quarter-hours from `start`; `None` is a missing slot.
    fn series(start: OffsetDateTime, values: &[Option<Decimal>]) -> Series {
        let ivs = values
            .iter()
            .enumerate()
            .filter_map(|(i, v)| {
                let from = start + Duration::minutes(15 * i as i64);
                v.map(|v| MeterInterval::quarter_hour(from, v, QualityFlag::Measured).unwrap())
            })
            .collect();
        Series::new(Resolution::QUARTER_HOUR, DayBoundary::Strom, ivs).unwrap()
    }

    fn noisy(n: usize) -> Vec<Option<Decimal>> {
        (0..n)
            .map(|i| Some(Decimal::from(10 + (i * 7) % 5)))
            .collect()
    }

    #[test]
    fn a_clean_series_grades_a_and_the_grade_follows_the_findings() {
        let d = day(date!(2026 - 06 - 03));
        let report = validate(&series(d.start(), &noisy(96)), &Rules::strom(d, NOW, None));
        assert!(report.is_clean(), "{:?}", report.findings);
        assert_eq!(report.grade(), Grade::A);
        assert_eq!(Grade::of([]), Grade::A);
        assert_eq!(
            report.skipped(),
            vec![Rule::Capacity, Rule::Laengsvergleich, Rule::Seasonal]
        );
        assert_eq!(report.coverage.pct(), Some(dec!(100)));
    }

    #[test]
    fn head_tail_and_interior_gaps_are_one_finding_each() {
        let d = day(date!(2026 - 06 - 03));
        let mut v = noisy(96);
        for i in [0, 1, 50, 95] {
            v[i] = None;
        }
        let report = validate(&series(d.start(), &v), &Rules::strom(d, NOW, None));
        let gaps: Vec<&Finding> = report.by_rule(Rule::Gap).collect();
        assert_eq!(gaps.len(), 3);
        assert!(gaps[0].message.contains("before the first"));
        assert!(gaps[1].message.contains("between"));
        assert!(gaps[2].message.contains("after the last"));
        assert_eq!(gaps[0].reference, Some(dec!(2)));
        assert_eq!(report.grade(), Grade::C);
        assert_eq!(report.coverage.pct(), Some(dec!(95.83)));
    }

    #[test]
    fn a_gap_in_the_repeated_hour_says_so() {
        let d = day(date!(2026 - 10 - 25));
        let mut v = noisy(100);
        v[12] = None; // 01:00 UTC = the second 02:00 local
        let report = validate(&series(d.start(), &v), &Rules::strom(d, NOW, None));
        let gap = report.by_rule(Rule::Gap).next().unwrap();
        assert!(gap.message.contains("repeated hour"), "{}", gap.message);
        v[12] = Some(dec!(1));
        v[60] = None;
        let report = validate(&series(d.start(), &v), &Rules::strom(d, NOW, None));
        assert!(
            !report
                .by_rule(Rule::Gap)
                .next()
                .unwrap()
                .message
                .contains("repeated")
        );
    }

    #[test]
    fn coverage_is_clipped_to_the_declared_period() {
        let d = day(date!(2026 - 06 - 03));
        let report = validate(&series(d.start(), &noisy(200)), &Rules::strom(d, NOW, None));
        assert!(report.coverage.is_complete());
        assert_eq!(report.coverage.pct(), Some(dec!(100)));
        assert_eq!(report.by_rule(Rule::OutsidePeriod).count(), 1);
        assert_eq!(report.grade(), Grade::B);
    }

    #[test]
    fn a_gap_ends_a_zero_run() {
        let d = day(date!(2026 - 06 - 03));
        let z = Some(Decimal::ZERO);
        let mut v = noisy(96);
        v[10..13].copy_from_slice(&[z, z, z]);
        v[13] = None;
        v[14..17].copy_from_slice(&[z, z, z]);
        let rules = Rules::strom(d, NOW, None).without_spike();
        assert_eq!(
            validate(&series(d.start(), &v), &rules)
                .by_rule(Rule::ZeroRun)
                .count(),
            0
        );
        v[13] = z;
        let report = validate(&series(d.start(), &v), &rules);
        let run = report.by_rule(Rule::ZeroRun).next().unwrap();
        assert_eq!(run.to - run.from, Duration::minutes(105));
    }

    #[test]
    fn a_frozen_non_zero_value_is_stale_and_zeros_are_not() {
        let d = day(date!(2026 - 06 - 03));
        let mut v = noisy(96);
        for slot in v.iter_mut().take(26).skip(20) {
            *slot = Some(dec!(3.3));
        }
        let rules = Rules::strom(d, NOW, None).without_spike();
        let report = validate(&series(d.start(), &v), &rules);
        assert_eq!(report.by_rule(Rule::Stale).count(), 1);
        v[25] = Some(dec!(3.4));
        let report = validate(&series(d.start(), &v), &rules);
        assert_eq!(report.by_rule(Rule::Stale).count(), 0);
        let zeros = vec![Some(Decimal::ZERO); 96];
        assert_eq!(
            validate(&series(d.start(), &zeros), &rules)
                .by_rule(Rule::Stale)
                .count(),
            0
        );
    }

    #[test]
    fn spikes_use_a_mad_floor_that_is_positive_for_strom() {
        let d = day(date!(2026 - 06 - 03));
        let mut v = vec![Some(dec!(1)); 96];
        v[40] = Some(dec!(1.001));
        let rules = Rules::strom(d, NOW, None).without_stale();
        assert_eq!(
            validate(&series(d.start(), &v), &rules)
                .by_rule(Rule::Spike)
                .count(),
            0
        );
        let bare = rules.clone().floor(Decimal::ZERO);
        assert_eq!(
            validate(&series(d.start(), &v), &bare)
                .by_rule(Rule::Spike)
                .count(),
            1
        );
        v[40] = Some(dec!(9));
        let report = validate(&series(d.start(), &v), &rules);
        let spike = report.by_rule(Rule::Spike).next().unwrap();
        assert_eq!(
            (spike.value, spike.reference),
            (Some(dec!(9)), Some(dec!(1)))
        );
        // Too short for one window: not evaluated, and the report says so.
        let short = validate(&series(d.start(), &v[..20]), &rules);
        assert!(short.skipped().contains(&Rule::Spike));
    }

    #[test]
    fn an_alternating_two_level_load_is_not_a_spike_but_a_tenfold_value_is() {
        let d = day(date!(2026 - 06 - 03));
        let mut v: Vec<Option<Decimal>> = (0..96)
            .map(|i| Some(if i % 2 == 0 { dec!(1) } else { dec!(2) }))
            .collect();
        let rules = Rules::strom(d, NOW, None).without_stale();
        assert_eq!(
            validate(&series(d.start(), &v), &rules)
                .by_rule(Rule::Spike)
                .count(),
            0
        );
        v[41] = Some(dec!(20));
        let report = validate(&series(d.start(), &v), &rules);
        let spikes: Vec<_> = report.by_rule(Rule::Spike).collect();
        assert_eq!(spikes.len(), 1, "{spikes:?}");
        assert_eq!(spikes[0].value, Some(dec!(20)));
    }

    #[test]
    fn seasonal_compares_with_the_same_slot_on_comparable_days() {
        let start = day(date!(2026 - 06 - 01)).start();
        let target = day(date!(2026 - 06 - 24));
        let days = (target.start() - start).whole_days() as usize;
        let mut v = vec![Some(dec!(2)); 96 * (days + 1) - 4];
        let i = 96 * days + 40;
        v[i] = Some(dec!(9));
        let rules = Rules::strom(target, NOW, None)
            .without_spike()
            .without_stale()
            .seasonal(Bundesland::Be, 4);
        let all = series(start, &v);
        let tail = Series::new(
            Resolution::QUARTER_HOUR,
            DayBoundary::Strom,
            all.iter()
                .filter(|iv| target.contains(iv.from()))
                .cloned()
                .collect(),
        )
        .unwrap();
        let history = Series::new(
            Resolution::QUARTER_HOUR,
            DayBoundary::Strom,
            all.iter()
                .filter(|iv| iv.from() < target.start())
                .cloned()
                .collect(),
        )
        .unwrap();
        let report = validate(&tail, &rules.clone().history(history));
        let f: Vec<_> = report.by_rule(Rule::Seasonal).collect();
        assert_eq!(f.len(), 1, "{f:?}");
        assert_eq!(f[0].reference, Some(dec!(2)));
        assert!(validate(&tail, &rules).skipped().contains(&Rule::Seasonal));
    }

    #[test]
    fn rejected_values_are_errors_and_the_future_blocks() {
        let d = day(date!(2026 - 06 - 03));
        let mut v = noisy(96);
        v[3] = Some(dec!(-1));
        v[7] = Some(dec!(40)); // 160 kW
        let mut s = series(d.start(), &v).into_intervals();
        s[9] = s[9].clone().with_quality(QualityFlag::Faulty);
        let s = Series::new(Resolution::QUARTER_HOUR, DayBoundary::Strom, s).unwrap();
        let rules = Rules::strom(d, NOW, Some(dec!(100))).without_spike();
        let report = validate(&s, &rules);
        assert_eq!(report.by_rule(Rule::Negative).count(), 1);
        assert_eq!(
            report.by_rule(Rule::Capacity).next().unwrap().reference,
            Some(dec!(100))
        );
        assert_eq!(report.by_rule(Rule::NonBillable).count(), 1);
        assert_eq!(report.grade(), Grade::C);
        assert!(report.evaluated.contains(&Rule::Capacity));

        let early = Rules::strom(d, d.start() + Duration::hours(12), None);
        let report = validate(&s, &early);
        let future = report.by_rule(Rule::Future).next().unwrap();
        assert_eq!(
            (future.from, future.to),
            (d.start() + Duration::hours(12), d.end())
        );
        assert_eq!(report.grade(), Grade::F);
    }

    #[test]
    fn the_laengsvergleich_compares_the_sum_with_the_register_advance() {
        let d = day(date!(2026 - 06 - 03));
        let s = series(d.start(), &vec![Some(dec!(0.25)); 96]);
        let reading = |at, v| MeterReading::measured(at, v);
        let check = |advance: Decimal| {
            let rules = Rules::strom(d, NOW, None).laengsvergleich(
                reading(d.start(), dec!(1000)),
                reading(d.end(), dec!(1000) + advance),
                dec!(0.007),
            );
            validate(&s, &rules)
        };
        // 24 against 24.1 is 0.41 % — inside a 0.7 % tolerance.
        assert!(
            check(dec!(24.1))
                .by_rule(Rule::Laengsvergleich)
                .next()
                .is_none()
        );
        let off = check(dec!(25));
        let f = off.by_rule(Rule::Laengsvergleich).next().unwrap();
        assert_eq!((f.value, f.reference), (Some(dec!(24.00)), Some(dec!(25))));
        assert_eq!(off.grade(), Grade::F);
        // A gap inside the interval explains a shortfall: not evaluated.
        let mut v = vec![Some(dec!(0.25)); 96];
        v[5] = None;
        let rules = Rules::strom(d, NOW, None).laengsvergleich(
            reading(d.start(), dec!(0)),
            reading(d.end(), dec!(24)),
            dec!(0.007),
        );
        assert!(
            validate(&series(d.start(), &v), &rules)
                .skipped()
                .contains(&Rule::Laengsvergleich)
        );
    }
}