datasettify + add config options
Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
parent
846813120b
commit
e684049b1b
4 changed files with 40 additions and 4 deletions
|
@ -1,7 +1,6 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/codemicro/analytics/analytics/config/internal/debug"
|
||||
)
|
||||
|
||||
|
@ -28,8 +27,6 @@ func Load() (*Config, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println(cl.rawConfigFileContents)
|
||||
|
||||
conf := new(Config)
|
||||
conf.Ingest.Address = asString(cl.withDefault("ingest.address", "127.0.0.1:7500"))
|
||||
conf.HTTP.Address = asString(cl.withDefault("http.address", "127.0.0.1:8080"))
|
||||
|
|
|
@ -13,6 +13,17 @@ func init() {
|
|||
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
|
||||
logger.Info().Msg("up")
|
||||
|
||||
if _, err := db.NewCreateTable().Model(&models.Config{}).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := db.NewInsert().Model(&[]*models.Config{
|
||||
{ID: "session_inactive_after", Value: "+2 hours"},
|
||||
{ID: "prune_requests_after", Value: "+14 days"},
|
||||
}).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := db.NewCreateTable().Model(&models.Session{}).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -20,10 +31,28 @@ func init() {
|
|||
return err
|
||||
}
|
||||
|
||||
views := []string{
|
||||
`CREATE VIEW "sessions_with_last_seen" AS SELECT *, (SELECT MAX("time") FROM "requests" WHERE "session_id" = "session"."id") AS "last_seen" FROM "sessions" AS "session";`,
|
||||
`CREATE VIEW "active_sessions" AS SELECT * FROM "sessions_with_last_seen" WHERE datetime() < datetime("last_seen", (SELECT "value" FROM "config" WHERE id='session_inactive_after'));`,
|
||||
`CREATE VIEW "top_urls" AS SELECT "host", "uri", COUNT(*) as "count" FROM "requests" GROUP BY "host", "uri" ORDER BY "count" DESC;`,
|
||||
`CREATE VIEW "top_user_agents" AS SELECT "user_agent", COUNT(*) AS "count" FROM "requests" GROUP BY "user_agent" ORDER BY "count" DESC;`,
|
||||
`CREATE VIEW "top_ip_addresses" AS SELECT "ip_addr", COUNT(*) AS "count" FROM "requests" GROUP BY "id_addr" ORDER BY "count" DESC;`,
|
||||
`CREATE VIEW "top_referers" AS SELECT "referer", COUNT(*) AS "count" FROM "requests" GROUP BY "referer" ORDER BY "count" DESC;`,
|
||||
}
|
||||
|
||||
for _, query := range views {
|
||||
if _, err := db.Exec(query); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}, func(ctx context.Context, db *bun.DB) error {
|
||||
logger.Info().Msg("down")
|
||||
|
||||
if _, err := db.NewDropTable().Model(&models.Config{}).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.NewDropTable().Model(&models.Request{}).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
10
analytics/db/models/config.go
Normal file
10
analytics/db/models/config.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package models
|
||||
|
||||
import "github.com/uptrace/bun"
|
||||
|
||||
type Config struct {
|
||||
bun.BaseModel `bun:"config"`
|
||||
|
||||
ID string `bun:",pk"`
|
||||
Value string
|
||||
}
|
|
@ -16,7 +16,7 @@ func (i *Ingest) assignToSession(tx bun.Tx, request *models.Request) (*models.Se
|
|||
Model(sess).
|
||||
Where("ip_addr = ?", request.IPAddr).
|
||||
Where("user_agent = ?", request.UserAgent).
|
||||
Where(`? < datetime((select max("time") as "time" from requests where session_id = "session"."id"), '+2 hours')`, request.Time).
|
||||
Where(`? < datetime((select max("time") as "time" from requests where session_id = "session"."id"), (select value from config where id='session_inactive_after'))`, request.Time).
|
||||
Scan(context.Background(), sess)
|
||||
if err != nil {
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
|
|
Reference in a new issue