147 lines
3.9 KiB
Go
147 lines
3.9 KiB
Go
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"))
|
|
|
|
} |