Details fixed about model (#80)

* Details fixed

* Fix typo

* Clean code
This commit is contained in:
陈键冬 2020-04-12 10:04:37 +08:00 committed by GitHub
parent d082b2a2e0
commit 6e17b9a2f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 53 additions and 51 deletions

View File

@ -168,9 +168,7 @@ func (l *LogCollect) Encode() error {
} }
func (l *LogCollect) Decode() error { func (l *LogCollect) Decode() error {
var err error err := json.Unmarshal([]byte(l.TagsStr), &l.Tags)
err = json.Unmarshal([]byte(l.TagsStr), &l.Tags)
if err != nil { if err != nil {
return err return err
} }
@ -192,7 +190,7 @@ func (p *PortCollect) Update() error {
return err return err
} }
if _, err = session.Id(p.Id).AllCols().Update(p); err != nil { if _, err = session.ID(p.Id).AllCols().Update(p); err != nil {
session.Rollback() session.Rollback()
return err return err
} }
@ -230,7 +228,7 @@ func (p *ProcCollect) Update() error {
return err return err
} }
if _, err = session.Id(p.Id).AllCols().Update(p); err != nil { if _, err = session.ID(p.Id).AllCols().Update(p); err != nil {
session.Rollback() session.Rollback()
return err return err
} }
@ -259,7 +257,7 @@ func GetLogCollects() ([]*LogCollect, error) {
return collects, err return collects, err
} }
func (p *LogCollect) Update() error { func (l *LogCollect) Update() error {
session := DB["mon"].NewSession() session := DB["mon"].NewSession()
defer session.Close() defer session.Close()
@ -268,18 +266,18 @@ func (p *LogCollect) Update() error {
return err return err
} }
if _, err = session.Id(p.Id).AllCols().Update(p); err != nil { if _, err = session.ID(l.Id).AllCols().Update(l); err != nil {
session.Rollback() session.Rollback()
return err return err
} }
b, err := json.Marshal(p) b, err := json.Marshal(l)
if err != nil { if err != nil {
session.Rollback() session.Rollback()
return err return err
} }
if err := saveHist(p.Id, "log", "update", p.Creator, string(b), session); err != nil { if err := saveHist(l.Id, "log", "update", l.Creator, string(b), session); err != nil {
session.Rollback() session.Rollback()
return err return err
} }
@ -348,7 +346,7 @@ func GetCollectByNid(collectType string, nids []int64) ([]interface{}, error) {
return res, err return res, err
default: default:
return nil, fmt.Errorf("采集类型不合法") return nil, fmt.Errorf("illegal collectType")
} }
} }
@ -370,10 +368,8 @@ func GetCollectById(collectType string, cid int64) (interface{}, error) {
return collect, err return collect, err
default: default:
return nil, fmt.Errorf("采集类型不合法") return nil, fmt.Errorf("illegal collectType")
} }
return nil, nil
} }
func GetCollectByName(collectType string, name string) (interface{}, error) { func GetCollectByName(collectType string, name string) (interface{}, error) {
@ -420,15 +416,14 @@ func saveHist(id int64, tp string, action, username, body string, session *xorm.
} }
func GetCollectsModel(t string) (interface{}, error) { func GetCollectsModel(t string) (interface{}, error) {
collects := make([]*PortCollect, 0)
switch t { switch t {
case "port": case "port":
collects := []*PortCollect{}
return collects, nil return collects, nil
case "proc": case "proc":
collects := []*ProcCollect{}
return collects, nil return collects, nil
default: default:
return nil, fmt.Errorf("采集类型不合法") return nil, fmt.Errorf("illegal collectType")
} }
} }

View File

@ -221,7 +221,7 @@ func EndpointBindings(endpointIds []int64) ([]EndpointBinding, error) {
ret := make([]EndpointBinding, 0, cnt) ret := make([]EndpointBinding, 0, cnt)
for i := 0; i < cnt; i++ { for i := 0; i < cnt; i++ {
nodeids := h2n[endpoints[i].Id] nodeids := h2n[endpoints[i].Id]
if nodeids == nil || len(nodeids) == 0 { if len(nodeids) == 0 {
continue continue
} }

View File

@ -55,7 +55,7 @@ func NodeGets(where string, args ...interface{}) (nodes []Node, err error) {
} }
func NodeGetsByPaths(paths []string) ([]Node, error) { func NodeGetsByPaths(paths []string) ([]Node, error) {
if paths == nil || len(paths) == 0 { if len(paths) == 0 {
return []Node{}, nil return []Node{}, nil
} }
@ -210,7 +210,7 @@ func (n *Node) Bind(endpointIds []int64, delOld int) error {
} }
func (n *Node) Unbind(hostIds []int64) error { func (n *Node) Unbind(hostIds []int64) error {
if hostIds == nil || len(hostIds) == 0 { if len(hostIds) == 0 {
return nil return nil
} }

View File

@ -24,7 +24,7 @@ func NodeIdsGetByEndpointId(endpointId int64) ([]int64, error) {
} }
func EndpointIdsByNodeIds(nodeIds []int64) ([]int64, error) { func EndpointIdsByNodeIds(nodeIds []int64) ([]int64, error) {
if nodeIds == nil || len(nodeIds) == 0 { if len(nodeIds) == 0 {
return []int64{}, nil return []int64{}, nil
} }
@ -34,7 +34,7 @@ func EndpointIdsByNodeIds(nodeIds []int64) ([]int64, error) {
} }
func NodeEndpointGetByEndpointIds(endpointsIds []int64) ([]NodeEndpoint, error) { func NodeEndpointGetByEndpointIds(endpointsIds []int64) ([]NodeEndpoint, error) {
if endpointsIds == nil || len(endpointsIds) == 0 { if len(endpointsIds) == 0 {
return []NodeEndpoint{}, nil return []NodeEndpoint{}, nil
} }
@ -71,7 +71,7 @@ func EndpointBindingsForMail(endpoints []string) []string {
} }
func NodeEndpointGetByNodeIds(nodeIds []int64) ([]NodeEndpoint, error) { func NodeEndpointGetByNodeIds(nodeIds []int64) ([]NodeEndpoint, error) {
if nodeIds == nil || len(nodeIds) == 0 { if len(nodeIds) == 0 {
return []NodeEndpoint{}, nil return []NodeEndpoint{}, nil
} }

View File

@ -131,7 +131,7 @@ func (s *Stra) Update() error {
return err return err
} }
exists, err := session.Id(s.Id).Get(&obj) exists, err := session.ID(s.Id).Get(&obj)
if err != nil { if err != nil {
session.Rollback() session.Rollback()
return err return err
@ -142,7 +142,7 @@ func (s *Stra) Update() error {
return fmt.Errorf("%d not exists", s.Id) return fmt.Errorf("%d not exists", s.Id)
} }
_, err = session.Id(s.Id).AllCols().Update(s) _, err = session.ID(s.Id).AllCols().Update(s)
if err != nil { if err != nil {
session.Rollback() session.Rollback()
return err return err
@ -187,7 +187,7 @@ func StraDel(id int64) error {
return err return err
} }
exists, err := session.Id(id).Get(&obj) exists, err := session.ID(id).Get(&obj)
if err != nil { if err != nil {
session.Rollback() session.Rollback()
return err return err
@ -198,7 +198,7 @@ func StraDel(id int64) error {
return fmt.Errorf("%d not exists", obj.Id) return fmt.Errorf("%d not exists", obj.Id)
} }
if _, err := session.Id(id).Delete(new(Stra)); err != nil { if _, err := session.ID(id).Delete(new(Stra)); err != nil {
session.Rollback() session.Rollback()
return err return err
} }
@ -349,12 +349,16 @@ func (s *Stra) Encode() error {
} }
s.ExprsStr = string(exprs) s.ExprsStr = string(exprs)
//校验exprs // 校验 exprs
var exprsTmp []Exp var exprsTmp []Exp
err = json.Unmarshal(exprs, &exprsTmp) err = json.Unmarshal(exprs, &exprsTmp)
if err != nil {
return fmt.Errorf("unmarshal exprs err:%v", err)
}
for _, exp := range exprsTmp { for _, exp := range exprsTmp {
if _, found := MathOperators[exp.Eopt]; !found { if _, found := MathOperators[exp.Eopt]; !found {
return fmt.Errorf("unknown exp.eopt:%s", exp) return fmt.Errorf("unknown exp.eopt:%v", exp)
} }
} }
@ -364,9 +368,13 @@ func (s *Stra) Encode() error {
} }
s.TagsStr = string(tags) s.TagsStr = string(tags)
//校验tags // 校验 tags
var tagsTmp []Tag var tagsTmp []Tag
err = json.Unmarshal(tags, &tagsTmp) err = json.Unmarshal(tags, &tagsTmp)
if err != nil {
return fmt.Errorf("unmarshal Tags err:%v", err)
}
for _, tag := range tagsTmp { for _, tag := range tagsTmp {
if tag.Topt != "=" && tag.Topt != "!=" { if tag.Topt != "=" && tag.Topt != "!=" {
return fmt.Errorf("unknown tag.topt") return fmt.Errorf("unknown tag.topt")
@ -470,22 +478,22 @@ func (s *Stra) Decode() error {
func checkDurationString(str string) error { func checkDurationString(str string) error {
slice := strings.Split(str, ":") slice := strings.Split(str, ":")
if len(slice) != 2 { if len(slice) != 2 {
return fmt.Errorf("illegal duration", str) return fmt.Errorf("illegal duration: %s", str)
} }
hour, err := strconv.Atoi(slice[0]) hour, err := strconv.Atoi(slice[0])
if err != nil { if err != nil {
return fmt.Errorf("illegal duration", str) return fmt.Errorf("illegal duration: %s", str)
} }
if hour < 0 || hour > 23 { if hour < 0 || hour > 23 {
return fmt.Errorf("illegal duration", str) return fmt.Errorf("illegal duration: %s", str)
} }
minute, err := strconv.Atoi(slice[1]) minute, err := strconv.Atoi(slice[1])
if err != nil { if err != nil {
return fmt.Errorf("illegal duration", str) return fmt.Errorf("illegal duration: %s", str)
} }
if minute < 0 || minute > 59 { if minute < 0 || minute > 59 {
return fmt.Errorf("illegal duration", str) return fmt.Errorf("illegal duration: %s", str)
} }
return nil return nil

View File

@ -131,7 +131,6 @@ func (u *User) CopyLdapAttr(sr *ldap.SearchResult) {
if attrs.Im != "" { if attrs.Im != "" {
u.Im = sr.Entries[0].GetAttributeValue(attrs.Im) u.Im = sr.Entries[0].GetAttributeValue(attrs.Im)
} }
return
} }
func InitRoot() { func InitRoot() {

View File

@ -65,7 +65,7 @@ func GetCachedAll() string {
func (this *counterCache) GetKeys() []string { func (this *counterCache) GetKeys() []string {
this.RLock() this.RLock()
retList := make([]string, 0) retList := make([]string, 0)
for k, _ := range this.Points { for k := range this.Points {
retList = append(retList, k) retList = append(retList, k)
} }
this.RUnlock() this.RUnlock()
@ -89,7 +89,7 @@ func (this *pushPointsCache) AddCounter(counter string) {
func (this *pushPointsCache) GetCounters() []string { func (this *pushPointsCache) GetCounters() []string {
ret := make([]string, 0) ret := make([]string, 0)
this.RLock() this.RLock()
for k, _ := range this.Counters { for k := range this.Counters {
ret = append(ret, k) ret = append(ret, k)
} }
this.RUnlock() this.RUnlock()

View File

@ -49,7 +49,7 @@ func UpdateConfigsLoop() {
} }
} }
for id, _ := range ManagerConfig { for id := range ManagerConfig {
if _, ok := strategyMap[id]; !ok { //如果策略中不存在,说明用户已删除 if _, ok := strategyMap[id]; !ok { //如果策略中不存在,说明用户已删除
cfg := &ConfigInfo{ cfg := &ConfigInfo{
Id: id, Id: id,

View File

@ -224,7 +224,7 @@ func addFloat64(val *float64, delta float64) (new float64) {
func (this *StrategyCounter) GetTmsList() []int64 { func (this *StrategyCounter) GetTmsList() []int64 {
tmsList := []int64{} tmsList := []int64{}
this.RLock() this.RLock()
for tms, _ := range this.TmsPoints { for tms := range this.TmsPoints {
tmsList = append(tmsList, tms) tmsList = append(tmsList, tms)
} }
this.RUnlock() this.RUnlock()
@ -328,7 +328,7 @@ func (this *GlobalCounter) GetStrategyCountByID(id int64) (*StrategyCounter, err
func (this *GlobalCounter) GetIDs() []int64 { func (this *GlobalCounter) GetIDs() []int64 {
this.RLock() this.RLock()
rList := make([]int64, 0) rList := make([]int64, 0)
for k, _ := range this.StrategyCounts { for k := range this.StrategyCounts {
rList = append(rList, k) rList = append(rList, k)
} }
this.RUnlock() this.RUnlock()

View File

@ -77,6 +77,6 @@ func parseProcName(fname string) (method string, name string, step int, err erro
method = arr[1] method = arr[1]
name = strings.Join(arr[2:len(arr)], "_") name = strings.Join(arr[2:], "_")
return return
} }

View File

@ -63,7 +63,7 @@ func SplitN(m, n int) [][]int {
var res [][]int var res [][]int
if n <= 0 { if n <= 0 {
return [][]int{[]int{0, m}} return [][]int{{0, m}}
} }
for i := 0; i < m; i = i + n { for i := 0; i < m; i = i + n {

View File

@ -82,7 +82,7 @@ func (n *NodeMap) GetNodes() []string {
n.RLock() n.RLock()
defer n.RUnlock() defer n.RUnlock()
var nodes []string var nodes []string
for node, _ := range n.data { for node := range n.data {
nodes = append(nodes, node) nodes = append(nodes, node)
} }
return nodes return nodes

View File

@ -9,10 +9,10 @@ import (
var ( var (
validFuncName = map[string]struct{}{ validFuncName = map[string]struct{}{
"sum": struct{}{}, "sum": {},
"avg": struct{}{}, "avg": {},
"max": struct{}{}, "max": {},
"min": struct{}{}, "min": {},
} }
) )

View File

@ -132,7 +132,7 @@ func (c *ConnPools) UpdatePools(addrs []string) []string {
c.M[addr] = createOnePool(addr, addr, ct, c.MaxConns, c.MaxIdle) c.M[addr] = createOnePool(addr, addr, ct, c.MaxConns, c.MaxIdle)
} }
for addr, _ := range c.M { //删除旧的地址 for addr := range c.M { //删除旧的地址
if _, exists := addrMap[addr]; !exists { if _, exists := addrMap[addr]; !exists {
delete(c.M, addr) delete(c.M, addr)
} }

View File

@ -49,7 +49,7 @@ func RebuildAllIndex(params ...[]string) error {
defer UpdateIndexLock.Release() defer UpdateIndexLock.Release()
var pushCnt = 0 var pushCnt = 0
var oldCnt = 0 var oldCnt = 0
for idx, _ := range IndexedItemCacheBigMap { for idx := range IndexedItemCacheBigMap {
keys := IndexedItemCacheBigMap[idx].Keys() keys := IndexedItemCacheBigMap[idx].Keys()
i := 0 i := 0

View File

@ -44,7 +44,7 @@ func updateIndexIncr() int {
ret := 0 ret := 0
aggrNum := 200 aggrNum := 200
for idx, _ := range UnIndexedItemCacheBigMap { for idx := range UnIndexedItemCacheBigMap {
if UnIndexedItemCacheBigMap[idx] == nil || UnIndexedItemCacheBigMap[idx].Size() <= 0 { if UnIndexedItemCacheBigMap[idx] == nil || UnIndexedItemCacheBigMap[idx].Size() <= 0 {
continue continue
} }

View File

@ -28,7 +28,7 @@ func SplitN(m, n int) [][]int {
var res [][]int var res [][]int
if n <= 0 { if n <= 0 {
return [][]int{[]int{0, m}} return [][]int{{0, m}}
} }
for i := 0; i < m; i = i + n { for i := 0; i < m; i = i + n {