fix: 修正雨量存储
This commit is contained in:
parent
0067c2d15e
commit
c6c9015cd3
25
db/db.go
25
db/db.go
@ -3,12 +3,11 @@ package db
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "gi
|
|
||||||
"log"
|
|
||||||
/go-sql-driver/mysql"
|
|
||||||
|
|
||||||
"rain_monitor/models"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"rain_monitor/models"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
@ -93,28 +92,28 @@ func SaveRainGaugeData(data *models.RainGaugeData) (int64, error) {
|
|||||||
FROM rain_gauge_data
|
FROM rain_gauge_data
|
||||||
WHERE timestamp BETWEEN ? AND ?
|
WHERE timestamp BETWEEN ? AND ?
|
||||||
ORDER BY timestamp DESC
|
ORDER BY timestamp DESC
|
||||||
|
`, fiveMinutesAgo, data.Timestamp)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("查询最近雨量计数据失败: %v", err)
|
log.Printf("查询最近雨量计数据失败: %v", err)
|
||||||
// 即使查询失败,我们仍然尝试插入新数据
|
// 即使查询失败,我们仍然尝试插入新数据
|
||||||
} else {
|
} else {
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
// 检查是否有相似数据
|
// 检查是否有相似数据
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var id int64
|
var id int64
|
||||||
var ts time.Time
|
var ts time.Time
|
||||||
|
var rainfall float64
|
||||||
|
|
||||||
if err := rows.Scan(&id, &ts, &rainfall); err != nil {
|
if err := rows.Scan(&id, &ts, &rainfall); err != nil {
|
||||||
log.Printf("扫描雨量计数据失败: %v", err)
|
log.Printf("扫描雨量计数据失败: %v", err)
|
||||||
continue
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// 如果时间非常接近(小于1分钟)且雨量数据相同或非常接近,则认为是重复数据
|
// 如果时间非常接近(小于1分钟)且雨量数据相同或非常接近,则认为是重复数据
|
||||||
timeDiff := data.Timestamp.Sub(ts)
|
timeDiff := data.Timestamp.Sub(ts)
|
||||||
|
rainfallDiff := math.Abs(data.TotalRainfall - rainfall)
|
||||||
|
|
||||||
if timeDiff < time.Minute && rainfallDiff < 0.1 {
|
if timeDiff < time.Minute && rainfallDiff < 0.1 {
|
||||||
log.Printf("检测到重复的雨量计数据,跳过插入。ID=%d, 时间=%v", id, ts)
|
log.Printf("检测到重复的雨量计数据,跳过插入。ID=%d, 时间=%v", id, ts)
|
||||||
@ -255,10 +254,10 @@ func GetAggregatedData(start, end time.Time) ([]models.AggregatedData, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("扫描行数据失败: %v", err)
|
log.Printf("扫描行数据失败: %v", err)
|
||||||
return nil, fmt.Errorf("扫描聚合数据失败: %v", err)
|
return nil, fmt.Errorf("扫描聚合数据失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// 设置格式化时间字符串
|
// 设置格式化时间字符串
|
||||||
|
data.FormattedTime = timestampStr
|
||||||
|
|
||||||
// 尝试解析时间字符串为time.Time类型
|
// 尝试解析时间字符串为time.Time类型
|
||||||
timestamp, err := time.Parse("2006-01-02 15:04:05", timestampStr)
|
timestamp, err := time.Parse("2006-01-02 15:04:05", timestampStr)
|
||||||
@ -267,7 +266,7 @@ func GetAggregatedData(start, end time.Time) ([]models.AggregatedData, error) {
|
|||||||
// 即使解析失败,也继续处理,前端会使用FormattedTime
|
// 即使解析失败,也继续处理,前端会使用FormattedTime
|
||||||
} else {
|
} else {
|
||||||
data.Timestamp = timestamp
|
data.Timestamp = timestamp
|
||||||
|
}
|
||||||
|
|
||||||
result = append(result, data)
|
result = append(result, data)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -160,7 +160,7 @@ LIMIT 1
|
|||||||
const QueryAggregatedDataSQL = `
|
const QueryAggregatedDataSQL = `
|
||||||
SELECT
|
SELECT
|
||||||
w.time_hour as timestamp,
|
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.temperature) as avg_temperature,
|
||||||
AVG(w.humidity) as avg_humidity,
|
AVG(w.humidity) as avg_humidity,
|
||||||
AVG(w.wind_speed) as avg_wind_speed,
|
AVG(w.wind_speed) as avg_wind_speed,
|
||||||
@ -176,7 +176,7 @@ FROM
|
|||||||
LEFT JOIN
|
LEFT JOIN
|
||||||
(SELECT
|
(SELECT
|
||||||
DATE_FORMAT(timestamp, '%Y-%m-%d %H:00:00') as time_hour,
|
DATE_FORMAT(timestamp, '%Y-%m-%d %H:00:00') as time_hour,
|
||||||
daily_rainfall
|
hourly_rainfall
|
||||||
FROM rain_gauge_data
|
FROM rain_gauge_data
|
||||||
WHERE timestamp BETWEEN ? AND ?
|
WHERE timestamp BETWEEN ? AND ?
|
||||||
) r ON w.time_hour = r.time_hour
|
) r ON w.time_hour = r.time_hour
|
||||||
|
|||||||
@ -247,7 +247,7 @@
|
|||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>时间</th>
|
<th>时间</th>
|
||||||
<th>降雨量(mm)</th>
|
<th>小时降雨量(mm)</th>
|
||||||
<th>温度(℃)</th>
|
<th>温度(℃)</th>
|
||||||
<th>湿度(%)</th>
|
<th>湿度(%)</th>
|
||||||
<th>风速(m/s)</th>
|
<th>风速(m/s)</th>
|
||||||
@ -600,7 +600,7 @@
|
|||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
type: 'bar',
|
type: 'bar',
|
||||||
label: '降雨量(mm)',
|
label: '小时降雨量(mm)',
|
||||||
data: data.map(item => item.rainfall !== null && !isNaN(item.rainfall) ? item.rainfall : 0),
|
data: data.map(item => item.rainfall !== null && !isNaN(item.rainfall) ? item.rainfall : 0),
|
||||||
backgroundColor: 'rgba(54, 162, 235, 0.5)',
|
backgroundColor: 'rgba(54, 162, 235, 0.5)',
|
||||||
borderColor: 'rgba(54, 162, 235, 1)',
|
borderColor: 'rgba(54, 162, 235, 1)',
|
||||||
@ -630,7 +630,7 @@
|
|||||||
position: 'left',
|
position: 'left',
|
||||||
title: {
|
title: {
|
||||||
display: true,
|
display: true,
|
||||||
text: '降雨量(mm)'
|
text: '小时降雨量(mm)'
|
||||||
},
|
},
|
||||||
grid: {
|
grid: {
|
||||||
drawOnChartArea: false
|
drawOnChartArea: false
|
||||||
@ -715,13 +715,13 @@
|
|||||||
function exportData() {
|
function exportData() {
|
||||||
// 从表格中获取完整数据
|
// 从表格中获取完整数据
|
||||||
const tableRows = document.querySelectorAll('#tableBody tr');
|
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 => {
|
tableRows.forEach(row => {
|
||||||
const cells = row.querySelectorAll('td');
|
const cells = row.querySelectorAll('td');
|
||||||
const rowData = [
|
const rowData = [
|
||||||
cells[0].textContent, // 时间
|
cells[0].textContent, // 时间
|
||||||
cells[1].textContent, // 降雨量
|
cells[1].textContent, // 小时降雨量
|
||||||
cells[2].textContent, // 温度
|
cells[2].textContent, // 温度
|
||||||
cells[3].textContent, // 湿度
|
cells[3].textContent, // 湿度
|
||||||
cells[4].textContent, // 风速
|
cells[4].textContent, // 风速
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user