refactor: move models to their own package to use the same as in the importer

This commit is contained in:
Patryk Hegenberg 2025-09-24 22:57:37 +02:00
parent 553a85562b
commit 9aa1b7384d
14 changed files with 170 additions and 152 deletions

View file

@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"time"
"tixel_watch/models"
)
type ServiceMonitor struct {
@ -23,7 +24,7 @@ func NewServiceMonitor(config ServiceConfig) *ServiceMonitor {
}
}
func (sm *ServiceMonitor) Start(ctx context.Context, out chan<- LogEntry) error {
func (sm *ServiceMonitor) Start(ctx context.Context, out chan<- models.LogMessage) error {
args := sm.buildJournalctlArgs()
slog.Info("starting journalctl", "arguments", args)
@ -116,13 +117,13 @@ func NewJournalEntryParser(serviceName, unitName string) *JournalEntryParser {
}
}
func (jep *JournalEntryParser) Parse(jsonLine string) (LogEntry, error) {
func (jep *JournalEntryParser) Parse(jsonLine string) (models.LogMessage, error) {
var journalData map[string]any
if err := json.Unmarshal([]byte(jsonLine), &journalData); err != nil {
return LogEntry{}, fmt.Errorf("JSON unmarshal error: %w", err)
return models.LogMessage{}, fmt.Errorf("JSON unmarshal error: %w", err)
}
entry := NewLogEntry("service_log")
entry := models.NewLogMessage("service_log", hostname)
entry.Service = jep.serviceName
entry.Unit = jep.unitName
@ -147,7 +148,6 @@ func (jep *JournalEntryParser) Parse(jsonLine string) (LogEntry, error) {
if pidStr, ok := journalData["_PID"].(string); ok {
if pid, err := strconv.Atoi(pidStr); err == nil {
entry.PID = pid
entry.SyslogInfo.ProcessInfo = strconv.FormatInt(int64(pid), 10)
}
}
@ -188,7 +188,7 @@ func (jep *JournalEntryParser) getPriorityName(priority string) string {
return "unknown"
}
func (jep *JournalEntryParser) extractSystemdFields(journalData map[string]any, entry *LogEntry) {
func (jep *JournalEntryParser) extractSystemdFields(journalData map[string]any, entry *models.LogMessage) {
systemdFields := []string{
"_SYSTEMD_UNIT", "_SYSTEMD_USER_UNIT", "_SYSTEMD_SLICE",
"_BOOT_ID", "_MACHINE_ID", "_HOSTNAME", "_TRANSPORT",
@ -201,15 +201,12 @@ func (jep *JournalEntryParser) extractSystemdFields(journalData map[string]any,
for _, field := range systemdFields {
if value, ok := journalData[field]; ok {
esFieldName := strings.ToLower(strings.TrimPrefix(field, "_"))
if entry.SyslogInfo.Fields == nil {
entry.SyslogInfo.Fields = make(map[string]any)
}
entry.SyslogInfo.Fields[esFieldName] = value
entry.Fields[esFieldName] = value
}
}
}
func (jep *JournalEntryParser) parseServiceSpecific(entry LogEntry) LogEntry {
func (jep *JournalEntryParser) parseServiceSpecific(entry models.LogMessage) models.LogMessage {
switch jep.serviceName {
case "tixstream":
return parseTixstreamService(entry)
@ -230,7 +227,7 @@ var (
amServicePattern = regexp.MustCompile(`^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z)\s+(\w+)\s+(\d+)\s+---\s+\[\s*([^\]]*)\]\s+([\w\.]+)\s*:\s*(.*)$`)
tccServicePattern = regexp.MustCompile(`^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z)\s+(\w+)\s+(\d+)\s+---\s+\[\s*([^\]]*)\]\s+([\w\.]+)\s*:\s*(.*)$`)
tjmServicePattern = regexp.MustCompile(`^(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\s+(?<level>\S+)\s+(?<pid>\d+).*?\[(?<collatation_id>[^\]]*)\]\s+\[(?<username>[^\]]*)\]\s+\[(?<thread>[^\]]*)\]\s+(?<class>.*?)\s+:\s+(?<message>.*)`)
tjmTransferNamePattern = regexp.MustCompile(`^(\d{8}T\d{6}-[A-Za-z0-9]+-[A-Za-z]+-(?:in|out)) ?: (.*)$`)
tjmTransferNamePattern = regexp.MustCompile(`^(\d{8}T\d{6}-[A-Za-z0-9]+-.+?-(?:in|out)) ?: (.*)$`)
tsServicePattern = regexp.MustCompile(`^(?<level>\S+)\s+(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6})\s+(?<message>.*)`)
tsTransferIDPattern = regexp.MustCompile(`^(?<transfer>\w{8}-\w{4}-\w{4}-\w{4}-\w{12})\s+(?<message>.*)`)
tjmTransferIDPattern1 = regexp.MustCompile(`(?P<transfer>\w{8}-\w{4}-\w{4}-\w{4}-\w{12}).*?(?P<message>.*)`)
@ -242,9 +239,9 @@ var (
nginxAccessPattern = regexp.MustCompile(`^(\S+)\s+\S+\s+(\S+)\s+\[([^\]]+)\]\s+"([^"]+)"\s+(\d+)\s+(\d+|-)\s*(?:"([^"]*)"\s+"([^"]*)")?`)
)
func parseTixstreamService(entry LogEntry) LogEntry {
func parseTixstreamService(entry models.LogMessage) models.LogMessage {
newEntry := entry
var baseInfo TSTransferInfo
var baseInfo models.TSTransferInfo
matches := tsServicePattern.FindStringSubmatch(newEntry.LogMessage)
if len(matches) > 0 {
@ -349,9 +346,9 @@ func parseTixstreamService(entry LogEntry) LogEntry {
return newEntry
}
func parseTJMService(entry LogEntry) LogEntry {
func parseTJMService(entry models.LogMessage) models.LogMessage {
newEntry := entry
var baseInfo TJMTransferInfo
var baseInfo models.TJMTransferInfo
logContent := entry.LogMessage
msg := strings.TrimSpace(logContent)
@ -375,7 +372,7 @@ func parseTJMService(entry LogEntry) LogEntry {
}
newEntry.LogLevel = strings.TrimSpace(matches[2])
newEntry.LogMessage = strings.TrimSpace(matches[8])
baseInfo = TJMTransferInfo{
baseInfo = models.TJMTransferInfo{
ProcessID: strings.TrimSpace(matches[3]),
CorrelationID: strings.TrimSpace(matches[4]),
Username: strings.TrimSpace(matches[5]),
@ -420,7 +417,7 @@ func parseTJMService(entry LogEntry) LogEntry {
return newEntry
}
func parseAMService(entry LogEntry) LogEntry {
func parseAMService(entry models.LogMessage) models.LogMessage {
newEntry := entry
logContent := newEntry.LogMessage
@ -436,7 +433,7 @@ func parseAMService(entry LogEntry) LogEntry {
}
newEntry.Timestamp = timeParsed
}
baseInfo := AMBaseInfo{
baseInfo := models.AMBaseInfo{
ProcessID: matches[3],
ThreadID: strings.TrimSpace(matches[4]),
LoggerName: matches[5],
@ -448,7 +445,7 @@ func parseAMService(entry LogEntry) LogEntry {
return newEntry
}
func parseTCCService(entry LogEntry) LogEntry {
func parseTCCService(entry models.LogMessage) models.LogMessage {
newEntry := entry
logContent := newEntry.LogMessage
@ -464,7 +461,7 @@ func parseTCCService(entry LogEntry) LogEntry {
}
newEntry.Timestamp = timeParsed
}
baseInfo := TCCBaseInfo{
baseInfo := models.TCCBaseInfo{
ProcessID: matches[3],
ThreadID: strings.TrimSpace(matches[4]),
LoggerName: matches[5],
@ -476,7 +473,7 @@ func parseTCCService(entry LogEntry) LogEntry {
return newEntry
}
func parseNginxService(entry LogEntry) LogEntry {
func parseNginxService(entry models.LogMessage) models.LogMessage {
newEntry := entry
matches := nginxAccessPattern.FindStringSubmatch(strings.TrimSpace(entry.LogMessage))
@ -491,7 +488,7 @@ func parseNginxService(entry LogEntry) LogEntry {
if err != nil {
slog.Error("cant parse bytessend", "error", err)
}
baseInfo := NGinXBaseInfo{
baseInfo := models.NGinXBaseInfo{
ClientIP: matches[1],
RemoteUser: matches[2],
Request: matches[4],