@johngaynor/ocnus 2.0.1-next.7 → 2.0.1-next.73

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +518 -98
  2. package/cli/lib/agents.sh +32 -16
  3. package/cli/lib/asks.sh +762 -0
  4. package/cli/lib/banner.sh +181 -0
  5. package/cli/lib/brief.sh +375 -0
  6. package/cli/lib/chain.sh +363 -0
  7. package/cli/lib/close.sh +5 -3
  8. package/cli/lib/cost.sh +500 -0
  9. package/cli/lib/finish.sh +30 -4
  10. package/cli/lib/github.sh +106 -26
  11. package/cli/lib/guard.sh +39 -10
  12. package/cli/lib/handoff.sh +196 -31
  13. package/cli/lib/host.sh +340 -384
  14. package/cli/lib/linear.sh +552 -56
  15. package/cli/lib/links.sh +6 -3
  16. package/cli/lib/merges.sh +126 -27
  17. package/cli/lib/sessions.sh +132 -0
  18. package/cli/lib/settings.sh +31 -15
  19. package/cli/lib/task.sh +169 -54
  20. package/cli/lib/tests.sh +302 -7
  21. package/cli/lib/usage.sh +204 -0
  22. package/cli/ocnus +274 -76
  23. package/herdr/README.md +930 -232
  24. package/herdr/layouts/default.json +30 -11
  25. package/herdr/layouts/showcase.json +291 -0
  26. package/herdr/layouts/vibe.json +162 -0
  27. package/herdr/layouts/vibe.svg +41 -0
  28. package/herdr/mods/agent-settings/main.js +19 -7
  29. package/herdr/mods/memory/main.js +128 -0
  30. package/herdr/mods/memory/mod.json +1 -0
  31. package/herdr/mods/memory/reading.js +88 -0
  32. package/herdr/mods/names/main.js +22 -4
  33. package/herdr/mods/step-counts/main.js +281 -0
  34. package/herdr/mods/step-counts/mod.json +1 -0
  35. package/herdr/mods/usage/main.js +149 -0
  36. package/herdr/mods/usage/mod.json +1 -0
  37. package/package.json +1 -1
  38. package/prompts/task-new.md +1 -1
  39. package/skills/brief/SKILL.md +8 -0
  40. package/skills/sdlc-breakdown/SKILL.md +66 -18
  41. package/skills/sdlc-build/SKILL.md +6 -2
  42. package/skills/sdlc-pr/SKILL.md +2 -2
  43. package/skills/sdlc-review/SKILL.md +5 -5
package/cli/lib/host.sh CHANGED
@@ -19,6 +19,8 @@
19
19
  # under a terminal labelled with a space, apart from everything else on the
20
20
  # host, which is one number each, and the recorder's own CPU apart from both.
21
21
  # A process outside those terminals is never written down, not even its pid.
22
+ # `host report` sums those numbers up for a period, as totals, averages and
23
+ # peaks, and how long each state lasted, and judges none of them.
22
24
  #
23
25
  # With each reading go the shares of those terminals, one each: the CPU and
24
26
  # memory of every process under it, the commands its agent started included,
@@ -33,9 +35,7 @@
33
35
  # turn, as herdr-core's status for its terminal says, and waiting while it is
34
36
  # open but its turn has ended, it waits on the human in its tab, it hit an
35
37
  # error, or it reports no status. A tab that holds its shell alone is not
36
- # open, so it is neither. `host report` shows, for each number of agents
37
- # working, how much of the time the host was at its limit, and from which
38
- # number the host was the limit.
38
+ # open, so it is neither.
39
39
  #
40
40
  # Beside the readings go the test runs: each run of a repo's tests that an
41
41
  # agent starts through `ocnus tests run` (tests.sh), with its card and
@@ -43,16 +43,12 @@
43
43
  # memory it held at once. A test run is named by its card and repo alone.
44
44
  # Its command is kept only as a fingerprint, a number the same for the same
45
45
  # command and from which its text cannot be read back, and nothing else of
46
- # its command line is kept. `host report` ranks the test runs too, and sets
47
- # runs of the same command in the same repo against each other to say how
48
- # much slower they were with more agents working.
46
+ # its command line is kept. `host report` ranks the test runs too.
49
47
  #
50
48
  # With each reading go the shares of the test runs going then, too, each
51
49
  # worked out as a terminal's is, from the processes under the run's job,
52
50
  # which are in their terminal's share as well. A run that ended since the
53
- # reading before gets the rest of its CPU in the next one. `host report`
54
- # names the cards' steps and the test runs going in the longest stretches
55
- # when the CPU or memory was at its limit.
51
+ # reading before gets the rest of its CPU in the next one.
56
52
  #
57
53
  # While recording is on, `task new` and `task build` start claude with hooks
58
54
  # that mark its agent's time (host_hooks): cli/host-mark appends a word and
@@ -64,16 +60,15 @@
64
60
  # those marks (host_splits): on the host, while its tools run, and waiting
65
61
  # on the model. The rest of the time its agent was open is neither, such as
66
62
  # waiting on the human. `host report` says where the steps' time went by
67
- # them, and ends with its verdict on the host, which weighs each reading by
68
- # the agents' time on the host in it: what to upgrade first, if anything.
63
+ # them.
69
64
  #
70
65
  # From the same marks, each wait on the model is kept too, as its start and
71
- # end alone, by the reading it ended in. A wait is slow at over twice the
72
- # median of every wait kept. When slow waits came while the CPU, memory and
73
- # disk were all below their limit, `host report` says the network may be the
74
- # cause, and when they were at least a tenth of the period's waits, and no
75
- # resource was the one to upgrade, its verdict is that the network was the
76
- # limit.
66
+ # end alone, by the reading it ended in.
67
+ #
68
+ # `host report --card <space> <card> --json` gives a program the readings
69
+ # taken while each of a card's steps ran, as one line of JSON (host_card):
70
+ # the whole host's CPU and memory, with each step's start and end from the
71
+ # hand-off store, which it only reads.
77
72
  #
78
73
  # A share's card, phase and step, a split's, a wait's, and a test run's card
79
74
  # and repo, are the only text the readings hold, and only the values
@@ -215,7 +210,7 @@ host_table() {
215
210
  [ -z "$sql" ] || store "$HOST_DB" "begin; ${sql}commit;"
216
211
  }
217
212
 
218
- HOST_USAGE='usage: ocnus host [on|off|report [<period>]] (see: ocnus help)'
213
+ HOST_USAGE='usage: ocnus host [on|off|report [<period>]|report --card <space> <card> --json] (see: ocnus help)'
219
214
  HOST_PERIODS='a period is <n>d or <n>h back from now, like 7d or 12h; a day, like 2026-11-29; or two days and every day between, like 2026-11-01..2026-11-29'
220
215
 
221
216
  # --- on and off -------------------------------------------------------------
@@ -1216,13 +1211,18 @@ host_day() {
1216
1211
  [ "$ok" = 1 ] && [ -n "$DAY_START" ] && [ -n "$DAY_END" ]
1217
1212
  }
1218
1213
 
