diff options
| -rw-r--r-- | service_report.go | 24 | ||||
| -rw-r--r-- | service_report_test.go | 30 | ||||
| -rw-r--r-- | worker.go | 7 |
3 files changed, 56 insertions, 5 deletions
diff --git a/service_report.go b/service_report.go index 2627bc2..291cd2b 100644 --- a/service_report.go +++ b/service_report.go @@ -3,9 +3,33 @@ package lilin +// historyKeepSize maximum report to keep in history. +const historyKeepSize = 120 + +// historyMax limit of history before its being purged to historyKeepSize. +const historyMax = historyKeepSize * 2 + // ServiceReport contains the scan report for single service. type ServiceReport struct { Name string Last ScanReport History []ScanReport } + +func NewServiceReport(name string) (svcReport *ServiceReport) { + svcReport = &ServiceReport{ + Name: name, + History: make([]ScanReport, 0, historyMax), + } + return svcReport +} + +func (svcReport *ServiceReport) Store(scanReport ScanReport) { + svcReport.Last = scanReport + svcReport.History = append(svcReport.History, scanReport) + if len(svcReport.History) == historyMax { + var start = len(svcReport.History) - historyKeepSize + copy(svcReport.History, svcReport.History[start:]) + svcReport.History = svcReport.History[:historyKeepSize-1] + } +} diff --git a/service_report_test.go b/service_report_test.go new file mode 100644 index 0000000..71652f6 --- /dev/null +++ b/service_report_test.go @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-only + +package lilin + +import ( + "testing" + "time" + + "git.sr.ht/~shulhan/pakakeh.go/lib/test" +) + +func TestServiceReport_push(t *testing.T) { + var svcReport = NewServiceReport(`test`) + var x int64 + for x = 1; x <= historyMax+1; x++ { + svcReport.Store(ScanReport{At: time.Unix(x, 0)}) + } + test.Assert(t, `len(History)`, historyKeepSize, len(svcReport.History)) + test.Assert(t, `cap(History)`, historyMax, cap(svcReport.History)) + + var expAtUnix int64 = int64(historyKeepSize + 1) + var gotAtUnix = svcReport.History[0].At.Unix() + test.Assert(t, `History[0].At`, expAtUnix, gotAtUnix) + + expAtUnix = int64(historyMax) + 1 + var lastIdx = len(svcReport.History) - 1 + gotAtUnix = svcReport.History[lastIdx].At.Unix() + test.Assert(t, `History[last].At`, expAtUnix, gotAtUnix) +} @@ -37,9 +37,7 @@ func newWorker(configDir string) (wrk *worker, err error) { Services: make(map[string]*ServiceReport, len(wrk.Services)), } for name := range wrk.Services { - wrk.Reports.Services[name] = &ServiceReport{ - Name: name, - } + wrk.Reports.Services[name] = NewServiceReport(name) } return wrk, nil } @@ -119,8 +117,7 @@ func (wrk *worker) start(updateq chan<- struct{}) { wrk.reportsMutex.Lock() var svcReport = wrk.Reports.Services[scanReport.Name] - svcReport.Last = scanReport - svcReport.History = append(svcReport.History, scanReport) + svcReport.Store(scanReport) wrk.reportsMutex.Unlock() updateq <- struct{}{} |
