From c6c9015cd3dead4aa4a7aac727ce1efba5f202a9 Mon Sep 17 00:00:00 2001 From: yarnom Date: Thu, 10 Jul 2025 12:19:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E9=9B=A8=E9=87=8F?= =?UTF-8?q?=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/db.go | 25 ++++++++++++------------- models/models.go | 4 ++-- static/index.html | 10 +++++----- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/db/db.go b/db/db.go index dc7bdec..f40b4a9 100644 --- a/db/db.go +++ b/db/db.go @@ -3,12 +3,11 @@ package db import ( "database/sql" "fmt" - _ "gi - "log" -/go-sql-driver/mysql" - - "rain_monitor/models" _ "github.com/go-sql-driver/mysql" + "log" + "math" + "rain_monitor/models" + "time" ) var db *sql.DB @@ -93,29 +92,29 @@ func SaveRainGaugeData(data *models.RainGaugeData) (int64, error) { FROM rain_gauge_data WHERE timestamp BETWEEN ? AND ? ORDER BY timestamp DESC + `, fiveMinutesAgo, data.Timestamp) - if err != nil { log.Printf("查询最近雨量计数据失败: %v", err) // 即使查询失败,我们仍然尝试插入新数据 } else { + defer rows.Close() - // 检查是否有相似数据 for rows.Next() { var id int64 var ts time.Time + var rainfall float64 - if err := rows.Scan(&id, &ts, &rainfall); err != nil { log.Printf("扫描雨量计数据失败: %v", err) continue + } - // 如果时间非常接近(小于1分钟)且雨量数据相同或非常接近,则认为是重复数据 timeDiff := data.Timestamp.Sub(ts) + rainfallDiff := math.Abs(data.TotalRainfall - rainfall) - if timeDiff < time.Minute && rainfallDiff < 0.1 { log.Printf("检测到重复的雨量计数据,跳过插入。ID=%d, 时间=%v", id, ts) return id, nil // 返回已存在的记录ID @@ -255,11 +254,11 @@ func GetAggregatedData(start, end time.Time) ([]models.AggregatedData, error) { if err != nil { log.Printf("扫描行数据失败: %v", err) return nil, fmt.Errorf("扫描聚合数据失败: %v", err) + } - // 设置格式化时间字符串 + data.FormattedTime = timestampStr - // 尝试解析时间字符串为time.Time类型 timestamp, err := time.Parse("2006-01-02 15:04:05", timestampStr) if err != nil { @@ -267,8 +266,8 @@ func GetAggregatedData(start, end time.Time) ([]models.AggregatedData, error) { // 即使解析失败,也继续处理,前端会使用FormattedTime } else { data.Timestamp = timestamp + } - result = append(result, data) } diff --git a/models/models.go b/models/models.go index 983e2bb..d776de4 100644 --- a/models/models.go +++ b/models/models.go @@ -160,7 +160,7 @@ LIMIT 1 const QueryAggregatedDataSQL = ` SELECT w.time_hour as timestamp, - MAX(r.daily_rainfall) as rainfall, + MAX(r.hourly_rainfall) as rainfall, AVG(w.temperature) as avg_temperature, AVG(w.humidity) as avg_humidity, AVG(w.wind_speed) as avg_wind_speed, @@ -176,7 +176,7 @@ FROM LEFT JOIN (SELECT DATE_FORMAT(timestamp, '%Y-%m-%d %H:00:00') as time_hour, - daily_rainfall + hourly_rainfall FROM rain_gauge_data WHERE timestamp BETWEEN ? AND ? ) r ON w.time_hour = r.time_hour diff --git a/static/index.html b/static/index.html index 2035d75..d5f8909 100644 --- a/static/index.html +++ b/static/index.html @@ -247,7 +247,7 @@ 时间 - 降雨量(mm) + 小时降雨量(mm) 温度(℃) 湿度(%) 风速(m/s) @@ -600,7 +600,7 @@ datasets: [ { type: 'bar', - label: '降雨量(mm)', + label: '小时降雨量(mm)', data: data.map(item => item.rainfall !== null && !isNaN(item.rainfall) ? item.rainfall : 0), backgroundColor: 'rgba(54, 162, 235, 0.5)', borderColor: 'rgba(54, 162, 235, 1)', @@ -630,7 +630,7 @@ position: 'left', title: { display: true, - text: '降雨量(mm)' + text: '小时降雨量(mm)' }, grid: { drawOnChartArea: false @@ -715,13 +715,13 @@ function exportData() { // 从表格中获取完整数据 const tableRows = document.querySelectorAll('#tableBody tr'); - let csv = '时间,降雨量(mm),温度(℃),湿度(%),风速(m/s),大气压(kPa),太阳辐射(W/m²)\n'; + let csv = '时间,小时降雨量(mm),温度(℃),湿度(%),风速(m/s),大气压(kPa),太阳辐射(W/m²)\n'; tableRows.forEach(row => { const cells = row.querySelectorAll('td'); const rowData = [ cells[0].textContent, // 时间 - cells[1].textContent, // 降雨量 + cells[1].textContent, // 小时降雨量 cells[2].textContent, // 温度 cells[3].textContent, // 湿度 cells[4].textContent, // 风速