dbquery

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package dbquery は走行直後にアプリの DB へ SQL を流し、その結果を収集グループに紐づけて保存する。

アクセスログからは「何が得点になったか」が分からないことが多い(金額・距離などはリクエストの 本文や DB にしかない)。走行で書き込まれた行を集計すれば得点の内訳や推移を復元できるが、 次の走行の initialize で消えるので、走行直後に吸い出して残しておく。

どの SQL を流すかは回ごとに違うので dbquery.yml に置く。ここでは結果を解釈しない (列名と行をそのまま保存する)。解釈は analysis パッケージが行う。

Index

Constants

View Source
const GroupIdLayout = "2006-01-02_15-04-05.999999"

GroupIdLayout は group.Collector が GroupId を作るときの書式。

Variables

This section is empty.

Functions

func Expand

func Expand(q string, since, until int64) string

Expand は {{since}} / {{until}} を置換する。

func HashConfig added in v0.3.0

func HashConfig(raw []byte) string

HashConfig は設定ファイルの中身を短いハッシュにする(SHA-256 の先頭12桁)。

func NewHandler

func NewHandler(opts *collect.Options, store storage.Storage) (*handler, error)

Types

type Config

type Config struct {
	// DSN は go-sql-driver/mysql の形式。observer からはトンネル越しに繋ぐ。
	DSN string `yaml:"dsn"`
	// Timeout は1クエリあたりの上限。既定 30s。
	Timeout string  `yaml:"timeout,omitempty"`
	Queries []Query `yaml:"queries"`
}

Config は dbquery.yml。

func ParseConfig

func ParseConfig(raw []byte) (*Config, error)

type Query

type Query struct {
	Name string `yaml:"name"`
	SQL  string `yaml:"sql"`
}

Query は1本の SQL。SQL 中の {{since}} / {{until}} は走行区間の unix 秒に置換される。

type QueryResult

type QueryResult struct {
	Name    string
	Columns []string
	// Rows は各セルを文字列で持つ。数値かどうかの解釈は読む側に任せる。
	Rows  [][]*string
	Error string `json:",omitempty"`
	// Elapsed はクエリにかかった秒数。重いクエリを dbquery.yml から外す判断に使う。
	Elapsed float64
}

type Result

type Result struct {
	Since int64
	Until int64
	// ConfigHash は実行したときの dbquery.yml のハッシュ(HashConfig)。
	// 走行間の比較で、SQL の定義が違う走行どうしを比べていないかを確かめるのに使う。
	ConfigHash string `json:",omitempty"`
	Queries    []QueryResult
}

Result は snapshot の本体として保存される JSON。

func Run

func Run(cfg *Config, since, until int64) (*Result, error)

Run は全クエリを流す。1本が失敗しても他は続け、失敗は QueryResult.Error に残す。 DB に繋がらないときだけ error を返す。

Jump to

Keyboard shortcuts

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