forked from goffee/core
140 lines
2.9 KiB
Go
140 lines
2.9 KiB
Go
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
|
// Use of this source code is governed by MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.smarteching.com/zeni/go-chart/v2"
|
|
"github.com/julienschmidt/httprouter"
|
|
)
|
|
|
|
func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
// $_GET values
|
|
|
|
service := ps.ByName("graph")
|
|
|
|
if service != "" {
|
|
|
|
//serv := strings.TrimPrefix(service, "/")
|
|
serv := strings.Split(service, "/")
|
|
|
|
switch serv[1] {
|
|
|
|
case "graph":
|
|
|
|
kindg := serv[2]
|
|
|
|
if kindg != "" {
|
|
|
|
switch kindg {
|
|
// case graph pie
|
|
case "pie":
|
|
|
|
queryValues := r.URL.Query()
|
|
labels := strings.Split(queryValues.Get("l"), "|")
|
|
values := strings.Split(queryValues.Get("v"), "|")
|
|
|
|
qtyl := len(labels)
|
|
qtyv := len(values)
|
|
|
|
// cjeck qty and equal values from url
|
|
if qtyl < 2 {
|
|
fmt.Fprintf(w, "Missing captions in pie")
|
|
return
|
|
}
|
|
if qtyv != qtyl {
|
|
fmt.Fprintf(w, "Labels and values mismatch")
|
|
return
|
|
}
|
|
|
|
var cvalues []chart.Value
|
|
var row chart.Value
|
|
// fill values
|
|
for index, line := range labels {
|
|
valuefloat, _ := strconv.ParseFloat(values[index], 64)
|
|
row.Value = valuefloat
|
|
row.Label = line
|
|
cvalues = append(cvalues, row)
|
|
}
|
|
|
|
pie := chart.PieChart{
|
|
Width: 512,
|
|
Height: 512,
|
|
Values: cvalues,
|
|
}
|
|
w.Header().Set("Content-Type", "image/png")
|
|
err := pie.Render(chart.PNG, w)
|
|
if err != nil {
|
|
fmt.Printf("Error rendering pie chart: %v\n", err)
|
|
}
|
|
|
|
// case graph bar
|
|
case "bar":
|
|
|
|
queryValues := r.URL.Query()
|
|
labels := strings.Split(queryValues.Get("l"), "|")
|
|
values := strings.Split(queryValues.Get("v"), "|")
|
|
|
|
qtyl := len(labels)
|
|
qtyv := len(values)
|
|
|
|
// cjeck qty and equal values from url
|
|
if qtyl < 2 {
|
|
fmt.Fprintf(w, "Missing captions in pie")
|
|
return
|
|
}
|
|
if qtyv != qtyl {
|
|
fmt.Fprintf(w, "Labels and values mismatch")
|
|
return
|
|
}
|
|
|
|
var cvalues []chart.Value
|
|
var row chart.Value
|
|
// fill values
|
|
for index, line := range labels {
|
|
valuefloat, _ := strconv.ParseFloat(values[index], 64)
|
|
row.Value = valuefloat
|
|
row.Label = line
|
|
cvalues = append(cvalues, row)
|
|
}
|
|
|
|
bar := chart.BarChart{
|
|
Height: 512,
|
|
BarWidth: 50,
|
|
Bars: cvalues,
|
|
}
|
|
w.Header().Set("Content-Type", "image/png")
|
|
err := bar.Render(chart.PNG, w)
|
|
if err != nil {
|
|
fmt.Printf("Error rendering pie chart: %v\n", err)
|
|
}
|
|
|
|
default:
|
|
fmt.Fprintf(w, "Unknown graph %s!\n", kindg)
|
|
}
|
|
|
|
} else {
|
|
fmt.Fprintf(w, "Kind graph not exists")
|
|
}
|
|
//
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(w, "Unknown option in core service %s!\n", ps.ByName("serv"))
|
|
|
|
}
|
|
fmt.Fprintf(w, "Service %s!\n", serv)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "Unknown core service %s!\n", ps.ByName("graph"))
|
|
}
|
|
|
|
}
|