swirl/dao/mongo/chart.go

89 lines
2.3 KiB
Go
Raw Normal View History

2018-03-22 08:13:54 +00:00
package mongo
import (
2021-12-06 12:24:22 +00:00
"context"
2018-03-22 08:13:54 +00:00
"github.com/cuigh/swirl/model"
2021-12-06 12:24:22 +00:00
"go.mongodb.org/mongo-driver/bson"
)
const (
Chart = "chart"
Dashboard = "dashboard"
2018-03-22 08:13:54 +00:00
)
2021-12-16 08:11:16 +00:00
func (d *Dao) ChartSearch(ctx context.Context, args *model.ChartSearchArgs) (charts []*model.Chart, count int, err error) {
2021-12-06 12:24:22 +00:00
filter := bson.M{}
2021-12-16 08:11:16 +00:00
if args.Title != "" {
filter["title"] = args.Title
2021-12-06 12:24:22 +00:00
}
2021-12-16 08:11:16 +00:00
if args.Dashboard != "" {
filter["dashboard"] = bson.M{"$in": []string{"", args.Dashboard}}
2021-12-06 12:24:22 +00:00
}
2021-12-16 08:11:16 +00:00
opts := searchOptions{filter: filter, sorter: bson.M{"updated_at": -1}, pageIndex: args.PageIndex, pageSize: args.PageSize}
2021-12-06 12:24:22 +00:00
charts = []*model.Chart{}
count, err = d.search(ctx, Chart, opts, &charts)
2018-03-22 08:13:54 +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.create(ctx, Chart, chart)
2018-03-22 08:13:54 +00:00
}
2021-12-06 12:24:22 +00:00
func (d *Dao) ChartGet(ctx context.Context, id string) (chart *model.Chart, err error) {
chart = &model.Chart{}
found, err := d.find(ctx, Chart, id, chart)
if !found {
return nil, err
}
2018-03-22 08:13:54 +00:00
return
}
2021-12-16 08:11:16 +00:00
func (d *Dao) ChartGetBatch(ctx context.Context, names ...string) (charts []*model.Chart, err error) {
2021-12-06 12:24:22 +00:00
charts = []*model.Chart{}
err = d.fetch(ctx, Chart, bson.M{"_id": bson.M{"$in": names}}, &charts)
2018-03-22 08:13:54 +00:00
return
}
2021-12-06 12:24:22 +00:00
func (d *Dao) ChartUpdate(ctx context.Context, chart *model.Chart) (err error) {
2021-12-15 12:15:25 +00:00
update := bson.M{
"$set": bson.M{
"title": chart.Title,
"desc": chart.Description,
"width": chart.Width,
"height": chart.Height,
"unit": chart.Unit,
"dashboard": chart.Dashboard,
"type": chart.Type,
"margin": chart.Margin,
"metrics": chart.Metrics,
"updated_at": chart.UpdatedAt,
2021-12-16 08:11:16 +00:00
"updated_by": chart.UpdatedBy,
2021-12-15 12:15:25 +00:00
},
}
return d.update(ctx, Chart, chart.ID, update)
2018-03-22 08:13:54 +00:00
}
2021-12-06 12:24:22 +00:00
func (d *Dao) ChartDelete(ctx context.Context, id string) (err error) {
return d.delete(ctx, Chart, id)
2018-03-22 08:13:54 +00:00
}
2018-03-27 08:32:30 +00:00
2021-12-06 12:24:22 +00:00
func (d *Dao) DashboardGet(ctx context.Context, name, key string) (dashboard *model.Dashboard, err error) {
dashboard = &model.Dashboard{
Name: name,
Key: key,
}
found, err := d.find(ctx, Dashboard, dashboard.ID(), dashboard)
if !found {
return nil, err
}
2018-03-27 08:32:30 +00:00
return
}
2021-12-06 12:24:22 +00:00
func (d *Dao) DashboardUpdate(ctx context.Context, dashboard *model.Dashboard) (err error) {
update := bson.M{
"$set": dashboard,
}
2021-12-15 12:15:25 +00:00
return d.upsert(ctx, Dashboard, dashboard.ID(), update)
2018-03-27 08:32:30 +00:00
}