Compare commits

...

2 Commits

Author SHA1 Message Date
ede43f5939
tmp 2023-05-06 23:09:59 +01:00
801e2918fa charts module 2023-05-06 22:20:51 +01:00
4 changed files with 232 additions and 0 deletions

25
src/mod/charts/Dockerfile Normal file
View File

@ -0,0 +1,25 @@
# syntax=docker/dockerfile:1
FROM golang:1.20
# Set destination for COPY
WORKDIR /app
# Download Go modules
COPY . ./
RUN go mod download
# Build
RUN CGO_ENABLED=0 GOOS=linux go build -o /charts .
# Optional:
# To bind to a TCP port, runtime parameters must be supplied to the docker command.
# But we can document in the Dockerfile what ports
# the application is going to listen on by default.
# https://docs.docker.com/engine/reference/builder/#expose
EXPOSE 8080
# Run
CMD ["/charts"]

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
)

42
src/mod/charts/go.sum Normal file
View File

@ -0,0 +1,42 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/labstack/echo/v4 v4.10.2 h1:n1jAhnq/elIFTHr1EYpiYtyKgx4RW9ccVgkqByZaN2M=
github.com/labstack/echo/v4 v4.10.2/go.mod h1:OEyqf2//K1DFdE57vw2DRgWY0M7s65IVQO2FzvI4J5k=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=