Skip to content

Commit

Permalink
一些bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Nov 1, 2024
1 parent dda0d92 commit b3a6df2
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 153 deletions.
2 changes: 1 addition & 1 deletion attach/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func ApiWrite(root string, params ...string) gin.HandlerFunc {
return func(ctx *gin.Context) {
filename := filepath.Join(viper.GetString("data"), root, filepath.Join(getParams(ctx, params)...), ctx.Param("name"))
_ = os.MkdirAll(filepath.Dir(filename), os.ModePerm) //创建目录
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.ModePerm)
f, err := os.Create(filename)
if err != nil {
Error(ctx, err)
return
Expand Down
4 changes: 2 additions & 2 deletions lib/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func LoadYaml(filename string, cfg any) error {
}

func StoreYaml(filename string, cfg any) error {
_ = os.MkdirAll(filepath.Dir(filename), os.ModePerm) //创建目录
y, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0755) //os.Create(filename)
_ = os.MkdirAll(filepath.Dir(filename), os.ModePerm) //创建目录
y, err := os.Create(filename) //os.Create(filename)
if err != nil {
return err
}
Expand Down
37 changes: 2 additions & 35 deletions table/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,6 @@ import (
"path/filepath"
)

type Info struct {
Name string `json:"name,omitempty"`
Label string `json:"label,omitempty"`
Fields int `json:"fields,omitempty"`
Schema bool `json:"schema,omitempty"`
Scripts int `json:"scripts,omitempty"`
Accumulations int `json:"accumulations,omitempty"`
TimeSeries bool `json:"time_series,omitempty"`
Snapshot bool `json:"snapshot,omitempty"`
}

func ApiList(ctx *gin.Context) {

var infos []*Info

tables.Range(func(name string, tab *Table) bool {
infos = append(infos, &Info{
Name: tab.Name,
Label: tab.Name,
Fields: len(tab.Fields),
Schema: tab.Schema != nil,
Scripts: len(tab.Scripts),
Accumulations: len(tab.Accumulations),
TimeSeries: tab.TimeSeries != nil,
Snapshot: tab.Snapshot != nil,
})
return true
})

//TODO 加入空间统计等

OK(ctx, infos)
}

func ApiConf(ctx *gin.Context) {
tab := ctx.Param("table")
conf := ctx.Param("conf")
Expand All @@ -67,7 +33,8 @@ func ApiConfUpdate(ctx *gin.Context) {

fn := filepath.Join(viper.GetString("data"), Path, tab, conf)
_ = os.MkdirAll(filepath.Dir(fn), os.ModePerm)
file, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY, os.ModePerm)
//file, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY, os.ModePerm)
file, err := os.Create(fn) //覆盖
if err != nil {
Error(ctx, err)
return
Expand Down
206 changes: 204 additions & 2 deletions table/load.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,212 @@
package table

import "github.com/gin-gonic/gin"
import (
"encoding/json"
"errors"
"github.com/dop251/goja"
"github.com/gin-gonic/gin"
"github.com/god-jason/bucket/log"
"github.com/god-jason/bucket/pkg/javascript"
"github.com/santhosh-tekuri/jsonschema/v6"
"github.com/spf13/viper"
"os"
"path/filepath"
)

var scripts = []string{
"before.insert",
"after.insert",
"before.delete",
"after.delete",
"before.update",
"after.update",
}

type InfoEx struct {
Name string `json:"name,omitempty"`
Label string `json:"label,omitempty"`
Fields int `json:"fields,omitempty"`
Schema bool `json:"schema,omitempty"`
Scripts int `json:"scripts,omitempty"`
Accumulations int `json:"accumulations,omitempty"`
TimeSeries bool `json:"time_series,omitempty"`
Snapshot bool `json:"snapshot,omitempty"`
}

func loadText(filename string) (string, error) {
buf, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return string(buf), nil
}

func loadJson(filename string, value any) error {
buf, err := os.ReadFile(filename)
if err != nil {
return err
}
return json.Unmarshal(buf, value)
}

func loadJavaScript(filename string) (*goja.Program, error) {
buf, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return javascript.Compile(string(buf))
}

func loadSchema(filename string) (*jsonschema.Schema, error) {
buf, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return compiler.Compile(string(buf))
}

func Load(id string, strict bool) error {
dir := filepath.Join(viper.GetString("data"), Path, id)

var table Table

//加载信息
fn := filepath.Join(dir, "info.json")
err := loadJson(fn, &table.Info)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
if strict {
return err
} else {
log.Error(fn, err)
}
}
}

table.Id = id //避免被Info覆盖

//加载字段
fn = filepath.Join(dir, "fields.json")
err = loadJson(fn, &table.Fields)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
if strict {
return err
} else {
log.Error(fn, err)
}
}
}

//加载模式
fn = filepath.Join(dir, "schema.json")
table.Schema, err = loadSchema(fn)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
if strict {
return err
} else {
log.Error(err)
}
}
}

//加载累加器
fn = filepath.Join(dir, "accumulations.json")
err = loadJson(fn, &table.Accumulations)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
if strict {
return err
} else {
log.Error(fn, err)
}
}
}
err = table.init()
if err != nil {
if strict {
return err
} else {
log.Error(err)
}
}

//加载时序配置
fn = filepath.Join(dir, "time-serials.json")
err = loadJson(fn, &table.TimeSeries)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
if strict {
return err
} else {
log.Error(fn, err)
}
}
}

//加载快照配置
fn = filepath.Join(dir, "snapshot.json")
err = loadJson(fn, &table.Snapshot)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
if strict {
return err
} else {
log.Error(fn, err)
}
}
}

//加载脚本
table.Scripts = make(map[string]*goja.Program)
for _, k := range scripts {
fn := filepath.Join(dir, k+".js")
js, err := loadJavaScript(fn)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
if strict {
return err
} else {
log.Error(fn, err)
}
}
continue
} else {
table.Scripts[k] = js
}
}

//直接注册表
Register(&table)

return nil
}

func ApiList(ctx *gin.Context) {

var infos []Info
//&InfoEx{
// Name: tab.Name,
// Label: tab.Name,
// Fields: len(tab.Fields),
// Schema: tab.Schema != nil,
// Scripts: len(tab.Scripts),
// Accumulations: len(tab.Accumulations),
// TimeSeries: tab.TimeSeries != nil,
// Snapshot: tab.Snapshot != nil,
//}
tables.Range(func(name string, tab *Table) bool {
infos = append(infos, tab.Info)
return true
})

OK(ctx, infos)
}

func ApiReload(ctx *gin.Context) {
tab := ctx.Param("table")
err := Load(tab)
err := Load(tab, true)
if err != nil {
Error(ctx, err)
return
Expand Down
8 changes: 7 additions & 1 deletion table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ import (

var NotFound = errors.New("找不到记录")

type Info struct {
Id string `json:"id"`
Name string `json:"name,omitempty"`
}

type Table struct {
Name string
Info

Fields []*Field

//Json Schema
Expand Down
Loading

0 comments on commit b3a6df2

Please sign in to comment.