charts module

This commit is contained in:
gaz8860 Gary 2023-05-06 22:20:51 +01:00
parent 5308961541
commit 801e2918fa
2 changed files with 165 additions and 0 deletions

147
src/mod/charts/charts.go Normal file
View File

@ -0,0 +1,147 @@
package main
import (
"net/http"
"fmt"
"math/rand"
"strconv"
"time"
"github.com/labstack/echo/v4"
)
type Stats struct {
Label string `json:"Label"`
Value uint64 `json:"Value"`
}
type TableComponent[T any] struct {
Id int `json:"id"`
Data []T `json:"Data"`
}
type GraphComponent struct {
Id int `json:"id"`
Graph string `json:graph,omitempty"`
Title string `json:title,omitempty"`
Stats []Stats `json:"stats,omitempty"`
}
// Create the first list of stats
var statsList1 = []Stats{
{Label: "Sales", Value: uint64(rand.Intn(100))},
{Label: "Profit", Value: uint64(rand.Intn(100))},
{Label: "Expenses", Value: uint64(rand.Intn(100))},
{Label: "Revenue", Value: uint64(rand.Intn(100))},
}
var statsList2 = []Stats{
{Label: "Active Users", Value: uint64(rand.Intn(1000))},
{Label: "New Users", Value: uint64(rand.Intn(100))},
{Label: "Page Views", Value: uint64(rand.Intn(10000))},
{Label: "Time Spent", Value: uint64(rand.Intn(100))},
}
var statsList3 = []Stats{
{Label: "Tickets Solved", Value: uint64(rand.Intn(100))},
{Label: "Open Tickets", Value: uint64(rand.Intn(100))},
{Label: "Response Time", Value: uint64(rand.Intn(10))},
{Label: "Customer Satisfaction", Value: uint64(rand.Intn(100))},
}
var statsList4 = []Stats{
{Label: "Temperature", Value: uint64(rand.Intn(100))},
{Label: "Humidity", Value: uint64(rand.Intn(100))},
{Label: "Pressure", Value: uint64(rand.Intn(100))},
{Label: "Wind Speed", Value: uint64(rand.Intn(100))},
}
func getSchema(c echo.Context) error {
var Tablecomponent []TableComponent[string]
Graphcomponents := []GraphComponent{
{Id: 1, Graph: "donut", Stats: statsList1, Title: "Sales"},
{Id: 2, Graph: "radial-bar", Stats: statsList2, Title: "Traffic"},
{Id: 3, Graph: "pie", Stats: statsList3, Title: "Tickets"},
{Id: 4, Graph: "bar", Stats: statsList4, Title: "Weather"},
}
response := map[string]interface{}{
"tables": Tablecomponent,
"graphs": Graphcomponents,
}
return c.JSON(http.StatusOK, response)
}
func updateGraphComponent(c echo.Context) error {
component := c.Param("Id")
var newStatsList1 = []Stats{
{Label: "Sales", Value: uint64(rand.Intn(100))},
{Label: "Profit", Value: uint64(rand.Intn(100))},
{Label: "Expenses", Value: uint64(rand.Intn(100))},
{Label: "Revenue", Value: uint64(rand.Intn(100))},
}
var newStatsList2 = []Stats{
{Label: "Active Users", Value: uint64(rand.Intn(1000))},
{Label: "New Users", Value: uint64(rand.Intn(100))},
{Label: "Page Views", Value: uint64(rand.Intn(10000))},
{Label: "Time Spent", Value: uint64(rand.Intn(100))},
}
var newStatsList3 = []Stats{
{Label: "Tickets Solved", Value: uint64(rand.Intn(100))},
{Label: "Open Tickets", Value: uint64(rand.Intn(100))},
{Label: "Response Time", Value: uint64(rand.Intn(10))},
{Label: "Customer Satisfaction", Value: uint64(rand.Intn(100))},
}
var newStatsList4 = []Stats{
{Label: "Temperature", Value: uint64(rand.Intn(100))},
{Label: "Humidity", Value: uint64(rand.Intn(100))},
{Label: "Pressure", Value: uint64(rand.Intn(100))},
{Label: "Wind Speed", Value: uint64(rand.Intn(100))},
}
Graphcomponents := []GraphComponent{
{Id: 1, Graph: "donut", Stats: newStatsList1, Title: "Sales"},
{Id: 2, Graph: "radial-bar", Stats: newStatsList2, Title: "Traffic"},
{Id: 3, Graph: "pie", Stats: newStatsList3, Title: "Tickets"},
{Id: 4, Graph: "bar", Stats: newStatsList4, Title: "Weather"},
}
componentMap := make(map[int]GraphComponent)
for _, component := range Graphcomponents {
componentMap[component.Id] = component
}
targetComponent, err := strconv.Atoi(component)
if err != nil {
fmt.Println("Error converting string to int:", err);
}
return c.JSON(http.StatusOK, componentMap[targetComponent])
}
func main() {
// Seed the random number generator
rand.Seed(time.Now().UnixNano())
e := echo.New()
e.GET("/schema", getSchema)
e.GET("/component/:Id", updateGraphComponent)
e.Logger.Fatal(e.Start(":8080"))
}

18
src/mod/charts/go.mod Normal file
View File

@ -0,0 +1,18 @@
module localhost.com
go 1.19
require github.com/labstack/echo/v4 v4.10.2
require (
github.com/labstack/gommon v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.7.0 // indirect
)