Add min max
This commit is contained in:
100
main.go
100
main.go
@ -12,7 +12,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "modernc.org/sqlite" // lightweight pure-Go SQLite driver
|
_ "modernc.org/sqlite"
|
||||||
"gocv.io/x/gocv"
|
"gocv.io/x/gocv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,6 +23,15 @@ type CloudResult struct {
|
|||||||
Error string `json:"error,omitempty"`
|
Error string `json:"error,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Summary struct {
|
||||||
|
From time.Time `json:"from"`
|
||||||
|
To time.Time `json:"to"`
|
||||||
|
Avg float64 `json:"avg"`
|
||||||
|
Min float64 `json:"min"`
|
||||||
|
Max float64 `json:"max"`
|
||||||
|
SampleCount int `json:"sample_count"`
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
imageURL = getEnv("CLOUD_IMAGE_URL", "http://clouds.local:8082/snapshot")
|
imageURL = getEnv("CLOUD_IMAGE_URL", "http://clouds.local:8082/snapshot")
|
||||||
refreshSecs = getEnvInt("CLOUD_REFRESH_SECONDS", 30)
|
refreshSecs = getEnvInt("CLOUD_REFRESH_SECONDS", 30)
|
||||||
@ -39,6 +48,7 @@ func main() {
|
|||||||
http.HandleFunc("/", dashboardHandler)
|
http.HandleFunc("/", dashboardHandler)
|
||||||
http.HandleFunc("/cloudcover", handleLatest)
|
http.HandleFunc("/cloudcover", handleLatest)
|
||||||
http.HandleFunc("/cloudtrend", handleTrend)
|
http.HandleFunc("/cloudtrend", handleTrend)
|
||||||
|
http.HandleFunc("/cloudsummary", handleSummary)
|
||||||
|
|
||||||
fmt.Printf("☁️ Cloud cover API started at http://localhost:8080\n")
|
fmt.Printf("☁️ Cloud cover API started at http://localhost:8080\n")
|
||||||
fmt.Printf("🔁 Refresh every %ds | Source: %s | DB: %s\n", refreshSecs, imageURL, dbPath)
|
fmt.Printf("🔁 Refresh every %ds | Source: %s | DB: %s\n", refreshSecs, imageURL, dbPath)
|
||||||
@ -104,7 +114,6 @@ func backgroundCollector() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = err.Error()
|
result.Error = err.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
storeResult(result)
|
storeResult(result)
|
||||||
fmt.Printf("[%s] Cloud cover: %.2f%%\n", result.Timestamp.Format("15:04:05"), result.CloudCover)
|
fmt.Printf("[%s] Cloud cover: %.2f%%\n", result.Timestamp.Format("15:04:05"), result.CloudCover)
|
||||||
time.Sleep(time.Duration(refreshSecs) * time.Second)
|
time.Sleep(time.Duration(refreshSecs) * time.Second)
|
||||||
@ -170,13 +179,42 @@ func getAll(limit int) ([]CloudResult, error) {
|
|||||||
results = append(results, r)
|
results = append(results, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reverse to chronological order
|
// reverse chronological order
|
||||||
for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 {
|
for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 {
|
||||||
results[i], results[j] = results[j], results[i]
|
results[i], results[j] = results[j], results[i]
|
||||||
}
|
}
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSummary() (*Summary, error) {
|
||||||
|
since := time.Now().Add(-24 * time.Hour)
|
||||||
|
rows := db.QueryRow(`
|
||||||
|
SELECT
|
||||||
|
AVG(cloud_cover),
|
||||||
|
MIN(cloud_cover),
|
||||||
|
MAX(cloud_cover),
|
||||||
|
COUNT(*)
|
||||||
|
FROM cloud_trend
|
||||||
|
WHERE timestamp > ?
|
||||||
|
`, since.Format(time.RFC3339))
|
||||||
|
|
||||||
|
var s Summary
|
||||||
|
var avg, min, max sql.NullFloat64
|
||||||
|
var count int
|
||||||
|
if err := rows.Scan(&avg, &min, &max, &count); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.From = since
|
||||||
|
s.To = time.Now()
|
||||||
|
s.SampleCount = count
|
||||||
|
if avg.Valid {
|
||||||
|
s.Avg = avg.Float64
|
||||||
|
s.Min = min.Float64
|
||||||
|
s.Max = max.Float64
|
||||||
|
}
|
||||||
|
return &s, nil
|
||||||
|
}
|
||||||
|
|
||||||
func handleLatest(w http.ResponseWriter, r *http.Request) {
|
func handleLatest(w http.ResponseWriter, r *http.Request) {
|
||||||
result, err := getLatest()
|
result, err := getLatest()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -197,6 +235,16 @@ func handleTrend(w http.ResponseWriter, r *http.Request) {
|
|||||||
json.NewEncoder(w).Encode(results)
|
json.NewEncoder(w).Encode(results)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleSummary(w http.ResponseWriter, r *http.Request) {
|
||||||
|
summary, err := getSummary()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(summary)
|
||||||
|
}
|
||||||
|
|
||||||
func dashboardHandler(w http.ResponseWriter, r *http.Request) {
|
func dashboardHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
html := `
|
html := `
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
@ -207,50 +255,67 @@ func dashboardHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||||
<style>
|
<style>
|
||||||
body { font-family: sans-serif; background: #111; color: #eee; text-align: center; margin: 0; padding: 1em; }
|
body { font-family: sans-serif; background: #111; color: #eee; text-align: center; margin: 0; padding: 1em; }
|
||||||
|
.summary { margin: 1em auto; width: 80%%; background: #222; border-radius: 10px; padding: 1em; }
|
||||||
canvas { background: #222; border-radius: 10px; margin-top: 20px; max-width: 90vw; }
|
canvas { background: #222; border-radius: 10px; margin-top: 20px; max-width: 90vw; }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>☁️ Cloud Cover Trend</h1>
|
<h1>☁️ Cloud Cover Trend</h1>
|
||||||
<p>Source: <code>` + imageURL + `</code></p>
|
<p>Source: <code>` + imageURL + `</code></p>
|
||||||
|
|
||||||
|
<div class="summary" id="summary">
|
||||||
|
<h3>Loading summary...</h3>
|
||||||
|
</div>
|
||||||
|
|
||||||
<canvas id="chart" width="800" height="400"></canvas>
|
<canvas id="chart" width="800" height="400"></canvas>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const ctx = document.getElementById('chart');
|
const ctx = document.getElementById("chart");
|
||||||
const chart = new Chart(ctx, {
|
const chart = new Chart(ctx, {
|
||||||
type: 'line',
|
type: "line",
|
||||||
data: {
|
data: {
|
||||||
labels: [],
|
labels: [],
|
||||||
datasets: [{
|
datasets: [{
|
||||||
label: 'Cloud Cover (%)',
|
label: "Cloud Cover (%)",
|
||||||
data: [],
|
data: [],
|
||||||
borderColor: '#4fc3f7',
|
borderColor: "#4fc3f7",
|
||||||
backgroundColor: 'rgba(79,195,247,0.2)',
|
backgroundColor: "rgba(79,195,247,0.2)",
|
||||||
fill: true,
|
fill: true,
|
||||||
tension: 0.3
|
tension: 0.3
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
scales: {
|
scales: {
|
||||||
x: { title: { display: true, text: 'Time' }, ticks: { color: '#aaa' } },
|
x: { title: { display: true, text: "Time" }, ticks: { color: "#aaa" } },
|
||||||
y: { beginAtZero: true, title: { display: true, text: 'Cloud %' }, ticks: { color: '#aaa' } }
|
y: { beginAtZero: true, title: { display: true, text: "Cloud %" }, ticks: { color: "#aaa" } }
|
||||||
},
|
},
|
||||||
plugins: {
|
plugins: { legend: { labels: { color: "#ccc" } } }
|
||||||
legend: { labels: { color: '#ccc' } }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
async function fetchData() {
|
async function fetchTrend() {
|
||||||
const res = await fetch('/cloudtrend');
|
const res = await fetch("/cloudtrend");
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
chart.data.labels = data.map(d => new Date(d.timestamp).toLocaleTimeString());
|
chart.data.labels = data.map(d => new Date(d.timestamp).toLocaleTimeString());
|
||||||
chart.data.datasets[0].data = data.map(d => d.cloud_cover);
|
chart.data.datasets[0].data = data.map(d => d.cloud_cover);
|
||||||
chart.update();
|
chart.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchData();
|
async function fetchSummary() {
|
||||||
setInterval(fetchData, 10000);
|
const res = await fetch("/cloudsummary");
|
||||||
|
const s = await res.json();
|
||||||
|
document.getElementById("summary").innerHTML =
|
||||||
|
"<h3>Last 24 hours</h3>" +
|
||||||
|
"<p>Average: " + s.avg.toFixed(1) + "% | " +
|
||||||
|
"Min: " + s.min.toFixed(1) + "% | " +
|
||||||
|
"Max: " + s.max.toFixed(1) + "% | " +
|
||||||
|
"Samples: " + s.sample_count + "</p>";
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchTrend();
|
||||||
|
fetchSummary();
|
||||||
|
setInterval(fetchTrend, 10000);
|
||||||
|
setInterval(fetchSummary, 60000);
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@ -259,6 +324,7 @@ func dashboardHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte(html))
|
w.Write([]byte(html))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func getEnv(key, def string) string {
|
func getEnv(key, def string) string {
|
||||||
if v, ok := os.LookupEnv(key); ok {
|
if v, ok := os.LookupEnv(key); ok {
|
||||||
return v
|
return v
|
||||||
|
|||||||
Reference in New Issue
Block a user