classstats

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
const (
	TrendWidth  = 100
	TrendHeight = 30
)
View Source
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.

View Source
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

type ClassCount struct {
	Class string `json:"class"`
	Count int64  `json:"count"`
}

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) Changes

func (c *Collector) Changes(slug string, days int) (*Changes, error)

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

func (c *Collector) ClassTree(ctx context.Context, ep *config.SparqlEndpoint) (*Results, error)

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) Enabled

func (c *Collector) Enabled() bool

Enabled reports whether any endpoint collects.

func (*Collector) For

func (c *Collector) For(slug string) (*Snapshot, string)

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

func (c *Collector) Latest(slug string) *Snapshot

Latest is the endpoint's own latest snapshot (nil if none). Callers must not modify it.

func (*Collector) Run

func (c *Collector) Run(ctx context.Context, slug string) (*Snapshot, error)

Run counts one endpoint now and stores the result as today's snapshot.

func (*Collector) Series

func (c *Collector) Series(slug, class string, days int) ([]Point, error)

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.

func (*Collector) Trend

func (c *Collector) Trend(slug, classIRI string, days int) (*Trend, error)

Trend builds the sparkline for classIRI on the endpoint slug (resolved through class_stats_from) over the last `days` days. nil when the endpoint has no statistics or the class was never counted in the window.

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.

func (*Trend) PctText

func (t *Trend) PctText() string

PctText is Pct for display: one decimal, signed, "0%" when it rounds to zero.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL