Skip to content
suit Documentation
Login

Documentation

#ifndef SUIT_TALLY_H
#define SUIT_TALLY_H

#include <stddef.h>
#include <stdint.h>

// Usage counts (docs/usage.md): what suit keeps, per machine, of how often each
// skill, agent and command was invoked, read from the session records the
// harnesses keep. $SUIT_HOME/usage/events and $SUIT_HOME/usage/offsets.

constexpr size_t TALLY_EVENTS_FILE_MAX = 33554432;  // the events file, in bytes
constexpr size_t TALLY_EVENTS_NEW_MAX = 8388608;    // the events one scan may add, in bytes
constexpr uint32_t TALLY_EVENTS_MAX = 500000;       // events kept
constexpr uint32_t TALLY_FILES_MAX = 16384;         // session files known at once
constexpr uint32_t TALLY_ROWS_MAX = 65536;          // distinct names in one report
constexpr int TALLY_DAYS_KEPT = 400;                // events older than this are dropped
constexpr int TALLY_FIRST_SCAN_DAYS = 45;           // a first scan reads files modified this recently
constexpr int TALLY_WINDOW_DEFAULT = 30;

// `suit usage scan`: read what is new in every harness's session records and
// add it to the tally. Prints one line. 0, 1 after a message, 2 for a config
// the scan cannot use, or EXIT_BUSY.
int cmd_usage_scan(void);

// `suit usage [--since DAYS] [--kind KIND] [--harness NAME] [--project KEY] [--tsv]`:
// a scan, then the table. ARGV is what follows "usage". 2 for bad arguments.
int cmd_usage(int argc, char **argv);

// `suit tend` with no name: tend every wardrobe, then scan once. The scan's
// failure is reported and does not change the exit status.
int cmd_tend_all(void);

// ---- for the tests ----------------------------------------------------------

// SEC seconds since 1970 as "YYYY-MM-DDTHH:MM:SS" into OUT (20 bytes).
void tally_civil(int64_t sec, char out[20]);
// Whether the LEN bytes start with a time of that form and hold no tab or newline.
bool tally_time_ok(const char *t, size_t len);
// Drop the lines of BUF (LEN bytes, each ending in a newline, starting with a
// time) whose time is before CUTOFF, then lines from the front until at most
// MAX_EVENTS lines and MAX_BYTES bytes remain. Returns the new length.
size_t tally_trim(char *buf, size_t len, const char *cutoff, uint32_t max_events, size_t max_bytes);
// Whether the PLEN bytes of PATTERN, with "*" for any run of bytes, match all of NAME.
bool tally_wild(const char *pattern, size_t plen, const char *name);

#endif