report

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 4 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 report is the pure energy report model (spec 12): complete days of the site quantities go in, sums, ratios, sections, patterns and key figures (with an optional comparison period) come out. No I/O, no clock, no logging; renderers (JSON, CSV, PDF) only format what Build returns.

Every ratio is computed from the summed energy of a period, never as an average of daily ratios. A day counts for a section only with a complete value of every series the section needs; other days are reported as missing, never as 0 kWh.

Index

Constants

View Source
const (
	KeyPV          = "pv"
	KeyConsumption = "consumption"
	KeyImport      = "import"
	KeyExport      = "export"
	KeyCharge      = "charge"
	KeyDischarge   = "discharge"
)

Keys of Day.Values and Slot.Values (Wh). Per-meter keys come from MeterKey.

View Source
const KeyLoadpoint = "lp"

KeyLoadpoint is the kind of a loadpoint's energy key.

View Source
const LowImportBelow = 500

LowImportBelow (Wh) is the daily grid import below which a day counts as a day with almost no grid import: inverters and meters draw a few hundred Wh even on days the house runs on solar and battery alone, so a threshold of zero would never match.

View Source
const (
	// MaxRangeDays caps a report at about ten years (spec 12).
	MaxRangeDays = 3660
)
View Source
const (
	// MinBalanceDays is the shortest period (complete days) with a battery quotient and a
	// balance difference: the battery level at the start and end is not stored, so over a
	// few days it dominates both values (spec 12).
	MinBalanceDays = 28
)
View Source
const SlotsPerDay = 96

SlotsPerDay is the number of 15-minute slots of a day without DST change.

Variables

View Source
var ErrInvalidRange = errors.New("invalid report parameters")

ErrInvalidRange: bad from/to, granularity or comparison parameters (400).

Functions

func MeterKey

func MeterKey(kind, id string) string

MeterKey returns the key of one meter's energy, e.g. MeterKey(KeyPV, "roof").

func ParseDay

func ParseDay(s string) (time.Time, error)

ParseDay parses a YYYY-MM-DD day as midnight UTC (the calendar day of Range).

Types

type Bucket

type Bucket struct {
	// Key is YYYY-MM-DD, YYYY-MM or YYYY.
	Key string
	// From and To are the bucket's days inside the range.
	From, To time.Time
	// Partial is true when the range cuts the bucket (a month or year not fully inside).
	Partial bool
	Summary Summary
}

Bucket is one group of days (a day, month or year) of the range.

type Category

type Category struct {
	Category   string
	In, Solar  float64
	SolarShare *float64
}

Category sums the loadpoints of one category.

type CompareInput

type CompareInput struct {
	Mode  CompareMode
	Range Range
	Days  []Day
}

CompareInput is the comparison period and its days.

type CompareMode

type CompareMode int

CompareMode selects the comparison period.

const (
	// CompareNone disables the comparison.
	CompareNone CompareMode = iota
	// ComparePrevious compares with the period directly before.
	ComparePrevious
	// CompareLastYear compares with the same dates one year earlier.
	CompareLastYear
)

func ParseCompare

func ParseCompare(s string) (CompareMode, error)

ParseCompare parses "previous", "last_year" or "none"; "" is the default last_year (seasons make the previous period a poor comparison for most ranges).

func (CompareMode) Of

func (m CompareMode) Of(r Range) (cmp Range, ok bool)

Of returns the comparison range of r; ok is false for CompareNone.

func (CompareMode) String

func (m CompareMode) String() string

String returns the API name of m.

type Comparison

type Comparison struct {
	Mode    CompareMode
	Range   Range
	Summary Summary
	// Matched is true when the periods cannot be compared as wholes (missing days or a
	// different length): the figures then compare only the days both periods have.
	Matched     bool
	MatchedDays int
}

Comparison is the comparison period.

type Day

type Day struct {
	// Date is the local calendar day as midnight UTC (calendar math without DST).
	Date time.Time
	// Values holds the series that are complete on this day (Wh); a missing key means
	// incomplete or no data.
	Values map[string]float64
	// Slots are the time-resolved sums of the day; nil when a bucket has a gap, so the
	// estimates fall back to the daily balance.
	Slots *DaySlots
}

Day is one local calendar day.

type DaySlots

type DaySlots struct {
	// GridCharge = Σ min(charge, import): battery charge that came from the grid.
	GridCharge float64
	// Solar is the loadpoint energy covered by PV, Σ loadpoint × solar share.
	Solar map[string]float64
	// Profile is the consumption per slot of day (Wh, a repeated DST hour adds up),
	// Count the number of buckets per slot.
	Profile [SlotsPerDay]float64
	Count   [SlotsPerDay]int
	// Peak is the highest 15-minute mean consumption (W) at PeakAt.
	Peak   float64
	PeakAt time.Time
}

DaySlots are the time-resolved sums of one day (spec 12, time-resolved estimates).

func EstimateDay

func EstimateDay(slots []Slot, loadpoints []string) *DaySlots

EstimateDay sums the slots of one day; nil without slots or when any slot has a gap. loadpoints are the loadpoint IDs.

