|
@@ -366,28 +366,24 @@ func (s *CollectorService) processSourceGroup(sourceID string, devices []models.
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
now := time.Now()
|
|
|
- count := 0
|
|
|
|
|
-
|
|
|
|
|
- // Helper to Save
|
|
|
|
|
|
|
+ var toInsert []db.ReadingRecord
|
|
|
|
|
+
|
|
|
|
|
+ // Helper to collect (batch insert at end of cycle)
|
|
|
saveMetric := func(deviceID, metric string, val float64, locID string) {
|
|
saveMetric := func(deviceID, metric string, val float64, locID string) {
|
|
|
// Update Cache
|
|
// Update Cache
|
|
|
if deviceMetricValues[deviceID] == nil {
|
|
if deviceMetricValues[deviceID] == nil {
|
|
|
deviceMetricValues[deviceID] = make(map[string]float64)
|
|
deviceMetricValues[deviceID] = make(map[string]float64)
|
|
|
}
|
|
}
|
|
|
deviceMetricValues[deviceID][metric] = val
|
|
deviceMetricValues[deviceID][metric] = val
|
|
|
- // DB Insert
|
|
|
|
|
- if err := db.InsertReading(deviceID, metric, val, locID, now); err == nil {
|
|
|
|
|
- count++
|
|
|
|
|
- if s.IsDebug() {
|
|
|
|
|
- log.Printf("调试: 已保存 设备=%s 指标=%s 值=%.2f", deviceID, metric, val)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Check Alarms Async
|
|
|
|
|
- if GlobalAlarmService != nil {
|
|
|
|
|
- GlobalAlarmService.CheckRules(deviceID, metric, val)
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- log.Printf("数据库错误: 保存失败 设备=%s 指标=%s 值=%.2f: %v", deviceID, metric, val, err)
|
|
|
|
|
|
|
+ toInsert = append(toInsert, db.ReadingRecord{
|
|
|
|
|
+ DeviceID: deviceID, Metric: metric, Value: val, LocationID: locID, Ts: now,
|
|
|
|
|
+ })
|
|
|
|
|
+ if s.IsDebug() {
|
|
|
|
|
+ log.Printf("调试: 已收集 设备=%s 指标=%s 值=%.2f", deviceID, metric, val)
|
|
|
|
|
+ }
|
|
|
|
|
+ // Check Alarms Async (before batch write)
|
|
|
|
|
+ if GlobalAlarmService != nil {
|
|
|
|
|
+ GlobalAlarmService.CheckRules(deviceID, metric, val)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -505,7 +501,17 @@ func (s *CollectorService) processSourceGroup(sourceID string, devices []models.
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // Batch insert to TDengine (one REST call per ~500 records)
|
|
|
|
|
+ count := 0
|
|
|
|
|
+ if len(toInsert) > 0 {
|
|
|
|
|
+ if err := db.BatchInsertReadings(toInsert); err != nil {
|
|
|
|
|
+ log.Printf("数据库错误: 批量写入失败 %d 条: %v", len(toInsert), err)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ count = len(toInsert)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
duration := time.Since(start)
|
|
duration := time.Since(start)
|
|
|
log.Printf("Source %s: Collected %d data points for %d devices in %v", source.Name, count, len(devices), duration)
|
|
log.Printf("Source %s: Collected %d data points for %d devices in %v", source.Name, count, len(devices), duration)
|
|
|
|
|
|