stats

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package stats reads the console's audit log and counts the refusals by code and by the token or node that triggered them, so that grammar work starts with the constructs users hit most. It reads a local file and never touches the network.

The export is the one form meant to leave the machine: it holds counts, codes, engine names and SQL shapes, statements whose literals, identifiers and comments are replaced by fixed markers. Every string in it comes from a closed vocabulary (ShapeToken), so it holds no literal, identifier or refusal message of the log.

Index

Constants

View Source
const (
	// Uncoded groups the refusals of a log written before refusal codes.
	Uncoded = "uncoded"
	// NoToken groups the refusals that point at no token.
	NoToken = "-"
	// MaxShape bounds a shape, in bytes.
	MaxShape = 300

	// ExportNote heads an export.
	ExportNote = "locksql refusal statistics: SQL shapes only, literals, identifiers and comments removed"
)

Variables

This section is empty.

Functions

func ExportToken

func ExportToken(tok string) string

ExportToken is a refusal token as an export may hold it: a keyword, a known function name or punctuation stays (upper-cased), any other word becomes "id", and no token is NoToken.

func Shape

func Shape(sql string) string

Shape replaces the literals, identifiers and comments of sql by markers and keeps its keywords, known function names and punctuation, one token per space. It reads any text, valid SQL or not, and never returns a byte of a literal, an identifier or a comment.

func ShapeToken

func ShapeToken(tok string) bool

ShapeToken reports whether tok belongs to the closed vocabulary of shapes: a marker, a keyword, a known function name or punctuation.

Types

type Count

type Count struct {
	Code  string `json:"code"`
	Token string `json:"token,omitempty"`
	N     int    `json:"n"`
}

Count is the number of refusals of a code, and of a token under it.

type Export

type Export struct {
	Version     string       `json:"locksql_version"`
	Note        string       `json:"note"`
	Refusals    int          `json:"refusals"`
	Skipped     int          `json:"skipped"`
	ByCode      []Count      `json:"by_code"`
	ByConstruct []Count      `json:"by_construct"`
	Shapes      []ShapeCount `json:"shapes"`
}

Export is the anonymised report.

type Report

type Report struct {
	// Refusals is the number of refusal records, Skipped the number of
	// lines that are not audit records.
	Refusals, Skipped int
	ByCode            []Count
	// ByToken counts per code and token (upper-cased; NoToken when the
	// refusal points at none).
	ByToken []Count
	// contains filtered or unexported fields
}

Report counts the refusals of an audit log.

func Read

func Read(r io.Reader) (*Report, error)

Read reads an audit log and counts its refusals.

func (*Report) WriteExport

func (rep *Report) WriteExport(w io.Writer, version string) error

WriteExport writes the anonymised report as JSON. version is the locksql version, kept only when it is a plain version string.

func (*Report) WriteText

func (rep *Report) WriteText(w io.Writer, top int)

WriteText writes the report for the console account's human: every code, then the top tokens.

type ShapeCount

type ShapeCount struct {
	Code   string `json:"code"`
	Engine string `json:"engine"`
	Shape  string `json:"shape"`
	N      int    `json:"n"`
}

ShapeCount is the number of refusals of one code, engine and shape.

Jump to

Keyboard shortcuts

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