1219
- # How the CPU, memory and disk were, over a period: how much of the time it
1220
- # was recorded, and how much of that each was at its limit. Time with no
1221
- # reading is shown as missing, never as idle. It ends with whether the
1222
- # network may be the cause of slow waits on the model, and the verdict on
1223
- # the host: what to upgrade first, if anything (host_verdict_sql).
1214
+ # How the host worked over a period, in its numbers alone: how much of the
1215
+ # period was recorded, in all and on each day, with time with no reading
1216
+ # shown as missing, never as idle (host_report_sql). Then the host's numbers
1217
+ # summed up (host_numbers_sql), the steps, phases and test runs that used the
1218
+ # most (host_ranks_sql), and it ends with where the steps' time went
1219
+ # (host_split_sql).
1224
1220
  host_report() {
1225
- local period=${1:-7d} now start end n first last row works runs working shared splits power slower waits
1221
+ local period=${1:-7d} now start end n first last row have ranks=0 works=0 splits=0
1222
+ # A card's readings, as JSON, are a form of their own.
1223
+ for n in "$@"; do
1224
+ case $n in --card | --json) host_card "$@"; return ;; esac
1225
+ done
1226
1226
  [ "$#" -le 1 ] || die "$HOST_USAGE"
1227
1227
  now=$(date +%s)
1228
1228
  case $period in
@@ -1262,59 +1262,40 @@ host_report() {
1262
1262
  say "Recording is on, and takes a reading every $HOST_EVERY seconds while an ocnus agent is open."
1263
1263
  return 0
1264
1264
  fi
1265
- # A store the recorder has not opened since the shares, the cores, the
1266
- # test runs and their shares, the agents working, the splits, the power
1267
- # state, the fingerprints of the test runs' commands, and the waits on the
1268
- # model came has none of them: one digit each, 1 for what it has, and for
1269
- # the power state how many of its 3 columns it has. The report only reads
1270
- # the store, so it never adds them, and leaves out what is missing.
1271
- row=$(store "$HOST_DB" "select (select count(*) from pragma_table_info('readings') where name = 'cores')
1272
- || (select count(*) from sqlite_master where type = 'table' and name = 'shares')
1273
- || (select count(*) from sqlite_master where type = 'table' and name = 'test_runs')
1274
- || (select count(*) from sqlite_master where type = 'table' and name = 'run_shares')
1275
- || (select count(*) from pragma_table_info('readings') where name = 'working')
1276
- || (select count(*) from pragma_table_info('shares') where name = 'working')
1277
- || (select count(*) from sqlite_master where type = 'table' and name = 'splits')
1278
- || (select count(*) from pragma_table_info('readings') where name in ('on_battery', 'low_power', 'thermal'))
1279
- || (select count(*) from pragma_table_info('test_runs') where name = 'cmd')
1280
- || (select count(*) from sqlite_master where type = 'table' and name = 'model_waits');") || row=
1281
- works=0 runs=0 working=0 shared=0 splits=0 power=0 slower=0 waits=0
1282
- case $row in ?1????????) works=1 ;; esac
1283
- case $row in ??11??????) runs=1 ;; esac
1284
- case $row in ????1?????) working=1 ;; esac
1285
- case $row in ?1???1????) shared=1 ;; esac
1286
- case $row in ?1????1???) splits=1 ;; esac
1287
- case $row in ???????3??) power=1 ;; esac
1288
- case $row in ????1???1?) slower=1 ;; esac
1289
- case $row in ?????????1) waits=1 ;; esac
1290
- case $row in 111???????) row=1 ;; *) row=0 ;; esac
1291
- store "$HOST_DB" "$(host_report_sql "$start" "$end" "$period" "$row")" ||
1292
- die "could not read $HOST_DB"
1293
- store "$HOST_DB" "$(host_working_sql "$start" "$end" "$working" "$slower")" ||
1294
- die "could not read $HOST_DB"
1295
- store "$HOST_DB" "$(host_peaks_sql "$start" "$end" "$works" "$runs" "$shared")" ||
1296
- die "could not read $HOST_DB"
1297
- if [ "$row" = 1 ]; then
1265
+ # A store the recorder has not opened since a table or a column came has
1266
+ # none of it, and the report only reads the store, so it never adds them.
1267
+ # A number whose column the store lacks is not read. The lists of what
1268
+ # used the most need the readings' cores, the shares and the test runs,
1269
+ # and where the steps' time went needs the shares, and the splits to split
1270
+ # it.
1271
+ have=$(host_columns) || have=' '
1272
+ case $have in *' shares.'*) works=1 ;; esac
1273
+ case $have in *' splits.'*) splits=$works ;; esac
1274
+ case $have in *' readings.cores '*) case $have in *' test_runs.'*) ranks=$works ;; esac ;; esac
1275
+ store "$HOST_DB" "$(host_report_sql "$start" "$end" "$period")" || die "could not read $HOST_DB"
1276
+ store "$HOST_DB" "$(host_numbers_sql "$start" "$end" "$have")" || die "could not read $HOST_DB"
1277
+ if [ "$ranks" = 1 ]; then
1298
1278
  store "$HOST_DB" "$(host_ranks_sql "$start" "$end")" || die "could not read $HOST_DB"
1299
1279
  else
1300
1280
  say ''
1301
1281
  say 'No step, phase or test run was recorded in this period.'
1302
1282
  fi
1303
1283
  store "$HOST_DB" "$(host_split_sql "$start" "$end" "$works" "$splits")" || die "could not read $HOST_DB"
1304
- store "$HOST_DB" "$(host_verdict_sql "$start" "$end" "$splits" "$power" "$waits")" || die "could not read $HOST_DB"
1305
1284
  }
1306
1285
 
1307
- # The report's query, for the period from $1 to $2, named $3. A reading
1308
- # covers the secs before its at, so each one counts for the part of that
1309
- # which falls in the period, and in each day. With $4 as 1, the store has
1310
- # test runs and the readings their cores, and what watching the test runs
1311
- # that started in the period used counts as recording's own CPU, as a share
1312
- # of the cores the readings counted over the recorded time.
1286
+ # The tables and the columns the readings' store has, each column as its
1287
+ # table and its name, like readings.cores, between spaces, from one query.
1288
+ host_columns() {
1289
+ store "$HOST_DB" "select ' ' || coalesce(group_concat(m.name || '.' || p.name, ' '), '') || ' '
1290
+ from sqlite_master m join pragma_table_info(m.name) p where m.type = 'table';"
1291
+ }
1292
+
1293
+ # The top of the report, for the period from $1 to $2, named $3: how much of
1294
+ # it was recorded and how much is missing, then the same for each day. A
1295
+ # reading covers the secs before its at, so each one counts for the part of
1296
+ # that which falls in the period, and in each day.
1313
1297
  host_report_sql() {
1314
- local s=$1 e=$2 name=$3 watch=0
1315
- [ "${4:-0}" != 1 ] ||
1316
- watch="coalesce(100.0 * (select sum(watch_secs) from test_runs where started >= $s and started < $e)
1317
- / (select nullif(max(cores), 0) from readings where at > $s and at < $e + $HOST_GAP and at - secs < $e), 0)"
1298
+ local s=$1 e=$2 name=$3
1318
1299
  cat <<EOF
1319
1300
  with recursive
1320
1301
  days(day, ds, de) as (
@@ -1326,216 +1307,156 @@ with recursive
1326
1307
  cast(strftime('%s', date(day, '+1 day'), 'utc') as integer),
1327
1308
  min($e, cast(strftime('%s', date(day, '+2 days'), 'utc') as integer))
1328
1309
  from days where de < $e),
