Skip to content
suit Artifact [90e6a48733]
Login

Artifact [90e6a48733]

Artifact 90e6a487337d990aad632da0929cacf241e0a9dbb1f9bf6e760a83f1f8982b11:


#ifndef SUIT_TALLY_H
#define SUIT_TALLY_H

#include "conf.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 `suit curate` (docs/curate.md) -----------------------------------------

// One event of the tally. The pointers stay valid until the process ends.
// TIME starts with "YYYY-MM-DDTHH:MM:SS" (TALLY_TIME_LEN bytes).
typedef struct {
    const char *time;
    UseKind kind;
    const char *name;
    size_t name_len;
    const char *project;
    size_t project_len;
} TallyEvent;

typedef void (*TallyEventSink)(void *state, const TallyEvent *ev);

constexpr size_t TALLY_TIME_LEN = 19;

// A scan as `suit usage` does it, then the events kept are held for tally_each.
// Returns what `suit usage` returns: 0, 1 after a message, 2 (no wardrobe, a bad
// config), or EXIT_BUSY. The config and the wardrobes it read, and the time the
// scan took as "now", are then available below.
int tally_open(void);
void tally_each(TallyEventSink sink, void *state);
const Conf *tally_conf(void);
const Wards *tally_wards(void);
int64_t tally_now(void);
// Whether the harness section SEC of tally_conf() has `sessions` and a `use` rule.
bool tally_scannable(const Section *sec);

// ---- 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