sightingdb 0.7.1

A database designed for Sightings, a technique to count items
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
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
//! The admin interface served at `/_management/`.
//!
//! This is a browser over the database — namespaces as folders, the values
//! inside them, and what each value has been seen doing — plus a view of what
//! the server is configured to do. Namespaces can be created and values added
//! from it; the configuration itself is read-only, since it comes from a file
//! read at startup.
//!
//! It is deliberately *not* served under `_config`, even though there would be
//! no routing conflict: `_config` already names the database namespace holding
//! API keys, and the data paths refuse it outright. Serving an interface under
//! the same word would leave `/_config/` and `/r/_config/` looking alike while
//! doing opposite things.
//!
//! Access requires a key holding the `admin` grant, *regardless* of the
//! `authenticate` setting. Turning authentication off is a decision about the
//! sighting API — it should not hand the admin interface to anyone who can
//! reach the port.

use std::path::PathBuf;

use actix_web::{HttpRequest, HttpResponse, Responder, web};
use serde::{Deserialize, Serialize};

use crate::acl::{Grant, validate_key, validate_namespace};

use crate::error::Message;
use crate::handlers::{SharedState, State};

/// The single-page interface, compiled in so there is nothing to deploy.
const UI: &str = include_str!("ui.html");
/// Vendored so the interface works without internet access. See assets/README.md.
const ECHARTS: &str = include_str!("../../assets/echarts.min.js");
/// The logo, compiled in for the same reason: an interface that fetches its own
/// branding from somewhere else does not work on an air-gapped host. 64px, for
/// a header that shows it at about a third of that.
const LOGO: &[u8] = include_bytes!("../../doc/sightingdb-logo3_64.png");

/// Paging is capped so one request cannot ask the server to sort and serialize
/// an entire large namespace.
const MAX_LIMIT: usize = 500;
const DEFAULT_LIMIT: usize = 50;

#[derive(Debug, Deserialize)]
pub struct BrowseQuery {
    /// Substring filter, case-insensitive.
    #[serde(default)]
    q: String,
    #[serde(default)]
    offset: usize,
    limit: Option<usize>,
}

impl BrowseQuery {
    fn limit(&self) -> usize {
        self.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, MAX_LIMIT)
    }
}

/// Which rejections to list.
#[derive(Debug, Deserialize)]
pub struct RejectionsQuery {
    /// Only rejections under this namespace, matched as a subtree the way an
    /// ACL grant is. Absent lists every namespace the key may read.
    namespace: Option<String>,
    limit: Option<usize>,
}

impl RejectionsQuery {
    fn limit(&self) -> usize {
        self.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, MAX_LIMIT)
    }
}

/// A page of rejections, and how many are held in total.
#[derive(Debug, Serialize)]
pub struct RejectionPage {
    rejections: Vec<crate::rejections::Rejection>,
    /// Everything currently kept, not just this page, so the interface can say
    /// "showing 50 of 1000".
    total: usize,
    /// The cap. Once `total` reaches it, the oldest rejections are being lost.
    capacity: usize,
}

#[derive(Debug, Deserialize)]
pub struct ValuesQuery {
    namespace: String,
    #[serde(default)]
    q: String,
    #[serde(default)]
    offset: usize,
    limit: Option<usize>,
}

impl ValuesQuery {
    fn limit(&self) -> usize {
        self.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, MAX_LIMIT)
    }
}

/// One level of the namespace tree, as the browser walks it.
#[derive(Debug, Deserialize)]
pub struct TreeQuery {
    /// The folder being opened. Empty is the root.
    #[serde(default)]
    path: String,
    /// Substring filter over the child names at this level, case-insensitive.
    #[serde(default)]
    q: String,
    #[serde(default)]
    offset: usize,
    limit: Option<usize>,
}

impl TreeQuery {
    fn limit(&self) -> usize {
        self.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, MAX_LIMIT)
    }
}

/// A namespace to create, which may name a whole path at once.
#[derive(Debug, Deserialize)]
pub struct NewNamespace {
    namespace: String,
}

/// Values to record, one namespace at a time.
///
/// A single value and a bulk paste are the same request with a list of one:
/// the interface has one code path, and so does this.
#[derive(Debug, Deserialize)]
pub struct NewValues {
    namespace: String,
    values: Vec<String>,
    /// Unix seconds. Absent means "now", as on the write API.
    #[serde(default)]
    timestamp: Option<i64>,
    /// Absent leaves whatever TTL an existing value had; 0 clears it.
    #[serde(default)]
    ttl: Option<u64>,
    /// Comma-separated tags applied to every value in this request, merged
    /// with whatever each already carried.
    #[serde(default)]
    tags: String,
}

/// What a create or an add did, per value, so a paste of a thousand lines can
/// report the eight that were rejected without losing the rest.
#[derive(Debug, Serialize)]
pub struct WriteReport {
    namespace: String,
    written: usize,
    /// Each value that was recorded, with its running total afterwards.
    ///
    /// The count comes back from the write itself, taken under the lock that
    /// incremented it, so reporting it costs nothing and saves the interface a
    /// read to find out what a paste actually did.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    counts: Vec<ValueCount>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    errors: Vec<ValueError>,
}

/// One recorded value and how often it has now been seen.
#[derive(Debug, Serialize)]
pub struct ValueCount {
    value: String,
    count: u64,
    /// Whether this was the first sighting of the value in this namespace.
    new: bool,
}

#[derive(Debug, Serialize)]
pub struct ValueError {
    value: String,
    error: String,
}

/// A value's tags, replaced outright.
#[derive(Debug, Deserialize)]
pub struct TagChange {
    namespace: String,
    value: String,
    /// The whole set, comma-separated. Empty clears it.
    tags: String,
}

/// A storage change from the interface or an automation.
///
/// Name any namespace: the setting lands on its top-level namespace, which is
/// the unit that is paged in and out, and so covers everything under it. Both
/// halves are optional, and `"default"` (or null) on either means "stop saying
/// anything here and take the configured default".
#[derive(Debug, Deserialize)]
pub struct TierChange {
    /// Any namespace in the shard to change. `shard` is the older spelling.
    #[serde(default, alias = "shard")]
    namespace: String,
    #[serde(default)]
    tier: Option<String>,
    /// Seconds a warm shard may sit untouched. Accepts a number or a string,
    /// since a form field hands over text.
    #[serde(default)]
    warm_idle: Option<serde_json::Value>,
}

impl TierChange {
    /// The settings to store, or an explanation of what was unreadable.
    fn entry(&self) -> Result<crate::tier::Entry, String> {
        let tier = match self.tier.as_deref().map(str::trim) {
            None | Some("") | Some("default") | Some("inherit") => None,
            Some(tier) => Some(crate::tier::Tier::parse(tier).map_err(|e| e.to_string())?),
        };

        let warm_idle = match &self.warm_idle {
            None | Some(serde_json::Value::Null) => None,
            Some(serde_json::Value::Number(number)) => match number.as_u64() {
                Some(seconds) => Some(seconds),
                None => return Err("warm_idle cannot be negative".to_string()),
            },
            Some(serde_json::Value::String(text)) => {
                let text = text.trim();
                match text {
                    "" | "default" | "inherit" => None,
                    _ => Some(
                        text.parse::<u64>()
                            .map_err(|_| format!("'{text}' is not a number of seconds"))?,
                    ),
                }
            }
            Some(other) => return Err(format!("warm_idle should be a number, found {other}")),
        };

        Ok(crate::tier::Entry { tier, warm_idle })
    }
}

#[derive(Debug, Deserialize)]
pub struct ValueQuery {
    namespace: String,
    value: String,
}

/// Where one value has been seen, for the relationship graph.
#[derive(Debug, Deserialize)]
pub struct SightingsQuery {
    value: String,
    limit: Option<usize>,
}

impl SightingsQuery {
    /// A graph is read by eye, so the cap is what stays legible rather than
    /// what the server could serialize.
    fn limit(&self) -> usize {
        self.limit.unwrap_or(200).clamp(1, MAX_LIMIT)
    }
}

/// What the server is doing, for the configuration view.
///
/// Configuration is read from a file at startup, so this reports rather than
/// edits: changing it means editing the file and restarting.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ServerInfo {
    pub version: &'static str,
    pub authenticate: bool,
    pub http_enabled: bool,
    /// Where the HTTP API listens, as `host:port`. Shown in the configuration
    /// view, and used to notice a peer being added that is this server itself.
    pub listen: String,
    pub config_path: String,
    pub dbdir: Option<String>,
    pub snapshot_interval: u64,
    pub sweep_interval: u64,
    pub stats_retention: usize,
    pub shadow_ttl: u64,
    pub dns: Option<DnsInfo>,
    pub zmq: Option<ZmqInfo>,
    pub namespaces: usize,
    pub apikeys: usize,
    pub default_tier: String,
    pub warm_idle: u64,
    /// Shards with a tier of their own.
    pub tiers: Vec<(String, String)>,
    /// What this server stores and who it knows about: the two things that
    /// decide its place in a galaxy.
    pub role: RoleInfo,
    /// When this server's TLS certificate runs out. `None` when it serves
    /// plain HTTP, or when the certificate cannot be read.
    pub tls: Option<crate::tls::Expiry>,
    /// Days of certificate life below which the interface calls it urgent, so
    /// the page and the log agree on what "soon" means.
    pub expiring_soon_days: i64,
}

/// This server's place in a galaxy.
///
/// Derived from `[storage] namespaces` and `[galaxy]`, and reported so that a
/// topology can be described from the outside — which is what a cluster view
/// has to read.
#[derive(Debug, Clone, Serialize)]
pub struct RoleInfo {
    /// `"node"`, `"router"`, or `"both"`.
    ///
    /// A node stores namespaces and forwards nothing; a router stores none of
    /// its own and exists to forward; both does each. The words describe a
    /// configuration rather than a type — any server can be any of them.
    pub kind: &'static str,
    /// Whether this server stores every namespace. A full mirror.
    pub mirrors_everything: bool,
    /// The namespace prefixes stored, when it is not everything. Empty on a
    /// router, which stores none.
    pub namespaces: Vec<String>,
    /// Peers, without their keys — a key is a credential and does not belong
    /// in a response the interface renders.
    pub peers: Vec<String>,
    pub max_hops: u8,
}

