add container prefix and registry stuff; provide Uid() for track; access track (including container) via UID
This commit is contained in:
parent
324c9b41c7
commit
9bb027ed4c
3 changed files with 54 additions and 12 deletions
|
@ -54,14 +54,20 @@ func StoreDB(db *sql.Storage, msg lib.Message) {
|
|||
|
||||
var container_definition *tracking.ContDef
|
||||
|
||||
const type_prefix = "msg"
|
||||
|
||||
func init() {
|
||||
hf := lib.StrSlice{"domain", "action", "class", "item"}
|
||||
ixs := []lib.StrSlice{hf}
|
||||
ixs = append(ixs, lib.StrSlice{"domain", "class", "item"})
|
||||
container_definition = (&tracking.ContDef{
|
||||
container_definition = &tracking.ContDef{
|
||||
Prefix: type_prefix,
|
||||
ContFactory: Messages,
|
||||
ItemFactory: MakeMessage,
|
||||
TableName: "messages",
|
||||
HeadFields: hf,
|
||||
IdFields: hf,
|
||||
Indexes: ixs,
|
||||
}).SetUp()
|
||||
}
|
||||
tracking.RegisterContainerDef(container_definition)
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
sqllib "database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
|
@ -14,6 +16,7 @@ import (
|
|||
sql "git.sr.ht/~cco/go-scopes/storage"
|
||||
)
|
||||
|
||||
type ContFactory func(*sql.Storage) *Container
|
||||
type ItemFactory func(*Container, ...string) *Track
|
||||
|
||||
// basic track implementation
|
||||
|
@ -34,6 +37,10 @@ func (tr *Track) Container() *Container {
|
|||
return tr.container
|
||||
}
|
||||
|
||||
func (tr *Track) Uid() string {
|
||||
return fmt.Sprintf("%s-%d", tr.container.Prefix, tr.trackId)
|
||||
}
|
||||
|
||||
func MakeTrack(cont *Container, h ...string) *Track {
|
||||
tr := Track{
|
||||
Head: lib.StrMap{},
|
||||
|
@ -78,6 +85,8 @@ func (tr *Track) ScanP(rows *sql.Rows) error {
|
|||
// basic container implementation
|
||||
|
||||
type ContDef struct {
|
||||
Prefix string
|
||||
ContFactory ContFactory
|
||||
ItemFactory ItemFactory
|
||||
TableName string
|
||||
HeadFields lib.StrSlice
|
||||
|
@ -85,13 +94,6 @@ type ContDef struct {
|
|||
Indexes []lib.StrSlice
|
||||
}
|
||||
|
||||
func (cd *ContDef) SetUp() *ContDef {
|
||||
if cd.IdFields == nil {
|
||||
cd.IdFields = cd.HeadFields
|
||||
}
|
||||
return cd
|
||||
}
|
||||
|
||||
type Container struct {
|
||||
*ContDef
|
||||
Storage *sql.Storage
|
||||
|
@ -263,6 +265,10 @@ func ParseDateTime(inp string) *time.Time {
|
|||
if err == nil {
|
||||
return &ts
|
||||
}
|
||||
ts, err = time.Parse("2006-01-02 15:04:05 -0700 MST", inp)
|
||||
if err == nil {
|
||||
return &ts
|
||||
}
|
||||
log.Error(err).Msg("storage.tracking.ParseDateTime")
|
||||
return nil
|
||||
}
|
||||
|
@ -276,18 +282,45 @@ func (cont *Container) CreateTable() {
|
|||
}
|
||||
}
|
||||
|
||||
// container definition
|
||||
// container registration
|
||||
|
||||
var container_registry map[string]*ContDef
|
||||
|
||||
func RegisterContainerDef(cd *ContDef) {
|
||||
container_registry[cd.Prefix] = cd
|
||||
}
|
||||
|
||||
func GetContainerDef(p string) *ContDef {
|
||||
return container_registry[p]
|
||||
}
|
||||
|
||||
func Get(db *sql.Storage, uid string) *Track {
|
||||
parts := strings.Split(uid, "-")
|
||||
cd := GetContainerDef(parts[0])
|
||||
cont := cd.ContFactory(db)
|
||||
id, _ := strconv.ParseUint(parts[1], 10, 64)
|
||||
return cont.Get(lib.Ident(id))
|
||||
}
|
||||
|
||||
// container definition for the tracking package
|
||||
|
||||
var container_definition *ContDef
|
||||
|
||||
const type_prefix = "rec"
|
||||
|
||||
func init() {
|
||||
container_registry = map[string]*ContDef{}
|
||||
hf := lib.StrSlice{"taskId", "userName"}
|
||||
ixs := []lib.StrSlice{hf}
|
||||
ixs = append(ixs, lib.StrSlice{"userName"})
|
||||
container_definition = (&ContDef{
|
||||
container_definition = &ContDef{
|
||||
Prefix: type_prefix,
|
||||
ContFactory: Tracks,
|
||||
ItemFactory: MakeTrack,
|
||||
TableName: "tracks",
|
||||
HeadFields: hf,
|
||||
IdFields: hf,
|
||||
Indexes: ixs,
|
||||
}).SetUp()
|
||||
}
|
||||
RegisterContainerDef(container_definition)
|
||||
}
|
||||
|
|
|
@ -78,6 +78,8 @@ func TrackingTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
|||
t.AssertEqual(tr3.Head["taskId"], "t01")
|
||||
tr3.Data = lib.Map{"desc": "go-scopes documentation"}
|
||||
cont.Update(tr3)
|
||||
tr4 := tracking.Get(db, "rec-1")
|
||||
t.AssertEqual(tr4.Data["desc"], "go-scopes documentation")
|
||||
}
|
||||
|
||||
func MessageTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
||||
|
@ -88,6 +90,7 @@ func MessageTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
|||
msg := message.New(cont, "", "data")
|
||||
t.AssertEqual(msg.TrackId(), lib.Ident(1))
|
||||
t.AssertEqual(msg.Domain(), "scopes")
|
||||
t.AssertEqual(msg.Uid(), "msg-1")
|
||||
}
|
||||
|
||||
func resetSqlite(db *sql.Storage) {
|
||||
|
|
Loading…
Add table
Reference in a new issue