1329
- parts(day, len, d, cpu_busy, mem_pressure, disk_free_pct, cpu_self) as (
1330
- select days.day, days.de - days.ds,
1331
- max(0, min(at, days.de) - max(at - secs, days.ds)), cpu_busy, mem_pressure, disk_free_pct, cpu_self
1310
+ parts(day, len, d) as (
1311
+ select days.day, days.de - days.ds, max(0, min(at, days.de) - max(at - secs, days.ds))
1332
1312
  from days left join readings on at > days.ds and at < days.de + $HOST_GAP and at - secs < days.de),
1333
- byday(day, len, rec, cpu, mem, disk, self_time, self_d) as (
1334
- select day, len, cast(coalesce(sum(d), 0) as integer),
1335
- cast(coalesce(sum(case when cpu_busy >= 90 then d end), 0) as integer),
1336
- cast(coalesce(sum(case when mem_pressure >= 2 then d end), 0) as integer),
1337
- cast(coalesce(sum(case when disk_free_pct < 10 then d end), 0) as integer),
1338
- coalesce(sum(cpu_self * d), 0), coalesce(sum(case when cpu_self is not null then d end), 0)
1339
- from parts group by day),
1340
- total(len, rec, cpu, mem, disk, self) as (
1341
- select $e - $s, sum(rec), sum(cpu), sum(mem), sum(disk),
1342
- case when sum(self_d) > 0 then (sum(self_time) + $watch) / sum(self_d) end
1343
- from byday),
1344
- shown(len, rec, missing, cpu, mem, disk, self) as (
1345
- select len, rec, max(0, len - rec), cpu, mem, disk, self from total),
1313
+ byday(day, len, rec) as (
1314
+ select day, len, cast(coalesce(sum(d), 0) as integer) from parts group by day),
1315
+ total(len, rec) as (
1316
+ select $e - $s, sum(rec) from byday),
1317
+ shown(len, rec, missing) as (
1318
+ select len, rec, max(0, len - rec) from total),
1346
1319
  lines(n, k, line) as (
1347
1320
  select 1, '', 'The host from ' || strftime('%Y-%m-%d %H:%M', $s, 'unixepoch', 'localtime') || ' to '
1348
1321
  || strftime('%Y-%m-%d %H:%M', $e, 'unixepoch', 'localtime') || ' ($name)' from shown
1349
1322
  union all select 2, '', ' recorded ' || $(sql_took rec) || ' of ' || $(sql_took len) from shown
1350
1323
  union all select 3, '', ' missing ' || $(sql_took missing) || ', shown as missing, not idle: no reading was taken then' from shown
1351
- union all select 4, '', ' CPU at its limit ' || $(sql_took cpu) || ', ' || printf('%.1f%%', 100.0 * cpu / rec)
1352
- || ' of the recorded time (at least 90% busy across all cores)' from shown
1353
- union all select 5, '', ' memory at its limit ' || $(sql_took mem) || ', ' || printf('%.1f%%', 100.0 * mem / rec)
1354
- || ' of the recorded time (memory pressure at warning or critical)' from shown
1355
- union all select 6, '', ' disk at its limit ' || $(sql_took disk) || ', ' || printf('%.1f%%', 100.0 * disk / rec)
1356
- || ' of the recorded time (under 10% free on the disk holding the home folder)' from shown
1357
- union all select 7, '', ' recording ' || case when self is null then 'its own use of the CPU was not read'
1358
- else 'used ' || printf('%.2f%%', self) || ' of the CPU, all cores together, on average' end from shown
1359
- union all select 8, '', '' union all select 9, '', 'By day:'
1360
- union all select 10, day, ' ' || day || ' recorded ' || $(sql_took rec) || ', missing ' || $(sql_took "max(0, len - rec)")
1361
- || case when rec > 0 then '; at its limit: CPU ' || printf('%.1f%%', 100.0 * cpu / rec)
1362
- || ', memory ' || printf('%.1f%%', 100.0 * mem / rec) || ', disk ' || printf('%.1f%%', 100.0 * disk / rec)
1363
- else '' end
1324
+ union all select 4, '', '' union all select 5, '', 'By day:'
1325
+ union all select 6, day, ' ' || day || ' recorded ' || $(sql_took rec) || ', missing ' || $(sql_took "max(0, len - rec)")
1364
1326
  from byday)
1365
1327
  select line from lines order by n, k;
1366
1328
  EOF
1367
1329
  }
1368
1330
 
1369
- # The agents working at once, from $1 to $2: for each number of agents
1370
- # working that the readings counted, from none up, how long that many were
1371
- # working, and how much of that time the CPU or the memory was at its limit,
1372
- # as the report counts it. Each reading counts for the part of it in the
1373
- # period. A reading from before the agents working were counted has no
1374
- # count, and nor has any when $3 is not 1, as the store has no column for
1375
- # them yet: it is left out, never counted as none working, and its time is
1376
- # on a line of its own. Last, the fewest working at which the CPU or memory
1377
- # was at its limit for at least a quarter of the time is named as the number
1378
- # from which the host was the limit; or it says there is no such number, or
1379
- # that no reading counted the agents working.
1331
+ # The numbers a reading holds of the host, in the order the report gives
1332
+ # them: CPU, memory, disk, network, and power and heat.
1333
+ HOST_NUMBERS='cpu_busy cpu_system cpu_agents cpu_rest cpu_self cpu_p cpu_e
1334
+ mem_pressure mem_used_mb mem_agents_mb mem_rest_mb compressed_mb swap_used_mb swap_ins swap_outs
1335
+ disk_read_kb disk_write_kb disk_free_pct
1336
+ net_in_kb net_out_kb net_wifi net_resent_pct wifi_rssi wifi_noise
1337
+ on_battery low_power thermal'
1338
+
1339
+ # The host's numbers from $1 to $2, under five headings: CPU, memory, disk,
1340
+ # network, and power and heat. They are read from r, the readings in the
1341
+ # period, each with d, the part of its secs in the period, as the report
1342
+ # counts a reading. Each number is summed up as what it is:
1343
+ # a level its average, each reading weighed by its d, over the readings
1344
+ # that hold it, and its highest in one reading, or its lowest
1345
+ # for the disk's free space and the Wi-Fi's signal (host_level)
1346
+ # an amount its total: the recorder writes what it grew by in each
1347
+ # reading, and one on the period's edge counts in proportion to
1348
+ # its d, as host_split_sql counts a split (host_amount)
1349
+ # a state how long each value lasted, and how long it was not read when
1350
+ # that is more than nothing (host_state)
1351
+ # A number no reading in the period holds says not read in place of its
1352
+ # figures, as SQLite's printf would print a null as 0, and sql_took of a
1353
+ # null is null. $3 lists the store's tables and columns, as host_columns
1354
+ # gives them, and a column the store lacks is read as null, so that its
1355
+ # number says not read too. Every line is worked out in one pass over the
1356
+ # readings, into one array, which gives them in its order.
1380
1357
  #
1381
- # Under each number from 2 up, with $4 as 1, which says the store has the
1382
- # fingerprints of the test runs' commands, comes how much slower test runs
1383
- # were than with one working. A test run that started in the period counts
1384
- # at the number working that covers most of its time, over the readings
1385
- # that counted them, and at the larger number on a tie. A run that was cut
1386
- # short, is still going, has no fingerprint, as one from before they were
1387
- # kept, or overlaps no reading with a count, is left out. A command is a
1388
- # repo and a fingerprint, so only runs of the same command in the same repo
1389
- # are set against each other, and a command counts at a number when it ran
1390
- # at least 3 times with that many working and 3 times with one. Its ratio is
1391
- # its runs' median time with that many working over their median time with
1392
- # one, and the figure is the median of the commands' ratios, so one odd run,
1393
- # such as one that failed early, does not move it. With no such command, it
1394
- # says it cannot tell yet.
1395
- host_working_sql() {
1396
- local s=$1 e=$2 working=working slower=0 runs='select null, null, null, null, null where 0' counted=null
1397
- [ "${3:-0}" = 1 ] || working=null
1398
- if [ "$working" = working ] && [ "${4:-0}" = 1 ]; then
1399
- slower=1 counted=g.working
1400
- runs="select id, repo, cmd, started, secs from test_runs
1401
- where started >= $s and started < $e and ended is not null and cut = 0 and cmd is not null and secs is not null"
1402
- fi
1358
+ # Recording's average adds what watching the test runs that started in the
1359
+ # period used, as a share of the cores the readings counted, over the time
1360
+ # its own CPU was read. No reading holds that, so its most in one reading
1361
+ # is the recorder's own.
1362
+ host_numbers_sql() {
1363
+ local s=$1 e=$2 have=$3 c cols='' watch=''
1364
+ for c in $HOST_NUMBERS; do
1365
+ case $have in *" readings.$c "*) cols="$cols, $c" ;; *) cols="$cols, null as $c" ;; esac
1366
+ done
1367
+ case $have in
1368
+ *' readings.cores '*)
1369
+ case $have in
1370
+ *' test_runs.watch_secs '*)
1371
+ watch="coalesce(100.0 * (select sum(watch_secs) from test_runs where started >= $s and started < $e)
1372
+ / (select nullif(max(cores), 0) from readings where at > $s and at < $e + $HOST_GAP and at - secs < $e), 0)"
1373
+ ;;
1374
+ esac
1375
+ ;;
1376
+ esac
1403
1377
  cat <<EOF
