first commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.DS_Store
|
37
go.mod
Normal file
37
go.mod
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
module "github.com/zuadi/tecamino-dbm.git"
|
||||||
|
|
||||||
|
go 1.24.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/coder/websocket v1.8.13
|
||||||
|
github.com/gin-gonic/gin v1.10.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/bytedance/sonic v1.11.6 // indirect
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||||
|
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||||
|
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||||
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||||
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||||
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
|
golang.org/x/arch v0.8.0 // indirect
|
||||||
|
golang.org/x/crypto v0.23.0 // indirect
|
||||||
|
golang.org/x/net v0.25.0 // indirect
|
||||||
|
golang.org/x/sys v0.20.0 // indirect
|
||||||
|
golang.org/x/text v0.15.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.34.1 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
91
go.sum
Normal file
91
go.sum
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||||
|
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||||
|
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||||
|
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||||
|
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||||
|
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||||
|
github.com/coder/websocket v1.8.13 h1:f3QZdXy7uGVz+4uCJy2nTZyM0yTBj8yANEHhqlXZ9FE=
|
||||||
|
github.com/coder/websocket v1.8.13/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||||
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||||
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
|
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||||
|
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||||
|
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||||
|
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||||
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||||
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
|
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||||
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||||
|
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||||
|
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||||
|
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
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/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
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.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||||
|
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||||
|
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||||
|
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||||
|
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||||
|
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||||
|
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||||
|
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||||
|
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||||
|
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||||
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||||
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||||
|
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||||
|
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
136
handlers/dbmHandler.go
Normal file
136
handlers/dbmHandler.go
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/zuadi/tecamino-dbm.git/models"
|
||||||
|
"github.com/zuadi/tecamino-dbm.git/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DBMHandler struct {
|
||||||
|
filePath string
|
||||||
|
DB models.Datapoint
|
||||||
|
Server *server.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDbmHandler(rootDir, dbName string) (*DBMHandler, error) {
|
||||||
|
dmaHandler := DBMHandler{
|
||||||
|
filePath: fmt.Sprintf("%s/%s.dma", rootDir, dbName),
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(dmaHandler.filePath); err == nil {
|
||||||
|
|
||||||
|
f, err := os.Open(dmaHandler.filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
dp := models.Datapoint{}
|
||||||
|
err = json.Unmarshal(scanner.Bytes(), &dp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
dmaHandler.ImportDatapoints(&dp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := dmaHandler.AddSystemDps(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &dmaHandler, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DBMHandler) SaveDb() (err error) {
|
||||||
|
f, err := os.OpenFile(d.filePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
for _, dp := range d.DB.GetAllDatapoints() {
|
||||||
|
b, er := json.Marshal(dp)
|
||||||
|
if er != nil {
|
||||||
|
return er
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = f.Write(b)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = f.Write([]byte("\n"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DBMHandler) CreateDatapoint(typ models.Type, value any, right models.Rights, path ...string) error {
|
||||||
|
return d.DB.CreateDatapoint(typ, value, right, path...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DBMHandler) ImportDatapoints(dps ...*models.Datapoint) error {
|
||||||
|
for _, dp := range dps {
|
||||||
|
err := d.DB.ImportDatapoint(dp, dp.Path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DBMHandler) UpdateDatapointValue(path string, value any) error {
|
||||||
|
return d.DB.UpdateDatapointValue(value, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DBMHandler) RemoveDatapoint(path string) error {
|
||||||
|
return d.DB.RemoveDatapoint(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DBMHandler) QueryDatapoints(key string) []*models.Datapoint {
|
||||||
|
return d.DB.QueryDatapoints(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DBMHandler) AddSystemDps() (err error) {
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
tim := "System:Time"
|
||||||
|
memory := "System:UsedMemory"
|
||||||
|
var m runtime.MemStats
|
||||||
|
|
||||||
|
err = d.DB.CreateDatapoint(models.STR, nil, models.Read, tim)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = d.DB.CreateDatapoint(models.STR, nil, models.Read, memory)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
t := time.Now().UnixMilli()
|
||||||
|
if er := d.DB.UpdateDatapointValue(t, tim); er != nil {
|
||||||
|
if err = d.DB.CreateDatapoint(models.STR, nil, models.Read, tim); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
runtime.ReadMemStats(&m)
|
||||||
|
if er := d.DB.UpdateDatapointValue(fmt.Sprintf("%.2f MB", float64(m.Sys)/1024/1024), memory); er != nil {
|
||||||
|
if err = d.DB.CreateDatapoint(models.STR, nil, models.Read, memory); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return
|
||||||
|
}
|
37
handlers/json.go
Normal file
37
handlers/json.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SaveJson(file string, data any) error {
|
||||||
|
f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
b, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = f.Write(b)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpenJson(file string, data any) (err error) {
|
||||||
|
b, err := os.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(b, &data)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
13
main.go
Normal file
13
main.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/zuadi/tecamino-dbm.git/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("start")
|
||||||
|
server := server.NewServer()
|
||||||
|
panic(server.Serve(8100))
|
||||||
|
}
|
183
models/datapoints.go
Normal file
183
models/datapoints.go
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Datapoint struct {
|
||||||
|
Datapoints map[string]*Datapoint `json:"-"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Value any `json:"value,omitempty"`
|
||||||
|
CreateDateTime int64 `json:"createDateTime,omitempty"`
|
||||||
|
UpdateDateTime int64 `json:"updateDateTime,omitempty"`
|
||||||
|
Type Type `json:"type"`
|
||||||
|
ReadWrite Rights `json:"readWrite"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var depth1 int
|
||||||
|
|
||||||
|
func (d *Datapoint) CreateDatapoint(typ Type, value any, rights Rights, paths ...string) error {
|
||||||
|
l := len(paths) - 1
|
||||||
|
if l == 0 {
|
||||||
|
paths = regexp.MustCompile(`[:]+`).Split(paths[0], -1)
|
||||||
|
l = len(paths) - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.Datapoints == nil {
|
||||||
|
d.Datapoints = make(map[string]*Datapoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
if l == depth1 {
|
||||||
|
if od, ok := d.Datapoints[paths[depth1]]; !ok {
|
||||||
|
d.Datapoints[paths[depth1]] = &Datapoint{
|
||||||
|
Path: strings.Join(paths, ":"),
|
||||||
|
Type: typ,
|
||||||
|
Value: typ.ConvertValue(value),
|
||||||
|
ReadWrite: rights.GetRights(),
|
||||||
|
CreateDateTime: time.Now().UnixMilli(),
|
||||||
|
UpdateDateTime: time.Now().UnixMilli(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
od.Type = typ
|
||||||
|
od.ReadWrite = rights.GetRights()
|
||||||
|
od.Value = typ.ConvertValue(value)
|
||||||
|
od.UpdateDateTime = time.Now().UnixMilli()
|
||||||
|
}
|
||||||
|
depth1 = 0
|
||||||
|
} else if datapoint, ok := d.Datapoints[paths[depth1]]; ok {
|
||||||
|
depth1 += 1
|
||||||
|
datapoint.CreateDatapoint(typ, value, rights, paths...)
|
||||||
|
} else {
|
||||||
|
da := Datapoint{
|
||||||
|
Path: strings.Join(paths[:depth1+1], ":"),
|
||||||
|
Type: NONE,
|
||||||
|
ReadWrite: rights.GetRights(),
|
||||||
|
CreateDateTime: time.Now().UnixMilli(),
|
||||||
|
UpdateDateTime: time.Now().UnixMilli(),
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Datapoints[paths[depth1]] = &da
|
||||||
|
depth1 += 1
|
||||||
|
da.CreateDatapoint(typ, value, rights, paths...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var depth2 int
|
||||||
|
|
||||||
|
func (d *Datapoint) ImportDatapoint(dp *Datapoint, paths ...string) error {
|
||||||
|
l := len(paths) - 1
|
||||||
|
if l == 0 {
|
||||||
|
paths = regexp.MustCompile(`[:]+`).Split(paths[0], -1)
|
||||||
|
l = len(paths) - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.Datapoints == nil {
|
||||||
|
d.Datapoints = make(map[string]*Datapoint)
|
||||||
|
}
|
||||||
|
if l == depth2 {
|
||||||
|
if od, ok := d.Datapoints[paths[depth2]]; !ok {
|
||||||
|
d.Datapoints[paths[depth2]] = dp
|
||||||
|
dp.ReadWrite = dp.ReadWrite.GetRights()
|
||||||
|
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||||
|
} else {
|
||||||
|
od.Type = dp.Type
|
||||||
|
od.Value = d.Type.ConvertValue(dp.Value)
|
||||||
|
od.ReadWrite = dp.ReadWrite.GetRights()
|
||||||
|
od.UpdateDateTime = time.Now().UnixMilli()
|
||||||
|
}
|
||||||
|
depth2 = 0
|
||||||
|
} else if datapoint, ok := d.Datapoints[paths[depth2]]; ok {
|
||||||
|
depth2 += 1
|
||||||
|
datapoint.ImportDatapoint(dp, paths...)
|
||||||
|
} else {
|
||||||
|
da := Datapoint{
|
||||||
|
Path: strings.Join(paths[:depth2+1], ":"),
|
||||||
|
Type: NONE,
|
||||||
|
UpdateDateTime: time.Now().UnixMilli(),
|
||||||
|
}
|
||||||
|
da.ReadWrite = da.ReadWrite.GetRights()
|
||||||
|
d.Datapoints[paths[depth2]] = &da
|
||||||
|
depth2 += 1
|
||||||
|
da.ImportDatapoint(dp, paths...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var depth3 int
|
||||||
|
|
||||||
|
func (d *Datapoint) UpdateDatapointValue(value any, paths ...string) error {
|
||||||
|
l := len(paths) - 1
|
||||||
|
if l == 0 {
|
||||||
|
paths = regexp.MustCompile(`[:]+`).Split(paths[0], -1)
|
||||||
|
l = len(paths) - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if l == depth3 {
|
||||||
|
if dp, ok := d.Datapoints[paths[depth3]]; ok {
|
||||||
|
fmt.Print("update dp:", dp.Path, " old value:", dp.Value)
|
||||||
|
dp.Value = dp.Type.ConvertValue(value)
|
||||||
|
fmt.Println(" new value:", dp.Value)
|
||||||
|
|
||||||
|
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||||
|
depth3 = 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
depth3 = 0
|
||||||
|
return fmt.Errorf("datapoint '%s' not found", strings.Join(paths, ":"))
|
||||||
|
} else if datapoint, ok := d.Datapoints[paths[depth3]]; ok {
|
||||||
|
depth3 += 1
|
||||||
|
if err := datapoint.UpdateDatapointValue(value, paths...); err != nil {
|
||||||
|
fmt.Println(100, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
depth3 = 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var depth4 int
|
||||||
|
|
||||||
|
func (d *Datapoint) RemoveDatapoint(paths ...string) error {
|
||||||
|
l := len(paths) - 1
|
||||||
|
if l == 0 {
|
||||||
|
paths = regexp.MustCompile(`[:]+`).Split(paths[0], -1)
|
||||||
|
l = len(paths) - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if l == depth4 {
|
||||||
|
if _, ok := d.Datapoints[paths[depth4]]; ok {
|
||||||
|
delete(d.Datapoints, paths[depth4])
|
||||||
|
fmt.Println("removed dp:", strings.Join(paths, ":"))
|
||||||
|
}
|
||||||
|
depth4 = 0
|
||||||
|
} else if datapoint, ok := d.Datapoints[paths[depth4]]; ok {
|
||||||
|
depth4 += 1
|
||||||
|
datapoint.RemoveDatapoint(paths...)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("datapoint '%s' not found", strings.Join(paths, ":"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Datapoint) GetAllDatapoints() (dps []*Datapoint) {
|
||||||
|
for _, dp := range d.Datapoints {
|
||||||
|
dps = append(dps, dp.GetAllDatapoints()...)
|
||||||
|
dps = append(dps, dp)
|
||||||
|
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Datapoint) QueryDatapoints(key string) (dps []*Datapoint) {
|
||||||
|
reg := regexp.MustCompile(key)
|
||||||
|
for _, dp := range d.Datapoints {
|
||||||
|
if reg.MatchString(dp.Path) {
|
||||||
|
dps = append(dps, dp)
|
||||||
|
}
|
||||||
|
dps = append(dps, dp.QueryDatapoints(key)...)
|
||||||
|
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
18
models/rights.go
Normal file
18
models/rights.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type Rights string
|
||||||
|
|
||||||
|
const (
|
||||||
|
Read Rights = "R"
|
||||||
|
Write Rights = "W"
|
||||||
|
ReadWrite Rights = "RW"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (r *Rights) GetRights() Rights {
|
||||||
|
if r == nil {
|
||||||
|
return ReadWrite
|
||||||
|
} else if *r == "" {
|
||||||
|
return ReadWrite
|
||||||
|
}
|
||||||
|
return *r
|
||||||
|
}
|
67
models/type.go
Normal file
67
models/type.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/zuadi/tecamino-dbm.git/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
NONE Type = "NONE"
|
||||||
|
BIT Type = "BIT"
|
||||||
|
BYU Type = "BYU" // UINT8
|
||||||
|
BYS Type = "BYS" // INT8
|
||||||
|
WOS Type = "WOS" // INT16
|
||||||
|
WOU Type = "WOU" // UINT16
|
||||||
|
DWS Type = "DWS" // INT32
|
||||||
|
DWU Type = "DWU" // UINT32
|
||||||
|
LOS Type = "LOS" // INT64
|
||||||
|
LOU Type = "LOU" // UINT64
|
||||||
|
F32 Type = "F32" // FLOAT32
|
||||||
|
F64 Type = "F64" // FLOAT64
|
||||||
|
STR Type = "STRING" // STRING
|
||||||
|
)
|
||||||
|
|
||||||
|
type Type string
|
||||||
|
|
||||||
|
func (t *Type) DefaultValue(v any) any {
|
||||||
|
switch *t {
|
||||||
|
case BIT:
|
||||||
|
return false
|
||||||
|
case BYS, BYU, WOS, WOU, DWS, DWU, LOS, LOU, F32, F64:
|
||||||
|
return 0
|
||||||
|
case STR:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Type) ConvertValue(v any) any {
|
||||||
|
switch *t {
|
||||||
|
case BIT:
|
||||||
|
return utils.BoolFrom(v)
|
||||||
|
case BYS:
|
||||||
|
return utils.Int8From(v)
|
||||||
|
case BYU:
|
||||||
|
return utils.Uint8From(v)
|
||||||
|
case WOS:
|
||||||
|
return utils.Int16From(v)
|
||||||
|
case WOU:
|
||||||
|
return utils.Uint16From(v)
|
||||||
|
case DWS:
|
||||||
|
return utils.Int32From(v)
|
||||||
|
case DWU:
|
||||||
|
return utils.Uint32From(v)
|
||||||
|
case LOS:
|
||||||
|
return utils.Int64From(v)
|
||||||
|
case LOU:
|
||||||
|
return utils.Uint64From(v)
|
||||||
|
case F32:
|
||||||
|
return utils.Float32From(v)
|
||||||
|
case F64:
|
||||||
|
return utils.Float64From(v)
|
||||||
|
case STR:
|
||||||
|
return fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
115
server/server.go
Normal file
115
server/server.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
"github.com/coder/websocket/wsjson"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
engine *gin.Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
conn *websocket.Conn
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
clients = make(map[*Client]bool)
|
||||||
|
clientsMu sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewServer() *Server {
|
||||||
|
s := Server{}
|
||||||
|
|
||||||
|
s.engine = gin.Default()
|
||||||
|
|
||||||
|
s.engine.GET("/ws", func(c *gin.Context) {
|
||||||
|
handleWebSocket(c.Writer, c.Request)
|
||||||
|
})
|
||||||
|
|
||||||
|
s.engine.GET("/", func(c *gin.Context) {
|
||||||
|
c.String(200, "WebSocket Broadcast Server is running!")
|
||||||
|
})
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Serve(port uint) error {
|
||||||
|
if err := s.engine.Run(fmt.Sprintf(":%d", port)); err != nil {
|
||||||
|
return fmt.Errorf("failed to run server: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||||
|
conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
|
||||||
|
OriginPatterns: []string{"*"},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("WebSocket accept error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close(websocket.StatusInternalError, "Internal error")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
client := &Client{conn: conn, ctx: ctx}
|
||||||
|
|
||||||
|
// Register client
|
||||||
|
registerClient(client)
|
||||||
|
defer unregisterClient(client)
|
||||||
|
|
||||||
|
// Read loop
|
||||||
|
for {
|
||||||
|
var msg string
|
||||||
|
err := wsjson.Read(ctx, conn, &msg)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Read error:", err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Received: %s", msg)
|
||||||
|
|
||||||
|
// Broadcast to all
|
||||||
|
broadcast("Broadcast: " + msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.Close(websocket.StatusNormalClosure, "Normal closure")
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerClient(c *Client) {
|
||||||
|
clientsMu.Lock()
|
||||||
|
defer clientsMu.Unlock()
|
||||||
|
clients[c] = true
|
||||||
|
log.Printf("Client connected (%d total)", len(clients))
|
||||||
|
}
|
||||||
|
|
||||||
|
func unregisterClient(c *Client) {
|
||||||
|
clientsMu.Lock()
|
||||||
|
defer clientsMu.Unlock()
|
||||||
|
delete(clients, c)
|
||||||
|
log.Printf("Client disconnected (%d total)", len(clients))
|
||||||
|
}
|
||||||
|
|
||||||
|
func broadcast(message string) {
|
||||||
|
clientsMu.Lock()
|
||||||
|
defer clientsMu.Unlock()
|
||||||
|
|
||||||
|
for c := range clients {
|
||||||
|
go func(client *Client) {
|
||||||
|
err := wsjson.Write(client.ctx, client.conn, message)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Broadcast error: %v", err)
|
||||||
|
}
|
||||||
|
}(c)
|
||||||
|
}
|
||||||
|
}
|
110
test/dbm_test.go
Normal file
110
test/dbm_test.go
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/zuadi/tecamino-dbm.git/handlers"
|
||||||
|
"github.com/zuadi/tecamino-dbm.git/models"
|
||||||
|
"github.com/zuadi/tecamino-dbm.git/server"
|
||||||
|
"github.com/zuadi/tecamino-dbm.git/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateDps(t *testing.T) {
|
||||||
|
dmaHandler, err := handlers.NewDbmHandler(".", "test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rand.NewSource(time.Now().UnixNano())
|
||||||
|
|
||||||
|
ndps := utils.ListofA2ZZ()
|
||||||
|
l := len(ndps)
|
||||||
|
s := time.Now()
|
||||||
|
for _, dp := range ndps {
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
err = dmaHandler.ImportDatapoints(&models.Datapoint{
|
||||||
|
Path: fmt.Sprintf("Test:%s:%03d", dp, i),
|
||||||
|
Type: RandomType(),
|
||||||
|
Value: rand.Int31(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("time used to create %d datapoints: %v\n", l*100, time.Since(s))
|
||||||
|
|
||||||
|
var m runtime.MemStats
|
||||||
|
runtime.ReadMemStats(&m)
|
||||||
|
|
||||||
|
fmt.Printf("Allocated: %.2f MB\n", float64(m.Alloc)/1024/1024)
|
||||||
|
fmt.Printf("Total Allocated (ever): %.2f MB\n", float64(m.TotalAlloc)/1024/1024)
|
||||||
|
fmt.Printf("System Memory Obtained: %.2f MB\n", float64(m.Sys)/1024/1024)
|
||||||
|
fmt.Printf("Heap In Use: %.2f MB\n", float64(m.HeapInuse)/1024/1024)
|
||||||
|
fmt.Printf("GC Runs: %d\n", m.NumGC)
|
||||||
|
|
||||||
|
err = dmaHandler.SaveDb()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuery(t *testing.T) {
|
||||||
|
dmaHandler, err := handlers.NewDbmHandler(".", "test")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// for i, o := range dmaHandler.QueryDatapoints(".*002.*") {
|
||||||
|
// fmt.Println(600, i, o)
|
||||||
|
// }
|
||||||
|
|
||||||
|
for i, o := range dmaHandler.QueryDatapoints("Test:A:000") {
|
||||||
|
fmt.Println(600, i, o)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdateDps(t *testing.T) {
|
||||||
|
dmaHandler, err := handlers.NewDbmHandler(".", "test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rand.NewSource(time.Now().UnixNano())
|
||||||
|
|
||||||
|
ndps := utils.ListofA2ZZ()
|
||||||
|
l := len(ndps)
|
||||||
|
s := time.Now()
|
||||||
|
for j, dp := range ndps {
|
||||||
|
if j > 2 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
err = dmaHandler.UpdateDatapointValue(fmt.Sprintf("Test:%s:%03d", dp, i), rand.Int31())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("time used to update %d datapoints: %v\n", l*100, time.Since(s))
|
||||||
|
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
|
fmt.Println("save data")
|
||||||
|
err = dmaHandler.SaveDb()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServer(t *testing.T) {
|
||||||
|
fmt.Println("start")
|
||||||
|
server := server.NewServer()
|
||||||
|
t.Fatal(server.Serve(8100))
|
||||||
|
|
||||||
|
}
|
41
test/helper.go
Normal file
41
test/helper.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
|
"github.com/zuadi/tecamino-dbm.git/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RandomType() models.Type {
|
||||||
|
n := rand.Intn(11) + 1
|
||||||
|
|
||||||
|
switch n {
|
||||||
|
case 1:
|
||||||
|
return "BIT"
|
||||||
|
case 2:
|
||||||
|
return "BYU"
|
||||||
|
case 3:
|
||||||
|
return "BYS"
|
||||||
|
case 4:
|
||||||
|
return "WOS"
|
||||||
|
case 5:
|
||||||
|
return "WOU"
|
||||||
|
case 6:
|
||||||
|
return "DWS"
|
||||||
|
case 7:
|
||||||
|
return "DWU"
|
||||||
|
case 8:
|
||||||
|
return "LOS"
|
||||||
|
case 9:
|
||||||
|
return "LOU"
|
||||||
|
case 10:
|
||||||
|
return "F32"
|
||||||
|
case 11:
|
||||||
|
return "F64"
|
||||||
|
case 12:
|
||||||
|
return "STRING"
|
||||||
|
default:
|
||||||
|
return "NONE"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
70906
test/test.dma
Normal file
70906
test/test.dma
Normal file
File diff suppressed because it is too large
Load Diff
430
utils/convert.go
Normal file
430
utils/convert.go
Normal file
@@ -0,0 +1,430 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Float32From(v any) float32 {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case float32:
|
||||||
|
return val
|
||||||
|
case float64:
|
||||||
|
return float32(val)
|
||||||
|
case int:
|
||||||
|
return float32(val)
|
||||||
|
case int8:
|
||||||
|
return float32(val)
|
||||||
|
case int16:
|
||||||
|
return float32(val)
|
||||||
|
case int32:
|
||||||
|
return float32(val)
|
||||||
|
case int64:
|
||||||
|
return float32(val)
|
||||||
|
case uint8:
|
||||||
|
return float32(val)
|
||||||
|
case uint16:
|
||||||
|
return float32(val)
|
||||||
|
case uint32:
|
||||||
|
return float32(val)
|
||||||
|
case uint64:
|
||||||
|
return float32(val)
|
||||||
|
case string:
|
||||||
|
if i, err := strconv.Atoi(val); err == nil {
|
||||||
|
return float32(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Float64From(v any) float64 {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case float32:
|
||||||
|
return float64(val)
|
||||||
|
case float64:
|
||||||
|
return val
|
||||||
|
case int:
|
||||||
|
return float64(val)
|
||||||
|
case int8:
|
||||||
|
return float64(val)
|
||||||
|
case int16:
|
||||||
|
return float64(val)
|
||||||
|
case int32:
|
||||||
|
return float64(val)
|
||||||
|
case int64:
|
||||||
|
return float64(val)
|
||||||
|
case uint8:
|
||||||
|
return float64(val)
|
||||||
|
case uint16:
|
||||||
|
return float64(val)
|
||||||
|
case uint32:
|
||||||
|
return float64(val)
|
||||||
|
case uint64:
|
||||||
|
return float64(val)
|
||||||
|
case string:
|
||||||
|
if i, err := strconv.Atoi(val); err == nil {
|
||||||
|
return float64(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Int8From(v any) int8 {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case int:
|
||||||
|
return int8(val)
|
||||||
|
case int8:
|
||||||
|
return val
|
||||||
|
case int16:
|
||||||
|
return int8(val)
|
||||||
|
case int32:
|
||||||
|
return int8(val)
|
||||||
|
case int64:
|
||||||
|
return int8(val)
|
||||||
|
case uint8:
|
||||||
|
return int8(val)
|
||||||
|
case uint16:
|
||||||
|
return int8(val)
|
||||||
|
case uint32:
|
||||||
|
return int8(val)
|
||||||
|
case uint64:
|
||||||
|
return int8(val)
|
||||||
|
case float32:
|
||||||
|
return int8(val)
|
||||||
|
case float64:
|
||||||
|
return int8(val)
|
||||||
|
case string:
|
||||||
|
if i, err := strconv.Atoi(val); err == nil {
|
||||||
|
return int8(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Int16From(v any) int16 {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case int:
|
||||||
|
return int16(val)
|
||||||
|
case int8:
|
||||||
|
return int16(val)
|
||||||
|
case int16:
|
||||||
|
return val
|
||||||
|
case int32:
|
||||||
|
return int16(val)
|
||||||
|
case int64:
|
||||||
|
return int16(val)
|
||||||
|
case uint8:
|
||||||
|
return int16(val)
|
||||||
|
case uint16:
|
||||||
|
return int16(val)
|
||||||
|
case uint32:
|
||||||
|
return int16(val)
|
||||||
|
case uint64:
|
||||||
|
return int16(val)
|
||||||
|
case float32:
|
||||||
|
return int16(val)
|
||||||
|
case float64:
|
||||||
|
return int16(val)
|
||||||
|
case string:
|
||||||
|
if i, err := strconv.Atoi(val); err == nil {
|
||||||
|
return int16(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Int32From(v any) int32 {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case int:
|
||||||
|
return int32(val)
|
||||||
|
case int8:
|
||||||
|
return int32(val)
|
||||||
|
case int16:
|
||||||
|
return int32(val)
|
||||||
|
case int32:
|
||||||
|
return val
|
||||||
|
case int64:
|
||||||
|
return int32(val)
|
||||||
|
case uint8:
|
||||||
|
return int32(val)
|
||||||
|
case uint16:
|
||||||
|
return int32(val)
|
||||||
|
case uint32:
|
||||||
|
return int32(val)
|
||||||
|
case uint64:
|
||||||
|
return int32(val)
|
||||||
|
case float32:
|
||||||
|
return int32(val)
|
||||||
|
case float64:
|
||||||
|
return int32(val)
|
||||||
|
case string:
|
||||||
|
if i, err := strconv.Atoi(val); err == nil {
|
||||||
|
return int32(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Int64From(v any) int64 {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case int:
|
||||||
|
return int64(val)
|
||||||
|
case int8:
|
||||||
|
return int64(val)
|
||||||
|
case int16:
|
||||||
|
return int64(val)
|
||||||
|
case int32:
|
||||||
|
return int64(val)
|
||||||
|
case int64:
|
||||||
|
return val
|
||||||
|
case uint8:
|
||||||
|
return int64(val)
|
||||||
|
case uint16:
|
||||||
|
return int64(val)
|
||||||
|
case uint32:
|
||||||
|
return int64(val)
|
||||||
|
case uint64:
|
||||||
|
return int64(val)
|
||||||
|
case float32:
|
||||||
|
return int64(val)
|
||||||
|
case float64:
|
||||||
|
return int64(val)
|
||||||
|
case string:
|
||||||
|
if i, err := strconv.Atoi(val); err == nil {
|
||||||
|
return int64(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Uint8From(v any) uint8 {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case int:
|
||||||
|
return uint8(val)
|
||||||
|
case int8:
|
||||||
|
return uint8(val)
|
||||||
|
case int16:
|
||||||
|
return uint8(val)
|
||||||
|
case int32:
|
||||||
|
return uint8(val)
|
||||||
|
case int64:
|
||||||
|
return uint8(val)
|
||||||
|
case uint8:
|
||||||
|
return val
|
||||||
|
case uint16:
|
||||||
|
return uint8(val)
|
||||||
|
case uint32:
|
||||||
|
return uint8(val)
|
||||||
|
case uint64:
|
||||||
|
return uint8(val)
|
||||||
|
case float32:
|
||||||
|
return uint8(val)
|
||||||
|
case float64:
|
||||||
|
return uint8(val)
|
||||||
|
case string:
|
||||||
|
if i, err := strconv.Atoi(val); err == nil {
|
||||||
|
return uint8(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Uint16From(v any) uint16 {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case int:
|
||||||
|
return uint16(val)
|
||||||
|
case int8:
|
||||||
|
return uint16(val)
|
||||||
|
case int16:
|
||||||
|
return uint16(val)
|
||||||
|
case int32:
|
||||||
|
return uint16(val)
|
||||||
|
case int64:
|
||||||
|
return uint16(val)
|
||||||
|
case uint8:
|
||||||
|
return uint16(val)
|
||||||
|
case uint16:
|
||||||
|
return val
|
||||||
|
case uint32:
|
||||||
|
return uint16(val)
|
||||||
|
case uint64:
|
||||||
|
return uint16(val)
|
||||||
|
case float32:
|
||||||
|
return uint16(val)
|
||||||
|
case float64:
|
||||||
|
return uint16(val)
|
||||||
|
case string:
|
||||||
|
if i, err := strconv.Atoi(val); err == nil {
|
||||||
|
return uint16(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Uint32From(v any) uint32 {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case int:
|
||||||
|
return uint32(val)
|
||||||
|
case int8:
|
||||||
|
return uint32(val)
|
||||||
|
case int16:
|
||||||
|
return uint32(val)
|
||||||
|
case int32:
|
||||||
|
return uint32(val)
|
||||||
|
case int64:
|
||||||
|
return uint32(val)
|
||||||
|
case uint8:
|
||||||
|
return uint32(val)
|
||||||
|
case uint16:
|
||||||
|
return uint32(val)
|
||||||
|
case uint32:
|
||||||
|
return val
|
||||||
|
case uint64:
|
||||||
|
return uint32(val)
|
||||||
|
case float32:
|
||||||
|
return uint32(val)
|
||||||
|
case float64:
|
||||||
|
return uint32(val)
|
||||||
|
case string:
|
||||||
|
if i, err := strconv.Atoi(val); err == nil {
|
||||||
|
return uint32(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Uint64From(v any) uint64 {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case int:
|
||||||
|
return uint64(val)
|
||||||
|
case int8:
|
||||||
|
return uint64(val)
|
||||||
|
case int16:
|
||||||
|
return uint64(val)
|
||||||
|
case int32:
|
||||||
|
return uint64(val)
|
||||||
|
case int64:
|
||||||
|
return uint64(val)
|
||||||
|
case uint8:
|
||||||
|
return uint64(val)
|
||||||
|
case uint16:
|
||||||
|
return uint64(val)
|
||||||
|
case uint32:
|
||||||
|
return uint64(val)
|
||||||
|
case uint64:
|
||||||
|
return val
|
||||||
|
case float32:
|
||||||
|
return uint64(val)
|
||||||
|
case float64:
|
||||||
|
return uint64(val)
|
||||||
|
case string:
|
||||||
|
if i, err := strconv.Atoi(val); err == nil {
|
||||||
|
return uint64(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BoolFrom(v any) bool {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
return val
|
||||||
|
case int:
|
||||||
|
return val > 0
|
||||||
|
case int8:
|
||||||
|
return val > 0
|
||||||
|
case int16:
|
||||||
|
return val > 0
|
||||||
|
case int32:
|
||||||
|
return val > 0
|
||||||
|
case int64:
|
||||||
|
return val > 0
|
||||||
|
case uint8:
|
||||||
|
return val > 0
|
||||||
|
case uint16:
|
||||||
|
return val > 0
|
||||||
|
case uint32:
|
||||||
|
return val > 0
|
||||||
|
case uint64:
|
||||||
|
return val > 0
|
||||||
|
case float32:
|
||||||
|
return val >= 1
|
||||||
|
case float64:
|
||||||
|
return val >= 1
|
||||||
|
case string:
|
||||||
|
return strings.ToLower(val) == "false" || v == "0"
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
20
utils/utils.go
Normal file
20
utils/utils.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
func ListofA2ZZ() (list []string) {
|
||||||
|
for i := 'A'; i <= 'Z'; i++ {
|
||||||
|
list = append(list, string(i))
|
||||||
|
}
|
||||||
|
for i := 'A'; i <= 'Z'; i++ {
|
||||||
|
for j := 'A'; j <= 'Z'; j++ {
|
||||||
|
list = append(list, string(i)+string(j))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// for i := 'A'; i <= 'Z'; i++ {
|
||||||
|
// for j := 'A'; j <= 'Z'; j++ {
|
||||||
|
// for k := 'A'; k <= 'Z'; k++ {
|
||||||
|
// list = append(list, string(i)+string(j)+string(k))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
return
|
||||||
|
}
|
Reference in New Issue
Block a user