feat:data debug config.
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"hr_receiver/config"
|
||||
"hr_receiver/models"
|
||||
"hr_receiver/mqtt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type mqttListenerConfigRequest struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
ExpireDays int `json:"expireDays"`
|
||||
DeleteExpired bool `json:"deleteExpired"`
|
||||
}
|
||||
|
||||
// @Summary 获取MQTT监听存储配置
|
||||
// @Description 获取测量数据监听启用状态、过期天数和过期删除开关
|
||||
// @Tags 系统调试
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {object} SwagAPIResponse "查询成功"
|
||||
// @Router /admin/system-debug/mqtt/listener-config [get]
|
||||
func (sc *SystemDebugController) GetMqttListenerConfig(c *gin.Context) {
|
||||
writeSuccess(c, http.StatusOK, "query success", mqtt.GetListenerStorageConfig())
|
||||
}
|
||||
|
||||
// @Summary 更新MQTT监听存储配置
|
||||
// @Description 更新测量数据监听启用状态、过期天数和过期删除开关
|
||||
// @Tags 系统调试
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {object} SwagAPIResponse "更新成功"
|
||||
// @Router /admin/system-debug/mqtt/listener-config [put]
|
||||
func (sc *SystemDebugController) UpdateMqttListenerConfig(c *gin.Context) {
|
||||
var payload mqttListenerConfigRequest
|
||||
if err := c.ShouldBindJSON(&payload); err != nil {
|
||||
writeError(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
cfg, err := mqtt.UpdateListenerStorageConfig(config.DB, mqtt.ListenerStorageConfig{
|
||||
Enabled: payload.Enabled,
|
||||
ExpireDays: payload.ExpireDays,
|
||||
DeleteExpired: payload.DeleteExpired,
|
||||
})
|
||||
if err != nil {
|
||||
writeError(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
writeSuccess(c, http.StatusOK, "update success", cfg)
|
||||
}
|
||||
|
||||
func StartMqttMeasurementCleanupJob(db *gorm.DB) {
|
||||
go func() {
|
||||
runCleanup := func() {
|
||||
if err := cleanupExpiredMqttMeasurementData(db); err != nil {
|
||||
log.Printf("mqtt measurement cleanup failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
runCleanup()
|
||||
|
||||
ticker := time.NewTicker(24 * time.Hour)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
runCleanup()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func cleanupExpiredMqttMeasurementData(db *gorm.DB) error {
|
||||
cfg := mqtt.GetListenerStorageConfig()
|
||||
if !cfg.DeleteExpired {
|
||||
return nil
|
||||
}
|
||||
|
||||
cutoffMillis := time.Now().AddDate(0, 0, -cfg.ExpireDays).UnixMilli()
|
||||
modelsToClean := []interface{}{
|
||||
&models.MqttHeartRateRecord{},
|
||||
&models.MqttStepCountRecord{},
|
||||
&models.MqttGatewayStatusRecord{},
|
||||
}
|
||||
for _, model := range modelsToClean {
|
||||
if err := db.Unscoped().Where("received_at < ?", cutoffMillis).Delete(model).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user