1404
1378
  with
1405
- parts(n, d, lim) as (
1406
- select $working, max(0, min(at, $e) - max(at - secs, $s)), coalesce(cpu_busy >= 90 or mem_pressure >= 2, 0)
1407
- from readings where at > $s and at < $e + $HOST_GAP and at - secs < $e),
1408
- counts(n, len, lim) as (
1409
- select n, sum(d), sum(case when lim then d else 0 end) from parts where n is not null group by n),
1410
- uncounted(len) as (select coalesce(sum(d), 0) from parts where n is null),
1411
- fewest(n) as (select min(n) from counts where len > 0 and 4 * lim >= len),
1412
- runs(id, repo, cmd, started, secs) as ($runs),
1413
- -- A cross join keeps the runs outside, so that each one looks up only its
1414
- -- own readings by their at, never every reading for each run.
1415
- over(id, n, d) as (
1416
- select r.id, $counted, sum(max(0, min(g.at, r.started + r.secs) - max(g.at - g.secs, r.started)))
1417
- from runs r cross join readings g
1418
- on g.at > r.started and g.at < r.started + r.secs + $HOST_GAP and g.at - g.secs < r.started + r.secs
1419
- where $counted is not null group by r.id, $counted),
1420
- at_n(id, n) as (
1421
- select id, n from (select id, n, row_number() over (partition by id order by d desc, n desc) k from over) where k = 1),
1422
- timed(repo, cmd, n, secs, i, c) as (
1423
- select r.repo, r.cmd, a.n, r.secs, row_number() over (partition by r.repo, r.cmd, a.n order by r.secs),
1424
- count(*) over (partition by r.repo, r.cmd, a.n)
1425
- from runs r join at_n a on a.id = r.id),
1426
- medians(repo, cmd, n, runs, secs) as (
1427
- select repo, cmd, n, max(c), avg(secs) from timed where i in ((c + 1) / 2, (c + 2) / 2) group by repo, cmd, n),
1428
- ratios(n, ratio, runs, base, i, c) as (
1429
- select m.n, m.secs / b.secs, m.runs, b.runs, row_number() over (partition by m.n order by m.secs / b.secs),
1430
- count(*) over (partition by m.n)
1431
- from medians m join medians b on b.repo = m.repo and b.cmd = m.cmd and b.n = 1
1432
- where m.n >= 2 and m.runs >= 3 and b.runs >= 3 and b.secs > 0),
1433
- slower(n, ratio) as (select n, avg(ratio) from ratios where i in ((c + 1) / 2, (c + 2) / 2) group by n),
1434
- rested(n, cmds, runs, base) as (select n, count(*), sum(runs), sum(base) from ratios group by n),
1435
- lines(part, n, o, line) as (
1436
- select 0, 0, 0, ''
1437
- union all select 0, 1, 0, 'Agents working at once:'
1438
- union all
1439
- select 1, n, 0, ' ' || printf('%-11s', n || ' working') || ' ' || $(sql_took len) || ', CPU or memory at its limit '
1440
- || $(sql_took lim) || ', ' || printf('%.1f%%', 100.0 * lim / nullif(len, 0)) || ' of it'
1441
- from counts
1442
- union all
1443
- select 1, c.n, 1, ' test runs' || case when s.ratio is null
1444
- then ': cannot tell yet, as no command ran at least 3 times with ' || c.n || ' working and 3 times with 1'
1445
- else ' ' || case when s.ratio >= 1 then printf('%.0f%% slower', 100.0 * (s.ratio - 1))
1446
- else printf('%.0f%% faster', 100.0 * (1 - s.ratio)) end
1447
- || ' than with 1 working, from ' || r.runs || ' runs of ' || r.cmds || case when r.cmds = 1 then ' command' else ' commands' end
1448
- || ' against ' || r.base || ' with 1 working' end
1449
- from counts c left join slower s on s.n = c.n left join rested r on r.n = c.n
1450
- where c.n >= 2 and $slower
1451
- union all
1452
- select 2, 0, 0, ' not counted ' || $(sql_took len) || ', by readings from before the agents working were counted'
1453
- from uncounted where len > 0
1454
- union all
1455
- select 3, 0, 0, case
1456
- when not exists (select 1 from counts) then ' No reading in this period counted the agents working.'
1457
- when n is null then ' The host was not the limit at any number of working agents in this period: at each, the CPU or memory was at its limit for under a quarter of the time.'
1458
- else ' The host was the limit from ' || n || ' working ' || case when n = 1 then 'agent' else 'agents' end
1459
- || ': the fewest at which the CPU or memory was at its limit for at least a quarter of the time.' end
1460
- from fewest)
1461
- select line from lines order by part, n, o;
1379
+ r as (
1380
+ select max(0, min(at, $e) - max(at - secs, $s)) as d, secs$cols from readings
1381
+ where at > $s and at < $e + $HOST_GAP and at - secs < $e and secs > 0),
1382
+ numbers(lines) as (
1383
+ select json_array(
1384
+ '', 'CPU, as a share of all cores'' time:',
1385
+ $(host_level busy cpu_busy '%.1f%%'),
1386
+ $(host_level system cpu_system '%.1f%%'),
1387
+ $(host_level 'the agents' cpu_agents '%.1f%%'),
1388
+ $(host_level 'the rest' cpu_rest '%.1f%%'),
1389
+ $(host_level recording cpu_self '%.2f%%' '' most "$watch"),
1390
+ $(host_level 'performance cores' cpu_p '%.1f%%' 'busy on average'),
1391
+ $(host_level 'efficiency cores' cpu_e '%.1f%%' 'busy on average'),
1392
+ '', 'Memory:',
1393
+ $(host_state pressure mem_pressure 'not read' normal '< 2' warning 'between 2 and 3' critical '>= 4'),
1394
+ $(host_level used mem_used_mb '%.0f MB'),
1395
+ $(host_level 'the agents' mem_agents_mb '%.0f MB'),
1396
+ $(host_level 'the rest' mem_rest_mb '%.0f MB'),
1397
+ $(host_level compressed compressed_mb '%.0f MB'),
1398
+ $(host_level 'swap used' swap_used_mb '%.0f MB'),
1399
+ $(host_amount 'swapped in' swap_ins '%.0f pages'),
1400
+ $(host_amount 'swapped out' swap_outs '%.0f pages'),
1401
+ '', 'Disk:',
1402
+ $(host_amount read disk_read_kb '%.1f MB' 1024),
1403
+ $(host_amount written disk_write_kb '%.1f MB' 1024),
1404
+ $(host_level free disk_free_pct '%.1f%%' '' lowest),
1405
+ '', 'Network:',
1406
+ $(host_amount received net_in_kb '%.1f MB' 1024),
1407
+ $(host_amount sent net_out_kb '%.1f MB' 1024),
1408
+ $(host_state interface net_wifi 'offline or not read' Wi-Fi '= 1' another '= 0'),
1409
+ $(host_level 'sent again' net_resent_pct '%.1f%%' 'of TCP bytes on average'),
1410
+ $(host_level 'Wi-Fi signal' wifi_rssi '%.0f dBm' '' lowest),
1411
+ $(host_level 'Wi-Fi noise' wifi_noise '%.0f dBm'),
1412
+ '', 'Power and heat:',
1413
+ $(host_state power on_battery 'not read' 'on battery' '= 1' 'on power' '= 0'),
1414
+ $(host_state 'Low Power Mode' low_power 'not read' on '= 1' off '= 0'),
1415
+ $(host_state 'thermal state' thermal 'not read' nominal '= 0' fair '= 1' serious '= 2' critical '= 3'))
1416
+ from r)
1417
+ select value from numbers, json_each(numbers.lines) order by key;
1462
1418
  EOF
