swirl/dao/bolt/chart.go

93 lines
2.2 KiB
Go
Raw Normal View History

2018-04-27 13:17:00 +00:00
package bolt
import (
2021-12-06 12:24:22 +00:00
"context"
"sort"
"github.com/cuigh/swirl/misc"
2018-04-27 13:17:00 +00:00
"github.com/cuigh/swirl/model"
)
2021-12-06 12:24:22 +00:00
func (d *Dao) ChartList(ctx context.Context, title, dashboard string, pageIndex, pageSize int) (charts []*model.Chart, count int, err error) {
2018-04-27 13:17:00 +00:00
err = d.each("chart", func(v Value) error {
chart := &model.Chart{}
err = v.Unmarshal(chart)
if err == nil {
2021-12-06 12:24:22 +00:00
match := true
if title != "" {
match = matchAny(title, chart.Title)
}
if match && dashboard != "" {
match = matchAny(chart.Dashboard, dashboard, "")
}
if match {
charts = append(charts, chart)
}
2018-04-27 13:17:00 +00:00
}
return err
})
2021-12-06 12:24:22 +00:00
if err == nil {
count = len(charts)
sort.Slice(charts, func(i, j int) bool {
return charts[i].CreatedAt.After(charts[j].UpdatedAt)
})
start, end := misc.Page(count, pageIndex, pageSize)
charts = charts[start:end]
}
2018-04-27 13:17:00 +00:00
return
}
2021-12-06 12:24:22 +00:00
func (d *Dao) ChartCreate(ctx context.Context, chart *model.Chart) (err error) {
return d.update("chart", chart.ID, chart)
2018-04-27 13:17:00 +00:00
}
2021-12-06 12:24:22 +00:00
func (d *Dao) ChartGet(ctx context.Context, name string) (chart *model.Chart, err error) {
2018-04-27 13:17:00 +00:00
var v Value
v, err = d.get("chart", name)
2021-12-06 12:24:22 +00:00
if err == nil && v != nil {
chart = &model.Chart{}
err = v.Unmarshal(chart)
2018-04-27 13:17:00 +00:00
}
return
}
2021-12-06 12:24:22 +00:00
func (d *Dao) ChartBatch(ctx context.Context, names ...string) (charts []*model.Chart, err error) {
2018-04-27 13:17:00 +00:00
err = d.slice("chart", func(v Value) error {
chart := &model.Chart{}
err = v.Unmarshal(chart)
if err == nil {
charts = append(charts, chart)
}
return err
}, names...)
return
}
2021-12-06 12:24:22 +00:00
func (d *Dao) ChartUpdate(ctx context.Context, chart *model.Chart) (err error) {
return d.update("chart", chart.ID, chart)
2018-04-27 13:17:00 +00:00
}
2021-12-06 12:24:22 +00:00
func (d *Dao) ChartDelete(ctx context.Context, name string) (err error) {
2018-04-27 13:17:00 +00:00
return d.delete("chart", name)
}
2021-12-06 12:24:22 +00:00
func (d *Dao) DashboardGet(ctx context.Context, name, key string) (dashboard *model.Dashboard, err error) {
cd := &model.Dashboard{
2018-04-27 13:17:00 +00:00
Name: name,
Key: key,
}
var v Value
2018-06-25 10:38:34 +00:00
v, err = d.get("dashboard", cd.ID())
if v != nil {
if err = v.Unmarshal(cd); err == nil {
return cd, nil
2018-04-27 13:17:00 +00:00
}
}
2018-06-25 10:38:34 +00:00
return nil, err
2018-04-27 13:17:00 +00:00
}
2021-12-06 12:24:22 +00:00
func (d *Dao) DashboardUpdate(ctx context.Context, dashboard *model.Dashboard) (err error) {
2018-04-27 13:17:00 +00:00
return d.update("dashboard", dashboard.ID(), dashboard)
}