package util import ( "bytes" "encoding/json" "errors" "fmt" "math" "os/exec" "strconv" "time" ) //脚本封装 func RunScript(str string) string { cmd := exec.Command("sh", "-c", str) var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { return "运行失败" } return out.String() } func Sourcelist(buf []byte) (sources []map[string]interface{}, err error) { var info interface{} json.Unmarshal(buf, &info) out, ok := info.(map[string]interface{}) if !ok { return nil, errors.New("http response interface can not change map[string]interface{}") } middle, ok := out["hits"].(map[string]interface{}) if !ok { return nil, errors.New("first hits change error!") } for _, in := range middle["hits"].([]interface{}) { tmpbuf, ok := in.(map[string]interface{}) if !ok { fmt.Println("change to source error!") continue } source, ok := tmpbuf["_source"].(map[string]interface{}) if !ok { fmt.Println("change _source error!") continue } sources = append(sources, source) } return sources, nil } func SourceTotal(buf []byte) (total int, err error) { var info interface{} json.Unmarshal(buf, &info) out, ok := info.(map[string]interface{}) if !ok { return -1, errors.New("http response interface can not change map[string]interface{}") } middle, ok := out["hits"].(map[string]interface{}) if !ok { return -1, errors.New("first total change error!") } tmp, b := middle["total"].(map[string]interface{}) if b != true { v := middle["total"].(float64) t := int(v) return t, nil } value := tmp["value"].(float64) total = int(value) return total, nil } func SourceCreated(buf []byte) (result bool, err error) { var info interface{} json.Unmarshal(buf, &info) out, ok := info.(map[string]interface{}) if !ok { return false, errors.New("http response interface can not change map[string]interface{}") } middle, ok := out["result"].(string) if !ok { return false, errors.New("first total change error!") } if middle == "created" || middle == "updated" { result = true } return result, nil } func SourceDeleted(buf []byte) (total int, err error) { var info interface{} json.Unmarshal(buf, &info) out, ok := info.(map[string]interface{}) if !ok { return -1, errors.New("http response interface can not change map[string]interface{}") } middle, ok := out["deleted"].(float64) if !ok { return -1, errors.New("first total change error!") } total = int(middle) return total, nil } func SourceUpdated(buf []byte) (total int, err error) { var info interface{} json.Unmarshal(buf, &info) out, ok := info.(map[string]interface{}) if !ok { return -1, errors.New("http response interface can not change map[string]interface{}") } //fmt.Println(out) middle, ok := out["updated"].(float64) if !ok { return -1, errors.New("first total change error!") } total = int(middle) return total, nil } func StartTimer(f func()) { for { f() now := time.Now() // 计算下一个零点 next := now.Add(time.Hour * 24) next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location()) t := time.NewTimer(next.Sub(now)) <-t.C t.Stop() } } //日期换算工具 func GetTimeArr(earliestDate string) int { now := time.Now().Format("2006-01-02") fmt.Println(earliestDate, now) a, _ := time.Parse("2006-01-02", earliestDate) b, _ := time.Parse("2006-01-02", now) d := a.Sub(b) date := d.Hours() / 24 return int(math.Abs(date)) } //日期遍历工具 // GetBetweenDates 根据开始日期和结束日期计算出时间段内所有日期 // 参数为日期格式,如:2020-01-01 func GetBetweenDates(sdate, edate string) []string { d := []string{} timeFormatTpl := "2006-01-02" if len(timeFormatTpl) != len(sdate) { timeFormatTpl = timeFormatTpl[0:len(sdate)] } date, err := time.Parse(timeFormatTpl, sdate) if err != nil { // 时间解析,异常 return d } date2, err := time.Parse(timeFormatTpl, edate) if err != nil { // 时间解析,异常 return d } if date2.Before(date) { // 如果结束时间小于开始时间,异常 return d } // 输出日期格式固定 timeFormatTpl = "2006-01-02" date2Str := date2.Format(timeFormatTpl) d = append(d, date.Format(timeFormatTpl)) for { date = date.AddDate(0, 0, 1) dateStr := date.Format(timeFormatTpl) d = append(d, dateStr) if dateStr == date2Str { break } } return d } func IpIntToString(ipInt int) string { ipSegs := make([]string, 4) var len int = len(ipSegs) buffer := bytes.NewBufferString("") for i := 0; i < len; i++ { tempInt := ipInt & 0xFF ipSegs[len-i-1] = strconv.Itoa(tempInt) ipInt = ipInt >> 8 } for i := 0; i < len; i++ { buffer.WriteString(ipSegs[i]) if i < len-1 { buffer.WriteString(".") } } return buffer.String() }