Dashboard search by tag, and tag cloud now works, god dam I hate SQL

This commit is contained in:
Torkel Ödegaard
2015-01-07 12:37:24 +01:00
parent bcdbec61d7
commit 1ae52d2472
6 changed files with 126 additions and 40 deletions
+61 -10
View File
@@ -35,6 +35,22 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
_, err = sess.Id(dash.Id).Update(dash)
}
// delete existing tabs
_, err = sess.Exec("DELETE FROM dashboard_tag WHERE dashboard_id=?", dash.Id)
if err != nil {
return err
}
// insert new tags
tags := dash.GetTags()
if len(tags) > 0 {
tagRows := make([]DashboardTag, len(tags))
for _, tag := range tags {
tagRows = append(tagRows, DashboardTag{Term: tag, DashboardId: dash.Id})
}
sess.InsertMulti(&tagRows)
}
cmd.Result = dash
return err
@@ -55,24 +71,59 @@ func GetDashboard(query *m.GetDashboardQuery) error {
return nil
}
type DashboardSearchProjection struct {
Id int64
Title string
Slug string
Term string
}
func SearchDashboards(query *m.SearchDashboardsQuery) error {
titleQuery := "%" + query.Query + "%"
titleQuery := "%" + query.Title + "%"
sess := x.Limit(100, 0).Where("account_id=? AND title LIKE ?", query.AccountId, titleQuery)
sess.Table("Dashboard")
sess := x.Table("dashboard")
sess.Join("LEFT OUTER", "dashboard_tag", "dashboard.id=dashboard_tag.dashboard_id")
sess.Where("account_id=? AND title LIKE ?", query.AccountId, titleQuery)
sess.Cols("dashboard.id", "dashboard.title", "dashboard.slug", "dashboard_tag.term")
sess.Limit(100, 0)
query.Result = make([]m.DashboardSearchHit, 0)
err := sess.Find(&query.Result)
if len(query.Tag) > 0 {
sess.And("dashboard_tag.term=?", query.Tag)
}
var res []DashboardSearchProjection
err := sess.Find(&res)
if err != nil {
return err
}
query.Result = make([]*m.DashboardSearchHit, 0)
hits := make(map[int64]*m.DashboardSearchHit)
for _, item := range res {
hit, exists := hits[item.Id]
if !exists {
hit = &m.DashboardSearchHit{
Title: item.Title,
Slug: item.Slug,
Tags: []string{},
}
query.Result = append(query.Result, hit)
hits[item.Id] = hit
}
if len(item.Term) > 0 {
hit.Tags = append(hit.Tags, item.Term)
}
}
return err
}
func GetDashboardTags(query *m.GetDashboardTagsQuery) error {
query.Result = []m.DashboardTagCloudItem{
m.DashboardTagCloudItem{Term: "test", Count: 10},
m.DashboardTagCloudItem{Term: "prod", Count: 20},
}
return nil
sess := x.Sql("select count() as count, term from dashboard_tag group by term")
err := sess.Find(&query.Result)
return err
}
func DeleteDashboard(cmd *m.DeleteDashboardCommand) error {
+24 -4
View File
@@ -51,7 +51,7 @@ func TestDashboardDataAccess(t *testing.T) {
Convey("Should be able to search for dashboard", func() {
query := m.SearchDashboardsQuery{
Query: "%test%",
Title: "%test%",
AccountId: 1,
}
@@ -59,6 +59,20 @@ func TestDashboardDataAccess(t *testing.T) {
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 1)
hit := query.Result[0]
So(len(hit.Tags), ShouldEqual, 2)
})
Convey("Should be able to search for dashboards using tags", func() {
query1 := m.SearchDashboardsQuery{Tag: "webapp", AccountId: 1}
query2 := m.SearchDashboardsQuery{Tag: "tagdoesnotexist", AccountId: 1}
err := SearchDashboards(&query1)
err = SearchDashboards(&query2)
So(err, ShouldBeNil)
So(len(query1.Result), ShouldEqual, 1)
So(len(query2.Result), ShouldEqual, 0)
})
Convey("Should not be able to save dashboard with same name", func() {
@@ -67,7 +81,7 @@ func TestDashboardDataAccess(t *testing.T) {
Dashboard: map[string]interface{}{
"id": nil,
"title": "test dash 23",
"tags": make([]interface{}, 0),
"tags": []interface{}{},
},
}
@@ -75,8 +89,14 @@ func TestDashboardDataAccess(t *testing.T) {
So(err, ShouldNotBeNil)
})
Convey("Should be able to get dashboard tags", func() {
query := m.GetDashboardTagsQuery{}
err := GetDashboardTags(&query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 3)
})
})
})
}
+7 -1
View File
@@ -27,11 +27,17 @@ var (
UseSQLite3 bool
)
type DashboardTag struct {
Id int64
DashboardId int64
Term string
}
func init() {
tables = make([]interface{}, 0)
tables = append(tables, new(m.Account), new(m.Dashboard),
new(m.Collaborator), new(m.DataSource))
new(m.Collaborator), new(m.DataSource), new(DashboardTag))
}
func Init() {