1463
1419
  }
1464
1420
 
1465
- # The longest stretches from $1 to $2 when the CPU or the memory was at its
1466
- # limit, as the report counts it, five at most, longest first. A stretch is
1467
- # readings in a row at the limit, each starting where the one before ended,
1468
- # and each counts for the part of it in the period, as the report counts a
1469
- # reading. Each says when it started, how long it lasted and which of the
1470
- # two was at its limit, then the work going in it: each card's step, or its
1471
- # phase when its card is in none, from the shares when $3 is 1, then each
1472
- # test run, from their shares when $4 is 1. Each has its share of all cores
1473
- # over the stretch, its share in each reading counted for that reading's
1474
- # part, and the most memory it held in one reading. Each card also has how
1475
- # much of the stretch its agent was working: the parts of the readings in
1476
- # which a share of its was working, over the stretch's length. With $5 not 1,
1477
- # the shares have no column for it, and with no share that says, as from
1478
- # before the agents working were told apart, it says it was not read. A
1479
- # share that names no card is left out. With no stretch, it says the host was
1480
- # never at its limit.
1481
- host_peaks_sql() {
1482
- local s=$1 e=$2 shares runs
1483
- shares='select at, card, phase, step, cpu, mem_mb, working from shares'
1484
- [ "${5:-0}" = 1 ] || shares='select at, card, phase, step, cpu, mem_mb, null from shares'
1485
- [ "${3:-0}" = 1 ] || shares='select null, null, null, null, null, null, null where 0'
1486
- runs='select r.at, r.run, t.card, t.repo, t.started, r.cpu, r.mem_mb from run_shares r join test_runs t on t.id = r.run'
1487
- [ "${4:-0}" = 1 ] || runs='select null, null, null, null, null, null, null where 0'
1488
- cat <<EOF
1489
- with
1490
- lim(at, start, d, cpu, mem, new) as (
1491
- select at, max(at - secs, $s), max(0, min(at, $e) - max(at - secs, $s)),
1492
- coalesce(cpu_busy >= 90, 0), coalesce(mem_pressure >= 2, 0),
1493
- case when lag(at) over (order by at) = at - secs then 0 else 1 end
1494
- from readings
1495
- where at > $s and at < $e + $HOST_GAP and at - secs < $e and (cpu_busy >= 90 or mem_pressure >= 2)),
1496
- grouped(at, start, d, cpu, mem, n) as (
1497
- select at, start, d, cpu, mem, sum(new) over (order by at rows between unbounded preceding and current row) from lim),
1498
- stretches(n, start, len, cpu, mem) as (
1499
- select n, min(start), sum(d), max(cpu), max(mem) from grouped group by n),
1500
- ranked(k, n, start, len, cpu, mem) as (
1501
- select row_number() over (order by len desc, start), n, start, len, cpu, mem from stretches),
1502
- top(k, n, start, len, cpu, mem) as (select k, n, start, len, cpu, mem from ranked where k <= 5),
1503
- parts(k, len, at, d) as (select t.k, t.len, g.at, g.d from top t join grouped g on g.n = t.n),
1504
- sh(at, card, phase, step, cpu, mem, working) as ($shares),
1505
- rs(at, run, card, repo, started, cpu, mem) as ($runs),
1506
- cards(k, len, at, d, name, cpu, mem, working) as (
1507
- select p.k, p.len, p.at, p.d, sh.card || ' ' || coalesce(sh.step || ' step', sh.phase || ' phase'), sum(sh.cpu), sum(sh.mem),
1508
- max(sh.working)
1509
- from parts p join sh on sh.at = p.at
1510
- where sh.card is not null and coalesce(sh.step, sh.phase) is not null
1511
- group by p.k, p.at, sh.card, sh.step, sh.phase),
1512
- work(k, part, name, cpu, mem, working) as (
1513
- select k, 1, name, sum(1.0 * cpu * d) / len, max(mem),
1514
- case when count(working) > 0 then 100.0 * sum(case when working = 1 then d else 0 end) / len end
1515
- from cards group by k, name
1516
- union all
1517
- select p.k, 2, rs.card || ' ' || rs.repo || ' test run ' || strftime('%Y-%m-%d %H:%M', rs.started, 'unixepoch', 'localtime'),
1518
- sum(1.0 * rs.cpu * p.d) / p.len, max(rs.mem), null
1519
- from parts p join rs on rs.at = p.at group by p.k, rs.run),
1520
- lines(k, part, o, line) as (
1521
- select -1, 0, 0, ''
1522
- union all select 0, 0, 0, 'Longest stretches with CPU or memory at its limit:' where exists (select 1 from top)
1523
- union all select 0, 0, 0, 'The host was never at its limit for CPU or memory in this period.' where not exists (select 1 from top)
1524
- union all
1525
- select k, 0, 0, ' ' || strftime('%Y-%m-%d %H:%M:%S', start, 'unixepoch', 'localtime') || ' for ' || $(sql_took len) || ', '
1526
- || case when cpu and mem then 'CPU and memory at their limit' when cpu then 'CPU at its limit' else 'memory at its limit' end
1527
- from top
1528
- union all
1529
- select k, part, row_number() over (partition by k, part order by cpu desc, name),
1530
- ' ' || printf('%-44s', name) || ' ' || case when cpu is null then 'CPU not read' else 'CPU ' || printf('%.1f%%', cpu) end
1531
- || ', ' || case when mem is null then 'memory not read' else 'memory ' || mem || ' MB' end
1532
- || case when part = 2 then '' when working is null then ', working not read'
1533
- else ', working ' || printf('%.0f%%', working) || ' of it' end
1534
- from work
1535
- union all
1536
- select k, 3, 0, ' no card or test run was going' from top t where not exists (select 1 from work w where w.k = t.k))
1537
- select line from lines order by k, part, o;
1538
- EOF
1421
+ # SQL for a line of the host's numbers on a level, over the readings r: the
1422
+ # label $1, then the average of the column $2, with the words $4 after it,
1423
+ # or "on average", and its highest in one reading, or its lowest with $5 as
1424
+ # lowest. $3 is the printf format of each figure. $6, when given, is added
1425
+ # to the readings' sum before it is divided by their seconds.
1426
+ host_level() {
1427
+ local peak="max($2)" most='at most'
1428
+ [ "${5:-}" != lowest ] || peak="min($2)" most='at the lowest'
1429
+ printf '%s' "' ' || printf('%-19s', '$1') || case when count($2) = 0 then 'not read'
1430
+ else printf('$3', (sum(1.0 * $2 * d)${6:+ + $6}) / sum(case when $2 is not null then d end))
1431
+ || ' ${4:-on average}, ' || printf('$3', $peak) || ' $most' end"
1432
+ }
1433
+
1434
+ # SQL for a line of the host's numbers on an amount, over the readings r:
1435
+ # the label $1, then the total of the column $2, divided by $4 when it is
1436
+ # given, in the printf format $3. Each reading counts what it grew by for
1437
+ # its d in proportion to its secs.
1438
+ host_amount() {
1439
+ printf '%s' "' ' || printf('%-19s', '$1') || case when count($2) = 0 then 'not read'
1440
+ else printf('$3', sum(1.0 * $2 * d / secs)${4:+ / $4}) end"
1441
+ }
1442
+
1443
+ # SQL for a line of the host's numbers on a state, over the readings r: the
1444
+ # label $1, then how long the column $2 held each value, $4 onward, each a
1445
+ # name and the SQL that tests the column for it, like normal '< 2'. Then,
1446
+ # when it is more than nothing, how long it held none of them, which $3
1447
+ # names.
1448
+ host_state() {
1449
+ local label=$1 col=$2 none=$3 shown='' left='sum(d)' v
1450
+ shift 3
1451
+ while [ "$#" -ge 2 ]; do
1452
+ v="sum(case when $col $2 then d else 0 end)"
1453
+ [ -z "$shown" ] || shown="$shown || ', ' || "
1454
+ shown="$shown'$1 ' || $(sql_took "$v")"
1455
+ left="$left - $v"
1456
+ shift 2
1457
+ done
1458
+ printf '%s' "' ' || printf('%-19s', '$label') || case when count($col) = 0 then 'not read'
1459
+ else $shown || case when $left > 0 then ', $none ' || $(sql_took "($left)") else '' end end"
1539
1460
  }
1540
1461
 
1541
1462
  # The steps and the phases that used the most CPU time, and that held the
@@ -1663,131 +1584,166 @@ select line from lines order by k;
1663
1584
  EOF
1664
1585
  }
