swirl/dao/bolt/chart.go

101 lines
2.4 KiB
Go
Raw Permalink Normal View History

2018-04-27 13:17:00 +00:00
package bolt
import (
2021-12-06 12:24:22 +00:00
"context"
"sort"
2021-12-16 12:23:08 +00:00
"time"
2021-12-06 12:24:22 +00:00
2021-12-23 11:28:31 +00:00
"github.com/cuigh/swirl/dao"
2021-12-06 12:24:22 +00:00
"github.com/cuigh/swirl/misc"
2018-04-27 13:17:00 +00:00
)
2021-12-16 08:11:16 +00:00
const (
Chart = "chart"
Dashboard = "dashboard"
)
2021-12-23 11:28:31 +00:00
func (d *Dao) ChartSearch(ctx context.Context, args *dao.ChartSearchArgs) (charts []*dao.Chart, count int, err error) {
2021-12-16 08:11:16 +00:00
err = d.each(Chart, func(v []byte) error {
2021-12-23 11:28:31 +00:00
chart := &dao.Chart{}
2021-12-16 08:11:16 +00:00
if err = decode(v, chart); err == nil {
2021-12-06 12:24:22 +00:00
match := true
2021-12-16 08:11:16 +00:00
if args.Title != "" {
match = matchAny(args.Title, chart.Title)
2021-12-06 12:24:22 +00:00
}
2021-12-16 08:11:16 +00:00
if match && args.Dashboard != "" {
match = matchAny(chart.Dashboard, args.Dashboard, "")
2021-12-06 12:24:22 +00:00
}
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 {
2021-12-16 12:23:08 +00:00
return time.Time(charts[i].UpdatedAt).After(time.Time(charts[j].UpdatedAt))
2021-12-06 12:24:22 +00:00
})
2021-12-16 08:11:16 +00:00
start, end := misc.Page(count, args.PageIndex, args.PageSize)
2021-12-06 12:24:22 +00:00
charts = charts[start:end]
}
2018-04-27 13:17:00 +00:00
return
}
2021-12-23 11:28:31 +00:00
func (d *Dao) ChartCreate(ctx context.Context, chart *dao.Chart) (err error) {
2021-12-16 08:11:16 +00:00
return d.replace(Chart, chart.ID, chart)
2018-04-27 13:17:00 +00:00
}
2021-12-23 11:28:31 +00:00
func (d *Dao) ChartGet(ctx context.Context, name string) (chart *dao.Chart, err error) {
chart = &dao.Chart{}
2021-12-16 08:11:16 +00:00
err = d.get(Chart, name, chart)
if err == ErrNoRecords {
return nil, nil
} else if err != nil {
chart = nil
2018-04-27 13:17:00 +00:00
}
return
}
2021-12-23 11:28:31 +00:00
func (d *Dao) ChartGetBatch(ctx context.Context, ids ...string) (charts []*dao.Chart, err error) {
2021-12-16 08:11:16 +00:00
err = d.slice(Chart, func(v []byte) error {
2021-12-23 11:28:31 +00:00
chart := &dao.Chart{}
2021-12-16 08:11:16 +00:00
if err = decode(v, chart); err == nil {
2018-04-27 13:17:00 +00:00
charts = append(charts, chart)
}
return err
2021-12-16 08:11:16 +00:00
}, ids...)
2018-04-27 13:17:00 +00:00
return
}
2021-12-23 11:28:31 +00:00
func (d *Dao) ChartUpdate(ctx context.Context, chart *dao.Chart) (err error) {
old := &dao.Chart{}
2021-12-16 08:11:16 +00:00
return d.update(Chart, chart.ID, old, func() interface{} {
chart.CreatedAt = old.CreatedAt
chart.CreatedBy = old.CreatedBy
return 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) {
2021-12-16 08:11:16 +00:00
return d.delete(Chart, name)
2018-04-27 13:17:00 +00:00
}
2021-12-23 11:28:31 +00:00
func (d *Dao) DashboardGet(ctx context.Context, name, key string) (dashboard *dao.Dashboard, err error) {
dashboard = &dao.Dashboard{
2018-04-27 13:17:00 +00:00
Name: name,
Key: key,
}
2021-12-16 08:11:16 +00:00
err = d.get(Dashboard, dashboard.ID(), dashboard)
if err == ErrNoRecords {
return nil, nil
} else if err != nil {
dashboard = nil
2018-04-27 13:17:00 +00:00
}
2021-12-16 08:11:16 +00:00
return
2018-04-27 13:17:00 +00:00
}
2021-12-23 11:28:31 +00:00
func (d *Dao) DashboardUpdate(ctx context.Context, dashboard *dao.Dashboard) (err error) {
2021-12-16 08:11:16 +00:00
return d.replace(Dashboard, dashboard.ID(), dashboard)
2018-04-27 13:17:00 +00:00
}