impl Default for RoleInfo {
    /// A server that stores everything and knows no peers, which is what every
    /// release before galaxies was.
    fn default() -> Self {
        RoleInfo {
            kind: "node",
            mirrors_everything: true,
            namespaces: Vec::new(),
            peers: Vec::new(),
            max_hops: 0,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct DnsInfo {
    pub listen: String,
    pub zone: String,
    pub ttl: u32,
    pub rate_limit: u32,
    pub shadow: bool,
    pub exposed: Vec<ExposedInfo>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ExposedInfo {
    pub label: String,
    pub namespace: String,
    pub encoding: String,
}

/// One key as the interface sees it.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct KeyEntry {
    pub key: String,
    pub admin: bool,
    /// Namespace prefixes this key may read. An empty string means all.
    pub read: Vec<String>,
    pub write: Vec<String>,
}

impl KeyEntry {
    pub fn from_grants(key: &str, grants: &[Grant]) -> Self {
        let mut entry = KeyEntry {
            key: key.to_string(),
            admin: false,
            read: Vec::new(),
            write: Vec::new(),
        };
        for grant in grants {
            if grant.admin {
                entry.admin = true;
            }
            if grant.read {
                entry.read.push(grant.prefix.clone());
            }
            if grant.write {
                entry.write.push(grant.prefix.clone());
            }
        }
        entry
    }

    /// Back to grants, collapsing a prefix granted for both into one `rw`.
    fn to_grants(&self) -> Vec<Grant> {
        let mut grants = Vec::new();
        for prefix in &self.read {
            grants.push(Grant {
                prefix: prefix.clone(),
                read: true,
                write: self.write.contains(prefix),
                admin: false,
            });
        }
        for prefix in &self.write {
            if !self.read.contains(prefix) {
                grants.push(Grant {
                    prefix: prefix.clone(),
                    read: false,
                    write: true,
                    admin: false,
                });
            }
        }
        if self.admin {
            grants.push(Grant::admin());
        }
        grants
    }

    fn validate(&self) -> anyhow::Result<()> {
        validate_key(&self.key)?;
        for prefix in self.read.iter().chain(self.write.iter()) {
            validate_namespace(prefix)?;
        }
        if !self.admin && self.read.is_empty() && self.write.is_empty() {
            anyhow::bail!("a key with no grants at all cannot do anything");
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct ZmqInfo {
    pub endpoint: String,
    pub topics: Vec<String>,
    pub format: String,
    pub require_to_ids: bool,
    pub mapped_types: usize,
}

/// Check the caller holds an admin grant, handing back the key so the caller
/// can go on to check its rights over a particular namespace.
fn require_admin<'a>(state: &SharedState, req: &'a HttpRequest) -> Result<&'a str, HttpResponse> {
    let Some(header) = req.headers().get("Authorization") else {
        log::debug!(
            "Refused {}: no API key, asked for the management interface",
            crate::handlers::peer_of(req)
        );
        return Err(
            HttpResponse::Unauthorized().json(Message::new("An admin API key is required."))
        );
    };
    let Ok(key) = header.to_str() else {
        return Err(HttpResponse::BadRequest()
            .json(Message::new("Authorization header is not valid UTF-8.")));
    };
    if state.acl().is_admin(key) {
        Ok(key)
    } else {
        // Louder than a data refusal: this is someone at the door of the
        // interface that hands out keys.
        log::warn!(
            "{}",
            crate::handlers::refusal(
                &crate::handlers::peer_of(req),
                key,
                state.acl().contains(key),
                "use",
                "the management interface",
            )
        );
        Err(HttpResponse::Forbidden().json(Message::new("That key is not an admin key.")))
    }
}

/// Reaching the interface is not the same as being allowed to read the data in
/// it: an `admin, r:feeds` key browses `feeds/*` and nothing else.
fn require_read(state: &SharedState, key: &str, namespace: &str) -> Result<(), HttpResponse> {
    if state.acl().can_read(key, namespace) {
        Ok(())
    } else {
        log::warn!(
            "{}",
            crate::handlers::refusal("an admin key", key, true, "read", namespace)
        );
        // Same answer as a namespace that does not exist, so browsing cannot
        // be used to enumerate what is out of reach.
        Err(HttpResponse::NotFound().json(Message::new("No such namespace.")))
    }
}

/// The same rule as [`require_read`], for the paths that change data: an
/// `admin, r:feeds` key browses `feeds/*` but does not add to it.
///
/// Unlike the sighting API this does not care whether `authenticate` is off.
/// That setting is about the sighting API; the management interface has always
/// demanded a key, and a key that says what it may write is the only thing
/// that makes an admin grant safe to hand out.
fn require_write(state: &SharedState, key: &str, namespace: &str) -> Result<(), HttpResponse> {
    if state.acl().can_write(key, namespace) {
        Ok(())
    } else {
        log::warn!(
            "{}",
            crate::handlers::refusal("an admin key", key, true, "write", namespace)
        );
        Err(HttpResponse::Forbidden().json(Message::new(format!(
            "That key is not permitted to write to '{namespace}'."
        ))))
    }
}

/// Tidy a namespace typed by a person into the name the database will store.
///
/// Browsing is by path segment, so stray or doubled slashes would otherwise
/// create a namespace that looks like a folder someone already made but sorts
/// and links as something else.
fn clean_namespace(namespace: &str) -> Result<String, HttpResponse> {
    let cleaned = namespace
        .split('/')
        .map(str::trim)
        .filter(|segment| !segment.is_empty())
        .collect::<Vec<_>>()
        .join("/");

    if cleaned.is_empty() {
        return Err(HttpResponse::BadRequest().json(Message::new("A namespace needs a name.")));
    }
    // The same rules as an ACL prefix, so that anything created here can also
    // be granted to a key later.
    if let Err(e) = validate_namespace(&cleaned) {
        return Err(HttpResponse::BadRequest().json(Message::new(e.to_string())));
    }
    Ok(cleaned)
}

pub async fn index() -> impl Responder {
    HttpResponse::Ok()
        .content_type("text/html; charset=utf-8")
        .body(UI)
}

pub async fn logo() -> impl Responder {
    HttpResponse::Ok()
        .content_type("image/png")
        // Immutable: it only changes when the binary does.
        .insert_header(("Cache-Control", "public, max-age=86400"))
        .body(LOGO)
}

pub async fn echarts() -> impl Responder {
    HttpResponse::Ok()
        .content_type("application/javascript; charset=utf-8")
        // Immutable: it only changes when the binary does.
        .insert_header(("Cache-Control", "public, max-age=86400"))
        .body(ECHARTS)
}

/// Confirms a key is an admin key, so the interface can show its login result.
pub async fn session(state: State, req: HttpRequest) -> HttpResponse {
    if let Err(resp) = require_admin(&state, &req) {
        return resp;
    }
    HttpResponse::Ok().json(Message::new("ok"))
}

pub async fn info(state: State, req: HttpRequest) -> HttpResponse {
    if let Err(resp) = require_admin(&state, &req) {
        return resp;
    }
    HttpResponse::Ok().json(state.info.clone())
}

pub async fn namespaces(
    state: State,
    query: web::Query<BrowseQuery>,
    req: HttpRequest,
) -> HttpResponse {
    let key = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    let acl = state.acl();
    let page = state
        .db
        .namespace_page(&query.q, query.offset, query.limit(), |name| {
            acl.can_read(&key, name)
        });
    drop(acl);
    HttpResponse::Ok().json(page)
}

/// `GET /_management/api/galaxy` — this server and the peers it knows.
///
/// What a topology view reads. Reports this server's own role and each peer's
/// health, so the picture can be drawn from one request against whichever
/// server the operator happens to be connected to.
///
/// Peer **keys are never included**: they are credentials, and this response is
/// rendered in a browser.
///
/// Nothing is forwarded to peers yet, so this describes the configured galaxy
/// and whether its members answer — not a traversal of it. A cascade is
/// reported one level deep: each peer appears as a peer, and asking *it* for
/// its own galaxy is how the next level is reached.
pub async fn galaxy(state: State, req: HttpRequest) -> HttpResponse {
    if let Err(resp) = require_admin(&state, &req) {
        return resp;
    }

    let Some(galaxy) = state.galaxy.as_ref() else {
        return HttpResponse::Ok().json(serde_json::json!({
            "self": describe(&state),
            "peers": [],
            "below": {},
        }));
    };

    // One level per hop. Asked for only when this request was not itself a
    // walk that has run out of budget, so a cascade is described all the way
    // down and a cycle still terminates.
    let hops = crate::galaxy::hops_left(&req, galaxy.max_hops()).unwrap_or(0);
    let below: serde_json::Map<String, serde_json::Value> = galaxy
        .walk(hops)
        .await
        .into_iter()
        .collect::<std::collections::BTreeMap<_, _>>()
        .into_iter()
        .collect();

    HttpResponse::Ok().json(serde_json::json!({
        // The server answering, so a view has a root to draw from without
        // being told separately which one it asked.
        "self": describe(&state),
        "peers": galaxy.health(),
        // Each peer's own answer, keyed by its url. A peer that is itself a
        // router has peers of its own in here, which is how a cascade is
        // drawn from one request.
        "below": below,
    }))
}

/// One peer as the interface lists it.
///
/// The key is **not** included. It is a credential this server holds, and a
/// topology view is not a reason to hand it back out; the interface shows that
/// one is set and lets it be replaced, which is all editing needs.
#[derive(Debug, Serialize)]
pub struct PeerRow {
    pub url: String,
    /// What the peer stores: `null` for a full mirror, else the prefixes.
    pub namespaces: Option<Vec<String>>,
    /// Whether this peer came from the configuration file, and so cannot be
    /// changed here.
    pub fixed: bool,
    pub health: crate::galaxy::PeerHealth,
}

#[derive(Debug, Serialize)]
pub struct PeersView {
    pub peers: Vec<PeerRow>,
    /// Whether peers can be added or removed here. False without a
    /// `peers_file`, or with no `[galaxy]` section at all.
    pub editable: bool,
    /// Why not, when they cannot be.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
}

/// `GET /_management/api/galaxy/peers` — the peer list, for editing.
///
/// Separate from `/_management/api/galaxy`, which walks the whole cascade to
/// draw it. This is one server's own list and answers without touching the
/// network, so the editor stays responsive while a peer is down.
pub async fn list_peers(state: State, req: HttpRequest) -> HttpResponse {
    if let Err(resp) = require_admin(&state, &req) {
        return resp;
    }

    let Some(galaxy) = state.galaxy.as_ref() else {
        return HttpResponse::Ok().json(PeersView {
            peers: Vec::new(),
            editable: false,
            note: Some(
                "This server has no [galaxy] section, so it has no peers to manage. Add \
                 one with a peers_file and restart; `sightingdb --setup` writes both."
                    .to_string(),
            ),
        });
    };

    let health: std::collections::HashMap<String, crate::galaxy::PeerHealth> = galaxy
        .health()
        .into_iter()
        .map(|known| (known.url.clone(), known))
        .collect();

    let peers = galaxy
        .peers()
        .into_iter()
        .map(|peer| PeerRow {
            namespaces: (!peer.stores.stores_everything()).then(|| peer.stores.prefixes().to_vec()),
            fixed: galaxy.is_fixed(&peer.url),
            health: health
                .get(&peer.url)
                .cloned()
                .unwrap_or_else(|| crate::galaxy::PeerHealth::unprobed(&peer.url)),
            url: peer.url,
        })
        .collect();

    let editable = state.galaxy_peers_file.is_some();
    HttpResponse::Ok().json(PeersView {
        peers,
        editable,
        note: (!editable).then(|| {
            "No peers_file is configured, so peers cannot be edited here. Set \
             peers_file in [galaxy] and restart."
                .to_string()
        }),
    })
}

/// A peer as the interface sends it.
#[derive(Debug, Deserialize)]
pub struct PeerChange {
    url: String,
    key: String,
    /// Absent or empty means a full mirror.
    #[serde(default)]
    namespaces: Option<Vec<String>>,
}

/// Where UI-added peers are written, or why they cannot be.
fn peers_file(state: &SharedState) -> Result<&PathBuf, HttpResponse> {
    if state.galaxy.is_none() {
        return Err(HttpResponse::Conflict().json(Message::new(
            "This server has no [galaxy] section, so it has no galaxy to add to. Add one \
             with a peers_file and restart.",
        )));
    }
    state.galaxy_peers_file.as_ref().ok_or_else(|| {
        HttpResponse::Conflict().json(Message::new(
            "No peers_file is configured, so peers cannot be edited here. Set peers_file \
             in [galaxy] and restart.",
        ))
    })
}

/// Write the peers the interface owns, then answer with the whole list.
///
/// Written after the change is already in effect, so a failed write is
/// reported with the galaxy already using the new peer — which is the right
/// way round: the peer works now and the file is what makes it survive a
/// restart. The message says exactly that rather than implying the change
/// did not happen.
fn save_peers(state: &SharedState) -> Result<(), HttpResponse> {
    let path = peers_file(state)?;
    let Some(galaxy) = state.galaxy.as_ref() else {
        return Ok(());
    };
    let rendered = crate::config::PeersFile::to_toml(&galaxy.editable_peers());

    let write = || -> std::io::Result<()> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let temp = path.with_extension("tmp");
        std::fs::write(&temp, rendered)?;
        std::fs::rename(&temp, path)
    };

    if let Err(e) = write() {
        log::error!("Could not write {}: {e}", path.display());
        return Err(
            HttpResponse::InternalServerError().json(Message::new(format!(
                "The galaxy was changed and is in effect now, but {} could not be written, \
             so the change will be lost on restart: {e}",
                path.display()
            ))),
        );
    }
    Ok(())
}

/// `POST /_management/api/galaxy/peers` — add a peer this galaxy does not have.
///
/// Adding and changing are separate verbs on purpose. An upsert would mean
/// that typing an address that already exists silently replaces its key, and
/// the one thing a peer's key does is bound what this server may do there —
/// replacing it by accident is not a mistake to make quietly.
pub async fn add_peer(state: State, body: web::Json<PeerChange>, req: HttpRequest) -> HttpResponse {
    save_peer(state, body, req, false).await
}

/// `PUT /_management/api/galaxy/peers` — change a peer this galaxy has.
pub async fn update_peer(
    state: State,
    body: web::Json<PeerChange>,
    req: HttpRequest,
) -> HttpResponse {
    save_peer(state, body, req, true).await
}

async fn save_peer(
    state: State,
    body: web::Json<PeerChange>,
    req: HttpRequest,
    replacing: bool,
) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };
    if let Err(resp) = peers_file(&state) {
        return resp;
    }
    let Some(galaxy) = state.galaxy.as_ref() else {
        return HttpResponse::Conflict().json(Message::new("This server has no galaxy."));
    };

    let change = body.into_inner();
    // An empty namespaces list from a form means "everything" rather than
    // "nothing": a peer that holds nothing is not a mirror, and the validator
    // refuses it.
    let namespaces = change
        .namespaces
        .as_ref()
        .filter(|list| !list.is_empty())
        .map(|list| list.as_slice());

    let peer = match crate::config::validated_peer(&change.url, &change.key, namespaces) {
        Ok(peer) => peer,
        Err(e) => return HttpResponse::BadRequest().json(Message::new(e)),
    };

    if galaxy.is_fixed(&peer.url) {
        return HttpResponse::Conflict().json(Message::new(format!(
            "{} is declared in the configuration file. Change it there, or remove it \
             from [galaxy] peers to manage it here.",
            peer.url
        )));
    }

    let url = peer.url.clone();
    if replacing {
        // Keeps its place in the order, so the list does not reshuffle under
        // someone editing it.
        if galaxy.update_peer(peer).is_none() {
            return HttpResponse::NotFound().json(Message::new(format!(
                "{url} is not in this galaxy. Add it instead."
            )));
        }
    } else if let Err(e) = galaxy.add_peer(peer, &state.info.listen) {
        return match e {
            crate::galaxy::AddPeer::Invalid(message) => {
                HttpResponse::BadRequest().json(Message::new(message))
            }
            crate::galaxy::AddPeer::AlreadyThere(message) => {
                HttpResponse::Conflict().json(Message::new(message))
            }
        };
    }

    if let Err(resp) = save_peers(&state) {
        return resp;
    }
    log::info!(
        "Peer '{url}' {} by '{caller}'",
        if replacing { "changed" } else { "added" }
    );
    list_peers(state, req).await
}

/// `DELETE /_management/api/galaxy/peers?url=...` — drop a peer.
///
/// By query rather than path segment because the value is a URL, which a path
/// would have to encode and middleware is free to normalise.
pub async fn delete_peer(
    state: State,
    query: web::Query<PeerQuery>,
    req: HttpRequest,
) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };
    if let Err(resp) = peers_file(&state) {
        return resp;
    }
    let Some(galaxy) = state.galaxy.as_ref() else {
        return HttpResponse::Conflict().json(Message::new("This server has no galaxy."));
    };

    if galaxy.is_fixed(&query.url) {
        return HttpResponse::Conflict().json(Message::new(format!(
            "{} is declared in the configuration file, so removing it here would not \
             last: it would come back at the next restart. Remove it from [galaxy] \
             peers instead.",
            query.url
        )));
    }
    if !galaxy.remove_peer(&query.url) {
        return HttpResponse::NotFound().json(Message::new("No such peer."));
    }
    if let Err(resp) = save_peers(&state) {
        return resp;
    }
    log::info!("Peer '{}' removed by '{caller}'", query.url);
    list_peers(state, req).await
}

#[derive(Debug, Deserialize)]
pub struct PeerQuery {
    url: String,
}

/// This server, as a topology view needs it.
fn describe(state: &SharedState) -> serde_json::Value {
    serde_json::json!({
        "node_id": state.db.node(),
        "version": env!("CARGO_PKG_VERSION"),
        "role": state.info.role.clone(),
        "uptime_seconds": state.started.elapsed().as_secs(),
    })
}

/// `GET /_management/api/rejections` — values that were not written.
///
/// Newest first, because the question is nearly always "what has just started
/// failing?". Every write path feeds this, including the ZMQ ingest, which has
/// no caller of its own to tell.
///
/// Filtered by what the key may *read*: a rejection names a namespace and a
/// value someone tried to put in it, which is not something to hand to a key
/// that could not have read that namespace anyway.
pub async fn rejections(
    state: State,
    query: web::Query<RejectionsQuery>,
    req: HttpRequest,
) -> HttpResponse {
    let key = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    if let Some(namespace) = &query.namespace
        && let Err(resp) = require_read(&state, &key, namespace)
    {
        return resp;
    }

    let acl = state.acl();
    // Over-fetch, then drop what this key may not read, so that filtering does
    // not silently return a short page.
    let mut found: Vec<_> = state
        .rejections
        .recent(query.namespace.as_deref(), MAX_LIMIT)
        .into_iter()
        .filter(|entry| acl.can_read(&key, &entry.namespace))
        .collect();
    drop(acl);
    found.truncate(query.limit());

    HttpResponse::Ok().json(RejectionPage {
        rejections: found,
        total: state.rejections.len(),
        capacity: state.rejections.capacity(),
    })
}

/// `DELETE /_management/api/rejections` — forget them all.
///
/// How an operator marks a feed as dealt with, so that what shows up next is
/// new rather than the same thousand entries they have already read.
pub async fn clear_rejections(state: State, req: HttpRequest) -> HttpResponse {
    let key = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    // Clearing is all-or-nothing, so it takes a key that could read all of it.
    if !state.acl().can_read(&key, "") {
        return HttpResponse::Forbidden().json(Message::new(
            "Clearing the rejection log needs a key with unscoped read access.",
        ));
    }

    let held = state.rejections.len();
    state.rejections.clear();
    log::info!("{held} rejection(s) cleared by '{key}'");
    HttpResponse::Ok().json(Message::new("ok"))
}

pub async fn values(
    state: State,
    query: web::Query<ValuesQuery>,
    req: HttpRequest,
) -> HttpResponse {
    let key = match require_admin(&state, &req) {
        Ok(key) => key,
        Err(resp) => return resp,
    };
    if let Err(resp) = require_read(&state, key, &query.namespace) {
        return resp;
    }

    // Statistics are per value and can be large, so the list omits them; the
    // detail view fetches them for one value at a time.
    match state.db.value_page(
        &query.namespace,
        &query.q,
        query.offset,
        query.limit(),
        false,
    ) {
        Some(page) => HttpResponse::Ok().json(page),
        None => HttpResponse::NotFound().json(Message::new("No such namespace.")),
    }
}

/// One level of the namespace tree, so the interface can browse namespaces the
/// way a file manager browses directories.
pub async fn tree(state: State, query: web::Query<TreeQuery>, req: HttpRequest) -> HttpResponse {
    let key = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    let acl = state.acl();
    let page =
        state
            .db
            .namespace_children(&query.path, &query.q, query.offset, query.limit(), |name| {
                acl.can_read(&key, name)
            });
    drop(acl);
    HttpResponse::Ok().json(page)
}

/// Create a namespace that holds nothing yet.
///
/// Nothing else in SightingDB needs this — writing a value brings its namespace
/// into being — but a browser wants somewhere to put things before it has them,
/// and a folder made in advance is how anyone expects that to work.
pub async fn create_namespace(
    state: State,
    body: web::Json<NewNamespace>,
    req: HttpRequest,
) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    let namespace = match clean_namespace(&body.namespace) {
        Ok(namespace) => namespace,
        Err(resp) => return resp,
    };
    if let Err(resp) = require_write(&state, &caller, &namespace) {
        return resp;
    }

    if !state.db.create_namespace(&namespace) {
        return HttpResponse::Conflict()
            .json(Message::new(format!("'{namespace}' already exists.")));
    }

    log::info!("Namespace '{namespace}' created by '{caller}'");
    HttpResponse::Ok().json(serde_json::json!({ "namespace": namespace }))
}

