\Drupal\strata\Storage ProviderStatStore

Keeps what each store was asked to do, accumulated per day.

A ProviderStats is an accumulator for one request; this is the record. Without it every reading the budget guard, the cost report and the telemetry pass can take describes only the process that is asking, so a site that flushes in a queue worker and reports in a web request can never see its own bill.

One row per provider, day and operation. A day is a whole number of days since the epoch at midnight UTC, which keeps a row addressable without a date function and makes the primary key do the accumulating: a second flush on the same day adds to the row the first one wrote.

Durations are stored in whole milliseconds. A float column would carry more precision than an HTTP round trip has, and an integer sums exactly however many times it is added to.

Nothing here is authoritative and nothing else reads it back to work: it is an observation of traffic that already happened, which is why an uninstall dropping the table costs nothing but the history of the bill.

Summary

Methods
Properties
Constants
__construct
record
prune
total
byOperation
byProvider
daily
providers
count
day
No public properties found
TABLE
No protected methods found
No protected properties found
No protected constants found
aggregate
milliseconds
database
time
DAY

Constant

TABLE

TABLE = 'strata_provider_stat'

The table this store lives in.

DAY

DAY = 86400

Seconds in a day.

Properties

$database

$database : \Drupal\Core\Database\Connection

Type

Connection

$time

$time : \Drupal\Component\Datetime\TimeInterface

Type

TimeInterface

Methods

__construct()

__construct(\Drupal\Core\Database\Connection  $database, \Drupal\Component\Datetime\TimeInterface  $time) : mixed

Constructs a store.

Parameters

\Drupal\Core\Database\Connection $database

The database.

\Drupal\Component\Datetime\TimeInterface $time

The clock, so a test can place a reading on a chosen day.

Returns

mixed —

record()

record(string  $provider, \Drupal\strata\Storage\ProviderStats  $stats, int|null  $at = null) : int

Folds one window into the rows for its day.

Idempotent in shape rather than in effect: calling it twice with the same window counts that window twice, which is correct, because a window is a set of requests that were made. A caller that persists an accumulator is expected to reset it, and ProviderStats::reset() exists for exactly that.

Parameters

string $provider

The provider id the window belongs to.

\Drupal\strata\Storage\ProviderStats $stats

The window. Left unchanged.

int|null $at

Unix timestamp the window belongs to. NULL uses the request time.

Returns

int —

How many operation rows were written or updated. Zero when the window recorded nothing.

prune()

prune(int  $before) : int

Drops every row for days that ended before a moment.

Parameters

int $before

Unix timestamp. The day this falls in is kept; earlier days go.

Returns

int —

How many rows were removed.

total()

total(int  $from = 0, int  $to = 0) : array{requests: int, failures: int, bytes: int, seconds: float, classA: int, classB: int}

Everything recorded over a window, as one set of figures.

Parameters

int $from

Unix timestamp to read from. Zero reads from the first row.

int $to

Unix timestamp to read to. Zero reads to the last row.

Returns

array{requests: int, failures: int, bytes: int, seconds: float, classA: int, classB: int} —

All zeroes when nothing was recorded in the window.

byOperation()

byOperation(int  $from = 0, int  $to = 0) : array<string,array{requests: int, failures: int, bytes: int, seconds: float, slowest: float}>

Per-operation figures over a window, summed across every provider.

Operations with no rows are left out, so a table rendered from this shows the verbs the store was actually asked for. Order follows ProviderStats::OPERATIONS.

Parameters

int $from

Unix timestamp to read from. Zero reads from the first row.

int $to

Unix timestamp to read to. Zero reads to the last row.

Returns

array

Keyed by operation.

byProvider()

byProvider(int  $from = 0, int  $to = 0) : array<string,array{requests: int, failures: int, bytes: int, seconds: float, slowest: float}>

Per-provider figures over a window, summed across every operation.

Parameters

int $from

Unix timestamp to read from. Zero reads from the first row.

int $to

Unix timestamp to read to. Zero reads to the last row.

Returns

array

Keyed by provider id, busiest first.

daily()

daily(int  $from = 0, int  $to = 0) : list<array{day: int, requests: int, failures: int, bytes: int, seconds: float, slowest: float}>

Daily totals over a window, oldest first.

A day with no traffic has no row, so a graph drawn from this joins the points it has rather than reading a gap as a zero.

Parameters

int $from

Unix timestamp to read from. Zero reads from the first row.

int $to

Unix timestamp to read to. Zero reads to the last row.

Returns

list

One entry per day that has traffic.

providers()

providers() : list<string>

Every provider that has a row.

Returns

list

Provider ids, alphabetically.

count()

count() : int

How many rows are held.

Returns

int —

The row count.

day()

day(int  $timestamp) : int

Midnight UTC of the day a moment falls in.

Parameters

int $timestamp

A Unix timestamp. A negative one is treated as the epoch.

Returns

int —

Unix timestamp of that midnight.

aggregate()

aggregate(string  $column, int  $from, int  $to) : array<string,array{requests: int, failures: int, bytes: int, seconds: float, slowest: float}>

Sums the figure columns grouped by one column.

Parameters

string $column

One of provider, day or operation.

int $from

Unix timestamp to read from. Zero reads from the first row.

int $to

Unix timestamp to read to. Zero reads to the last row.

Returns

array

Keyed by the grouping column's value.

milliseconds()

milliseconds(float  $seconds) : int

Whole milliseconds, rounded to nearest and never negative.

Parameters

float $seconds

A duration.

Returns

int —

Milliseconds.