Updated plugin interface

- Updated plugin interface to support static path routing
- Added autosave for statistic data (workaround for #561)
This commit is contained in:
Toby Chui
2025-03-02 09:15:50 +08:00
parent b7e3888513
commit 39d6d16c2a
36 changed files with 1398 additions and 149 deletions

View File

@@ -50,6 +50,8 @@ type CollectorOption struct {
type Collector struct {
rtdataStopChan chan bool
autoSaveTicker *time.Ticker
autSaveStop chan bool
DailySummary *DailySummary
Option *CollectorOption
}
@@ -78,6 +80,35 @@ func NewStatisticCollector(option CollectorOption) (*Collector, error) {
return &thisCollector, nil
}
// Set the autosave duration, the collector will save the daily summary to database
// set saveInterval to 0 to disable autosave
func (c *Collector) SetAutoSave(saveInterval int) {
//Stop the current ticker if exists
if c.autSaveStop != nil {
c.autSaveStop <- true
}
if saveInterval == 0 {
return
}
c.autSaveStop = make(chan bool)
ticker := time.NewTicker(time.Duration(saveInterval) * time.Second)
c.autoSaveTicker = ticker
go func() {
for {
select {
case <-ticker.C:
c.SaveSummaryOfDay()
case <-c.autSaveStop:
ticker.Stop()
return
}
}
}()
}
// Write the current in-memory summary to database file
func (c *Collector) SaveSummaryOfDay() {
//When it is called in 0:00am, make sure it is stored as yesterday key
@@ -122,7 +153,6 @@ func (c *Collector) Close() {
//Write the buffered data into database
c.SaveSummaryOfDay()
}
// Main function to record all the inbound traffics