type DayValue

type DayValue struct {
	Date  time.Time
	Value float64
}

DayValue is one day and a value of it.

type Direction

type Direction int

Direction says which change of a figure is an improvement.

const (
	// Neutral: neither direction is better (consumption, export, battery).
	Neutral Direction = iota
	// HigherIsBetter: an increase is an improvement.
	HigherIsBetter
	// LowerIsBetter: a decrease is an improvement.
	LowerIsBetter
)

func (Direction) String

func (d Direction) String() string

String returns the API name of d: higher, lower or neutral.

type Energy

type Energy struct {
	PV, Consumption, Import, Export, Charge, Discharge float64
}

Energy holds the site quantities of one day or a sum of days (Wh).

type Figure

type Figure struct {
	// ID names the figure (renderers map it to a label).
	ID     string
	Kind   FigureKind
	Better Direction
	// Value is the range's value over all its complete days; nil = no data.
	Value *float64
	// Compared and Base are the two values the change is computed from: the range's and
	// the comparison period's. With Comparison.Matched both cover only the matched days
	// (so Compared can differ from Value); otherwise Compared equals Value and Base is
	// the whole comparison period. Nil without a comparison or without data.
	Compared, Base *float64
	// Change = Compared - Base: Wh, or a fraction for ratios (x100 = percentage points).
	Change *float64
	// ChangePct is Change / base for energy figures (a fraction); nil when base is 0.
	ChangePct *float64
}

Figure is one key figure of the report.

type FigureKind

type FigureKind int

FigureKind says how a key figure is measured and compared.

const (
	// KindEnergy is Wh; changes in Wh and percent.
	KindEnergy FigureKind = iota
	// KindRatio is a share 0..1; changes in percentage points (as a fraction).
	KindRatio
)

func (FigureKind) String

func (k FigureKind) String() string

String returns the API name of k: energy or ratio.

type Granularity

type Granularity int

Granularity is how buckets group the days of a range.

const (
	// ByDay is one bucket per day.
	ByDay Granularity = iota
	// ByMonth is one bucket per calendar month.
	ByMonth
	// ByYear is one bucket per calendar year.
	ByYear
)

func AutoGranularity

func AutoGranularity(r Range) Granularity

AutoGranularity groups up to ~2 months by day, up to 2 years by month, longer by year.

func ParseGranularity

func ParseGranularity(s string, r Range) (Granularity, error)

ParseGranularity parses "day", "month" or "year"; "" picks AutoGranularity(r).

func (Granularity) String

func (g Granularity) String() string

String returns the API name of g.

type Input

type Input struct {
	Range       Range
	Granularity Granularity
	Layout      Layout
	// Days of the range (others are ignored), any order.
	Days []Day
	// Compare is the comparison period; nil = no comparison.
	Compare *CompareInput
	// TodayExcluded says the requested range ended today and was cut to end yesterday.
	TodayExcluded bool
	// Quality lists the meters' data quality in the range.
	Quality []MeterQuality
}

Input is everything Build needs.

type Layout

type Layout struct {
	// PV are the top-level PV meters; empty = no PV.
	PV []Meter
	// Batteries are the batteries; empty = no battery section.
	Batteries []Meter
	// Loadpoints are the consumers directly under the site.
	Loadpoints []Meter
}

Layout is the part of the site that decides which sections exist (spec 12).

func (Layout) HasBattery

func (l Layout) HasBattery() bool

HasBattery reports whether the site has a battery.

func (Layout) HasPV

func (l Layout) HasPV() bool

HasPV reports whether the site has PV.

type Loadpoints

type Loadpoints struct {
	Section
	Categories []Category
}

Loadpoints is the loadpoint section.

type Meter

type Meter struct {
	ID, Label, Category string
	// KWp is the peak power of a PV meter (0 = unknown).
	KWp float64
}

Meter is a meter a section reports on.

type MeterEnergy

type MeterEnergy struct {
	Meter
	// In / Out (Wh): battery charged / discharged, PV yield in Out, loadpoint in In.
	In, Out float64
	// Share of the section total (PV); Solar ≈ PV-covered energy and SolarShare
	// (loadpoints); Ratio = Out / In (battery, from MinBalanceDays); SpecificYield =
	// kWh/kWp (PV with kwp).
	Share, SolarShare, Ratio, SpecificYield *float64
	Solar                                   float64
}

MeterEnergy is the energy of one meter over the days its section counts.

type MeterQuality

type MeterQuality struct {
	ID, Label string
	// MissingDays have no complete value; EstimatedDays contain estimated buckets.
	MissingDays, EstimatedDays int
}

MeterQuality is the data quality of one meter in the range (spec 12, data quality).

type Patterns

type Patterns struct {
	// Extremes; nil without complete days. Ties go to the earliest day.
	BestPV, LowestPV, MaxConsumption, MaxImport *DayValue // values in Wh
	// WeekdayConsumption / WeekendConsumption are the average consumption per complete
	// Monday-Friday / Saturday-Sunday; nil without such days.
	WeekdayConsumption, WeekendConsumption *float64
	WeekdayDays, WeekendDays               int
	// LowImportDays imported less than LowImportBelow from the grid.
	LowImportDays int
}

