metrics

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package metrics provides a formula registry for computing derived signal metrics.

The registry allows pluggable Compute functions for metrics like frustration, momentum, reach, and urgency. Each formula is registered by name and can be retrieved and executed against a set of signals.

Example:

import "github.com/plexusone/omnisignal/metrics"

// Compute frustration for a signal group
formula := metrics.Get("frustration")
result, err := formula.Compute(ctx, signals, metrics.Options{
    Weights: map[string]float64{"support_ticket": 1.5},
})

Index

Constants

View Source
const DefaultMomentumWindowDays = 30

Variables

View Source
var (
	ErrFormulaNotFound = errors.New("formula not found")
	ErrNoSignals       = errors.New("no signals provided")
	ErrInvalidWeight   = errors.New("invalid weight value")
)

Common errors.

View Source
var DefaultFrustrationWeights = map[string]float64{
	"support_ticket":      1.0,
	"cloud_incident":      2.0,
	"security_finding":    1.5,
	"posture_drift":       0.5,
	"alert":               0.5,
	"outage":              3.0,
	"vulnerability":       1.0,
	"feedback":            0.8,
	"enhancement_request": 0.5,
	"competitive_gap":     0.3,
	"competitor_launch":   0.2,
	"analyst_finding":     0.2,
	"market_observation":  0.1,
}

Default weights by signal type for frustration score.

DefaultSeverityWeights maps severity levels to weights.

Functions

func ClearRegistry

func ClearRegistry()

ClearRegistry removes all formulas. Primarily for testing.

func IsRegistered

func IsRegistered(name string) bool

IsRegistered checks if a formula is registered.

func List

func List() []string

List returns all registered formula names in alphabetical order.

func Register

func Register(f Formula)

Register adds a formula to the global registry. Later registrations with the same name override earlier ones.

func RegisterBuiltins

func RegisterBuiltins()

RegisterBuiltins re-registers all built-in formulas. Use after ClearRegistry() in tests that need the standard formulas.

func Unregister

func Unregister(name string)

Unregister removes a formula from the registry.

Types

type Config

type Config struct {
	// Weights overrides default weights for signal types, sources, or severities.
	Weights map[string]float64 `json:"weights,omitempty"`

	// WindowDays is the default trailing window for time-bounded metrics.
	WindowDays int `json:"window_days,omitempty"`

	// Extra holds formula-specific configuration.
	Extra map[string]any `json:"extra,omitempty"`
}

Config holds metrics configuration including weight overrides.

func ConfigFromProviderOptions

func ConfigFromProviderOptions(opts map[string]any) Config

ConfigFromProviderOptions extracts metrics Config from omnisignal.Config.Options. Looks for "metrics" key containing a Config-shaped object.

func LoadConfig

func LoadConfig(path string) (Config, error)

LoadConfig reads metrics configuration from a JSON file.

func LoadConfigOrDefault

func LoadConfigOrDefault(path string) Config

LoadConfigOrDefault reads metrics configuration from a JSON file. If the file doesn't exist, returns an empty Config (no error).

func (Config) Merge

func (c Config) Merge(other Config) Config

Merge combines two Configs. Values from other override values from c.

func (Config) ToOptions

func (c Config) ToOptions() Options

ToOptions converts Config to Options for use with Compute.

type Formula

type Formula interface {
	// Name returns the metric identifier.
	Name() string

	// Description returns a brief explanation of what the metric measures.
	Description() string

	// Compute calculates the metric for the given signals.
	Compute(ctx context.Context, signals []signal.Signal, opts Options) (Result, error)
}

Formula defines a pluggable metric computation.

func Get

func Get(name string) Formula

Get retrieves a formula by name. Returns nil if not found.

func MustGet

func MustGet(name string) Formula

MustGet retrieves a formula, panicking if not found.

func NewFormula

func NewFormula(name, description string, fn func(ctx context.Context, signals []signal.Signal, opts Options) (Result, error)) Formula

NewFormula creates a Formula from a function.

type FormulaFunc

type FormulaFunc struct {
	// contains filtered or unexported fields
}

FormulaFunc adapts a function to the Formula interface.

func (FormulaFunc) Compute

func (f FormulaFunc) Compute(ctx context.Context, signals []signal.Signal, opts Options) (Result, error)

Compute delegates to the wrapped function.

func (FormulaFunc) Description

func (f FormulaFunc) Description() string

Description returns a brief explanation.

func (FormulaFunc) Name

func (f FormulaFunc) Name() string

Name returns the metric identifier.

type Options

type Options struct {
	// Weights overrides default weights by signal type or source.
	// Keys can be signal types (e.g., "support_ticket") or source names
	// (e.g., "pagerduty"). More specific keys take precedence.
	Weights map[string]float64

	// Now overrides the current time for age calculations.
	// Zero value uses time.Now().
	Now time.Time

	// WindowDays limits signals to those observed within this many days.
	// Zero means no time window filter.
	WindowDays int

	// Extra holds formula-specific options.
	Extra map[string]any
}

Options configures metric computation.

func (Options) GetNow

func (o Options) GetNow() time.Time

GetNow returns the effective "now" time for the options.

func (Options) GetWeight

func (o Options) GetWeight(typ signal.Type, sourceName string, defaultWeight float64) float64

GetWeight returns the weight for a signal, checking type then source. Returns defaultWeight if no override is found.

type Result

type Result struct {
	// Name is the metric name (e.g., "frustration", "momentum").
	Name string

	// Value is the computed metric value.
	Value float64

	// Breakdown provides per-component contributions to the value.
	// Keys are component identifiers (signal types, sources, etc.).
	Breakdown map[string]float64

	// ComputedAt is when this result was generated.
	ComputedAt time.Time

	// SignalCount is the number of signals used in computation.
	SignalCount int

	// Metadata holds formula-specific additional data.
	Metadata map[string]any
}

Result holds the output of a metric computation.

func Compute

func Compute(ctx context.Context, name string, signals []signal.Signal, opts Options) (Result, error)

Compute is a convenience function that gets a formula and computes it. Returns ErrFormulaNotFound if the formula doesn't exist.

func ComputeAll

func ComputeAll(ctx context.Context, signals []signal.Signal, opts Options) ([]Result, []error)

ComputeAll runs all registered formulas and returns their results. Errors from individual formulas are collected in the returned error slice.

Jump to

Keyboard shortcuts

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