/// Record one value or a pasted list of them, in one namespace.
///
/// The namespace does not have to exist: writing is what creates it, here as
/// everywhere else. Values are counted towards consensus exactly as a `/w/`
/// write would be, so nothing added here is a second class of sighting.
pub async fn add_values(
    state: State,
    body: web::Json<NewValues>,
    req: HttpRequest,
) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    let body = body.into_inner();
    let namespace = match clean_namespace(&body.namespace) {
        Ok(namespace) => namespace,
        Err(resp) => return resp,
    };
    if let Err(resp) = require_write(&state, &caller, &namespace) {
        return resp;
    }

    let when = match body
        .timestamp
        .map(crate::sighting_writer::timestamp_to_instant)
    {
        Some(Ok(when)) => Some(when),
        Some(Err(e)) => return HttpResponse::BadRequest().json(Message::new(e.to_string())),
        None => None,
    };

    // Whitespace-only lines are what a paste ends with, not something someone
    // meant to record.
    let values: Vec<&str> = body
        .values
        .iter()
        .map(|value| value.trim())
        .filter(|value| !value.is_empty())
        .collect();
    if values.is_empty() {
        return HttpResponse::BadRequest().json(Message::new("No values to add."));
    }

    let mut report = WriteReport {
        namespace: namespace.clone(),
        written: 0,
        counts: Vec::with_capacity(values.len()),
        errors: Vec::new(),
    };
    for value in values {
        match crate::sighting_writer::write_tagged(
            &state.db, &namespace, value, when, body.ttl, &body.tags,
        ) {
            Ok(written) => {
                report.written += 1;
                report.counts.push(ValueCount {
                    value: value.to_string(),
                    count: written.count,
                    new: written.new,
                });
            }
            Err(e) => {
                state.rejections.record(
                    &namespace,
                    value,
                    &e.to_string(),
                    crate::rejections::Source::Management,
                );
                report.errors.push(ValueError {
                    value: value.to_string(),
                    error: e.to_string(),
                });
            }
        }
    }

    log::info!(
        "{} value(s) added to '{namespace}' by '{caller}'{}",
        report.written,
        if report.errors.is_empty() {
            String::new()
        } else {
            format!(", {} rejected", report.errors.len())
        }
    );

    // Every value failing is a client error; a mix still reports what landed,
    // the same way `/wb` does.
    if report.written == 0 {
        HttpResponse::BadRequest().json(report)
    } else {
        HttpResponse::Ok().json(report)
    }
}

/// One value with its hourly statistics, which is what the histogram draws.
pub async fn value(state: State, query: web::Query<ValueQuery>, req: HttpRequest) -> HttpResponse {
    let key = match require_admin(&state, &req) {
        Ok(key) => key,
        Err(resp) => return resp,
    };
    if let Err(resp) = require_read(&state, key, &query.namespace) {
        return resp;
    }

    let consensus = state.db.count(crate::db::ALL_NAMESPACE, &query.value);
    match state
        .db
        .view(&query.namespace, &query.value, consensus, true)
    {
        Some(view) => HttpResponse::Ok().json(view),
        None => HttpResponse::NotFound().json(Message::new("No such value.")),
    }
}

/// Every namespace one value appears in, which is what the graph draws.
///
/// Only namespaces this key may read are returned. The count in `_all` — shown
/// beside the graph as consensus — still reflects every namespace, so a scoped
/// key can tell that it is not seeing all of them without being told their
/// names.
pub async fn sightings(
    state: State,
    query: web::Query<SightingsQuery>,
    req: HttpRequest,
) -> HttpResponse {
    let key = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };
    if query.value.is_empty() {
        return HttpResponse::BadRequest().json(Message::new("No value to look for."));
    }

    let acl = state.acl();
    let found = state
        .db
        .sightings_of(&query.value, query.limit(), |name| acl.can_read(&key, name));
    drop(acl);

    HttpResponse::Ok().json(serde_json::json!({
        "value": query.value,
        "consensus": state.db.count(crate::db::ALL_NAMESPACE, &query.value),
        "items": found.items,
        "truncated": found.truncated,
        "paged_in": found.paged_in,
    }))
}

/// Replace one value's tags.
///
/// Adding tags is a write like any other and goes through `/w`; this exists for
/// the other direction, since a wrong tag can only come off by replacing the
/// set. It is not a sighting: nothing is counted and no timestamp moves.
pub async fn set_tags(state: State, body: web::Json<TagChange>, req: HttpRequest) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    let change = body.into_inner();
    let namespace = match clean_namespace(&change.namespace) {
        Ok(namespace) => namespace,
        Err(resp) => return resp,
    };
    if let Err(resp) = require_write(&state, &caller, &namespace) {
        return resp;
    }

    // Where the value actually lives decides how the change gets there. A
    // server that holds the namespace changes its own copy and tells the
    // mirrors; one that does not — a router — has no copy to change, so it
    // asks a mirror for the value, applies the change to that, and offers the
    // result back to all of them.
    let held = state.db.holds(&namespace);
    if held {
        if !state.db.set_tags(&namespace, &change.value, &change.tags) {
            return HttpResponse::NotFound().json(Message::new("No such value."));
        }
    } else if let Err(resp) = retag_through_galaxy(&state, &namespace, &change).await {
        return resp;
    }

    log::info!(
        "Tags of '{}' in '{namespace}' set by '{caller}'",
        change.value
    );

    // A tag change is not a sighting, so nothing else would ever carry it to
    // the mirrors: without this it would sit here until someone noticed the
    // two copies disagreed. Pushed rather than waited for, because the whole
    // point of editing a tag is that it is wrong *now*.
    let spread = spread_tags(&state, &namespace, &change.value).await;

    let consensus = state.db.count(crate::db::ALL_NAMESPACE, &change.value);
    match state.db.view(&namespace, &change.value, consensus, false) {
        Some(view) => match spread {
            // The common case: nothing to say beyond the value itself, which
            // is the shape every existing client already reads.
            None => HttpResponse::Ok().json(view),
            Some(report) => HttpResponse::Ok().json(TaggedAcross {
                value: view,
                mirrors: report,
            }),
        },
        None => HttpResponse::Ok().json(Message::new("ok")),
    }
}

/// A tag change, and which mirrors it reached.
#[derive(Debug, Serialize)]
pub struct TaggedAcross {
    #[serde(flatten)]
    value: crate::attribute::AttributeView,
    /// One entry per mirror that holds the namespace. Absent when this server
    /// stands alone, so a lone server's answer is unchanged.
    mirrors: Vec<MirrorOutcome>,
}

#[derive(Debug, Serialize)]
pub struct MirrorOutcome {
    url: String,
    ok: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    error: Option<String>,
}

/// Offer this server's copy of a value to every mirror of its namespace.
///
/// `None` when there is nothing to offer it to. Reported rather than silent,
/// because "the tag is off here but still on a mirror" is precisely the state
/// someone editing tags needs to know about.
async fn spread_tags(
    state: &SharedState,
    namespace: &str,
    value: &str,
) -> Option<Vec<MirrorOutcome>> {
    let galaxy = state.galaxy.as_ref()?;
    let payload = state.db.merge_payload(namespace, value)?;
    let outcomes = galaxy
        .push_value(namespace, value, &payload, galaxy.max_hops())
        .await;
    if outcomes.is_empty() {
        return None;
    }
    Some(
        outcomes
            .into_iter()
            .map(|(url, outcome)| match outcome {
                Ok(_) => MirrorOutcome {
                    url,
                    ok: true,
                    error: None,
                },
                Err(e) => {
                    log::warn!("Could not send the tag change for '{value}' to {url}: {e}");
                    MirrorOutcome {
                        url,
                        ok: false,
                        error: Some(e),
                    }
                }
            })
            .collect(),
    )
}

/// Retag a value this server does not hold.
///
/// A router keeps no copy of the value, so there is nothing here to set tags
/// on. It fetches the value as a mirror holds it, applies the replacement to
/// that, and writes the result into its own database — from where
/// [`spread_tags`] offers it to every mirror. Writing it here is what makes
/// the local copy the thing that was agreed on; it is held in the router's
/// database like any other merge, and the router's own storage policy decides
/// whether it is kept beyond that.
async fn retag_through_galaxy(
    state: &SharedState,
    namespace: &str,
    change: &TagChange,
) -> Result<(), HttpResponse> {
    let Some(galaxy) = state.galaxy.as_ref() else {
        return Err(HttpResponse::NotFound().json(Message::new("No such value.")));
    };

    let path = format!(
        "/r/{}?val={}&noshadow&for_merge",
        namespace,
        crate::galaxy::urlencoding_of(&change.value)
    );
    let answer = galaxy
        .forward_read(namespace, &change.value, &path, galaxy.max_hops(), "")
        .await
        .map_err(|e| {
            HttpResponse::BadGateway().json(Message::new(format!(
                "Could not reach a mirror holding '{namespace}': {e}"
            )))
        })?;

    if !(200..300).contains(&answer.status) {
        // Relayed, so "no such value" from the mirror reads as it would if the
        // interface had asked the mirror directly.
        return Err(HttpResponse::build(
            actix_web::http::StatusCode::from_u16(answer.status)
                .unwrap_or(actix_web::http::StatusCode::BAD_GATEWAY),
        )
        .content_type("application/json")
        .body(answer.body));
    }

    let mut payload: crate::attribute::Merge =
        serde_json::from_slice(&answer.body).map_err(|e| {
            HttpResponse::BadGateway()
                .json(Message::new(format!("A mirror answered unreadably: {e}")))
        })?;

    payload.tags = change.tags.clone();
    payload.tags_at = chrono::Utc::now().timestamp_millis();

    state.db.merge(namespace, &change.value, &payload);
    Ok(())
}

// ---------------------------------------------------------------------------
// The tag vocabulary
// ---------------------------------------------------------------------------

/// One row of the tags table.
#[derive(Debug, Serialize)]
pub struct TagRow {
    /// The tag, or a family if it ends in `:`.
    pub name: String,
    pub colour: String,
    pub description: String,
    /// Whether this is a family, colouring every tag under it.
    pub family: bool,
    /// How many loaded values carry it. For a family, how many carry a tag
    /// under it. `None` for a tag that is defined but not in use.
    pub used: Option<u64>,
    /// Whether the vocabulary defines it, or it was only found on values.
    pub defined: bool,
}

/// `GET /_management/api/tags` — the vocabulary, and what is actually in use.
///
/// Two things at once, deliberately: the tags someone has given a colour to,
/// and the tags found on values. A feed brings whatever tags it brings, so the
/// second set is not a subset of the first, and seeing an undefined tag in the
/// same table is what makes it one click to adopt rather than something to
/// discover by accident.
#[derive(Debug, Serialize)]
pub struct TagsView {
    pub tags: Vec<TagRow>,
    /// The colour an undefined tag is shown in.
    pub unknown_colour: String,
    /// Whether the vocabulary can be edited here. False without a
    /// `tags_file`.
    pub editable: bool,
    /// Usage was counted over this many namespaces, of this many that exist.
    /// Less than all of them means cold namespaces were not paged in to count
    /// — see [`crate::db::Database::tag_usage`].
    pub counted_namespaces: usize,
    pub total_namespaces: usize,
}

pub async fn list_tags(state: State, req: HttpRequest) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    let (usage, counted, total) = state.db.tag_usage();

    // A key that cannot read everything would otherwise learn which tags
    // exist in namespaces it has no access to. Counted over what it may read
    // only — which needs the per-namespace walk, so it is done the simple way
    // here: a scoped key sees the vocabulary but no counts.
    let everywhere = state
        .acl
        .read()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .can_read(&caller, "/");
    let usage = if everywhere {
        usage
    } else {
        std::collections::BTreeMap::new()
    };

    let vocabulary = state
        .tags
        .read()
        .unwrap_or_else(std::sync::PoisonError::into_inner);

    let mut rows: Vec<TagRow> = Vec::new();
    for (name, tag) in vocabulary.entries() {
        let family = name.ends_with(':');
        // A family's count is everything under it, which is the only number
        // that means anything for a name no value carries literally.
        let used: u64 = if family {
            usage
                .iter()
                .filter(|(found, _)| found.starts_with(name.as_str()))
                .map(|(_, count)| *count)
                .sum()
        } else {
            usage.get(name).copied().unwrap_or(0)
        };
        rows.push(TagRow {
            name: name.clone(),
            colour: tag.colour.clone(),
            description: tag.description.clone(),
            family,
            used: (used > 0).then_some(used),
            defined: true,
        });
    }

    // Tags found on values that the vocabulary does not define.
    for (name, count) in &usage {
        if vocabulary.get(name).is_some() {
            continue;
        }
        rows.push(TagRow {
            name: name.clone(),
            // What it is shown in today, so adopting it can start from that
            // rather than from an empty field.
            colour: vocabulary
                .colour_of(name)
                .unwrap_or(crate::tags::UNKNOWN_COLOUR)
                .to_string(),
            description: String::new(),
            family: false,
            used: Some(*count),
            defined: false,
        });
    }

    rows.sort_by(|a, b| a.name.cmp(&b.name));

    HttpResponse::Ok().json(TagsView {
        tags: rows,
        unknown_colour: crate::tags::UNKNOWN_COLOUR.to_string(),
        editable: state.tags_file.is_some(),
        counted_namespaces: counted,
        total_namespaces: total,
    })
}

/// A tag's presentation, as the interface sends it.
#[derive(Debug, Deserialize)]
pub struct TagDefinition {
    name: String,
    colour: String,
    #[serde(default)]
    description: String,
}

/// Where the vocabulary is written. Without one, colours are read-only: the
/// main configuration is hand-maintained and this does not rewrite it.
fn tags_file(state: &SharedState) -> Result<&PathBuf, HttpResponse> {
    state.tags_file.as_ref().ok_or_else(|| {
        HttpResponse::Conflict().json(Message::new(
            "No tags_file is configured, so tag colours cannot be edited here. Set \
             tags_file in [daemon] and restart.",
        ))
    })
}

/// Persist the vocabulary, then adopt it. Temp file and rename, like the ACL.
fn save_tags(state: &SharedState, vocabulary: crate::tags::Vocabulary) -> Result<(), HttpResponse> {
    let path = tags_file(state)?;

    let write = || -> std::io::Result<()> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let temp = path.with_extension("tmp");
        std::fs::write(&temp, vocabulary.to_toml())?;
        std::fs::rename(&temp, path)
    };

    if let Err(e) = write() {
        log::error!("Could not write {}: {e}", path.display());
        return Err(
            HttpResponse::InternalServerError().json(Message::new(format!(
                "Could not write the tag vocabulary: {e}"
            ))),
        );
    }

    *state
        .tags
        .write()
        .unwrap_or_else(std::sync::PoisonError::into_inner) = vocabulary;
    Ok(())
}

/// `POST /_management/api/tags/vocabulary` — define or redefine one tag.
pub async fn define_tag(
    state: State,
    body: web::Json<TagDefinition>,
    req: HttpRequest,
) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };
    if let Err(resp) = tags_file(&state) {
        return resp;
    }

    let definition = body.into_inner();
    let mut vocabulary = state
        .tags
        .read()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .clone();

    if let Err(e) = vocabulary.set(
        &definition.name,
        &definition.colour,
        &definition.description,
    ) {
        return HttpResponse::BadRequest().json(Message::new(e));
    }
    if let Err(resp) = save_tags(&state, vocabulary) {
        return resp;
    }

    log::info!("Tag '{}' defined by '{caller}'", definition.name.trim());
    list_tags(state, req).await
}

/// `DELETE /_management/api/tags/vocabulary?tag=...` — forget a tag's colour.
///
/// By query rather than path segment because a tag contains `:` and often `/`,
/// which a path would have to encode and middleware would be free to
/// normalise. **Values keep the tag itself**: this is a presentation setting,
/// and deleting data from a colour picker would be a trap.
pub async fn undefine_tag(
    state: State,
    query: web::Query<TagQuery>,
    req: HttpRequest,
) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };
    if let Err(resp) = tags_file(&state) {
        return resp;
    }

    let mut vocabulary = state
        .tags
        .read()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .clone();
    if !vocabulary.remove(&query.tag) {
        return HttpResponse::NotFound().json(Message::new("No such tag in the vocabulary."));
    }
    if let Err(resp) = save_tags(&state, vocabulary) {
        return resp;
    }

    log::info!("Tag '{}' undefined by '{caller}'", query.tag);
    list_tags(state, req).await
}