Patterns are day-level observations of the complete days of the range.

type Profile

type Profile struct {
	Mean   [SlotsPerDay]*float64
	Peak   *float64
	PeakAt time.Time
}

Profile is the load profile: mean consumption per slot of day (W, nil without data) and the highest 15-minute mean.

type Range

type Range struct {
	From, To time.Time
}

Range is an inclusive range of calendar days, each as midnight UTC.

func ParseRange

func ParseRange(from, to, today string) (Range, error)

ParseRange parses from/to (YYYY-MM-DD, inclusive) and checks from <= to <= today and the maximum length. today is the caller's local day (server TZ).

func (Range) Contains

func (r Range) Contains(day time.Time) bool

Contains reports whether day lies in r.

func (Range) Days

func (r Range) Days() int

Days returns the number of calendar days in r.

func (Range) Finished

func (r Range) Finished(today time.Time) (out Range, excluded bool, err error)

Finished returns r without today. Today's counters are still running, so a report covers finished days only: a range ending today ends yesterday instead (excluded is true). A range of today alone has no finished day and wraps ErrInvalidRange.

func (Range) String

func (r Range) String() string

String formats r as "YYYY-MM-DD..YYYY-MM-DD".

type Report

type Report struct {
	Range       Range
	Granularity Granularity
	Layout      Layout
	// Summary covers the days with every core series.
	Summary  Summary
	Patterns Patterns
	// Buckets group the range by Granularity, oldest first, empty ones included.
	Buckets []Bucket
	// Sections; nil when the site has nothing to show (spec 12 table).
	PVSources  *Section
	Batteries  *Section
	Loadpoints *Loadpoints
	Profile    Profile
	// Comparison is nil without a comparison period.
	Comparison *Comparison
	Figures    []Figure
	// MissingDays are the days without every core series (at most maxListedDays).
	MissingDays []time.Time
	Quality     []MeterQuality
	// TodayExcluded is true when the requested range ended today and the report ends
	// yesterday.
	TodayExcluded bool
}

Report is the computed report.

func Build

func Build(in Input) Report

Build computes the report.

type Section

type Section struct {
	CompleteDays int
	Meters       []MeterEnergy
}

Section is a list of meters over the days with every series of the section.

type Slot

type Slot struct {
	At     time.Time // local start of the slot
	Values map[string]float64
	Gap    bool
}

Slot is one 15-minute bucket of the site (Wh per key); Gap marks a missing or incomplete value of any needed series.

type Split

type Split struct {
	Solar, Battery, Grid                float64
	SolarShare, BatteryShare, GridShare *float64
}

Split divides one energy amount into three parts (Wh) with their shares (0..1). Sources: Grid = imported, Battery = discharged minus the grid charge (bought energy counts once, as grid), Solar = the rest; they add up to the consumption. PV split: Solar = used directly, Battery = charged from PV, Grid = exported.

type Summary

type Summary struct {
	// Days is the number of calendar days, CompleteDays those with every core series.
	Days, CompleteDays int
	Total              Energy
	// PerDay is Total / CompleteDays; nil without complete days.
	PerDay *Energy
	// SelfSufficiency = (consumption − import) / consumption; SelfConsumption =
	// (PV − export) / PV; 0..1, nil without a denominator.
	SelfSufficiency, SelfConsumption *float64
	// GridCharge ≈ battery charge from the grid (time-resolved, spec 12).
	GridCharge float64
	// Sources: where the consumption came from; PVSplit: where the PV went (≈).
	Sources, PVSplit Split
	// BatteryRatio = discharged / charged (a plain quotient, never an efficiency);
	// BalanceDiff = energy in − energy out (Wh), BalanceDiffShare = BalanceDiff / in.
	// Nil below MinBalanceDays complete days.
	BatteryRatio, BalanceDiff, BalanceDiffShare *float64
	// FallbackDays had a gap, so their estimates come from the daily balance.
	FallbackDays int
}

Summary is the evaluation of the complete days of one period.

func (Summary) Complete

func (s Summary) Complete() bool

Complete reports whether every day of the period has data.

Directories

Path Synopsis
Package csv writes a report's buckets as CSV: one row per day, month or year with the site quantities and the derived columns (this app's estimates end in _est_kwh).
Package csv writes a report's buckets as CSV: one row per day, month or year with the site quantities and the derived columns (this app's estimates end in _est_kwh).
Package pdf renders a report as an A4 PDF (spec 12): a summary page with key figures, energy charts and where the energy came from and went, the sections the site has (battery, PV sources, loadpoints, load profile), patterns, comparison and data quality, a compact table and a last page on how the report is calculated.
Package pdf renders a report as an A4 PDF (spec 12): a summary page with key figures, energy charts and where the energy came from and went, the sections the site has (battery, PV sources, loadpoints, load profile), patterns, comparison and data quality, a compact table and a last page on how the report is calculated.

Jump to

Keyboard shortcuts

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