diff --git a/api/host_groups.go b/api/host_groups.go index c5abf9d..266021c 100644 --- a/api/host_groups.go +++ b/api/host_groups.go @@ -58,7 +58,7 @@ func listProductHostGroupHosts(c *gin.Context) { c.GetInt("limit"), ) response["total"] = total - response["hosts"] = warpHosts(hosts) + response["hosts"] = warpProductHosts(c.GetInt("product_id"), hosts) } func listNotInProductHostGroupHosts(c *gin.Context) { diff --git a/api/hosts.go b/api/hosts.go index e46655a..33b47e2 100644 --- a/api/hosts.go +++ b/api/hosts.go @@ -132,17 +132,37 @@ func warpHosts(hosts []Host) []WarpHost { return warpHosts } +func warpProductHosts(productID int, hosts []Host) []WarpHost { + warpHosts := []WarpHost{} + for _, host := range hosts { + warpHosts = append(warpHosts, warpProductHost(productID, host)) + } + return warpHosts +} + func warpHost(host Host) WarpHost { pluginCnt, _ := mydb.getHostPlugins(host.ID, false, "", "", 0, 0) return WarpHost{ host, mydb.getHostAppNames(host.ID), pluginCnt, - mydb.getHostHostGroups(host.ID), + mydb.getHostHostGroups(0, host.ID), mydb.getHostProducts(host.ID), } } +func warpProductHost(productID int, host Host) WarpHost { + pluginCnt, _ := mydb.getHostPlugins(host.ID, false, "", "", 0, 0) + return WarpHost{ + host, + mydb.getHostAppNames(host.ID), + pluginCnt, + mydb.getHostHostGroups(productID, host.ID), + nil, + // mydb.getHostProducts(host.ID), + } +} + func listHostPlugins(c *gin.Context) { response := gin.H{"code": http.StatusOK} defer c.JSON(http.StatusOK, response) diff --git a/api/mysql.go b/api/mysql.go index a9f3f30..6529d10 100644 --- a/api/mysql.go +++ b/api/mysql.go @@ -1256,14 +1256,20 @@ func (d *db) getHostAppNames(hostID string) []string { return appNames } -func (d *db) getHostHostGroups(hostID string) []HostGroup { +func (d *db) getHostHostGroups(productID int, hostID string) []HostGroup { var ( hostGroups = make([]HostGroup, 0) err error + rawSQL string ) - rawSQL := fmt.Sprintf("select id, name, description, creator, DATE_FORMAT(create_at,'%s') as create_at,"+ + rawSQL = fmt.Sprintf("select id, name, description, creator, DATE_FORMAT(create_at,'%s') as create_at,"+ "DATE_FORMAT(update_at,'%s') as update_at from host_group where id in (select host_group_id from host_group_host where host_id='%s')", dbDateFormat, dbDateFormat, hostID) + if productID != 0 { + rawSQL = fmt.Sprintf("select id, name, description, creator, DATE_FORMAT(create_at,'%s') as create_at,"+ + "DATE_FORMAT(update_at,'%s') as update_at from host_group where product_id = %d and id in (select host_group_id from host_group_host where host_id='%s')", + dbDateFormat, dbDateFormat, productID, hostID) + } log.Println(rawSQL) if err = d.Select(&hostGroups, rawSQL); err != nil { log.Println(err) diff --git a/api/product.go b/api/product.go index 17b781b..9a8cdf4 100644 --- a/api/product.go +++ b/api/product.go @@ -226,8 +226,9 @@ func listProductHosts(c *gin.Context) { order = "status asc" } var ( - noGroup bool - err error + noGroup bool + err error + productID = c.GetInt("product_id") ) noGroup, err = strconv.ParseBool(c.DefaultQuery("no_group", "false")) if err != nil { @@ -235,7 +236,7 @@ func listProductHosts(c *gin.Context) { } total, hosts := mydb.getProductHosts( - c.GetInt("product_id"), + productID, noGroup, c.GetBool("paging"), c.DefaultQuery("query", ""), @@ -244,7 +245,7 @@ func listProductHosts(c *gin.Context) { c.GetInt("limit"), ) response["total"] = total - response["hosts"] = warpHosts(hosts) + response["hosts"] = warpProductHosts(productID, hosts) } func listNotInProductHosts(c *gin.Context) {