forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type HostField struct { | ||
Id int64 `json:"id"` | ||
FieldIdent string `json:"field_ident"` | ||
FieldName string `json:"field_name"` | ||
FieldType string `json:"field_type"` | ||
FieldRequired int `json:"field_required"` | ||
FieldExtra string `json:"field_extra"` | ||
FieldCate string `json:"field_cate"` | ||
} | ||
|
||
func (hf *HostField) Validate() error { | ||
if len(hf.FieldIdent) > 255 { | ||
return fmt.Errorf("field ident too long") | ||
} | ||
|
||
if len(hf.FieldName) > 255 { | ||
return fmt.Errorf("field name too long") | ||
} | ||
|
||
if len(hf.FieldExtra) > 2048 { | ||
return fmt.Errorf("field extra too long") | ||
} | ||
|
||
if len(hf.FieldCate) > 255 { | ||
return fmt.Errorf("field cate too long") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func HostFieldNew(objPtr *HostField) error { | ||
if err := objPtr.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
cnt, err := DB["ams"].Where("field_ident=?", objPtr.FieldIdent).Count(new(HostField)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if cnt > 0 { | ||
return fmt.Errorf("%s already exists", objPtr.FieldIdent) | ||
} | ||
|
||
_, err = DB["ams"].Insert(objPtr) | ||
return err | ||
} | ||
|
||
func (hf *HostField) Update(cols ...string) error { | ||
if err := hf.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
_, err := DB["ams"].Where("id=?", hf.Id).Cols(cols...).Update(hf) | ||
return err | ||
} | ||
|
||
func HostFieldGet(where string, args ...interface{}) (*HostField, error) { | ||
var obj HostField | ||
has, err := DB["ams"].Where(where, args...).Get(&obj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !has { | ||
return nil, nil | ||
} | ||
|
||
return &obj, nil | ||
} | ||
|
||
func (hf *HostField) Del() error { | ||
_, err := DB["ams"].Where("id=?", hf.Id).Delete(new(HostField)) | ||
return err | ||
} | ||
|
||
// HostFieldGets 条数非常少,全部返回 | ||
func HostFieldGets() ([]HostField, error) { | ||
var objs []HostField | ||
err := DB["ams"].OrderBy("field_cate, field_ident").Find(&objs) | ||
return objs, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type HostFieldValue struct { | ||
Id int64 `json:"id"` | ||
HostId int64 `json:"host_id"` | ||
FieldIdent string `json:"field_ident"` | ||
FieldValue string `json:"field_value"` | ||
} | ||
|
||
func (hfv *HostFieldValue) Validate() error { | ||
if len(hfv.FieldValue) > 1024 { | ||
return fmt.Errorf("field value too long") | ||
} | ||
return nil | ||
} | ||
|
||
// HostFieldValueGets 条数非常少,全部返回 | ||
func HostFieldValueGets(hostId int64) ([]HostFieldValue, error) { | ||
var objs []HostFieldValue | ||
err := DB["ams"].Where("host_id = ?", hostId).OrderBy("field_ident").Find(&objs) | ||
return objs, err | ||
} | ||
|
||
func HostFieldValuePuts(hostId int64, objs []HostFieldValue) error { | ||
count := len(objs) | ||
|
||
session := DB["ams"].NewSession() | ||
defer session.Close() | ||
|
||
for i := 0; i < count; i++ { | ||
num, err := session.Where("host_id = ? and field_ident = ?", hostId, objs[i].FieldIdent).Count(new(HostFieldValue)) | ||
if err != nil { | ||
return fmt.Errorf("count host_field_value fail: %v", err) | ||
} | ||
|
||
if num > 0 { | ||
_, err = session.Exec("UPDATE host_field_value SET field_value = ? WHERE host_id = ? and field_ident = ?", objs[i].FieldValue, hostId, objs[i].FieldIdent) | ||
if err != nil { | ||
return fmt.Errorf("update host_field_value fail: %v", err) | ||
} | ||
} else { | ||
_, err = session.InsertOne(HostFieldValue{ | ||
HostId: hostId, | ||
FieldIdent: objs[i].FieldIdent, | ||
FieldValue: objs[i].FieldValue, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("insert host_field_value fail: %v", err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package http | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/didi/nightingale/src/models" | ||
) | ||
|
||
func hostFieldsGets(c *gin.Context) { | ||
lst, err := models.HostFieldGets() | ||
renderData(c, lst, err) | ||
} | ||
|
||
func hostFieldGet(c *gin.Context) { | ||
obj, err := models.HostFieldGet("id = ?", urlParamInt64(c, "id")) | ||
renderData(c, obj, err) | ||
} | ||
|
||
func hostFieldGets(c *gin.Context) { | ||
lst, err := models.HostFieldValueGets(urlParamInt64(c, "id")) | ||
renderData(c, lst, err) | ||
} | ||
|
||
func hostFieldPuts(c *gin.Context) { | ||
var objs []models.HostFieldValue | ||
bind(c, &objs) | ||
|
||
loginUser(c).CheckPermGlobal("ams_host_modify") | ||
|
||
renderMessage(c, models.HostFieldValuePuts(urlParamInt64(c, "id"), objs)) | ||
} |