Files
wahrwelt-a-d-setup/components/neo/internal/server/logs/logline.go
T

73 lines
1.8 KiB
Go

package server
import (
"fmt"
"strconv"
"github.com/mitchellh/mapstructure"
logspb "github.com/c4t-but-s4d/neo/v2/proto/go/logs"
)
func NewLogLineFromValues(vals map[string]interface{}) (*LogLine, error) {
var line LogLine
if err := mapstructure.Decode(vals, &line); err != nil {
return nil, fmt.Errorf("разбор структуры строки журнала: %w", err)
}
return &line, nil
}
func NewLogLineFromProto(p *logspb.LogLine) *LogLine {
return &LogLine{
Exploit: p.Exploit,
Version: strconv.FormatInt(p.Version, 10),
Message: p.Message,
Level: p.Level,
Team: p.Team,
}
}
type LogLine struct {
Exploit string `mapstructure:"exploit"`
Version string `mapstructure:"version"`
Message string `mapstructure:"message"`
Level string `mapstructure:"level"`
Team string `mapstructure:"team"`
}
func (l *LogLine) String() string {
return fmt.Sprintf("Line(%s.v%s)", l.Exploit, l.Version)
}
func (l *LogLine) DumpValues() (map[string]interface{}, error) {
res := make(map[string]interface{})
if err := mapstructure.Decode(l, &res); err != nil {
return nil, fmt.Errorf("кодирование структуры строки журнала: %w", err)
}
return res, nil
}
func (l *LogLine) EstimateSize() int {
const (
estNum = 5
estDenom = 4
structSize = 8 * 5
)
sizeEst := structSize + len(l.Exploit) + len(l.Version) + len(l.Message) + len(l.Level) + len(l.Team)
return sizeEst * estNum / estDenom
}
func (l *LogLine) ToProto() (*logspb.LogLine, error) {
version, err := strconv.ParseInt(l.Version, 10, 64)
if err != nil {
return nil, fmt.Errorf("преобразование версии %v: %w", l.Version, err)
}
return &logspb.LogLine{
Exploit: l.Exploit,
Version: version,
Message: l.Message,
Level: l.Level,
Team: l.Team,
}, nil
}