Documentation
¶
Index ¶
Examples ¶
Constants ¶
View Source
const (
AgentName = "common:sql:agent/retrieval/postgres"
)
Variables ¶
View Source
var Retriever = func() *Interface { return &Interface{ Marshal: func(ctx context.Context, name, sql string, args ...any) (*bytes.Buffer, error) { if ctx == nil { ctx = context.Background() } start := time.Now().UTC() rows, err := agent.retrieve(ctx, name, sql, args) agent.log(start, time.Since(start), retrievalRouteName, newRequest(name), newResponse(agent.statusCode(err)), ctx) if err != nil { return nil, err } return Marshaler(createColumnNames(rows.FieldDescriptions()), rows) }, Scan: func(ctx context.Context, fn ScanFunc, name, sql string, args ...any) error { if ctx == nil { ctx = context.Background() } start := time.Now().UTC() rows, err := agent.retrieve(ctx, name, sql, args) agent.log(start, time.Since(start), retrievalRouteName, newRequest(name), newResponse(agent.statusCode(err)), ctx) if err != nil { return err } return Scanner(fn, createColumnNames(rows.FieldDescriptions()), rows) }, } }()
Retriever -
Functions ¶
func Marshaler ¶
Example ¶
package main
import (
bytes2 "bytes"
"encoding/json"
"fmt"
)
func _ExampleMarshaler_Error() {
buf, err := Marshaler(nil, nil)
fmt.Printf("test: Marshaler() [bytes:%v] [%v]\n", buf.Len(), err)
buf, err = Marshaler(columnNames, nil)
fmt.Printf("test: Marshaler() [bytes:%v] [%v]\n", buf.Len(), err)
buf, err = Marshaler(columnNames, newTestRows(entries))
fmt.Printf("test: Marshaler() [bytes:%v] [%v]\n", buf.Len(), err)
Output: test: Marshaler() [bytes:0] [column names list is empty]
func NewAgent ¶
Example ¶
a := newAgent()
fmt.Printf("test: newAgent() -> [%v]\n", a)
Output: test: newAgent() -> [common:sql:agent/retrieval/postgres]
func Scanner ¶
Scanner - function for scanning rows
Example ¶
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/appellative-ai/common/iox"
"time"
)
const (
EntriesPath = "file://[cwd]/retrievaltest/entry.json"
StartTimeName = "start_time"
DurationName = "duration_ms"
TrafficName = "traffic"
CreatedTSName = "created_ts"
OriginName = "origin"
RegionName = "region"
ZoneName = "zone"
SubZoneName = "sub_zone"
HostName = "host"
MethodName = "method"
UrlName = "url"
PathName = "path"
StatusCodeName = "status_code"
RouteName = "route"
)
type testRows struct {
index int
rows []testEntry
result []testEntry
}
func newTestRows(entries []testEntry) *testRows {
e := new(testRows)
if len(entries) > 0 {
e.rows = append(e.rows, entries...)
}
e.index = -1
return e
}
func (t *testRows) Close() {}
func (t *testRows) Err() error { return nil }
func (t *testRows) Next() bool {
if len(t.rows) == 0 || (t.index+1) >= len(t.rows) {
return false
}
t.index++
return true
}
func (t *testRows) Values() (result []any, err error) {
result = append(result, t.rows[t.index].StartTime)
result = append(result, t.rows[t.index].Duration) //int64 `json:"duration"`
result = append(result, t.rows[t.index].Traffic) //string `json:"traffic"`
result = append(result, t.rows[t.index].CreatedTS) //time.Time `json:"created-ts"`
result = append(result, t.rows[t.index].Origin) //string `json:"region"`
//result = append(result, t.rows[t.index].Zone) //string `json:"zone"`
result = append(result, t.rows[t.index].SubZone) //string `json:"sub-zone"`
result = append(result, t.rows[t.index].Host) //string `json:"host"`
result = append(result, t.rows[t.index].Method) //string `json:"method"`
result = append(result, t.rows[t.index].Url) //string `json:"url"`
result = append(result, t.rows[t.index].Path) //string `json:"path"`
result = append(result, t.rows[t.index].StatusCode) //int32 `json:"status-code"`
result = append(result, t.rows[t.index].Route) //string `json:"route"`
return
}
func (t *testRows) scan(columnNames []string, values []any) error {
entry := testEntry{}
for i, name := range columnNames {
switch name {
case StartTimeName:
entry.StartTime = values[i].(time.Time)
case DurationName:
entry.Duration = values[i].(int64)
case TrafficName:
entry.Traffic = values[i].(string)
case CreatedTSName:
entry.CreatedTS = values[i].(time.Time)
case OriginName:
o := values[i].(testOrigin)
entry.Origin.Region = o.Region //values[i].(string)
entry.Origin.Zone = o.Zone
/*
case RegionName:
entry.Origin.Region = values[i].(string)
case ZoneName:
entry.Origin.Zone = values[i].(string)
*/
case SubZoneName:
entry.SubZone = values[i].(string)
case HostName:
entry.Host = values[i].(string)
case MethodName:
entry.Method = values[i].(string)
case UrlName:
entry.Url = values[i].(string)
case PathName:
entry.Path = values[i].(string)
case StatusCodeName:
entry.StatusCode = values[i].(int32)
case RouteName:
entry.Route = values[i].(string)
default:
return errors.New(fmt.Sprintf("invalid field name: %v", name))
}
}
t.result = append(t.result, entry)
return nil
}
var (
entries = []testEntry{
{time.Now().UTC(), 100, "egr,,ess", time.Now().UTC(), testOrigin{
Region: "us-west", Zone: "oregon",
}, "dc1", "www.test-host.com", "GET", "https://www.google.com/search?q-golang", "/search", 200, "google-search"},
{time.Now().UTC(), 100, "egress,", time.Now().UTC(), testOrigin{
Region: "us-central", Zone: "iowa",
}, "dc1", "localhost:8081", "GET", "http://localhost:8081/advanced-go/search:google?q-golang", "/search", 200, "search"},
}
columnNames = []string{
StartTimeName, DurationName, TrafficName, CreatedTSName,
OriginName,
//RegionName, ZoneName,
SubZoneName, HostName,
MethodName, UrlName, PathName, StatusCodeName, RouteName,
}
)
type testOrigin struct {
Region string `json:"region"`
Zone string `json:"zone"`
Count int `json:"count"`
}
// testEntry - timeseries access log struct
type testEntry struct {
StartTime time.Time `json:"start-time"`
Duration int64 `json:"duration"`
Traffic string `json:"traffic"`
CreatedTS time.Time `json:"created-ts"`
//Region string `json:"region"`
//Zone string `json:"zone"`
Origin testOrigin `json:"origin"`
SubZone string `json:"sub-zone"`
Host string `json:"host"`
Method string `json:"method"`
Url string `json:"url"`
Path string `json:"path"`
StatusCode int32 `json:"status-code"`
Route string `json:"route"`
}
func main() {
rows := newTestRows(entries)
err := Scanner(rows.scan, columnNames, rows)
fmt.Printf("test: Scanner() -> [%v] [count:%v] [err:%v]\n", nil, len(rows.result), err)
}
func _ExampleMarshal() {
buf, err := json.Marshal(entries)
fmt.Printf("test: json.Marshal() -> [buf:%v] [err:%v]\n", string(buf), err)
buf, err = iox.ReadFile(EntriesPath)
fmt.Printf("test: iox.ReadFile(\"%v\") -> [err:%v]\n", EntriesPath, err)
//list2, err2 := json2.New[[]testEntry](EntriesPath, nil)
//fmt.Printf("test: json2.New() -> [len:%v] [err:%v]\n", len(list2), err2)
Output: test: Scanner() -> [<nil>] [count:2] [err:<nil>]
Types ¶
Source Files
¶
Click to show internal directories.
Click to hide internal directories.