diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e33e2b7..2f15c2d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Starting from v2.2.5, all notable changes to this project will be documented in this file. +## v2.12.0 + +- Support lifecycle configuration for records in database + ## v2.11.10 - Set minimum TLS version to 1.2 for HTTP request utility @@ -9,7 +13,7 @@ Starting from v2.2.5, all notable changes to this project will be documented in ## v2.11.9 -- Show free disk space in banner instead of station UUID +- Show disk free space in banner instead of station UUID ## v2.11.8 diff --git a/VERSION b/VERSION index 637f933c..9f94b801 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v2.11.10 +v2.12.0 diff --git a/build/assets/config.json b/build/assets/config.json index 31dce63c..7a5f9bc1 100644 --- a/build/assets/config.json +++ b/build/assets/config.json @@ -39,7 +39,8 @@ "port": 5432, "username": "postgres", "password": "passw0rd", - "database": "observer" + "database": "observer", + "lifecycle": 10 }, "server_settings": { "host": "0.0.0.0", diff --git a/config/types.go b/config/types.go index 95062e31..6863f4fc 100644 --- a/config/types.go +++ b/config/types.go @@ -39,13 +39,14 @@ type ntpclient struct { } type archiver struct { - Engine string `json:"engine"` - Enable bool `json:"enable"` - Host string `json:"host"` - Port int `json:"port"` - Username string `json:"username"` - Password string `json:"password"` - Database string `json:"database"` + Engine string `json:"engine"` + Enable bool `json:"enable"` + Host string `json:"host"` + Port int `json:"port"` + LifeCycle int `json:"lifecycle"` + Username string `json:"username"` + Password string `json:"password"` + Database string `json:"database"` } type server struct { diff --git a/driver/dao/delete.go b/driver/dao/delete.go new file mode 100644 index 00000000..357010e3 --- /dev/null +++ b/driver/dao/delete.go @@ -0,0 +1,8 @@ +package dao + +import "gorm.io/gorm" + +func Delete(db *gorm.DB, start, end int64) error { + var records []dbRecord + return db.Table(DB_TABLENAME).Where("ts >= ? AND ts <= ?", start, end).Delete(&records).Error +} diff --git a/feature/archiver/cleanup.go b/feature/archiver/cleanup.go new file mode 100644 index 00000000..14d71856 --- /dev/null +++ b/feature/archiver/cleanup.go @@ -0,0 +1,32 @@ +package archiver + +import ( + "time" + + "github.com/anyshake/observer/driver/dao" + "github.com/anyshake/observer/publisher" + "github.com/anyshake/observer/utils/duration" + "gorm.io/gorm" +) + +func (a *Archiver) handleCleanup(status *publisher.Status, db *gorm.DB, lifeCycle int) { + for { + // Wait until system is ready + if status.ReadyTime.IsZero() { + time.Sleep(time.Second) + continue + } + + // Get start and end time + currentTime, _ := duration.Timestamp(status.System.Offset) + endTime := currentTime.Add(-time.Duration(lifeCycle) * time.Hour * 24) + + // Remove expired records + err := dao.Delete(db, 0, endTime.UnixMilli()) + if err != nil { + a.OnError(nil, err) + } + + time.Sleep(time.Hour) + } +} diff --git a/feature/archiver/daemon.go b/feature/archiver/daemon.go index 7a27ca50..be76e15b 100644 --- a/feature/archiver/daemon.go +++ b/feature/archiver/daemon.go @@ -46,6 +46,12 @@ func (a *Archiver) Run(options *feature.FeatureOptions, waitGroup *sync.WaitGrou } options.Database = pdb + // Start cleanup routine if life cycle bigger than 0 + lifeCycle := options.Config.MiniSEED.LifeCycle + if lifeCycle > 0 { + go a.handleCleanup(options.Status, pdb, lifeCycle) + } + // Archive when new message arrived expressionForSubscribe := true go func() { diff --git a/frontend/dist/asset-manifest.json b/frontend/dist/asset-manifest.json index de782575..d687eb08 100644 --- a/frontend/dist/asset-manifest.json +++ b/frontend/dist/asset-manifest.json @@ -1,7 +1,7 @@ { "files": { "main.css": "./static/css/main.7a040865.css", - "main.js": "./static/js/main.f9ed9c3f.js", + "main.js": "./static/js/main.6ea9d68f.js", "static/css/594.d6bfd15f.chunk.css": "./static/css/594.d6bfd15f.chunk.css", "static/js/594.7755917b.chunk.js": "./static/js/594.7755917b.chunk.js", "static/js/846.88ce4ddb.chunk.js": "./static/js/846.88ce4ddb.chunk.js", @@ -49,6 +49,6 @@ }, "entrypoints": [ "static/css/main.7a040865.css", - "static/js/main.f9ed9c3f.js" + "static/js/main.6ea9d68f.js" ] } \ No newline at end of file diff --git a/frontend/dist/index.html b/frontend/dist/index.html index 8e7fdb5a..69260c49 100644 --- a/frontend/dist/index.html +++ b/frontend/dist/index.html @@ -1 +1 @@ -