1665
1586
 
1666
- # The verdict on the host from $1 to $2, from the splits when $3 is 1, and
1667
- # the power state of the readings when $4 is 1, and the waits on the model
1668
- # when $5 is 1: the report's last line, after the network line when there
1669
- # is one.
1587
+ # The readings taken while each of a card's steps ran, as one line of JSON:
1588
+ # `host report --card <space> <card> --json`. Anything else it is asked is
1589
+ # refused on stderr, with nothing on stdout, so that a program never reads
1590
+ # words as JSON.
1670
1591
  #
1671
- # A wait on the model is slow when it took over twice the median of every
1672
- # wait in model_waits, so 90 days at most: the wait halfway in order of
1673
- # length, the later of the two for an even count. A slow wait counts for the
1674
- # network when there is a reading over it, and every reading over it, in
1675
- # whole or in part, had the CPU, memory and disk below their limit, as the
1676
- # report counts it. The period's waits are those that ended in it. With any
1677
- # that count, the network line, between blank lines, says the network may
1678
- # be the cause: how many of the period's waits were slow while the host was
1679
- # below its limit, and the median. A store with no waits on the model has
1680
- # none.
1592
+ # A step is each time the card was in it: hand-offs of one step in a row,
1593
+ # from the hand-off before the first to the last, so the time it waited on
1594
+ # the human is inside it. The times are log_view's, as `task log` shows
1595
+ # them, so the two cannot disagree. A `new` or a `start` row has no time
1596
+ # there, so it has no entry here. The step under way runs to now and is
1597
+ # open: the one the log counts as under way (card_agent in agents.sh), or
1598
+ # one whose question still waits.
1681
1599
  #
