utils

package
v0.0.0-...-9b1755c Latest Latest
Warning

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

Go to latest
Published: Mar 15, 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"`
	Dir       struct {
		Names string `default:"" env:"DirNames"`
		Codes string `default:"" env:"DirCodes"`
	}
	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"`
	}
	System struct {
		Timetype     string `default:"m" env:"SystemTimetype"`
		Timeduration int64  `default:"3" env:"SystemTimeduration"`
	}
	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"`
	}
	Faultmsg struct {
		Device  string `default:"" env:"FaultmsgDevice"`
		Plugin  string `default:"" env:"FaultmsgPlugin"`
		Logsync string `default:"" env:"FaultmsgLogsync"`
	}
	Log struct {
		Overtime int64 `default:"15" env:"LogOvertime"`
		Synctime int64 `default:"5" env:"LogSynctime"`
	}
}{}
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 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 GetCache

func GetCache() *cache.Cache

func GetCacheToken

func GetCacheToken() string

func GetDeviceDir

func GetDeviceDir(deviceTypeId int64) (string, error)

获取日志类型目录

func GetDeviceTypeId

func GetDeviceTypeId(deviceDir string) (int64, error)

获取日志类型目录

func GetLocation

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

获取时区

func GetPingMsg

func GetPingMsg(devIp string) (bool, string)

func GetSessionId

func GetSessionId() *http.Cookie

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) error

func ListDir

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

func OpenFile

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

func Request

func Request(method, url, data string, auth bool) []byte

func ResizePng

func ResizePng(imgPath, imgName string) error

func SetCacheToken

func SetCacheToken(token string)

func SetSessionId

func SetSessionId(cookies []*http.Cookie)

func SyncServices

func SyncServices(path, data string) (interface{}, error)

http://fyxt.t.chindeo.com/platform/report/device 发送设备日志信息 http://fyxt.t.chindeo.com/platform/report/service 提交服务监控信息

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"`
}

func GetDevices

func GetDevices() ([]*Device, error)

http://fyxt.t.chindeo.com/platform/report/getDevice 获取设备列表

type GormLogger

type GormLogger struct {
	*log.Logger
}

Logger default logger

func (GormLogger) Print

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

Print format & print log

type Req

type Req struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

type Restful

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

func GetRestfuls

func GetRestfuls() ([]*Restful, error)

http://fyxt.t.chindeo.com/platform/report/getRestful 获取接口列表

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"`
	ServiceTypeId int64  `json:"service_type_id"`
	ServiceName   string `json:"service_name"`
	ServiceTitle  string `json:"service_title"`
}

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

returns time.Now() no matter what!

type Token

type Token struct {
	XToken string `json:"X-Token"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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