repo

package
v0.0.0-...-7eaa09d Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: BSD-3-Clause Imports: 17 Imported by: 0

README

Repo

This package aggregates queries and stores them into a PG database.

Helpful DB Queries

Show all queries by database ordered by the most queried.

select d.name as db_name, sum(total_count) as tc, sum(total_duration) as td
from databases d join queries q ON d.uid = q.database_uid
group by d.uid
order by tc desc;

Show all queries by query ordered by the most queries in a specific database.

select masked_query, sum(total_count) as tc, sum(total_duration) as td
from databases d join queries q ON d.uid = q.database_uid
where d.name = 'datname'
group by q.uid
order by tc desc;

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Env        = "dev"
	DBHost     = "localhost"
	DBPort     = "5432"
	DBUser     = "postgres"
	DBPassword = ""
	DBName     = "lantern"
)

Default config values. These can be overwritten by the params passed in.

View Source
var UuidNamespace = uuid.MustParse("018e1b50-ee98-73f2-9839-420223323163")

UuidNamespace is the namespace for the uuid. There are 4 predefined namespaces, but you can also create your own.

Functions

func Conn

func Conn() *sql.DB

Conn returns a connection to the database.

func DisplayConfig

func DisplayConfig()

func ExecuteQuery

func ExecuteQuery(db *sql.DB, sql string)

func HasErr

func HasErr(msg string, err error) bool

func MarshalJSON

func MarshalJSON(data interface{}) string

func OverrideConfig

func OverrideConfig(strArgs map[string]*string, intArgs map[string]*int, boolArgs map[string]*bool)

func Ping

func Ping(db *sql.DB)

func ShaQuery

func ShaQuery(query string) string

ShaQuery creates a sha of the query

func UnmarshalJSON

func UnmarshalJSON(data []byte, v interface{})

func UuidFromString

func UuidFromString(uid string) uuid.UUID

func UuidString

func UuidString(query string) string

func UuidV5

func UuidV5(str string) uuid.UUID

Types

type Databases

type Databases struct {
	Databases map[string]uuid.UUID `json:"databases,omitempty"` // the key is the UUIDv5 sha of the database
}

func NewDatabases

func NewDatabases() *Databases

func (*Databases) Upsert

func (d *Databases) Upsert()

type Queries

type Queries struct {
	Queries             map[string]*Query                         `json:"queries,omitempty"`
	FunctionsInQueries  map[string]*extractor.FunctionsInQueries  `json:"functions_in_queries,omitempty"`
	ColumnsInQueries    map[string]*extractor.ColumnsInQueries    `json:"columns_in_queries,omitempty"`
	TablesInQueries     map[string]*extractor.TablesInQueries     `json:"tables_in_queries,omitempty"`
	TableJoinsInQueries map[string]*extractor.TableJoinsInQueries `json:"table_joins_in_queries,omitempty"`
}

func NewQueries

func NewQueries() *Queries

NewQueries creates a new Queries struct

func (*Queries) Analyze

func (q *Queries) Analyze(w QueryWorker) bool

Analyze processes a query and returns a bool whether or not the query was parsed successfully

func (*Queries) ExtractStats

func (q *Queries) ExtractStats()

func (*Queries) Process

func (q *Queries) Process() bool

func (*Queries) Upsert

func (q *Queries) Upsert()

func (*Queries) UpsertColumnsInQueries

func (q *Queries) UpsertColumnsInQueries()

func (*Queries) UpsertTablesInQueries

func (q *Queries) UpsertTablesInQueries()

type Query

type Query struct {
	UID           uuid.UUID       `json:"uid,omitempty"`            // unique sha of the query
	DatabaseUID   uuid.UUID       `json:"database_uid,omitempty"`   // the dataset the query belongs to
	SourceUID     uuid.UUID       `json:"source_uid,omitempty"`     // the source the query belongs to
	Command       token.TokenType `json:"command,omitempty"`        // the type of query
	TotalCount    int64           `json:"total_count,omitempty"`    // the number of times the query was executed
	TotalDuration int64           `json:"total_duration,omitempty"` // the total duration of all executions of the query in microseconds
	MaskedQuery   string          `json:"masked_query,omitempty"`   // the query with parameters masked
	UnmaskedQuery string          `json:"unmasked_query,omitempty"` // the query with parameters unmasked
	SourceQuery   string          `json:"source,omitempty"`         // the original query from the source
}

func (*Query) MarshalJSON

func (q *Query) MarshalJSON() ([]byte, error)

func (*Query) Process

func (q *Query) Process(w QueryWorker, qs *Queries) bool

Process processes a query and returns a bool whether or not the query was parsed successfully

func (*Query) UnmarshalJSON

func (q *Query) UnmarshalJSON(data []byte) error

type QueryWorker

type QueryWorker struct {
	Databases   *Databases
	Source      *Source
	SourceUID   uuid.UUID
	Database    string
	DatabaseUID uuid.UUID
	Input       string // Original query. This may contain many queries
	Duration    int64
	MustExtract bool
	Command     token.TokenType
	Masked      string // Masked query. This is the query with all values replaced with ?
	Unmasked    string // Unmasked query. This is the query with all values left alone
}

This is an interim struct with additional Query meta data. The struct is passed around and built up instead of passing around a ton of individual variables. This is used both initially when compiling a list of queries and then individually when processing each query

type Source

type Source struct {
	UID  uuid.UUID `json:"uid,omitempty"`  // unique UUID of the source
	Name string    `json:"name,omitempty"` // the name of the source
	URL  string    `json:"url,omitempty"`  // the url of the source
}

func NewSource

func NewSource(name, url string) *Source

type Sources

type Sources struct {
	Sources map[string]*Source `json:"sources,omitempty"` // the key is the sha of the database
}

func NewSources

func NewSources() *Sources

func (*Sources) Add

func (d *Sources) Add(name, url string) *Source

func (*Sources) Upsert

func (d *Sources) Upsert()

Jump to

Keyboard shortcuts

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