#[derive(Debug, Deserialize)]
pub struct TagQuery {
    tag: String,
}

// ---------------------------------------------------------------------------
// Key management
// ---------------------------------------------------------------------------

/// Where the ACL is written. Without one configured, keys are read-only:
/// rewriting the daemon configuration in place is not something this does.
fn acl_file(state: &SharedState) -> Result<&PathBuf, HttpResponse> {
    state.acl_file.as_ref().ok_or_else(|| {
        HttpResponse::Conflict().json(Message::new(
            "No acl_file is configured, so keys cannot be edited here. Set acl_file in \
             [daemon] and restart.",
        ))
    })
}

/// Persist the ACL, then adopt it. Written to a temporary file and renamed, so
/// a crash mid-write cannot leave a truncated file that locks everyone out.
fn save_acl(state: &SharedState, acl: crate::acl::Acl) -> Result<(), HttpResponse> {
    let path = acl_file(state)?;

    let write = || -> std::io::Result<()> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let temp = path.with_extension("tmp");
        std::fs::write(&temp, acl.to_toml())?;
        std::fs::rename(&temp, path)
    };

    if let Err(e) = write() {
        log::error!("Could not write {}: {e}", path.display());
        return Err(HttpResponse::InternalServerError()
            .json(Message::new(format!("Could not write the ACL file: {e}"))));
    }

    *state
        .acl
        .write()
        .unwrap_or_else(std::sync::PoisonError::into_inner) = acl;
    Ok(())
}

/// `GET /_management/api/keys/drift` — where this server's keys and its peers'
/// disagree.
///
/// Worth a view of its own because of one deliberate limitation: the periodic
/// offer of keys to peers is *additive*, so a key revoked while a peer was down
/// stays live on that peer. That is a security hole if it is invisible, and
/// merely a chore if it is not — this is what makes it visible.
///
/// Reports, per peer:
///
///   * `revoked_but_present` — keys this server **revoked** and the peer still
///     accepts. A revocation that did not land, and the reason this exists.
///   * `only_on_peer` — keys the peer has that this server never knew about.
///     Ordinary: a peer has its own keys, including the one this server
///     authenticates with. Kept separate so it does not drown the case above.
///   * `missing` — keys this server has and the peer does not. Usually a peer
///     that has not had the periodic offer yet.
///
/// The record of revocations is in memory, so it is lost on restart: after one,
/// a revocation that never landed moves from `revoked_but_present` into
/// `only_on_peer` and stops being flagged. Checking the peer's own key list is
/// then the way to find it.
///
/// Asked through each peer's own management interface, so a peer this server
/// does not administer reports why rather than appearing to agree.
pub async fn key_drift(state: State, req: HttpRequest) -> HttpResponse {
    if let Err(resp) = require_admin(&state, &req) {
        return resp;
    }

    let Some(galaxy) = state.galaxy.as_ref() else {
        return HttpResponse::Ok().json(serde_json::json!({ "peers": [] }));
    };

    let mine: std::collections::BTreeSet<String> = state
        .acl()
        .entries()
        .iter()
        .map(|(key, _)| key.clone())
        .collect();
    let revoked_here: std::collections::BTreeSet<String> =
        state.revoked_keys().into_iter().collect();

    let mut rows = Vec::new();
    for peer in galaxy.peer_keys().await {
        let theirs: std::collections::BTreeSet<String> = peer.keys.iter().cloned().collect();
        let unknown: Vec<&String> = theirs.difference(&mine).collect();

        // The dangerous case: this server revoked it and the peer still takes
        // it. Separated from the rest, because a peer having keys this server
        // never knew about is ordinary — it has its own, including the one
        // this server authenticates with, which would otherwise be reported as
        // stale for ever.
        let (stale, theirs_alone): (Vec<&String>, Vec<&String>) = unknown
            .into_iter()
            .partition(|key| revoked_here.contains(*key));

        let missing: Vec<&String> = mine.difference(&theirs).collect();

        rows.push(serde_json::json!({
            "url": peer.url,
            "readable": peer.readable,
            "error": peer.error,
            "revoked_but_present": stale,
            "only_on_peer": theirs_alone,
            "missing": missing,
            "agrees": peer.readable && stale.is_empty() && missing.is_empty(),
        }));
    }

    HttpResponse::Ok().json(serde_json::json!({ "peers": rows }))
}

/// `PUT /_management/api/keys` — hold exactly these keys and no others.
///
/// What makes a galaxy have one place to manage keys, and the only thing that
/// makes a revocation reach a server that was offline for it: the periodic
/// offer of individual keys is additive and cannot delete.
///
/// **Refused unless this server has said it may be replaced.** `acl_replaceable`
/// in `[galaxy]` is off by default, because a server quietly having its keys
/// rewritten is not a state to arrive at by accident. With it off, the offer
/// falls back to being additive and nothing is lost.
///
/// Two guards, both the same shape as the ones on a single-key change:
///
///   * the set must contain an admin key, or nobody could use the interface
///     again;
///   * it must contain the key making the request, or the server doing the
///     replacing locks itself out of the server it just took over.
///
/// Keys removed are noted as revoked, so this server's own drift view can
/// point out any peer that still holds them.
pub async fn replace_keys(
    state: State,
    body: web::Json<KeyList>,
    req: HttpRequest,
) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    if !state.acl_replaceable {
        return HttpResponse::Forbidden().json(Message::new(
            "This server does not let another replace its keys. Set acl_replaceable              in [galaxy] if it should.",
        ));
    }

    let wanted = body.into_inner().keys;
    for entry in &wanted {
        if let Err(e) = entry.validate() {
            return HttpResponse::BadRequest().json(Message::new(e.to_string()));
        }
    }

    let mut acl = crate::acl::Acl::new();
    for entry in &wanted {
        acl.set(&entry.key, entry.to_grants());
    }

    if acl.admin_count() == 0 {
        return HttpResponse::Conflict().json(Message::new(
            "That set has no admin key, and would lock everyone out of this interface.",
        ));
    }
    if !acl.contains(&caller) {
        return HttpResponse::Conflict().json(Message::new(
            "That set does not include the key making this request, which would lock              the caller out of the server it is replacing.",
        ));
    }

    // What is going away, before it does, so the drift view can say which
    // peers still take it.
    let going: Vec<String> = state
        .acl()
        .entries()
        .iter()
        .map(|(key, _)| key.clone())
        .filter(|key| !acl.contains(key))
        .collect();

    if let Err(resp) = save_acl(&state, acl) {
        return resp;
    }
    for key in &going {
        state.note_revoked(key);
    }
    for entry in &wanted {
        state.note_unrevoked(&entry.key);
    }

    log::warn!(
        "Key list replaced by '{caller}': {} key(s) held, {} revoked",
        wanted.len(),
        going.len()
    );

    HttpResponse::Ok().json(serde_json::json!({
        "held": wanted.len(),
        "revoked": going,
    }))
}

/// A whole key list, for [`replace_keys`].
#[derive(Debug, Deserialize)]
pub struct KeyList {
    pub keys: Vec<KeyEntry>,
}

pub async fn list_keys(state: State, req: HttpRequest) -> HttpResponse {
    if let Err(resp) = require_admin(&state, &req) {
        return resp;
    }
    let entries: Vec<KeyEntry> = state
        .acl()
        .entries()
        .iter()
        .map(|(key, grants)| KeyEntry::from_grants(key, grants))
        .collect();
    HttpResponse::Ok().json(entries)
}

/// Create or replace one key.
pub async fn save_key(state: State, body: web::Json<KeyEntry>, req: HttpRequest) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    let entry = body.into_inner();
    if let Err(e) = entry.validate() {
        return HttpResponse::BadRequest().json(Message::new(e.to_string()));
    }

    let mut acl = state.acl().clone();
    let was_admin = acl.is_admin(&entry.key);
    acl.set(&entry.key, entry.to_grants());

    // Removing the admin grant from the last admin would lock everyone out of
    // the interface with no way back in short of editing the file by hand.
    if was_admin && !entry.admin && acl.admin_count() == 0 {
        return HttpResponse::Conflict().json(Message::new(
            "That would leave no admin key. Grant admin to another key first.",
        ));
    }

    if let Err(resp) = save_acl(&state, acl) {
        return resp;
    }
    log::info!("Key '{}' saved by '{caller}'", entry.key);
    // A key put back is not a stale revocation any more.
    state.note_unrevoked(&entry.key);
    gossip_key(&state, &req, &entry).await;
    HttpResponse::Ok().json(entry)
}

/// Pass a key change on to the peers this server administers.
///
/// After the change has been saved here, so a peer never hears about something
/// this server then failed to keep. Best effort: a peer that is down misses it
/// and picks it up from the periodic offer — see [`crate::galaxy::gossip`].
///
/// A change that *arrived* as gossip is passed along with one hop less, so a
/// cascade propagates and a cycle dies on the budget. A change made by a person
/// here starts with the full budget.
async fn gossip_key(state: &State, req: &HttpRequest, entry: &KeyEntry) {
    let Some(galaxy) = state.galaxy.as_ref() else {
        return;
    };
    let Some(hops) = crate::galaxy::hops_left(req, galaxy.max_hops()) else {
        return;
    };
    let report = galaxy.push_key(&serde_json::json!(entry), hops).await;
    if report.accepted > 0 || report.failed > 0 {
        log::info!(
            "Key '{}' passed to {} peer(s); {} could not take it, {} do not let this \
             server administer them",
            entry.key,
            report.accepted,
            report.failed,
            report.refused
        );
    }
}

pub async fn delete_key(state: State, path: web::Path<String>, req: HttpRequest) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };
    let key = path.into_inner();

    let mut acl = state.acl().clone();
    if !acl.contains(&key) {
        return HttpResponse::NotFound().json(Message::new("No such key."));
    }
    acl.remove(&key);

    if acl.admin_count() == 0 {
        return HttpResponse::Conflict().json(Message::new(
            "That would revoke the last admin key. Grant admin to another key first.",
        ));
    }

    if let Err(resp) = save_acl(&state, acl) {
        return resp;
    }
    log::warn!("Key '{key}' revoked by '{caller}'");
    // Noted so the interface can point out a peer that still holds it: the
    // periodic offer never deletes, so a revocation missed while a peer was
    // down is the one thing here that is genuinely dangerous.
    state.note_revoked(&key);

    if let Some(galaxy) = state.galaxy.as_ref()
        && let Some(hops) = crate::galaxy::hops_left(&req, galaxy.max_hops())
    {
        let report = galaxy.push_key_removal(&key, hops).await;
        if report.accepted > 0 || report.failed > 0 {
            log::warn!(
                "Revocation of '{key}' passed to {} peer(s); {} could not take it, {} do \
                 not let this server administer them",
                report.accepted,
                report.failed,
                report.refused
            );
        }
    }

    HttpResponse::Ok().json(Message::new("ok"))
}

/// Suggest a strong key, so nobody has to invent one.
pub async fn generate_key(state: State, req: HttpRequest) -> HttpResponse {
    if let Err(resp) = require_admin(&state, &req) {
        return resp;
    }
    HttpResponse::Ok().json(serde_json::json!({ "key": random_key() }))
}

fn random_key() -> String {
    use rand::RngExt;
    const ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    let mut rng = rand::rng();
    (0..40)
        .map(|_| ALPHABET[rng.random_range(0..ALPHABET.len())] as char)
        .collect()
}

/// Change a shard's tier and write it back, so it survives a restart.
/// Set, change or clear the storage settings of the shard a namespace is in.
///
/// The tier decides whether the shard is kept in memory, and `warm_idle` how
/// long a warm one waits before being written out — the second being the thing
/// that could only be changed in the configuration file until now.
///
/// It applies to the whole top-level namespace, and the reply says so: a change
/// made from a row deep in a tree is a change to everything beside it, which is
/// better said than discovered.
pub async fn set_tier(state: State, body: web::Json<TierChange>, req: HttpRequest) -> HttpResponse {
    let caller = match require_admin(&state, &req) {
        Ok(key) => key.to_string(),
        Err(resp) => return resp,
    };

    let change = body.into_inner();
    let namespace = change.namespace.trim().trim_matches('/');
    if namespace.is_empty() {
        return HttpResponse::BadRequest().json(Message::new("Name the namespace to change."));
    }
    if namespace.starts_with('_') {
        return HttpResponse::BadRequest().json(Message::new(
            "Internal namespaces are always hot and cannot be retiered.",
        ));
    }
    let entry = match change.entry() {
        Ok(entry) => entry,
        Err(e) => return HttpResponse::BadRequest().json(Message::new(e)),
    };

    // Changing what a namespace costs to keep is a change to it.
    if let Err(resp) = require_write(&state, &caller, namespace) {
        return resp;
    }

    let Some(path) = state.tiers_file.as_ref() else {
        return HttpResponse::Conflict().json(Message::new(
            "No tiers_file is configured, so tiers cannot be changed here. Set tiers_file in \
             [storage] and restart.",
        ));
    };

    let shard = crate::persistence::shard_of(namespace).to_string();
    state.db.set_policy(&shard, entry);
    if let Err(e) = state.db.tier_policy().save(path) {
        log::error!("Could not write {}: {e:#}", path.display());
        return HttpResponse::InternalServerError()
            .json(Message::new(format!("Could not write the tier file: {e}")));
    }

    let resolved = state.db.resolved_policy(&shard);
    log::info!(
        "Storage of '{shard}' set to {} (warm_idle {}s) by '{caller}'",
        resolved.tier.as_str(),
        resolved.warm_idle,
    );

    // Passed on to the peers this server administers, under the same rule as a
    // key change: the peer's own ACL decides whether to accept it. A tier only
    // ever takes a value — there is no unset — so this needs no deletion to
    // chase, which is the one thing that made keys awkward.
    if let Some(galaxy) = state.galaxy.as_ref()
        && let Some(hops) = crate::galaxy::hops_left(&req, galaxy.max_hops())
    {
        let report = galaxy
            .push_tier(
                &serde_json::json!({
                    "namespace": shard,
                    "tier": resolved.tier.as_str(),
                    "warm_idle": resolved.warm_idle,
                }),
                hops,
            )
            .await;
        if report.accepted > 0 || report.failed > 0 {
            log::info!(
                "Storage of '{shard}' passed to {} peer(s); {} could not take it, {} do \
                 not let this server administer them",
                report.accepted,
                report.failed,
                report.refused
            );
        }
    }

    HttpResponse::Ok().json(serde_json::json!({
        "shard": shard,
        "tier": resolved.tier.as_str(),
        "warm_idle": resolved.warm_idle,
        "own_tier": resolved.own_tier.is_some(),
        "own_warm_idle": resolved.own_warm_idle.is_some(),
        "effect": state.db.shard_effect(&shard),
    }))
}

