Skip to content

Commit a3a277c

Browse files
committed
Add check for existing logs before updating cl.Logs
1 parent c1c6dae commit a3a277c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

pkg/schema/v1/container.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,19 @@ func (cl *ContainerLog) syncContainerLogs(ctx context.Context, clientset *kubern
129129
currentTimeMillis := time.Now().UnixMilli()
130130
periodStartMillis := currentTimeMillis - (currentTimeMillis % (3600 * 1000))
131131
cl.Period = types.UnixMilli(time.UnixMilli(periodStartMillis))
132-
cl.Logs += string(logs)
132+
133+
// Check if logs for the current period already exist
134+
existingLog := &ContainerLog{}
135+
err = db.Get(existingLog, "SELECT * FROM container_log WHERE container_id = ? AND pod_id = ? AND period = ?", cl.ContainerId, cl.PodId, cl.Period)
136+
if errors.Is(err, sql.ErrNoRows) {
137+
// No existing logs for this period, insert new log
138+
cl.Logs = string(logs)
139+
} else if err != nil {
140+
return err
141+
} else {
142+
// Existing logs found for this period, concatenate logs
143+
cl.Logs += string(logs)
144+
}
133145
entities := make(chan interface{}, 1)
134146
entities <- cl
135147
close(entities)

0 commit comments

Comments
 (0)