Documentation
¶
Overview ¶
Package classstats counts the instances of every used class, once a day, on the endpoints that opt in (class_stats = true), and keeps the history in SQLite (./data/classstats.db).
On GraphDB (LINDAS) each class is counted from the index statistics — one query per class, 0.2 s even for millions of instances (see queries.go); other stores get a live COUNT per class with a timeout and a run budget.
Readers: the Graph Explorer class tree (/api/class-tree), the monitoring page, the MCP tools and the Prometheus /metrics endpoint.
Index ¶
- Constants
- type Change
- type Changes
- type ClassCount
- type Collector
- func (c *Collector) Changes(slug string, days int) (*Changes, error)
- func (c *Collector) ClassLabels(ctx context.Context, ep *config.SparqlEndpoint, iris []string) (*Results, error)
- func (c *Collector) ClassTree(ctx context.Context, ep *config.SparqlEndpoint) (*Results, error)
- func (c *Collector) Collect(ch chan<- prometheus.Metric)
- func (c *Collector) Describe(ch chan<- *prometheus.Desc)
- func (c *Collector) Enabled() bool
- func (c *Collector) For(slug string) (*Snapshot, string)
- func (c *Collector) Latest(slug string) *Snapshot
- func (c *Collector) Run(ctx context.Context, slug string) (*Snapshot, error)
- func (c *Collector) Series(slug, class string, days int) ([]Point, error)
- func (c *Collector) Start()
- func (c *Collector) Stop()
- func (c *Collector) Trend(slug, classIRI string, days int) (*Trend, error)
- type Point
- type Results
- type Snapshot
- type Term
- type Trend
Constants ¶
const ( MethodStats = "stats" // GraphDB index statistics (approximate) MethodCount = "count" // live COUNT(*) MethodTimeout = "timeout" // timed out, or the run budget was spent MethodError = "error" )
Methods recorded per count.
const ( TrendWidth = 100 TrendHeight = 30 )
const ClassTreeQuery = `` /* 385-byte string literal not displayed */
ClassTreeQuery lists the declared classes that have instances — directly or through a subclass, so the hierarchy stays connected — with their labels (all languages) and parents: 589 classes in 0.36 s on LINDAS, where "every class in use" (SELECT DISTINCT ?class { ?s a ?class }) times out. Classes used without being declared rdfs:Class / owl:Class are not found here; the daily run adds them (queryTypesInUse) and /api/class-tree appends the ones it counted.
The variable names are Graph Explorer's classTreeQuery bindings, so the /api/class-tree route can hand the result to GE's own tree builder.
const DropThreshold = 0.2
DropThreshold flags a class that lost more than this share overnight — usually a failed import rather than real data loss.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Change ¶
type Change struct {
Class string `json:"class"`
Before *int64 `json:"before"` // nil: new class
After *int64 `json:"after"` // nil: vanished
Delta int64 `json:"delta"`
Pct *float64 `json:"pct,omitempty"` // relative change; nil for new classes
Drop bool `json:"drop"` // shrank by more than DropThreshold
}
Change is one class that differs between two snapshots.
type Changes ¶
type Changes struct {
From string `json:"from"` // "" when there is no older snapshot yet
To string `json:"to"`
TriplesBefore *int64 `json:"triplesBefore"`
TriplesAfter *int64 `json:"triplesAfter"`
Classes int `json:"classes"`
Changes []Change `json:"changes"`
}
Changes compares the latest snapshot with the latest one at least `days` days older. Sorted by the size of the change, largest first.
type ClassCount ¶
ClassCount is one class and its instance count.
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector runs the daily counts and answers questions about them.
func New ¶
func New(cfg *config.ApplicationConfig, dataDir string) (*Collector, error)
New opens (or creates) dataDir/classstats.db and loads the latest snapshots.
func (*Collector) ClassLabels ¶ added in v0.4.3
func (c *Collector) ClassLabels(ctx context.Context, ep *config.SparqlEndpoint, iris []string) (*Results, error)
ClassLabels fetches the rdfs:labels (all languages) of classes, as ?class ?label rows — for the undeclared types /api/class-tree appends. Unlabelled classes have no row.
func (*Collector) ClassTree ¶
ClassTree runs ClassTreeQuery against the endpoint (for /api/class-tree).
func (*Collector) Collect ¶
func (c *Collector) Collect(ch chan<- prometheus.Metric)
Collect implements prometheus.Collector.
func (*Collector) Describe ¶
func (c *Collector) Describe(ch chan<- *prometheus.Desc)
Describe implements prometheus.Collector.
func (*Collector) For ¶
For resolves an endpoint (any slug) to the snapshot that serves it — its own or its class_stats_from source — and that source's slug.
func (*Collector) Latest ¶
Latest is the endpoint's own latest snapshot (nil if none). Callers must not modify it.
func (*Collector) Series ¶
Series is one class's daily counts, oldest first, over the last `days` days.
func (*Collector) Start ¶
func (c *Collector) Start()
Start launches the scheduler: a catch-up run for every endpoint whose last run is older than a day, then one run per day at 07:00 Swiss time.
func (*Collector) Stop ¶
func (c *Collector) Stop()
Stop ends the scheduler; a run in progress stops at its next class.
type Point ¶
type Point struct {
Day string `json:"day"`
Count *int64 `json:"count"` // nil: not counted that day (timeout / error)
}
Point is one day of a series.
type Results ¶
type Results struct {
Head struct {
Vars []string `json:"vars"`
} `json:"head"`
Results struct {
Bindings []map[string]Term `json:"bindings"`
} `json:"results"`
}
Results is a SPARQL 1.1 JSON result document.
type Snapshot ¶
type Snapshot struct {
Day string // YYYY-MM-DD (UTC) of the run
Counts map[string]int64 // class IRI → instances (classes with a count only)
Triples *int64 // store size, nil when unknown
Engine string // "graphdb" (statistics) or "sparql" (live counts)
Started time.Time
Finished time.Time
Classes int // classes listed
Failures int // classes without a count (timeout / error)
}
Snapshot is one endpoint's latest completed run.
func (*Snapshot) Top ¶
func (s *Snapshot) Top(limit int) []ClassCount
Top is the snapshot's classes by count, largest first, at most limit.
type Term ¶
type Term struct {
Type string `json:"type"`
Value string `json:"value"`
Lang string `json:"xml:lang,omitempty"`
Datatype string `json:"datatype,omitempty"`
}
Term is one RDF term of a SPARQL JSON result.
type Trend ¶
type Trend struct {
Path string // SVG path data; a failed day (nil count) starts a new segment
EndX float64 // the latest counted point, drawn as a dot
EndY float64
Days int // window length
From string // oldest counted day in the window
To string // latest counted day
First int64 // count on From
Last int64 // count on To
Pct *float64 // change From→To in percent; nil with a single point or First == 0
Dir int // sign of Pct after rounding to one decimal: 1, -1 or 0
}
Trend is the geometry of a metric card's sparkline: one class's daily counts over a window, laid out in a TrendWidth × TrendHeight viewBox. The SVG markup lives in the sparqlMetricValue template; only the numbers are computed here.