/// Register the admin routes.
pub fn routes(cfg: &mut web::ServiceConfig) {
    // Order matters: the catch-all must come last or it would swallow the API.
    cfg.route("/_management/echarts.min.js", web::get().to(echarts))
        .route("/_management/logo.png", web::get().to(logo))
        .route("/_management/api/session", web::get().to(session))
        .route("/_management/api/info", web::get().to(info))
        .route("/_management/api/namespaces", web::get().to(namespaces))
        .route(
            "/_management/api/namespaces",
            web::post().to(create_namespace),
        )
        .route("/_management/api/tree", web::get().to(tree))
        .route("/_management/api/galaxy", web::get().to(galaxy))
        .route("/_management/api/galaxy/peers", web::get().to(list_peers))
        .route("/_management/api/galaxy/peers", web::post().to(add_peer))
        .route("/_management/api/galaxy/peers", web::put().to(update_peer))
        .route(
            "/_management/api/galaxy/peers",
            web::delete().to(delete_peer),
        )
        .route("/_management/api/rejections", web::get().to(rejections))
        .route(
            "/_management/api/rejections",
            web::delete().to(clear_rejections),
        )
        .route("/_management/api/values", web::get().to(values))
        .route("/_management/api/values", web::post().to(add_values))
        .route("/_management/api/tags", web::post().to(set_tags))
        .route("/_management/api/tags", web::get().to(list_tags))
        .route(
            "/_management/api/tags/vocabulary",
            web::post().to(define_tag),
        )
        .route(
            "/_management/api/tags/vocabulary",
            web::delete().to(undefine_tag),
        )
        .route("/_management/api/value", web::get().to(value))
        .route("/_management/api/sightings", web::get().to(sightings))
        .route("/_management/api/keys", web::get().to(list_keys))
        .route("/_management/api/keys", web::post().to(save_key))
        .route("/_management/api/keys", web::put().to(replace_keys))
        .route(
            "/_management/api/keys/generate",
            web::get().to(generate_key),
        )
        // Before the `{key}` route, or "drift" would be taken for a key name.
        .route("/_management/api/keys/drift", web::get().to(key_drift))
        .route("/_management/api/keys/{key}", web::delete().to(delete_key))
        .route("/_management/api/tier", web::post().to(set_tier))
        .route("/_management", web::get().to(index))
        .route("/_management/{namespace:.*}", web::get().to(index));
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::acl::parse_grants;
    use crate::db::WriteOpts;
    use actix_web::http::StatusCode;
    use actix_web::{App, test};
    use chrono::Utc;
    use serde_json::{Value as Json, json};

    const ADMIN: &str = "changeme";
    /// Spelled out so `None` has a type at the call sites.
    const NO_KEY: Option<&str> = None;

    fn state() -> State {
        let mut inner = SharedState::new(false);
        inner.acl.get_mut().unwrap().grant_full(ADMIN);
        inner
            .acl
            .get_mut()
            .unwrap()
            .set("plain", parse_grants("rw").unwrap());

        for value in ["1.2.3.4", "5.6.7.8", "9.9.9.9"] {
            inner.db.write(
                "feeds/ips",
                value,
                Utc::now(),
                WriteOpts {
                    consensus: true,
                    ttl: None,
                },
            );
        }
        web::Data::new(inner)
    }

    macro_rules! app {
        ($state:expr) => {
            test::init_service(App::new().app_data($state.clone()).configure(routes)).await
        };
    }

    /// The management routes *and* the data routes, for tests that drive a
    /// write and then ask the management interface what it made of it.
    macro_rules! app_with_data {
        ($state:expr) => {
            test::init_service(
                App::new()
                    .app_data($state.clone())
                    .configure(routes)
                    .configure(crate::handlers::routes),
            )
            .await
        };
    }

    /// A GET with an optional key; a macro rather than a function so the
    /// service type does not have to be named.
    macro_rules! get {
        ($app:expr, $uri:expr, $key:expr $(,)?) => {{
            let mut req = test::TestRequest::get().uri($uri);
            if let Some(key) = $key {
                req = req.insert_header(("Authorization", key));
            }
            test::call_service(&$app, req.to_request()).await
        }};
    }

    #[actix_web::test]
    async fn the_interface_is_served_without_a_key() {
        let st = state();
        let app = app!(st);

        // The page itself is just markup; everything it shows needs the key.
        let resp = get!(app, "/_management/", NO_KEY);
        assert_eq!(resp.status(), StatusCode::OK);
        let body = test::read_body(resp).await;
        assert!(String::from_utf8_lossy(&body).contains("<title>"));
    }

    #[actix_web::test]
    async fn the_data_endpoints_need_an_admin_key() {
        let st = state();
        let app = app!(st);

        for uri in [
            "/_management/api/info",
            "/_management/api/namespaces",
            "/_management/api/values?namespace=feeds/ips",
            "/_management/api/value?namespace=feeds/ips&value=1.2.3.4",
            "/_management/api/session",
        ] {
            assert_eq!(
                get!(app, uri, NO_KEY).status(),
                StatusCode::UNAUTHORIZED,
                "{uri}"
            );
            // A valid key that is not an admin key is not enough either.
            assert_eq!(
                get!(app, uri, Some("plain")).status(),
                StatusCode::FORBIDDEN,
                "{uri}"
            );
            assert_eq!(
                get!(app, uri, Some(ADMIN)).status(),
                StatusCode::OK,
                "{uri}"
            );
        }
    }

    /// A paste reports what each value now stands at, so the interface does not
    /// have to read the values back to say what it just did.
    #[actix_web::test]
    async fn adding_values_reports_each_running_count() {
        let st = state();
        let app = app!(st);

        // `1.2.3.4` is already in `feeds/ips` once, from `state()`.
        let resp = test::call_service(
            &app,
            test::TestRequest::post()
                .uri("/_management/api/values")
                .insert_header(("Authorization", ADMIN))
                .set_json(serde_json::json!({
                    "namespace": "feeds/ips",
                    "values": ["1.2.3.4", "fresh", "   "],
                }))
                .to_request(),
        )
        .await;

        assert_eq!(resp.status(), StatusCode::OK);
        let body: serde_json::Value = test::read_body_json(resp).await;

        // The blank line was dropped before anything was tried.
        assert_eq!(body["written"], 2, "{body}");

        let counts = body["counts"].as_array().unwrap();
        assert_eq!(counts.len(), 2, "{body}");
        assert_eq!(counts[0]["value"], "1.2.3.4");
        assert_eq!(counts[0]["count"], 2, "an existing value was not added to");
        assert_eq!(counts[1]["value"], "fresh");
        assert_eq!(counts[1]["count"], 1, "{body}");

        // And the counts are what the database actually holds.
        assert_eq!(st.db.count("feeds/ips", "1.2.3.4"), 2);
        assert_eq!(st.db.count("feeds/ips", "fresh"), 1);
    }

    /// Every write path feeds the rejection log, and the log is what answers
    /// "which values errored" after the response has gone.
    #[actix_web::test]
    async fn rejections_are_listed_newest_first() {
        let st = state();
        let app = app_with_data!(st);

        // A rejection from the single-value route, and two from a bulk write.
        test::call_service(
            &app,
            test::TestRequest::get()
                .uri("/w/feeds/ips?val=x&timestamp=99999999999999")
                .to_request(),
        )
        .await;
        test::call_service(
            &app,
            test::TestRequest::post()
                .uri("/wb")
                .set_json(serde_json::json!({"items": [
                    {"namespace": "feeds/ips", "value": ""},
                    {"namespace": "_config/acl/apikeys/mine", "value": "sneaky"}
                ]}))
                .to_request(),
        )
        .await;

        let resp = get!(app, "/_management/api/rejections", Some(ADMIN));
        assert_eq!(resp.status(), StatusCode::OK);
        let body: serde_json::Value = test::read_body_json(resp).await;

        let found = body["rejections"].as_array().unwrap();
        assert_eq!(found.len(), 3, "{body}");
        assert_eq!(body["total"], 3, "{body}");
        assert_eq!(
            body["capacity"],
            crate::rejections::DEFAULT_CAPACITY,
            "{body}"
        );

        // Newest first.
        assert_eq!(found[0]["value"], "sneaky", "{body}");
        assert_eq!(found[0]["source"], "bulkwrite", "{body}");
        assert!(
            found[0]["reason"].as_str().unwrap().contains("_config"),
            "{body}"
        );

        // The empty value is kept as itself, which is the whole reason this is
        // not a namespace of sightings.
        assert_eq!(found[1]["value"], "", "{body}");

        assert_eq!(found[2]["value"], "x", "{body}");
        assert_eq!(found[2]["source"], "write", "{body}");
        assert!(found[2]["when"].as_i64().unwrap() > 0, "{body}");
    }

    /// A dry run writes nothing, so it must leave no rejections behind either
    /// — otherwise checking a batch would pollute the record of real failures,
    /// and anyone could flood it without writing a thing.
    #[actix_web::test]
    async fn a_dry_run_records_no_rejections() {
        let st = state();
        let app = app_with_data!(st);

        let resp = test::call_service(
            &app,
            test::TestRequest::post()
                .uri("/vwb")
                .set_json(serde_json::json!({"items": [
                    {"namespace": "feeds/ips", "value": ""}
                ]}))
                .to_request(),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);

        assert_eq!(
            st.rejections.len(),
            0,
            "the dry run left a rejection behind"
        );
    }

    /// The management add path guards its namespace before it writes, so it
    /// cannot currently reach a rejection at all.
    ///
    /// Pinned because that is load-bearing for the claim above it: the
    /// recording wired into `add_values` is unreachable today and kept only so
    /// that loosening the guard does not silently lose the value. If this test
    /// starts failing, that path has become live and wants a test of its own.
    #[actix_web::test]
    async fn the_management_add_path_rejects_before_it_writes() {
        let st = state();
        let app = app!(st);

        let resp = test::call_service(
            &app,
            test::TestRequest::post()
                .uri("/_management/api/values")
                .insert_header(("Authorization", ADMIN))
                .set_json(serde_json::json!({
                    "namespace": "_config/acl/apikeys/mine",
                    "values": ["sneaky"],
                }))
                .to_request(),
        )
        .await;

        // Turned away by `clean_namespace`, not by the writer.
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
        assert_eq!(st.rejections.len(), 0, "a rejection was recorded after all");
        assert!(!st.db.namespace_exists("_config/acl/apikeys/mine"));
    }

    /// A rejection names a namespace and what someone tried to put in it, so a
    /// key that could not read that namespace must not see it here.
    #[actix_web::test]
    async fn rejections_are_filtered_by_what_the_key_may_read() {
        let st = state();
        st.rejections
            .record("feeds/ips", "a", "nope", crate::rejections::Source::Ingest);
        st.rejections
            .record("secrets", "b", "nope", crate::rejections::Source::Ingest);

        // An admin key scoped to `feeds` only.
        st.acl
            .write()
            .unwrap()
            .set("scoped", parse_grants("admin, rw:feeds").unwrap());
        let app = app!(st);

        let resp = get!(app, "/_management/api/rejections", Some("scoped"));
        assert_eq!(resp.status(), StatusCode::OK);
        let body: serde_json::Value = test::read_body_json(resp).await;
        let found = body["rejections"].as_array().unwrap();

        assert_eq!(found.len(), 1, "a scoped key saw another subtree: {body}");
        assert_eq!(found[0]["namespace"], "feeds/ips", "{body}");

        // The full-access key sees both.
        let resp = get!(app, "/_management/api/rejections", Some(ADMIN));
        let body: serde_json::Value = test::read_body_json(resp).await;
        assert_eq!(body["rejections"].as_array().unwrap().len(), 2, "{body}");
    }

    /// Asking for one subtree must be authorized like any other read of it —
    /// including the answer, which is `404` rather than `403` throughout the
    /// management interface so that browsing cannot enumerate what is out of
    /// reach. See [`require_read`].
    #[actix_web::test]
    async fn filtering_by_a_namespace_needs_read_access_to_it() {
        let st = state();
        st.acl
            .write()
            .unwrap()
            .set("scoped", parse_grants("admin, rw:feeds").unwrap());
        let app = app!(st);

        let resp = get!(
            app,
            "/_management/api/rejections?namespace=secrets",
            Some("scoped")
        );
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[actix_web::test]
    async fn rejections_can_be_cleared() {
        let st = state();
        st.rejections
            .record("feeds/ips", "a", "nope", crate::rejections::Source::Ingest);
        st.acl
            .write()
            .unwrap()
            .set("scoped", parse_grants("admin, rw:feeds").unwrap());
        let app = app!(st);

        // A scoped key cannot clear what it cannot wholly see.
        let resp = test::call_service(
            &app,
            test::TestRequest::delete()
                .uri("/_management/api/rejections")
                .insert_header(("Authorization", "scoped"))
                .to_request(),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
        assert_eq!(st.rejections.len(), 1);

        let resp = test::call_service(
            &app,
            test::TestRequest::delete()
                .uri("/_management/api/rejections")
                .insert_header(("Authorization", ADMIN))
                .to_request(),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(st.rejections.len(), 0);
    }

    /// A server that has not opted in keeps its own keys, whoever asks.
    ///
    /// The default, because a server quietly having its key list rewritten is
    /// not a state to arrive at by accident.
    #[actix_web::test]
    async fn keys_cannot_be_replaced_unless_the_server_allows_it() {
        let st = state();
        let app = app!(st);

        let resp = test::call_service(
            &app,
            test::TestRequest::put()
                .uri("/_management/api/keys")
                .insert_header(("Authorization", ADMIN))
                .set_json(serde_json::json!({"keys": [
                    {"key": ADMIN, "admin": true, "read": [""], "write": [""]}
                ]}))
                .to_request(),
        )
        .await;

        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
        let body: serde_json::Value = test::read_body_json(resp).await;
        assert!(
            body["message"]
                .as_str()
                .unwrap()
                .contains("acl_replaceable"),
            "the refusal does not say how to allow it: {body}"
        );
    }

    /// With it allowed, a replace removes what is not in the set — which is
    /// the whole reason it exists, since offering keys one at a time cannot
    /// delete and so cannot carry a revocation to a server that was offline.
    #[actix_web::test]
    async fn an_allowed_replace_removes_what_is_not_offered() {
        let mut inner = SharedState::new(false);
        inner.acl.get_mut().unwrap().grant_full(ADMIN);
        inner
            .acl
            .get_mut()
            .unwrap()
            .set("doomed", parse_grants("rw:feeds").unwrap());
        let dir = TempDir::new("replace");
        inner.acl_file = Some(dir.0.join("acl.toml"));
        inner.acl_replaceable = true;
        let st: State = web::Data::new(inner);
        let app = app!(st);

        let resp = test::call_service(
            &app,
            test::TestRequest::put()
                .uri("/_management/api/keys")
                .insert_header(("Authorization", ADMIN))
                .set_json(serde_json::json!({"keys": [
                    {"key": ADMIN, "admin": true, "read": [""], "write": [""]},
                    {"key": "kept", "admin": false, "read": ["feeds"], "write": []}
                ]}))
                .to_request(),
        )
        .await;

        assert_eq!(resp.status(), StatusCode::OK);
        let body: serde_json::Value = test::read_body_json(resp).await;
        assert_eq!(body["held"], 2, "{body}");
        assert_eq!(body["revoked"][0], "doomed", "{body}");

        let acl = st.acl();
        assert!(acl.contains("kept"));
        assert!(!acl.contains("doomed"), "the revocation did not land");
        drop(acl);

        // Noted, so this server's drift view can point out a peer that still
        // takes it.
        assert!(st.revoked_keys().contains(&"doomed".to_string()));
    }

    /// A replace that would lock the interface or the caller out is refused,
    /// the same way a single-key change is.
    #[actix_web::test]
    async fn a_replace_cannot_lock_anyone_out() {
        let dir = TempDir::new("lockout");
        let fresh = || {
            let mut inner = SharedState::new(false);
            inner.acl.get_mut().unwrap().grant_full(ADMIN);
            inner.acl_file = Some(dir.0.join("acl.toml"));
            inner.acl_replaceable = true;
            web::Data::new(inner) as State
        };

        // No admin at all.
        let st = fresh();
        let app = app!(st);
        let resp = test::call_service(
            &app,
            test::TestRequest::put()
                .uri("/_management/api/keys")
                .insert_header(("Authorization", ADMIN))
                .set_json(serde_json::json!({"keys": [
                    {"key": "reader", "admin": false, "read": ["feeds"], "write": []}
                ]}))
                .to_request(),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::CONFLICT);
        assert!(st.acl().contains(ADMIN), "the ACL was replaced anyway");

        // An admin, but not the caller: the server replacing it would lose its
        // own way in.
        let st = fresh();
        let app = app!(st);
        let resp = test::call_service(
            &app,
            test::TestRequest::put()
                .uri("/_management/api/keys")
                .insert_header(("Authorization", ADMIN))
                .set_json(serde_json::json!({"keys": [
                    {"key": "someone-else", "admin": true, "read": [""], "write": [""]}
                ]}))
                .to_request(),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::CONFLICT);
        let body: serde_json::Value = test::read_body_json(resp).await;
        assert!(
            body["message"]
                .as_str()
                .unwrap()
                .contains("making this request"),
            "{body}"
        );
        assert!(st.acl().contains(ADMIN));
    }

    #[actix_web::test]
    async fn namespaces_are_paged_and_filtered() {
        let st = state();
        let app = app!(st);

        let resp = get!(app, "/_management/api/namespaces?limit=1", Some(ADMIN));
        let body: Json = test::read_body_json(resp).await;
        assert_eq!(body["items"].as_array().unwrap().len(), 1);
        assert_eq!(body["total"], 1);

        let resp = get!(app, "/_management/api/namespaces?q=nomatch", Some(ADMIN));
        let body: Json = test::read_body_json(resp).await;
        assert_eq!(body["total"], 0);
    }

    #[actix_web::test]
    async fn values_are_paged_and_omit_statistics() {
        let st = state();
        let app = app!(st);

        let resp = get!(
            app,
            "/_management/api/values?namespace=feeds/ips&offset=1&limit=1",
            Some(ADMIN),
        );
        let body: Json = test::read_body_json(resp).await;

        assert_eq!(body["total"], 3);
        assert_eq!(body["offset"], 1);
        let items = body["items"].as_array().unwrap();
        assert_eq!(items.len(), 1);
        // The list would be enormous with per-value histograms in it.
        assert!(items[0].get("stats").is_none(), "{items:?}");
    }

    #[actix_web::test]
    async fn a_single_value_carries_its_histogram() {
        let st = state();
        let app = app!(st);

        let resp = get!(
            app,
            "/_management/api/value?namespace=feeds/ips&value=1.2.3.4",
            Some(ADMIN),
        );
        let body: Json = test::read_body_json(resp).await;

        assert_eq!(body["value"], "1.2.3.4");
        assert!(body["stats"].is_object(), "{body}");
    }

    #[actix_web::test]
    async fn missing_things_are_404_not_500() {
        let st = state();
        let app = app!(st);

        assert_eq!(
            get!(app, "/_management/api/values?namespace=nope", Some(ADMIN)).status(),
            StatusCode::NOT_FOUND
        );
        assert_eq!(
            get!(
                app,
                "/_management/api/value?namespace=feeds/ips&value=nope",
                Some(ADMIN)
            )
            .status(),
            StatusCode::NOT_FOUND
        );
    }

    #[actix_web::test]
    async fn the_page_size_is_capped() {
        let st = state();
        let app = app!(st);

        let resp = get!(
            app,
            "/_management/api/values?namespace=feeds/ips&limit=100000",
            Some(ADMIN),
        );
        assert_eq!(resp.status(), StatusCode::OK);
        // Capped rather than refused, so a careless caller still gets an answer.
        let body: Json = test::read_body_json(resp).await;
        assert_eq!(body["items"].as_array().unwrap().len(), 3);
    }

    // -- galaxy peers -------------------------------------------------------

    /// A router with a peers file and one peer declared in the configuration,
    /// so both the editable and the read-only case are present.
    fn peered_state(dir: &std::path::Path) -> State {
        let mut inner = SharedState::new(true);
        inner.acl.get_mut().unwrap().grant_full(ADMIN);
        inner.info.listen = "127.0.0.1:9999".to_string();
        inner.galaxy_peers_file = Some(dir.join("peers.toml"));
        inner.galaxy = Some(crate::galaxy::Galaxy::new(&crate::config::GalaxySettings {
            peers: vec![crate::config::Peer {
                url: "http://from-the-file:9999".to_string(),
                key: "k".to_string(),
                stores: crate::db::StoragePolicy::everything(),
            }],
            peers_file: Some(dir.join("peers.toml")),
            fixed: vec!["http://from-the-file:9999".to_string()],
            max_hops: 4,
            health_interval: 30,
            sync_interval: 0,
            reconcile_interval: 0,
            gossip_interval: 0,
            acl_authority: false,
            acl_replaceable: false,
            verify_tls: true,
        }));
        web::Data::new(inner)
    }

    macro_rules! peer {
        ($app:expr, $method:ident, $body:expr) => {
            test::call_service(
                &$app,
                test::TestRequest::$method()
                    .uri("/_management/api/galaxy/peers")
                    .insert_header(("Authorization", ADMIN))
                    .set_json($body)
                    .to_request(),
            )
            .await
        };
    }

    /// A peer added here is in effect at once and written down, so it survives
    /// a restart. Both halves matter: in effect but unwritten would vanish,
    /// written but not in effect would need a restart.
    #[actix_web::test]
    async fn a_peer_added_here_takes_effect_and_is_written() {
        let dir = TempDir::new("peers-add");
        let st = peered_state(&dir.0);
        let app = app!(st);

        let resp = peer!(
            app,
            post,
            serde_json::json!({"url": "http://added:9999", "key": "secret"})
        );
        assert_eq!(resp.status(), StatusCode::OK);

        // In effect: the galaxy will forward to it now.
        assert!(
            st.galaxy
                .as_ref()
                .unwrap()
                .holders("feeds/ips")
                .iter()
                .any(|peer| peer.url == "http://added:9999"),
            "the new peer is not being used for forwarding"
        );

        // Written down, with its key, which is what a restart needs.
        let written = std::fs::read_to_string(dir.0.join("peers.toml")).expect("the file");
        assert!(written.contains("http://added:9999"), "{written}");
        assert!(written.contains("secret"), "{written}");
        // And *without* the peer the configuration owns, which would otherwise
        // be declared in two files.
        assert!(
            !written.contains("from-the-file"),
            "a configured peer was copied into the machine-owned file: {written}"
        );
    }

    /// Adding and changing are separate verbs: an address typed into the add
    /// form must not silently replace an existing peer's key, which is the one
    /// thing bounding what this server may do there.
    #[actix_web::test]
    async fn adding_a_peer_that_exists_is_refused_rather_than_an_upsert() {
        let dir = TempDir::new("peers-twice");
        let st = peered_state(&dir.0);
        let app = app!(st);

        peer!(
            app,
            post,
            serde_json::json!({"url": "http://added:9999", "key": "first"})
        );
        let resp = peer!(
            app,
            post,
            serde_json::json!({"url": "http://added:9999", "key": "second"})
        );
        assert_eq!(resp.status(), StatusCode::CONFLICT);

        // The original key is intact.
        let written = std::fs::read_to_string(dir.0.join("peers.toml")).unwrap();
        assert!(written.contains("first"), "{written}");
        assert!(!written.contains("second"), "{written}");
    }

    #[actix_web::test]
    async fn changing_a_peer_keeps_one_entry() {
        let dir = TempDir::new("peers-put");
        let st = peered_state(&dir.0);
        let app = app!(st);

        peer!(
            app,
            post,
            serde_json::json!({"url": "http://added:9999", "key": "first"})
        );
        let resp = peer!(
            app,
            put,
            serde_json::json!({"url": "http://added:9999", "key": "second"})
        );
        assert_eq!(resp.status(), StatusCode::OK);

        let body: Json = test::read_body_json(resp).await;
        let urls: Vec<&str> = body["peers"]
            .as_array()
            .unwrap()
            .iter()
            .map(|p| p["url"].as_str().unwrap())
            .collect();
        assert_eq!(urls, vec!["http://from-the-file:9999", "http://added:9999"]);

        let written = std::fs::read_to_string(dir.0.join("peers.toml")).unwrap();
        assert!(written.contains("second"), "{written}");
    }

    #[actix_web::test]
    async fn changing_a_peer_that_is_not_there_is_not_an_add() {
        let dir = TempDir::new("peers-put-missing");
        let st = peered_state(&dir.0);
        let app = app!(st);

        let resp = peer!(
            app,
            put,
            serde_json::json!({"url": "http://nowhere:9999", "key": "k"})
        );
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    /// A peer from the hand-maintained configuration is shown but not
    /// editable: "removing" it here would last until the next restart, which
    /// is worse than refusing.
    #[actix_web::test]
    async fn a_configured_peer_cannot_be_changed_or_removed_here() {
        let dir = TempDir::new("peers-fixed");
        let st = peered_state(&dir.0);
        let app = app!(st);

        let body: Json =
            test::read_body_json(get!(app, "/_management/api/galaxy/peers", Some(ADMIN))).await;
        assert_eq!(body["peers"][0]["fixed"], true);

        let resp = test::call_service(
            &app,
            test::TestRequest::delete()
                .uri("/_management/api/galaxy/peers?url=http://from-the-file:9999")
                .insert_header(("Authorization", ADMIN))
                .to_request(),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::CONFLICT);
        let body: Json = test::read_body_json(resp).await;
        assert!(
            body["message"].as_str().unwrap().contains("configuration"),
            "the refusal should say where it came from: {body}"
        );

        let resp = peer!(
            app,
            put,
            serde_json::json!({"url": "http://from-the-file:9999", "key": "k"})
        );
        assert_eq!(resp.status(), StatusCode::CONFLICT);
    }

    /// Its own address is refused, because a server mirroring itself is never
    /// what was meant.
    #[actix_web::test]
    async fn a_server_cannot_be_added_to_its_own_galaxy() {
        let dir = TempDir::new("peers-self");
        let st = peered_state(&dir.0);
        let app = app!(st);

        let resp = peer!(
            app,
            post,
            serde_json::json!({"url": "http://127.0.0.1:9999", "key": "k"})
        );
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
        let body: Json = test::read_body_json(resp).await;
        assert!(
            body["message"].as_str().unwrap().contains("own address"),
            "{body}"
        );
    }

    #[actix_web::test]
    async fn a_peer_needs_a_key_and_a_real_url() {
        let dir = TempDir::new("peers-bad");
        let st = peered_state(&dir.0);
        let app = app!(st);

        for body in [
            serde_json::json!({"url": "http://ok:9999", "key": ""}),
            serde_json::json!({"url": "ok:9999", "key": "k"}),
            serde_json::json!({"url": "", "key": "k"}),
        ] {
            let resp = peer!(app, post, body.clone());
            assert_eq!(resp.status(), StatusCode::BAD_REQUEST, "accepted {body}");
        }
    }

    /// The key is a credential this server holds. A topology view is not a
    /// reason to hand it back out.
    #[actix_web::test]
    async fn a_peers_key_is_never_sent_back() {
        let dir = TempDir::new("peers-secret");
        let st = peered_state(&dir.0);
        let app = app!(st);

        peer!(
            app,
            post,
            serde_json::json!({"url": "http://added:9999", "key": "do-not-leak"})
        );
        let resp = get!(app, "/_management/api/galaxy/peers", Some(ADMIN));
        let body = test::read_body(resp).await;
        let text = String::from_utf8_lossy(&body);
        assert!(
            !text.contains("do-not-leak"),
            "a peer key was returned: {text}"
        );
    }

    /// Without a peers file the list is read-only, and the interface is told
    /// so rather than finding out when a save fails.
    #[actix_web::test]
    async fn peers_are_read_only_without_a_peers_file() {
        let st = state();
        let app = app!(st);

        let body: Json =
            test::read_body_json(get!(app, "/_management/api/galaxy/peers", Some(ADMIN))).await;
        assert_eq!(body["editable"], false);
        assert!(body["note"].as_str().is_some(), "{body}");

        let resp = peer!(
            app,
            post,
            serde_json::json!({"url": "http://x:1", "key": "k"})
        );
        assert_eq!(resp.status(), StatusCode::CONFLICT);
    }

    #[actix_web::test]
    async fn the_peer_list_needs_a_key() {
        let st = state();
        let app = app!(st);
        assert_eq!(
            get!(app, "/_management/api/galaxy/peers", NO_KEY).status(),
            StatusCode::UNAUTHORIZED
        );
    }

    // -- the tag vocabulary -------------------------------------------------

    /// State with a real tags_file and some tagged values, so the table has
    /// something to count.
    fn tagged_state(dir: &std::path::Path) -> State {
        let mut inner = SharedState::new(true);
        inner.acl.get_mut().unwrap().grant_full(ADMIN);
        inner.tags_file = Some(dir.join("tags.toml"));
        inner.db.write_tagged(
            "feeds/ips",
            "1.1.1.1",
            chrono::Utc::now(),
            crate::db::WriteOpts::default(),
            "tlp:green,stix-type:ipv4-addr",
        );
        inner.db.write_tagged(
            "feeds/ips",
            "2.2.2.2",
            chrono::Utc::now(),
            crate::db::WriteOpts::default(),
            "tlp:green,home-grown",
        );
        web::Data::new(inner)
    }

    fn row<'a>(body: &'a Json, name: &str) -> &'a Json {
        body["tags"]
            .as_array()
            .expect("tags")
            .iter()
            .find(|t| t["name"] == name)
            .unwrap_or_else(|| panic!("no row for {name} in {body}"))
    }

    /// The table shows what is defined *and* what turned up on values, because
    /// a feed brings tags nobody defined and those are the ones worth seeing.
    #[actix_web::test]
    async fn the_tags_table_lists_defined_and_merely_seen_tags() {
        let dir = TempDir::new("tags-list");
        let st = tagged_state(&dir.0);
        let app = app!(st);

        let resp = get!(app, "/_management/api/tags", Some(ADMIN));
        assert_eq!(resp.status(), StatusCode::OK);
        let body: Json = test::read_body_json(resp).await;

        // Defined by the seed, and in use.
        assert_eq!(row(&body, "tlp:green")["used"], 2);
        assert_eq!(row(&body, "tlp:green")["defined"], true);
        // On a value, defined by nobody.
        assert_eq!(row(&body, "home-grown")["used"], 1);
        assert_eq!(row(&body, "home-grown")["defined"], false);
        // Defined and unused: still listed, with no count.
        assert_eq!(row(&body, "tlp:amber")["used"], Json::Null);
        assert_eq!(body["editable"], true);
    }

    /// A family's count is everything under it, which is the only number that
    /// means anything for a name no value carries literally.
    #[actix_web::test]
    async fn a_family_counts_the_tags_beneath_it() {
        let dir = TempDir::new("tags-family");
        let st = tagged_state(&dir.0);
        let app = app!(st);

        let body: Json =
            test::read_body_json(get!(app, "/_management/api/tags", Some(ADMIN))).await;
        assert_eq!(row(&body, "stix-type:")["family"], true);
        assert_eq!(row(&body, "stix-type:")["used"], 1);
    }

    /// A definition lands on disk and takes effect at once, like a key.
    #[actix_web::test]
    async fn a_defined_tag_is_written_and_adopted() {
        let dir = TempDir::new("tags-define");
        let st = tagged_state(&dir.0);
        let app = app!(st);

        let resp = test::call_service(
            &app,
            test::TestRequest::post()
                .uri("/_management/api/tags/vocabulary")
                .insert_header(("Authorization", ADMIN))
                .set_json(serde_json::json!({
                    "name": "home-grown",
                    "colour": "#AA33CC",
                    "description": "Ours, not from a feed."
                }))
                .to_request(),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::OK);

        // Answered with the whole table, so the interface need not refetch.
        let body: Json = test::read_body_json(resp).await;
        assert_eq!(row(&body, "home-grown")["defined"], true);
        assert_eq!(row(&body, "home-grown")["colour"], "#aa33cc");
        assert_eq!(row(&body, "home-grown")["used"], 1, "it was already in use");

        let written = std::fs::read_to_string(dir.0.join("tags.toml")).expect("the file");
        assert!(written.contains("home-grown"), "{written}");
        assert!(written.contains("#aa33cc"), "{written}");
        // In effect without a restart.
        assert_eq!(
            st.tags.read().unwrap().colour_of("home-grown"),
            Some("#aa33cc")
        );
    }

    /// Removing a colour must not remove the tag from the values, which would
    /// make a colour picker delete data.
    #[actix_web::test]
    async fn undefining_a_tag_leaves_it_on_the_values() {
        let dir = TempDir::new("tags-undefine");
        let st = tagged_state(&dir.0);
        let app = app!(st);

        let resp = test::call_service(
            &app,
            test::TestRequest::delete()
                .uri("/_management/api/tags/vocabulary?tag=tlp:green")
                .insert_header(("Authorization", ADMIN))
                .to_request(),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::OK);

        let body: Json = test::read_body_json(resp).await;
        // Still in the table, because values still carry it -- just undefined.
        assert_eq!(row(&body, "tlp:green")["defined"], false);
        assert_eq!(row(&body, "tlp:green")["used"], 2);

        let view = st.db.view("feeds/ips", "1.1.1.1", 0, false).expect("value");
        assert!(
            view.tags.contains("tlp:green"),
            "the tag was taken off the value: {}",
            view.tags
        );
    }

    #[actix_web::test]
    async fn a_tag_that_was_never_defined_cannot_be_undefined() {
        let dir = TempDir::new("tags-missing");
        let st = tagged_state(&dir.0);
        let app = app!(st);

        let resp = test::call_service(
            &app,
            test::TestRequest::delete()
                .uri("/_management/api/tags/vocabulary?tag=never-existed")
                .insert_header(("Authorization", ADMIN))
                .to_request(),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    /// Without a `tags_file` the colours are read-only, and the interface is
    /// told so rather than finding out when a save fails.
    #[actix_web::test]
    async fn colours_are_read_only_without_a_tags_file() {
        let st = state();
        let app = app!(st);

        let body: Json =
            test::read_body_json(get!(app, "/_management/api/tags", Some(ADMIN))).await;
        assert_eq!(body["editable"], false);

        let resp = test::call_service(
            &app,
            test::TestRequest::post()
                .uri("/_management/api/tags/vocabulary")
                .insert_header(("Authorization", ADMIN))
                .set_json(serde_json::json!({"name": "x", "colour": "#ffffff"}))
                .to_request(),
        )
        .await;
        assert_eq!(resp.status(), StatusCode::CONFLICT);
        let body: Json = test::read_body_json(resp).await;
        assert!(
            body["message"].as_str().unwrap().contains("tags_file"),
            "the refusal should name the setting: {body}"
        );
    }

    /// The vocabulary is server-wide, so a key that cannot read everywhere
    /// would learn from the counts which tags exist in namespaces it has no
    /// access to.
    #[actix_web::test]
    async fn a_scoped_key_sees_no_usage_counts() {
        let dir = TempDir::new("tags-scoped");
        let st = tagged_state(&dir.0);
        st.acl
            .write()
            .unwrap()
            .set("scoped", parse_grants("admin, rw:other").unwrap());
        let app = app!(st);

        let body: Json =
            test::read_body_json(get!(app, "/_management/api/tags", Some("scoped"))).await;
        assert_eq!(
            row(&body, "tlp:green")["used"],
            Json::Null,
            "a scoped key was told how many values carry a tag"
        );
        // The vocabulary itself is not a secret.
        assert_eq!(row(&body, "tlp:green")["defined"], true);
    }

    #[actix_web::test]
    async fn the_tags_table_needs_a_key() {
        let st = state();
        let app = app!(st);
        assert_eq!(
            get!(app, "/_management/api/tags", NO_KEY).status(),
            StatusCode::UNAUTHORIZED
        );
    }

    // -- key management ----------------------------------------------------

    /// State with a real acl_file, so saves actually hit the disk.
    fn writable_state(dir: &std::path::Path) -> State {
        let mut inner = SharedState::new(true);
        inner.acl.get_mut().unwrap().grant_full(ADMIN);
        inner.acl_file = Some(dir.join("acl.toml"));
        web::Data::new(inner)
    }

    struct TempDir(std::path::PathBuf);
    impl TempDir {
        fn new(tag: &str) -> Self {
            let path = std::env::temp_dir().join(format!("sightingdb-keys-{tag}"));
            let _ = std::fs::remove_dir_all(&path);
            std::fs::create_dir_all(&path).unwrap();
            TempDir(path)
        }
    }
    impl Drop for TempDir {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    macro_rules! post_key {
        ($app:expr, $body:expr, $key:expr) => {
            test::call_service(
                &$app,
                test::TestRequest::post()
                    .uri("/_management/api/keys")
                    .insert_header(("Authorization", $key))
                    .set_json($body)
                    .to_request(),
            )
            .await
        };
    }

    #[actix_web::test]
    async fn a_saved_key_lands_on_disk_and_takes_effect_at_once() {
        let dir = TempDir::new("save");
        let st = writable_state(&dir.0);
        let app = app!(st);

        let resp = post_key!(
            app,
            json!({"key": "analyst", "admin": false, "read": ["feeds"], "write": []}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::OK);

        // In the running ACL, with no restart.
        assert!(st.acl().can_read("analyst", "feeds/misp"));
        assert!(!st.acl().can_write("analyst", "feeds/misp"));
        assert!(!st.acl().can_read("analyst", "secrets"));

        // And on disk, in a form that reads back.
        let written = std::fs::read_to_string(dir.0.join("acl.toml")).unwrap();
        assert!(written.contains("\"analyst\" = \"r:feeds\""), "{written}");
        assert!(written.contains("\"changeme\""), "{written}");
    }

    #[actix_web::test]
    async fn read_and_write_on_one_prefix_collapse_to_rw() {
        let dir = TempDir::new("rw");
        let st = writable_state(&dir.0);
        let app = app!(st);

        post_key!(
            app,
            json!({"key": "feed", "admin": false, "read": ["feeds"], "write": ["feeds"]}),
            ADMIN
        );

        let written = std::fs::read_to_string(dir.0.join("acl.toml")).unwrap();
        assert!(written.contains("\"feed\" = \"rw:feeds\""), "{written}");
    }

    #[actix_web::test]
    async fn a_key_can_be_revoked() {
        let dir = TempDir::new("revoke");
        let st = writable_state(&dir.0);
        let app = app!(st);
        post_key!(
            app,
            json!({"key": "temp", "admin": false, "read": [""], "write": []}),
            ADMIN
        );
        assert!(st.acl().contains("temp"));

        let resp = test::call_service(
            &app,
            test::TestRequest::delete()
                .uri("/_management/api/keys/temp")
                .insert_header(("Authorization", ADMIN))
                .to_request(),
        )
        .await;

        assert_eq!(resp.status(), StatusCode::OK);
        assert!(!st.acl().contains("temp"));
        assert!(
            !std::fs::read_to_string(dir.0.join("acl.toml"))
                .unwrap()
                .contains("temp")
        );
    }

    /// Locking every admin out would leave no way back in short of editing the
    /// file by hand, so both routes to it are refused.
    #[actix_web::test]
    async fn the_last_admin_cannot_be_removed() {
        let dir = TempDir::new("lastadmin");
        let st = writable_state(&dir.0);
        let app = app!(st);

        let demote = post_key!(
            app,
            json!({"key": ADMIN, "admin": false, "read": [""], "write": [""]}),
            ADMIN
        );
        assert_eq!(demote.status(), StatusCode::CONFLICT);

        let revoke = test::call_service(
            &app,
            test::TestRequest::delete()
                .uri(&format!("/_management/api/keys/{ADMIN}"))
                .insert_header(("Authorization", ADMIN))
                .to_request(),
        )
        .await;
        assert_eq!(revoke.status(), StatusCode::CONFLICT);

        // Still an admin, on disk and in memory.
        assert!(st.acl().is_admin(ADMIN));
    }

    #[actix_web::test]
    async fn demoting_an_admin_is_fine_once_another_exists() {
        let dir = TempDir::new("secondadmin");
        let st = writable_state(&dir.0);
        let app = app!(st);

        post_key!(
            app,
            json!({"key": "other", "admin": true, "read": [""], "write": [""]}),
            ADMIN
        );
        let resp = post_key!(
            app,
            json!({"key": ADMIN, "admin": false, "read": [""], "write": [""]}),
            ADMIN
        );

        assert_eq!(resp.status(), StatusCode::OK);
        assert!(!st.acl().is_admin(ADMIN));
        assert!(st.acl().is_admin("other"));
    }

    /// A key carrying `"` or a newline could rewrite the file as something
    /// else, so it never reaches the disk.
    #[actix_web::test]
    async fn keys_that_would_corrupt_the_file_are_refused() {
        let dir = TempDir::new("badkey");
        let st = writable_state(&dir.0);
        let app = app!(st);

        for bad in ["", "has space", "has\"quote", "has\nnewline", "has=equals"] {
            let resp = post_key!(
                app,
                json!({"key": bad, "admin": false, "read": [""], "write": []}),
                ADMIN
            );
            assert_eq!(resp.status(), StatusCode::BAD_REQUEST, "{bad:?}");
        }
    }

    #[actix_web::test]
    async fn granting_an_internal_namespace_is_refused() {
        let dir = TempDir::new("internal");
        let st = writable_state(&dir.0);
        let app = app!(st);

        let resp = post_key!(
            app,
            json!({"key": "sneaky", "admin": false, "read": ["_config/acl"], "write": []}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
        assert!(!st.acl().contains("sneaky"));
    }

    #[actix_web::test]
    async fn a_key_with_no_grants_is_refused() {
        let dir = TempDir::new("nogrants");
        let st = writable_state(&dir.0);
        let app = app!(st);

        let resp = post_key!(
            app,
            json!({"key": "useless", "admin": false, "read": [], "write": []}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    }

    /// Without an acl_file there is nowhere to write, and rewriting the daemon
    /// configuration in place is not something this does.
    #[actix_web::test]
    async fn editing_without_an_acl_file_says_so() {
        let st = state(); // no acl_file
        let app = app!(st);

        let resp = post_key!(
            app,
            json!({"key": "x", "admin": false, "read": [""], "write": []}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::CONFLICT);
        let body: Json = test::read_body_json(resp).await;
        assert!(
            body["message"].as_str().unwrap().contains("acl_file"),
            "{body}"
        );
    }

    #[actix_web::test]
    async fn keys_are_listed_and_generated() {
        let dir = TempDir::new("list");
        let st = writable_state(&dir.0);
        let app = app!(st);

        let listed: Json =
            test::read_body_json(get!(app, "/_management/api/keys", Some(ADMIN))).await;
        let entries = listed.as_array().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0]["key"], ADMIN);
        assert_eq!(entries[0]["admin"], true);

        let generated: Json =
            test::read_body_json(get!(app, "/_management/api/keys/generate", Some(ADMIN))).await;
        let suggestion = generated["key"].as_str().unwrap();
        assert_eq!(suggestion.len(), 40);
        assert!(crate::acl::validate_key(suggestion).is_ok());
    }

    #[actix_web::test]
    async fn key_management_needs_an_admin_key() {
        let dir = TempDir::new("keyauth");
        let st = writable_state(&dir.0);
        let app = app!(st);

        assert_eq!(
            get!(app, "/_management/api/keys", NO_KEY).status(),
            StatusCode::UNAUTHORIZED
        );
        let resp = post_key!(
            app,
            json!({"key": "x", "admin": true, "read": [""], "write": [""]}),
            "not-a-key"
        );
        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
    }

    // -- tiers -------------------------------------------------------------

    fn tiered_state(dir: &std::path::Path) -> State {
        let mut inner = SharedState::new(true);
        inner.acl.get_mut().unwrap().grant_full(ADMIN);
        inner.tiers_file = Some(dir.join("tiers.toml"));
        inner.db.write(
            "myorg/one",
            "v",
            Utc::now(),
            WriteOpts {
                consensus: true,
                ttl: None,
            },
        );
        web::Data::new(inner)
    }

    macro_rules! post_tier {
        ($app:expr, $body:expr, $key:expr) => {
            test::call_service(
                &$app,
                test::TestRequest::post()
                    .uri("/_management/api/tier")
                    .insert_header(("Authorization", $key))
                    .set_json($body)
                    .to_request(),
            )
            .await
        };
    }

    #[actix_web::test]
    async fn a_namespace_listing_carries_its_tier_and_residency() {
        let dir = TempDir::new("tierlist");
        let st = tiered_state(&dir.0);
        let app = app!(st);

        let body: Json =
            test::read_body_json(get!(app, "/_management/api/namespaces", Some(ADMIN))).await;
        let item = &body["items"][0];

        assert_eq!(item["namespace"], "myorg/one");
        // The tier belongs to the top-level namespace, which the row names so
        // the interface can say what a change will affect.
        assert_eq!(item["shard"], "myorg");
        assert_eq!(item["tier"], "hot");
        assert_eq!(item["resident"], true);
    }

    #[actix_web::test]
    async fn a_tier_change_takes_effect_and_is_written_out() {
        let dir = TempDir::new("tierset");
        let st = tiered_state(&dir.0);
        let app = app!(st);

        let resp = post_tier!(app, json!({"shard": "myorg", "tier": "cold"}), ADMIN);
        assert_eq!(resp.status(), StatusCode::OK);
        let body: Json =
            test::read_body_json(get!(app, "/_management/api/namespaces", Some(ADMIN))).await;
        assert_eq!(body["items"][0]["tier"], "cold");

        let written = std::fs::read_to_string(dir.0.join("tiers.toml")).unwrap();
        assert!(written.contains("\"myorg\" = \"cold\""), "{written}");
    }

    #[actix_web::test]
    async fn the_idle_window_can_be_changed_and_cleared() {
        let dir = TempDir::new("tieridle");
        let st = tiered_state(&dir.0);
        let app = app!(st);

        // The thing that could only be set in the configuration file before.
        let resp = post_tier!(
            app,
            json!({"namespace": "myorg/one", "tier": "warm", "warm_idle": 86400}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::OK);
        let body: Json = test::read_body_json(resp).await;
        // Named a namespace, changed its shard, and said so.
        assert_eq!(body["shard"], "myorg");
        assert_eq!(body["tier"], "warm");
        assert_eq!(body["warm_idle"], 86400);
        assert!(
            body["effect"]
                .as_str()
                .unwrap()
                .contains("everything under it"),
            "{body}"
        );

        let written = std::fs::read_to_string(dir.0.join("tiers.toml")).unwrap();
        assert!(
            written.contains(r#""myorg" = { tier = "warm", warm_idle = 86400 }"#),
            "{written}"
        );

        // A form hands over text, and "default" means stop saying anything.
        let resp = post_tier!(
            app,
            json!({"namespace": "myorg", "tier": "warm", "warm_idle": "default"}),
            ADMIN
        );
        let body: Json = test::read_body_json(resp).await;
        assert_eq!(body["own_warm_idle"], false);
        assert_eq!(body["warm_idle"], 3600, "back to the configured default");

        // Clearing both leaves nothing behind.
        let resp = post_tier!(app, json!({"namespace": "myorg", "tier": "default"}), ADMIN);
        assert_eq!(resp.status(), StatusCode::OK);
        let body: Json = test::read_body_json(resp).await;
        assert_eq!(body["own_tier"], false);
        let written = std::fs::read_to_string(dir.0.join("tiers.toml")).unwrap();
        assert!(!written.contains("myorg"), "{written}");
    }

    #[actix_web::test]
    async fn a_row_reports_the_window_its_shard_is_on() {
        let dir = TempDir::new("tierrow");
        let st = tiered_state(&dir.0);
        let app = app!(st);
        post_tier!(
            app,
            json!({"namespace": "myorg", "tier": "warm", "warm_idle": 900}),
            ADMIN
        );

        for uri in [
            "/_management/api/namespaces",
            "/_management/api/tree?path=myorg",
        ] {
            let body: Json = test::read_body_json(get!(app, uri, Some(ADMIN))).await;
            let item = &body["items"][0];
            assert_eq!(item["tier"], "warm", "{uri}");
            assert_eq!(item["warm_idle"], 900, "{uri}");
            assert_eq!(item["own_tier"], true, "{uri}");
            assert_eq!(item["own_warm_idle"], true, "{uri}");
        }
    }

    #[actix_web::test]
    async fn an_unreadable_idle_window_is_refused() {
        let dir = TempDir::new("tierjunk");
        let st = tiered_state(&dir.0);
        let app = app!(st);

        for body in [
            json!({"namespace": "myorg", "warm_idle": "soon"}),
            json!({"namespace": "myorg", "warm_idle": -5}),
            json!({"namespace": "", "tier": "warm"}),
        ] {
            assert_eq!(
                post_tier!(app, body.clone(), ADMIN).status(),
                StatusCode::BAD_REQUEST,
                "{body}"
            );
        }
        assert!(!dir.0.join("tiers.toml").exists());
    }

    #[actix_web::test]
    async fn an_unknown_tier_is_refused() {
        let dir = TempDir::new("tierbad");
        let st = tiered_state(&dir.0);
        let app = app!(st);

        let resp = post_tier!(app, json!({"shard": "myorg", "tier": "tepid"}), ADMIN);
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
        assert!(!dir.0.join("tiers.toml").exists());
    }

    /// Consensus and API keys are consulted constantly, so the internal shard
    /// is not something the interface may demote.
    #[actix_web::test]
    async fn internal_namespaces_cannot_be_retiered() {
        let dir = TempDir::new("tierinternal");
        let st = tiered_state(&dir.0);
        let app = app!(st);

        for shard in ["_all", "_config", ""] {
            let resp = post_tier!(app, json!({"shard": shard, "tier": "cold"}), ADMIN);
            assert_eq!(resp.status(), StatusCode::BAD_REQUEST, "{shard:?}");
        }
    }

    #[actix_web::test]
    async fn changing_a_tier_needs_an_admin_key() {
        let dir = TempDir::new("tierauth");
        let st = tiered_state(&dir.0);
        let app = app!(st);

        let resp = post_tier!(app, json!({"shard": "myorg", "tier": "cold"}), "plain");
        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
    }

    /// Without a file there is nowhere to record the change, and a tier that
    /// silently reverted on restart would be worse than refusing.
    #[actix_web::test]
    async fn changing_a_tier_without_a_file_says_so() {
        let st = state();
        let app = app!(st);

        let resp = post_tier!(app, json!({"shard": "feeds", "tier": "cold"}), ADMIN);
        assert_eq!(resp.status(), StatusCode::CONFLICT);
        let body: Json = test::read_body_json(resp).await;
        assert!(
            body["message"].as_str().unwrap().contains("tiers_file"),
            "{body}"
        );
    }

    // -- browsing, creating and adding ------------------------------------

    macro_rules! post_json {
        ($app:expr, $uri:expr, $body:expr, $key:expr) => {
            test::call_service(
                &$app,
                test::TestRequest::post()
                    .uri($uri)
                    .insert_header(("Authorization", $key))
                    .set_json($body)
                    .to_request(),
            )
            .await
        };
    }

    #[actix_web::test]
    async fn the_tree_walks_one_level_at_a_time() {
        let st = state();
        let app = app!(st);

        // At the root, `feeds` is a folder: it holds `feeds/ips` but nothing
        // of its own.
        let body: Json =
            test::read_body_json(get!(app, "/_management/api/tree", Some(ADMIN))).await;
        assert_eq!(body["total"], 1);
        assert_eq!(body["items"][0]["name"], "feeds");
        assert_eq!(body["items"][0]["path"], "feeds");
        assert_eq!(body["items"][0]["is_namespace"], false);
        assert_eq!(body["items"][0]["descendants"], 1);

        // A level down, `ips` is the namespace holding the values.
        let body: Json =
            test::read_body_json(get!(app, "/_management/api/tree?path=feeds", Some(ADMIN))).await;
        assert_eq!(body["items"][0]["name"], "ips");
        assert_eq!(body["items"][0]["path"], "feeds/ips");
        assert_eq!(body["items"][0]["is_namespace"], true);
        assert_eq!(body["items"][0]["descendants"], 0);

        // And nothing below that.
        let body: Json = test::read_body_json(get!(
            app,
            "/_management/api/tree?path=feeds/ips",
            Some(ADMIN)
        ))
        .await;
        assert_eq!(body["total"], 0);
    }

    /// A key scoped to one subtree must not learn the names of the others,
    /// which for the tree means not even seeing the folder above them.
    #[actix_web::test]
    async fn the_tree_only_shows_what_the_key_may_read() {
        let mut inner = SharedState::new(false);
        inner.acl.get_mut().unwrap().grant_full(ADMIN);
        inner
            .acl
            .get_mut()
            .unwrap()
            .set("scoped", parse_grants("admin, r:feeds").unwrap());
        for namespace in ["feeds/ips", "private/ips"] {
            inner
                .db
                .write(namespace, "1.2.3.4", Utc::now(), WriteOpts::default());
        }
        let st = web::Data::new(inner);
        let app = app!(st);

        let body: Json =
            test::read_body_json(get!(app, "/_management/api/tree", Some("scoped"))).await;
        assert_eq!(body["total"], 1);
        assert_eq!(body["items"][0]["name"], "feeds");
    }

    #[actix_web::test]
    async fn a_created_namespace_is_empty_and_browsable() {
        let st = state();
        let app = app!(st);

        let resp = post_json!(
            app,
            "/_management/api/namespaces",
            json!({"namespace": "feeds/domains"}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::OK);

        // It exists as a folder under `feeds`...
        let body: Json =
            test::read_body_json(get!(app, "/_management/api/tree?path=feeds", Some(ADMIN))).await;
        let names: Vec<&str> = body["items"]
            .as_array()
            .unwrap()
            .iter()
            .map(|item| item["name"].as_str().unwrap())
            .collect();
        assert_eq!(names, ["domains", "ips"]);

        // ...and as a namespace holding nothing yet, rather than a 404.
        let body: Json = test::read_body_json(get!(
            app,
            "/_management/api/values?namespace=feeds/domains",
            Some(ADMIN)
        ))
        .await;
        assert_eq!(body["total"], 0);
    }

    #[actix_web::test]
    async fn creating_a_namespace_twice_is_refused() {
        let st = state();
        let app = app!(st);

        let body = json!({"namespace": "feeds/ips"});
        let resp = post_json!(app, "/_management/api/namespaces", body, ADMIN);
        assert_eq!(resp.status(), StatusCode::CONFLICT);
    }

    /// Slashes are how the interface nests folders, so a name doubling or
    /// trailing them must not create a second namespace beside the first.
    #[actix_web::test]
    async fn a_namespace_name_is_tidied_before_it_is_stored() {
        let st = state();
        let app = app!(st);

        let resp = post_json!(
            app,
            "/_management/api/namespaces",
            json!({"namespace": "/feeds//domains/ "}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::OK);
        let body: Json = test::read_body_json(resp).await;
        assert_eq!(body["namespace"], "feeds/domains");
        assert!(st.db.namespace_exists("feeds/domains"));
    }

    #[actix_web::test]
    async fn internal_and_empty_namespaces_are_refused() {
        let st = state();
        let app = app!(st);

        for name in ["_config/acl/apikeys/mine", "  ", "/", "with space"] {
            let resp = post_json!(
                app,
                "/_management/api/namespaces",
                json!({ "namespace": name }),
                ADMIN
            );
            assert_eq!(resp.status(), StatusCode::BAD_REQUEST, "{name}");
        }
    }

    #[actix_web::test]
    async fn values_can_be_added_one_at_a_time_or_in_bulk() {
        let st = state();
        let app = app!(st);

        let resp = post_json!(
            app,
            "/_management/api/values",
            json!({"namespace": "feeds/domains", "values": ["example.com"]}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::OK);
        let body: Json = test::read_body_json(resp).await;
        assert_eq!(body["written"], 1);
        // Writing is what creates a namespace, here as everywhere else.
        assert!(st.db.namespace_exists("feeds/domains"));

        // A pasted list, blank lines and all, lands as the values in it.
        let resp = post_json!(
            app,
            "/_management/api/values",
            json!({"namespace": "feeds/domains", "values": ["a.example", "", "  ", "b.example"]}),
            ADMIN
        );
        let body: Json = test::read_body_json(resp).await;
        assert_eq!(body["written"], 2);
        assert!(body["errors"].is_null(), "{body}");

        let page = st.db.value_page("feeds/domains", "", 0, 10, false).unwrap();
        assert_eq!(page.total, 3);
        // Counted towards consensus, exactly as a `/w/` write would be.
        assert_eq!(st.db.count(crate::db::ALL_NAMESPACE, "example.com"), 1);
    }

    #[actix_web::test]
    async fn added_values_take_a_ttl_and_a_time() {
        let st = state();
        let app = app!(st);

        // An hour ago with an hour to live: recorded then, and still here now.
        let seen = Utc::now().timestamp() - 60;
        let resp = post_json!(
            app,
            "/_management/api/values",
            json!({
                "namespace": "feeds/domains",
                "values": ["example.com"],
                "ttl": 3600,
                "timestamp": seen,
            }),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::OK);

        let view = st
            .db
            .view("feeds/domains", "example.com", 0, false)
            .unwrap();
        assert_eq!(view.first_seen, seen);
        assert_eq!(view.ttl, 3600);

        // A time the calendar cannot hold is a client error, not a panic.
        let resp = post_json!(
            app,
            "/_management/api/values",
            json!({"namespace": "feeds/domains", "values": ["x"], "timestamp": i64::MAX}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    }

    #[actix_web::test]
    async fn a_request_with_nothing_to_add_is_a_bad_request() {
        let st = state();
        let app = app!(st);

        let resp = post_json!(
            app,
            "/_management/api/values",
            json!({"namespace": "feeds/ips", "values": ["", " "]}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    }

    /// Reaching the interface is not the same as being allowed to change the
    /// data in it: an `admin, r:feeds` key browses but does not write.
    #[actix_web::test]
    async fn writing_needs_a_write_grant_over_the_namespace() {
        let mut inner = SharedState::new(false);
        inner.acl.get_mut().unwrap().grant_full(ADMIN);
        inner
            .acl
            .get_mut()
            .unwrap()
            .set("reader", parse_grants("admin, r").unwrap());
        let st = web::Data::new(inner);
        let app = app!(st);

        for (uri, body) in [
            (
                "/_management/api/namespaces",
                json!({"namespace": "feeds/domains"}),
            ),
            (
                "/_management/api/values",
                json!({"namespace": "feeds/domains", "values": ["example.com"]}),
            ),
        ] {
            assert_eq!(
                post_json!(app, uri, body.clone(), "reader").status(),
                StatusCode::FORBIDDEN,
                "{uri}"
            );
            // A key that is not an admin key at all does not get further.
            assert_eq!(
                post_json!(app, uri, body, "nobody").status(),
                StatusCode::FORBIDDEN,
                "{uri}"
            );
        }
        assert!(!st.db.namespace_exists("feeds/domains"));
    }

    // -- the relationship graph -------------------------------------------

    #[actix_web::test]
    async fn a_value_reports_where_else_it_has_been_seen() {
        let mut inner = SharedState::new(false);
        inner.acl.get_mut().unwrap().grant_full(ADMIN);
        inner
            .acl
            .get_mut()
            .unwrap()
            .set("scoped", parse_grants("admin, r:feeds").unwrap());
        for namespace in ["feeds/misp/ips", "feeds/otx/ips", "private/ips"] {
            inner.db.write(
                namespace,
                "1.2.3.4",
                Utc::now(),
                WriteOpts {
                    consensus: true,
                    ttl: None,
                },
            );
        }
        let st = web::Data::new(inner);
        let app = app!(st);

        let body: Json = test::read_body_json(get!(
            app,
            "/_management/api/sightings?value=1.2.3.4",
            Some(ADMIN)
        ))
        .await;
        let namespaces: Vec<&str> = body["items"]
            .as_array()
            .unwrap()
            .iter()
            .map(|item| item["namespace"].as_str().unwrap())
            .collect();
        assert_eq!(
            namespaces,
            ["feeds/misp/ips", "feeds/otx/ips", "private/ips"]
        );
        assert_eq!(body["items"][0]["shard"], "feeds");
        assert_eq!(body["consensus"], 3);
        assert_eq!(body["truncated"], false);

        // A scoped key sees its own subtree; consensus still says how many
        // namespaces hold the value, so it can tell the graph is not all of it.
        let body: Json = test::read_body_json(get!(
            app,
            "/_management/api/sightings?value=1.2.3.4",
            Some("scoped")
        ))
        .await;
        assert_eq!(body["items"].as_array().unwrap().len(), 2);
        assert_eq!(body["consensus"], 3);
    }

    #[actix_web::test]
    async fn the_graph_needs_an_admin_key_and_a_value() {
        let st = state();
        let app = app!(st);

        assert_eq!(
            get!(app, "/_management/api/sightings?value=1.2.3.4", NO_KEY).status(),
            StatusCode::UNAUTHORIZED
        );
        assert_eq!(
            get!(
                app,
                "/_management/api/sightings?value=1.2.3.4",
                Some("plain")
            )
            .status(),
            StatusCode::FORBIDDEN
        );
        assert_eq!(
            get!(app, "/_management/api/sightings?value=", Some(ADMIN)).status(),
            StatusCode::BAD_REQUEST
        );
    }

    // -- tags and the STIX export -------------------------------------------

    #[actix_web::test]
    async fn values_can_be_added_with_tags_and_retagged_afterwards() {
        let st = state();
        let app = app!(st);

        let resp = post_json!(
            app,
            "/_management/api/values",
            json!({
                "namespace": "feeds/ips",
                "values": ["8.8.8.8"],
                "tags": "stix-type:ipv4-addr, tlp:green",
            }),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(
            st.db.view("feeds/ips", "8.8.8.8", 0, false).unwrap().tags,
            "stix-type:ipv4-addr,tlp:green"
        );

        // Replacing is how a wrong tag comes off, and it is not a sighting:
        // the count stays where it was.
        let before = st.db.view("feeds/ips", "8.8.8.8", 0, false).unwrap().count;
        let resp = post_json!(
            app,
            "/_management/api/tags",
            json!({"namespace": "feeds/ips", "value": "8.8.8.8", "tags": "stix-type:ipv4-addr"}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::OK);
        let view = st.db.view("feeds/ips", "8.8.8.8", 0, false).unwrap();
        assert_eq!(view.tags, "stix-type:ipv4-addr");
        assert_eq!(view.count, before);
    }

    #[actix_web::test]
    async fn retagging_a_value_that_is_not_there_is_a_not_found() {
        let st = state();
        let app = app!(st);

        let resp = post_json!(
            app,
            "/_management/api/tags",
            json!({"namespace": "feeds/ips", "value": "203.0.113.9", "tags": "tlp:red"}),
            ADMIN
        );
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[actix_web::test]
    async fn tagging_needs_a_write_grant() {
        let mut inner = SharedState::new(false);
        inner.acl.get_mut().unwrap().grant_full(ADMIN);
        inner
            .acl
            .get_mut()
            .unwrap()
            .set("reader", parse_grants("admin, r").unwrap());
        inner
            .db
            .write("feeds/ips", "1.2.3.4", Utc::now(), WriteOpts::default());
        let st = web::Data::new(inner);
        let app = app!(st);

        let resp = post_json!(
            app,
            "/_management/api/tags",
            json!({"namespace": "feeds/ips", "value": "1.2.3.4", "tags": "tlp:red"}),
            "reader"
        );
        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
        assert!(
            st.db
                .view("feeds/ips", "1.2.3.4", 0, false)
                .unwrap()
                .tags
                .is_empty()
        );
    }

    #[actix_web::test]
    async fn the_logo_is_served_from_the_binary() {
        let st = state();
        let app = app!(st);

        // No key: the page shows it before anyone has signed in.
        let resp = get!(app, "/_management/logo.png", NO_KEY);
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(resp.headers().get("content-type").unwrap(), "image/png");

        let body = test::read_body(resp).await;
        assert!(body.starts_with(b"\x89PNG"), "not a PNG");
    }

    #[actix_web::test]
    async fn echarts_is_served_from_the_binary() {
        let st = state();
        let app = app!(st);

        let resp = get!(app, "/_management/echarts.min.js", NO_KEY);
        assert_eq!(resp.status(), StatusCode::OK);
        let body = test::read_body(resp).await;
        assert!(body.len() > 100_000, "expected the real library");
    }
}