package service
|
|
import (
|
"basic.com/valib/logger.git"
|
"encoding/json"
|
"fmt"
|
"strconv"
|
"strings"
|
"time"
|
"webserver/cache"
|
"webserver/extend/config"
|
"webserver/extend/esutil"
|
)
|
|
func GetVerificationData() []map[string]interface{} {
|
localConf, err2 := cache.GetServerInfo()
|
if err2 !=nil || localConf.AlarmIp == "" || localConf.ServerId == "" {
|
logger.Debug("localConfig is wrong!!!")
|
return nil
|
}
|
url := "http://" + localConf.AlarmIp + ":" + strconv.Itoa(int(localConf.AlarmPort)) +
|
"/" + config.EsInfo.EsIndex.DbTables.IndexName + "/_search"
|
|
jsonDSL := `{
|
"query": {
|
"bool": {
|
"filter": [{
|
"term": {
|
"isDelete": "0"
|
}
|
},
|
{
|
"terms": {
|
"analyServerId": ["` + localConf.ServerId + `",""]
|
}
|
}
|
]
|
}
|
},
|
"size": 10000
|
}
|
`
|
buf, err := esutil.EsReq("POST", url, []byte(jsonDSL))
|
if err != nil {
|
logger.Debug(err)
|
}
|
|
sources, err := esutil.Sourcelist(buf)
|
if err != nil {
|
logger.Debug(err)
|
}
|
return sources
|
}
|
|
func VerificationTime(source []map[string]interface{}) map[string][]string {
|
resultInfo := make(map[string][]string, 0)
|
currentTime := time.Now()
|
for _, v := range source {
|
id := v["id"].(string)
|
startTime := v["startTime"].(string)
|
endTime := ""
|
if v["endTime"] != nil {
|
endTime = v["endTime"].(string)
|
}
|
st, _ := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
|
if endTime != "" {
|
et, _ := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
|
if et.After(currentTime) && st.Before(currentTime) {
|
resultInfo["effective"] = append(resultInfo["effective"], id)
|
} else {
|
resultInfo["invalid"] = append(resultInfo["invalid"], id)
|
}
|
} else if st.Before(currentTime) && endTime == "" {
|
resultInfo["effective"] = append(resultInfo["effective"], id)
|
} else {
|
resultInfo["invalid"] = append(resultInfo["invalid"], id)
|
}
|
}
|
//fmt.Println(resultInfo)
|
return resultInfo
|
}
|
|
func UpdateEnableStatus(id []string, flag bool) (message string) {
|
ids := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1)
|
status := 0
|
if flag == true {
|
status = 1
|
}
|
enable := strconv.Itoa(status)
|
localConf, err2 := cache.GetServerInfo()
|
if err2 !=nil || localConf.AlarmIp == "" || localConf.ServerId == "" {
|
logger.Debug("localConfig is wrong!!!")
|
return "localConf wrong"
|
}
|
url := "http://" + localConf.AlarmIp + ":" + strconv.Itoa(int(localConf.AlarmPort)) +
|
"/" + config.EsInfo.EsIndex.DbTables.IndexName + "/_update_by_query?refresh"
|
jsonDsl := `
|
{
|
"script": {
|
"lang": "painless",
|
"inline": "ctx._source.enable = ` + enable + `"
|
},
|
"query": {
|
"terms": {
|
"id": ["` + ids + `"]
|
}
|
}
|
}
|
`
|
buf, err := esutil.EsReq("POST", url, []byte(jsonDsl))
|
if err != nil {
|
logger.Debug("http request info is err!")
|
message = "修改失败"
|
}
|
var info interface{}
|
json.Unmarshal(buf, &info)
|
out, ok := info.(map[string]interface{})
|
if !ok {
|
logger.Debug("http response interface can not change map[string]interface{}")
|
message = "修改失败"
|
}
|
middle, ok := out["updated"].(float64)
|
if !ok {
|
logger.Debug("first result change error!")
|
message = "修改失败"
|
}
|
if middle > 0 {
|
logger.Debug("修改成功")
|
//if flag {//生效的
|
// discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
|
// Type: protomsg.EsCacheChanged_T_DbTable,
|
// PersonId: "",
|
// TableId: id,
|
// Feature: "",
|
// Action: protomsg.DbAction_Insert,
|
// })
|
//} else {//失效的
|
// discovery.AddDbMessage(&protomsg.EsPersonCacheChange{
|
// Type: protomsg.EsCacheChanged_T_DbTable,
|
// PersonId: "",
|
// TableId: id,
|
// Feature: "",
|
// Action: protomsg.DbAction_Insert,
|
// })
|
//}
|
message = "修改成功"
|
}
|
return message
|
}
|
|
func InitEnableStatus() (messageInfo string) {
|
verificationData := GetVerificationData()
|
verificationTime := VerificationTime(verificationData)
|
idOfEffective := verificationTime["effective"]
|
idOfinvalid := verificationTime["invalid"]
|
mesInfo := false
|
effectiveMesInfo := ""
|
invalidMesInfo := ""
|
if len(idOfEffective) > 0 {
|
effectiveMesInfo = UpdateEnableStatus(idOfEffective, true)
|
} else {
|
effectiveMesInfo = "修改成功"
|
}
|
if len(idOfinvalid) > 0 {
|
invalidMesInfo = UpdateEnableStatus(idOfinvalid, false)
|
} else {
|
invalidMesInfo = "修改成功"
|
}
|
if effectiveMesInfo == "修改成功" && invalidMesInfo == "修改成功" {
|
mesInfo = true
|
}
|
if mesInfo == true {
|
messageInfo = "修改成功"
|
}
|
return messageInfo
|
}
|