1682
- # The agents' time on the host is their splits' seconds on the host, an
1683
- # agent's second each, each split counted for the part of its reading in the
1684
- # period, as the report counts a reading. The verdict is the first of these
1685
- # that holds:
1686
- # - too little data yet, with under HOST_VERDICT_STEPS steps with a split
1687
- # in the period, each one card's own, or under HOST_VERDICT_SECS of the
1688
- # agents' time on the host. It says how many and how long, against them.
1689
- # - of the CPU, memory and disk, the one at its limit, as the report counts
1690
- # it, for the most of the agents' time on the host, and for at least
1691
- # HOST_VERDICT_PCT% of it, the CPU before memory before the disk when two
1692
- # were as long: the power state was the limit, when over half of that
1693
- # one's time at its limit was on battery, in Low Power Mode or with the
1694
- # thermal state at serious or critical, and otherwise it is the one to
1695
- # upgrade first.
1696
- # - the network was the limit, when the waits that count for it are at
1697
- # least HOST_VERDICT_PCT% of the period's waits on the model. It gives
1698
- # that share and the median.
1699
- # - no upgrade is needed.
1700
- # All but the first say how much of the agents' time on the host each was at
1701
- # its limit. A store with no splits has too little data, and one whose
1702
- # readings have no power state counts them as on power, as a reading with
1703
- # none of it does.
1704
- HOST_VERDICT_STEPS=10
1705
- HOST_VERDICT_SECS=3600
1706
- HOST_VERDICT_PCT=10
1707
- host_verdict_sql() {
1708
- local s=$1 e=$2 splits power waits took median
1709
- splits='select at, card, step, host_secs from splits'
1710
- [ "${3:-0}" = 1 ] || splits='select null, null, null, null where 0'
1711
- power='coalesce(on_battery = 1 or low_power = 1 or thermal >= 2, 0)'
1712
- [ "${4:-0}" = 1 ] || power=0
1713
- waits='select ended - started, started, ended from model_waits'
1714
- [ "${5:-0}" = 1 ] || waits='select null, null, null where 0'
1715
- took=$(sql_took 'cast(round(t.host) as integer)')
1716
- # A median under a minute is given to a tenth of a second.
1717
- median="case when m < 59.95 then printf('%.1fs', m) else $(sql_took 'cast(round(m) as integer)') end"
1718
- cat <<EOF
1719
- with
1720
- parts(at, secs, d, cpu, mem, disk, power) as (
1721
- select at, secs, max(0, min(at, $e) - max(at - secs, $s)),
1722
- coalesce(cpu_busy >= 90, 0), coalesce(mem_pressure >= 2, 0), coalesce(disk_free_pct < 10, 0), $power
1723
- from readings where at > $s and at < $e + $HOST_GAP and at - secs < $e and secs > 0),
1724
- x(at, card, step, host) as ($splits),
1725
- sp(card, step, host, cpu, mem, disk, power) as (
1726
- select x.card, x.step, 1.0 * coalesce(x.host, 0) * p.d / p.secs, p.cpu, p.mem, p.disk, p.power
1727
- from parts p join x on x.at = p.at),
1728
- total(steps, host) as (
1729
- select (select count(*) from (select distinct card, step from sp where card is not null and step is not null)),
1730
- coalesce(sum(host), 0) from sp),
1731
- res(o, name, lim, pow) as (
1732
- select 1, 'the CPU', coalesce(sum(case when cpu then host end), 0), coalesce(sum(case when cpu and power then host end), 0) from sp
1733
- union all
1734
- select 2, 'memory', coalesce(sum(case when mem then host end), 0), coalesce(sum(case when mem and power then host end), 0) from sp
1735
- union all
1736
- select 3, 'the disk', coalesce(sum(case when disk then host end), 0), coalesce(sum(case when disk and power then host end), 0) from sp),
1737
- pcts(cpu, mem, disk) as (
1738
- select max(case when o = 1 then pct end), max(case when o = 2 then pct end), max(case when o = 3 then pct end)
1739
- from (select o, printf('%.1f%%', 100.0 * lim / nullif(t.host, 0)) pct from res, total t)),
1740
- named(o, name, lim, pow) as (
1741
- select o, name, lim, pow from res, total t
1742
- where lim > 0 and 100.0 * lim >= $HOST_VERDICT_PCT * t.host order by lim desc, o limit 1),
1743
- w(d, started, ended) as ($waits),
1744
- med(m) as (select d from w order by d limit 1 offset (select count(*) / 2 from w)),
1745
- inp(d, started, ended) as (select d, started, ended from w where ended >= $s and ended < $e),
1746
- slow(started, ended) as (select i.started, i.ended from inp i, med where i.d > 2 * med.m),
1747
- span(secs) as (select coalesce(max(secs), 0) from readings),
1748
- below(n) as (
1749
- select count(*) from slow, span
1750
- where exists (select 1 from readings r
1751
- where r.at > slow.started and r.at < slow.ended + span.secs and r.at - r.secs < slow.ended)
1752
- and not exists (select 1 from readings r
1753
- where r.at > slow.started and r.at < slow.ended + span.secs and r.at - r.secs < slow.ended
1754
- and (coalesce(r.cpu_busy >= 90, 0) or coalesce(r.mem_pressure >= 2, 0) or coalesce(r.disk_free_pct < 10, 0)))),
1755
- net(n, total, median) as (select b.n, (select count(*) from inp), $median from below b, med),
1756
- lines(k, line) as (
1757
- select 0, ''
1758
- union all
1759
- select 1, 'The network may be the cause: ' || n || ' of ' || total || case when total = 1 then ' wait' else ' waits' end
1760
- || ' on the model ' || case when n = 1 then 'was' else 'were' end || ' slow (over twice the median of ' || median
1761
- || ') while CPU, memory and disk were below their limit.'
1762
- from net where n > 0
1763
- union all
1764
- select 2, '' from net where n > 0
1765
- union all
1766
- select 3, 'Verdict: ' || case
1767
- when t.steps < $HOST_VERDICT_STEPS or t.host < $HOST_VERDICT_SECS then
1768
- 'too little data yet: ' || t.steps || case when t.steps = 1 then ' step had its' else ' steps had their' end
1769
- || ' time split, with ' || $took || ' of the agents'' time on the host, and a verdict takes at least '
1770
- || '$HOST_VERDICT_STEPS steps and ' || $(sql_took $HOST_VERDICT_SECS) || '.'
1771
- when f.o is not null then
1772
- case when 2 * f.pow > f.lim then 'the power state was the limit, not ' || f.name else 'upgrade ' || f.name || ' first' end
1773
- || ': it was at its limit for ' || printf('%.1f%%', 100.0 * f.lim / t.host) || ' of the agents'' ' || $took || ' on the host'
1774
- || case when 2 * f.pow > f.lim then ', ' || printf('%.1f%%', 100.0 * f.pow / f.lim)
1775
- || ' of that on battery, in Low Power Mode or with the thermal state at serious or critical' else '' end
1776
- || ' (' || case f.o when 1 then 'memory ' || p.mem || ', disk ' || p.disk
1777
- when 2 then 'CPU ' || p.cpu || ', disk ' || p.disk else 'CPU ' || p.cpu || ', memory ' || p.mem end || ').'
1778
- when nt.n > 0 and 100 * nt.n >= $HOST_VERDICT_PCT * nt.total then
1779
- 'the network was the limit: ' || printf('%.1f%%', 100.0 * nt.n / nt.total) || ' of the ' || nt.total
1780
- || case when nt.total = 1 then ' wait on the model was' else ' waits on the model were' end
1781
- || ' slow (over twice the median of ' || nt.median || ') while CPU, memory and disk were below their limit,'
1782
- || ' and each was at its limit for under $HOST_VERDICT_PCT% of the agents'' ' || $took
1783
- || ' on the host (CPU ' || p.cpu || ', memory ' || p.mem || ', disk ' || p.disk || ').'
1784
- else
1785
- 'no upgrade is needed: CPU, memory and disk were each at their limit for under $HOST_VERDICT_PCT% of the agents'' '
1786
- || $took || ' on the host (CPU ' || p.cpu || ', memory ' || p.mem || ', disk ' || p.disk || ').'
1787
- end
1788
- from total t left join named f on 1 left join pcts p on 1 left join net nt on 1)
1789
- select line from lines order by k;
1790
- EOF
1600
+ # A reading counts for its seconds inside the step, as the report counts
1601
+ # one in a period. A step with more than HOST_CARD_POINTS readings is cut
1602
+ # into that many equal parts, to the second, and each part with a reading in
1603
+ # it gives one point: the average CPU and memory over its recorded seconds,
1604
+ # and the worst memory pressure in it. Time with no reading gives no point,
1605
+ # so it is never a number. With recording off, or with no readings kept, the
1606
+ # steps are still listed, each with none.
1607
+ HOST_CARD_POINTS=60
1608
+ HOST_CARD_FORM='ocnus host report --card <space> <card> --json'
1609
+ host_card() {
1610
+ local name='' card='' json=0 asked=0
1611
+ while [ "$#" -gt 0 ]; do
1612
+ case $1 in
1613
+ --json)
1614
+ [ "$json" = 0 ] || die "--json is given once: $HOST_CARD_FORM"
1615
+ json=1
1616
+ ;;
1617
+ --card)
1618
+ [ "$asked" = 0 ] || die "--card is given once, for one card: $HOST_CARD_FORM"
1619
+ asked=1
1620
+ case ${2:-} in '' | --*) die "--card takes a space and a card: $HOST_CARD_FORM" ;; esac
1621
+ case ${3:-} in '' | --*) die "--card takes a space and a card: $HOST_CARD_FORM" ;; esac
1622
+ name=$2 card=$3
1623
+ shift 2
1624
+ ;;
1625
+ *)
1626
+ [ "$asked" = 1 ] || case " $* " in *' --card '*) asked=1 ;; esac
1627
+ [ "$asked" = 1 ] ||
1628
+ die "--json needs --card: only a card's readings come as JSON, with $HOST_CARD_FORM. The report on a period is in words."
1629
+ die "'$1' does not go with --card: a card's readings are those of its steps, whatever the period. Ask with: $HOST_CARD_FORM"
1630
+ ;;
1631
+ esac
1632
+ shift
1633
+ done
1634
+ [ "$asked" = 1 ] ||
1635
+ die "--json needs --card: only a card's readings come as JSON, with $HOST_CARD_FORM. The report on a period is in words."
1636
+ [ "$json" = 1 ] || die "--card needs --json: a card's readings come as JSON alone. Ask with: $HOST_CARD_FORM"
1637
+ space_named "$name" || die "there is no ocnus space called '$name'. See: ocnus space ls"
1638
+ [[ $card =~ $CARD_RE ]] || die "'$card' is not a card ID like ABC-122."
1639
+ card=$(printf '%s' "$card" | tr '[:lower:]' '[:upper:]')
1640
+ have_sqlite || die "$SQLITE is not installed, and the hand-offs and the readings are kept with it."
1641
+
1642
+ local recording=false now uri attach='' out
1643
+ [ ! -d "$HOST_DIR" ] || recording=true
1644
+ # As `task log --json` with no store: the same keys, and no steps.
1645
+ if [ ! -e "$STORE" ]; then
1646
+ printf '{"space":"%s","card":"%s","recording":%s,"steps":[]}\n' "$(json_str "$name")" "$(json_str "$card")" "$recording"
1647
+ return
1648
+ fi
1649
+ now=$(date +%s)
1650
+ # With no readings kept, a table with none stands in for them.
1651
+ local readings='(select 0 as at, 0 as secs, null as cpu_busy, null as mem_used_mb, null as mem_pressure where 0)'
1652
+ if [ -e "$HOST_DB" ]; then
1653
+ # In a URI, % starts an escape, ? the options and # the end.
1654
+ uri=${HOST_DB//\%/%25}; uri=${uri//\?/%3F}; uri=${uri//\#/%23}
1655
+ attach="attach $(sql_str "file:$uri?mode=ro") as host;"
1656
+ readings=host.readings
1657
+ fi
1658
+ # Each table is worked out once, whatever the card's length:
1659
+ # flagged, grouped the card's rows, numbered by the time it was in a step
1660
+ # spans, latest each of those times' first and last row, and the
1661
+ # card's latest row with the step under way, if any
1662
+ # ended, steps the steps, in order, the one under way running to now
1663
+ # inside, counts each reading's seconds inside each step, and how
1664
+ # many readings a step has
1665
+ # spread, pieces for a step to thin, each reading's seconds in each of
1666
+ # the equal parts it reaches: the first and the last of
1667
+ # them are worked out, so no reading is tried against
1668
+ # every part
1669
+ # points, drawn each step's points, and all of them as one array
1670
+ # SQLite builds the JSON, so no value is escaped by hand.
1671
+ out=$(store "$STORE" "$attach
1672
+ $(log_view "$name" "$card"),
1673
+ flagged(id, run, step, kind, status, next, after, t, prev_t, first) as (
1674
+ select id, run, step, kind, status, next, after,
1675
+ cast(strftime('%s', at) as integer), cast(strftime('%s', prev_at) as integer),
1676
+ case when lag(step) over (order by id) is step then 0 else 1 end
1677
+ from shown),
1678
+ grouped(id, run, step, kind, status, next, after, t, prev_t, g) as (
1679
+ select id, run, step, kind, status, next, after, t, prev_t,
1680
+ sum(first) over (order by id rows between unbounded preceding and current row)
1681
+ from flagged),
1682
+ spans(g, first_id, last_id) as (
1683
+ select g, min(id), max(id) from grouped where kind != 'start' group by g),
1684
+ latest(run, step, t, under) as (
1685
+ select run, step, t, case when status in ('answered', 'needs-human') then step
1686
+ when status = 'done' and coalesce(next, '') not in ('', 'start', 'setup', 'merged') then next end
1687
+ from grouped where after is null),
1688
+ ended(o, run, step, s, e, open) as (
1689
+ select sp.g, l.run, l.step, coalesce(f.prev_t, f.t), l.t, l.after is null and l.step is u.under
1690
+ from spans sp join grouped f on f.id = sp.first_id join grouped l on l.id = sp.last_id left join latest u on 1
1691
+ union all
1692
+ select (select max(g) from grouped) + 1, run, under, t, t, 1 from latest
1693
+ where under is not null and under is not step),
1694
+ steps(n, run, step, s, e, open) as (
1695
+ select row_number() over (order by o), run, step, s, case when open then max($now, e) else e end, open from ended),
1696
+ inside(n, at, a, b, cpu, mem, pressure) as (
1697
+ select st.n, g.at, max(g.at - g.secs, st.s), min(g.at, st.e), g.cpu_busy, g.mem_used_mb, g.mem_pressure
1698
+ from steps st cross join $readings g
1699
+ on g.at > st.s and g.at < st.e + $HOST_GAP and g.at - g.secs < st.e
1700
+ where min(g.at, st.e) > max(g.at - g.secs, st.s)),
1701
+ counts(n, readings, recorded) as (select n, count(*), sum(b - a) from inside group by n),
1702
+ spread(n, k, last, s, len, a, b, cpu, mem, pressure) as (
1703
+ select p.n, ($HOST_CARD_POINTS * (p.a - st.s + 1) - 1) / (st.e - st.s), ($HOST_CARD_POINTS * (p.b - st.s) - 1) / (st.e - st.s),
1704
+ st.s, st.e - st.s, p.a, p.b, p.cpu, p.mem, p.pressure
1705
+ from inside p join counts c on c.n = p.n and c.readings > $HOST_CARD_POINTS join steps st on st.n = p.n
1706
+ union all
1707
+ select n, k + 1, last, s, len, a, b, cpu, mem, pressure from spread where k < last),
1708
+ pieces(n, k, lo, hi, d, cpu, mem, pressure) as (
1709
+ select n, k, lo, hi, min(b, hi) - max(a, lo), cpu, mem, pressure from (
1710
+ select n, k, s + len * k / $HOST_CARD_POINTS as lo, s + len * (k + 1) / $HOST_CARD_POINTS as hi, a, b, cpu, mem, pressure
1711
+ from spread)
1712
+ where min(b, hi) > max(a, lo)),
1713
+ points(n, o, a, b, d, cpu, mem, pressure) as (
1714
+ select p.n, p.at, p.a, p.b, p.b - p.a, p.cpu, p.mem, p.pressure
1715
+ from inside p join counts c on c.n = p.n and c.readings <= $HOST_CARD_POINTS
1716
+ union all
1717
+ select n, k, lo, hi, sum(d), 1.0 * sum(cpu * d) / sum(case when cpu is not null then d end),
1718
+ cast(round(1.0 * sum(mem * d) / sum(case when mem is not null then d end)) as integer), max(pressure)
1719
+ from pieces group by n, k),
1720
+ drawn(n, i, points) as (
1721
+ select n, row_number() over (partition by n order by o),
1722
+ json_group_array(json_object(
1723
+ 'start', strftime('%Y-%m-%dT%H:%M:%SZ', a, 'unixepoch'),
1724
+ 'end', strftime('%Y-%m-%dT%H:%M:%SZ', b, 'unixepoch'), 'seconds', d,
1725
+ 'cpu_busy', round(cpu, 1), 'mem_used_mb', mem,
1726
+ 'mem_pressure', case when pressure >= 4 then 'critical' when pressure >= 2 then 'warning'
1727
+ when pressure is not null then 'normal' end))
1728
+ over (partition by n order by o rows between unbounded preceding and unbounded following)
1729
+ from points)
1730
+ select json_object('space', $(sql_str "$name"), 'card', $(sql_str "$card"), 'recording', json('$recording'),
1731
+ 'steps', json((select json_group_array(json(entry)) from (
1732
+ select json_object('run', st.run, 'step', st.step,
1733
+ 'start', strftime('%Y-%m-%dT%H:%M:%SZ', st.s, 'unixepoch'),
1734
+ 'end', strftime('%Y-%m-%dT%H:%M:%SZ', st.e, 'unixepoch'),
1735
+ 'seconds', max(st.e - st.s, 0), 'open', json(case when st.open then 'true' else 'false' end),
1736
+ 'readings', coalesce(c.readings, 0), 'recorded', coalesce(c.recorded, 0),
1737
+ 'points', json(coalesce(d.points, '[]'))) as entry
1738
+ from steps st left join counts c on c.n = st.n left join drawn d on d.n = st.n and d.i = 1
1739
+ order by st.n))));") || {
1740
+ # Which of the two it could not read: the hand-offs alone are tried again.
1741
+ [ -n "$attach" ] && store "$STORE" 'select count(*) from handoffs where 0;' >/dev/null ||
1742
+ die "could not read the hand-offs in $STORE. It may not be ocnus's store."
1743
+ die "could not read the readings in $HOST_DB. They may not be the ones ocnus host records."
1744
+ }
1745
+ [ -n "$out" ] || die "could not read the hand-offs in $STORE. It may not be ocnus's store."
1746
+ say "$out"
1791
1747
  }
1792
1748
 
1793
1749
  cmd_host() {