utils

package
v0.0.0-...-630b3dd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2021 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DateLayout      = "2006-01-02"
	DateTimeLayout  = "2006-01-02 15:04:05"
	TimeLayout      = "15:04:05"
	BuildTimeLayout = "2006.0102.150405"
	TimestampLayout = "20060102150405"
)

日期格式

Variables

View Source
var Config = struct {
	Host        string `default:"" env:"host"`
	Appid       string `default:"" env:"appid"`
	Appsecret   string `default:"" env:"appsecret"`
	DB          string `default:"" env:"db"`
	Exts        string `default:"" env:"exts"`
	Imgexts     string `default:"" env:"imagexts"`
	Root        string `default:"" env:"root"`
	Outdir      string `default:"" env:"outdir"`
	Timeout     int64  `default:"" env:"timeout"`
	Timeover    int64  `default:"" env:"timeover"`
	Isresizeimg bool   `default:"false" env:"isresizeimg"`
	Devicesize  int    `default:"5" env:"devicesize"`
	Ftp         struct {
		Ip       string `default:"" env:"FtpIp"`
		Username string `default:"" env:"FtpUsername"`
		Password string `default:"" env:"FtpPassword"`
	}
	Device struct {
		Timetype     string `default:"m" env:"DeviceTimetype"`
		Timeduration int64  `default:"3" env:"DeviceTimeduration"`
	}
	Data struct {
		Timetype     string `default:"h" env:"DataTimetype"`
		Timeduration int64  `default:"1" env:"DataTimeduration"`
	}
	Restful struct {
		Timetype     string `default:"m" env:"RestfulTimetype"`
		Timeduration int64  `default:"3" env:"RestfulTimeduration"`
	}
	Service struct {
		Timetype     string `default:"m" env:"ServiceTimetype"`
		Timeduration int64  `default:"3" env:"ServiceTimeduration"`
	}
	Port struct {
		Timetype     string `default:"m" env:"PortTimetype"`
		Timeduration int64  `default:"3" env:"PortTimeduration"`
	}
	DeviceImg struct {
		Timetype     string `default:"m" env:"DeviceImgTimetype"`
		Timeduration int64  `default:"3" env:"DeviceImgTimeduration"`
	}
	Web struct {
		Account  string `default:"" env:"WebAccount"`
		Password string `default:"" env:"WebPassword"`
		Indir    string `default:"" env:"WebIndir"`
	}
	Android struct {
		Account  string `default:"" env:"AndroidAccount"`
		Password string `default:"" env:"AndroidPassword"`
		Indir    string `default:"" env:"AndroidIndir"`
	}
	Log struct {
		Overtime int64 `default:"15" env:"LogOvertime"`
		Synctime int64 `default:"5" env:"LogSynctime"`
	}
	Teluri      string `default:"/platform/report/synctel" env:"Teluri"`
	Telgroupuri string `default:"/platform/report/synctelgroup" env:"Telgroupuri"`
	Loginuri    string `default:"/platform/application/login" env:"Loginuri"`
	Refreshuri  string `default:"/platform/application/update_token" env:"Refreshuri"`
	Devicesql   string `default:"" env:"Devicesql"`
	Servicename string `default:"log-agent" env:"Servicename"`
}{}
View Source
var (
	DefaultGormLogger = GormLogger{log.New(os.Stdout, "[ORM] ", 0)}
)
View Source
var LogFormatter = func(values ...interface{}) (messages []interface{}) {
	if len(values) > 1 {
		var (
			sql             string
			formattedValues []string
			level           = values[0]
			currentTime     = time.Now().Format("2006/01/02 15:04:05")
		)

		messages = []interface{}{currentTime}

		if level == "sql" {

			messages = append(messages, fmt.Sprintf("[%.2fms]", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))

			for _, value := range values[4].([]interface{}) {
				indirectValue := reflect.Indirect(reflect.ValueOf(value))
				if indirectValue.IsValid() {
					value = indirectValue.Interface()
					if t, ok := value.(time.Time); ok {
						formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05")))
					} else if b, ok := value.([]byte); ok {
						if str := string(b); isPrintable(str) {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
						} else {
							formattedValues = append(formattedValues, "'<binary>'")
						}
					} else if r, ok := value.(driver.Valuer); ok {
						if value, err := r.Value(); err == nil && value != nil {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
						} else {
							formattedValues = append(formattedValues, "NULL")
						}
					} else {
						formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
					}
				} else {
					formattedValues = append(formattedValues, "NULL")
				}
			}

			if numericPlaceHolderRegexp.MatchString(values[3].(string)) {
				sql = values[3].(string)
				for index, value := range formattedValues {
					placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1)
					sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1")
				}
			} else {
				formattedValuesLength := len(formattedValues)
				for index, value := range sqlRegexp.Split(values[3].(string), -1) {
					sql += value
					if index < formattedValuesLength {
						sql += formattedValues[index]
					}
				}
			}

			messages = append(messages, sql)
			messages = append(messages, fmt.Sprintf("\r\n%v", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned"))
		} else {
			messages = append(messages, values[2:]...)
		}
	}

	return
}
View Source
var StartTime = time.Now()

Functions

func AppendFile

func AppendFile(filePath string, b []byte) error

添加文本

func CWD

func CWD() string

func CheckFreeSwtich

func CheckFreeSwtich(raddr string) (bool, error)

监控freeswitch 服务是否正常 https://github.com/jart/gosip/blob/master/example/rawsip/rawsip_test.go 本地环境可以监控,医院环境无法实现,报错超时

func ConfigFile

func ConfigFile(fielname string) string

func CreateDir

func CreateDir(path string) error

func CreateFile

func CreateFile(filePath string, b []byte) error

func Ellipsis

func Ellipsis(text string, length int) string

func Exist

func Exist(path string) bool

func GetDeviceDir

func GetDeviceDir(deviceTypeId int64, dirs []*DeviceType) (string, error)

获取日志类型目录

func GetLocation

func GetLocation() (*time.Location, error)

获取时区

func HasChinese

func HasChinese(str string) bool

func InStrArray

func InStrArray(s string, ss []string) bool

func InitConfig

func InitConfig()

func IsGBK

func IsGBK(data []byte) bool

func IsPortInUse

func IsPortInUse(host string, port int64) bool

func ListDir

func ListDir(dirPth string, suffix string) ([]string, error)

func LocalIP

func LocalIP() string

func OpenFile

func OpenFile(filePath string) ([]byte, error)

func ResizePng

func ResizePng(imgPath, imgName string) error

func SleepRandomDuration

func SleepRandomDuration()

func UpTime

func UpTime() time.Duration

func UpTimeString

func UpTimeString() string

Types

type Cli

type Cli struct {
	IP       string //IP地址
	Username string //用户名
	Password string //密码
	Port     int    //端口号

	LastResult string //最近一次Run的结果
	// contains filtered or unexported fields
}

func New

func New(ip string, username string, password string, port ...int) *Cli

创建命令行对象 @param ip IP地址 @param username 用户名 @param password 密码 @param port 端口号,默认22

func (Cli) Run

func (c Cli) Run(shell string) (string, error)

执行shell @param shell shell脚本命令

type DateTime

type DateTime time.Time

DateTime 自定义事件类型

func (DateTime) MarshalJSON

func (dt DateTime) MarshalJSON() ([]byte, error)

func (*DateTime) Scan

func (dt *DateTime) Scan(v interface{}) error

func (DateTime) String

func (dt DateTime) String() string

func (*DateTime) UnmarshalJSON

func (dt *DateTime) UnmarshalJSON(data []byte) (err error)

func (DateTime) Value

func (dt DateTime) Value() (driver.Value, error)

type Device

type Device struct {
	IsError   int64  `json:"is_error" `
	DevStatus int64  `json:"device_status"`
	DevCode   string `json:"device_code"`
	LogAt     Time   `json:"log_at"`
}

type DeviceType

type DeviceType struct {
	DeviceName   string `json:"device_name"`
	DeviceTypeID int64  `json:"device_type_id"`
	Code         string `json:"code"`
}

"device_name": "护士站主机", "device_type_id": 4, "code": "nws" DeviceType 设备类型

func GetDeviceType

func GetDeviceType() ([]*DeviceType, error)

type GormLogger

type GormLogger struct {
	*log.Logger
}

Logger default logger

func (GormLogger) Print

func (logger GormLogger) Print(values ...interface{})

Print format & print log

type Port

type Port struct {
	Id        int64  `json:"id"`
	Ip        string `json:"ip"`
	Port      int64  `json:"port"`
	Desc      string `json:"desc"`
	FaultType int    `json:"fault_type"`
}

type ResponseDeviceCode

type ResponseDeviceCode struct {
	net.ResponseInfo
	Data struct {
		Uri  map[string]string `json:"uri"`
		List []string          `json:"list"`
	} `json:"data"`
}

type ResponseDeviceType

type ResponseDeviceType struct {
	net.ResponseInfo
	Data []*DeviceType `json:"data"`
}

type ResponsePort

type ResponsePort struct {
	net.ResponseInfo
	Data struct {
		Uri  string  `json:"uri"`
		List []*Port `json:"list"`
	} `json:"data"`
}

type ResponseRestful

type ResponseRestful struct {
	net.ResponseInfo
	Data struct {
		Uri  string     `json:"uri"`
		List []*Restful `json:"list"`
	} `json:"data"`
}

type ResponseServer

type ResponseServer struct {
	net.ResponseInfo
	Data struct {
		Uri  string    `json:"uri"`
		List []*Server `json:"list"`
	} `json:"data"`
}

type Restful

type Restful struct {
	Id  int64  `json:"id"`
	Url string `json:"url"`
}

type Server

type Server struct {
	Id            int64  `json:"id"`
	Ip            string `json:"ip"`
	Port          int64  `json:"port"`
	Account       string `json:"account"`
	Pwd           string `json:"pwd"`
	HealthUrl     string `json:"health_url"`
	ServiceTypeId int64  `json:"service_type_id"`
	ServiceName   string `json:"service_name"`
	ServiceTitle  string `json:"service_title"`
	FaultType     int    `json:"fault_type"`
}

type StringArray

type StringArray string

func (StringArray) MarshalJSON

func (r StringArray) MarshalJSON() ([]byte, error)

type Time

type Time struct {
	time.Time
}

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(b []byte) error

UnmarshalJSON returns time.Now() no matter what!

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL