app/history/history.go

118 lines
1.9 KiB
Go

package history
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
"kumoly.io/kumoly/app/errors"
"kumoly.io/kumoly/app/store"
)
var PROD = false
const (
ERROR = "ERROR"
INFO = "INFO"
)
type History struct {
ID uint `gorm:"primaryKey"`
CreatedAt time.Time
Module string
Type string
Caller string
Trace string
BodyJson string
Name string
Message string
Body interface{} `gorm:"-"`
Issuer string
// Group to identify auth, empty for admin
Group string
// Scope for users to narrow down view point
Scope string
}
func (h *History) BeforeCreate(tx *gorm.DB) (err error) {
if h.Body != nil {
body, err := json.Marshal(h.Body)
if err != nil {
h.BodyJson = string(body)
}
}
return
}
var Tunnel chan *History
var quit chan struct{}
var started chan struct{}
var wg sync.WaitGroup
func init() {
Tunnel = make(chan *History)
quit = make(chan struct{}, 1)
started = make(chan struct{}, 1)
}
func Start(r Receiver) {
if len(started) == 1 {
panic(errors.New(500, "history reporter has already started!"))
}
started <- struct{}{}
go func() {
for {
select {
case h := <-Tunnel:
wg.Add(1)
if Interceptor != nil {
Interceptor(h)
}
r(h)
wg.Done()
case <-quit:
<-started
return
}
}
}()
}
func Stop() {
quit <- struct{}{}
wg.Wait()
}
func Send(h *History) {
if len(started) == 0 {
log.Warn().Str("mod", "history").
Interface("history", h).
Msg("history reporter has not started, report will be discarded")
}
if h.Type == "" {
h.Type = INFO
}
Tunnel <- h
}
type Receiver func(*History)
var Interceptor func(*History) = nil
var DBReceiver Receiver = func(h *History) {
err := store.DB.Create(h).Error
if err != nil {
log.Error().Str("mod", "history").
Interface("history", h).
Err(err).Msg("DBReceiver error")
}
}
var ConsoleReceiver Receiver = func(h *History) {
fmt.Printf("%+v\n", h)
}