123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package utils
- import (
- "fmt"
- "math"
- "strconv"
- "strings"
- beeLogger "github.com/beego/bee/v2/logger"
- "github.com/beego/beego/v2/server/web"
- "github.com/qiniu/qmgo"
- "github.com/shopspring/decimal"
- )
- var (
- RunMode string //运行模式
- MYSQL_URL string //数据库连接
- MYSQL_URL_DATA string
- MYSQL_URL_EDB string
- MgoUrlData string // mongodb数据库连接配置
- REDIS_CACHE string //缓存地址
- Re error //redis错误
- Rc RedisClient //redis缓存
- MgoDataCli *qmgo.Client // mongodb客户端连接
- MgoDataDbName string // mongodb指标数据的库名
- )
- var (
- APPNAME string
- APP_NAME_EN string
- Md5Key string
- EmailSendToUsers string
- AlarmMsgUrl string
- )
- var (
- EDB_LIB_URL string
- APP_EDB_LIB_NAME_EN string
- EDB_LIB_Md5_KEY string
- ETA_MINI_URL string
- ETA_MINI_DES_KEY string
- )
- var (
- LogPath string
- LogFile string
- BinLogPath string
- BinLogFile string
- ApiLogPath string
- ApiLogFile string
- LogMaxDays int //日志最大保留天数
- )
- var (
- UseMongo bool // 是否使用mongo
- )
- func init() {
- tmpRunMode, err := web.AppConfig.String("run_mode")
- if err != nil {
- panic("配置文件读取run_mode错误 " + err.Error())
- }
- RunMode = tmpRunMode
- if RunMode == "" {
- localIp, err := GetLocalIP()
- if localIp == "10.0.0.123" {
- RunMode = "debug"
- } else {
- RunMode = "release"
- }
- configPath := `/home/code/config/eta_gn/eta_chart_lib/conf/app.conf`
- err = web.LoadAppConfig("ini", configPath)
- if err != nil {
- fmt.Println("web.LoadAppConfig Err:" + err.Error())
- }
- }
- config, err := web.AppConfig.GetSection(RunMode)
- if err != nil {
- panic("配置文件读取错误 " + err.Error())
- }
- beeLogger.Log.Info(RunMode + " 模式")
- APP_NAME_EN = config["app_name_en"]
- Md5Key = config["md5_key"]
- MYSQL_URL = config["mysql_url"]
- MYSQL_URL_DATA = config["mysql_url_data"]
- MYSQL_URL_EDB = config["mysql_url_edb"]
- MgoUrlData = config["mgo_url_data"]
- REDIS_CACHE = config["beego_cache"]
- if len(REDIS_CACHE) <= 0 {
- panic("redis链接参数没有配置")
- }
- redisClient, err := initRedis(config["redis_type"], config["beego_cache"])
- if err != nil {
- fmt.Println("redis链接异常:", err)
- panic(any(Re))
- }
- Rc = redisClient
- appNameCn, err := web.AppConfig.String("app_name_cn")
- if err != nil {
- panic(any("配置文件读取app_name_cn错误 " + err.Error()))
- }
- APPNAME = appNameCn
- EmailSendToUsers = config["email_send_to_users"]
- AlarmMsgUrl = config["alarm_msg_url"]
- {
- EDB_LIB_URL = config["edb_lib_url"]
- APP_EDB_LIB_NAME_EN = config["app_edb_lib_name_en"]
- EDB_LIB_Md5_KEY = config["edb_lib_md5_key"]
- ETA_MINI_URL = config["eta_mini_url"]
- ETA_MINI_DES_KEY = config["eta_mini_des_key"]
- }
- {
- LogPath = config["log_path"]
- LogFile = config["log_file"]
- BinLogPath = config["binlog_path"]
- BinLogFile = config["binlog_file"]
- ApiLogPath = config["apilog_path"]
- ApiLogFile = config["apilog_file"]
- logMaxDaysStr := config["log_max_day"]
- LogMaxDays, _ = strconv.Atoi(logMaxDaysStr)
- }
- }
- func FormatMixTableDataShowValue(x float64) (res string) {
- res = strconv.FormatFloat(x, 'f', -1, 64)
- return
- }
- func FormatTableDataShowValue(x float64) (res string) {
- if x > 1 || x < -1 {
- res = decimal.NewFromFloat(x).Round(2).String()
- return
- }
- xStr := strconv.FormatFloat(x, 'f', -10, 64)
- xParts := strings.Split(xStr, ".")
- if len(xParts) < 2 {
- res = fmt.Sprint(x)
- return
- }
- xDecimals := len(xParts[1])
- if xDecimals > 2 {
- parts := xParts[1]
- partLen := len(xParts[1])
- if partLen <= 2 {
- res = xParts[0] + "." + parts
- return
- }
- one := 0
- for k, p := range parts {
- if p != 48 {
- if one == 0 {
- one = k + 1
- }
- }
- }
- n := partLen - one
- if n >= 1 {
- n -= 1
- } else {
- one -= 1
- }
- var partFloat float64
- partInt, _ := strconv.Atoi(parts)
- partFloat, _ = decimal.NewFromInt(int64(partInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(n)))).Float64()
- partFloat = math.Round(partFloat)
- partFloat, _ = decimal.NewFromFloat(partFloat).Div(decimal.NewFromFloat(math.Pow(10, float64(one+1)))).Float64()
- resParts := strings.Split(fmt.Sprint(partFloat), ".")
- resPart := ""
- if len(resParts) > 1 {
- resPart = resParts[1]
- } else {
- resPart = resParts[0]
- }
- res = xParts[0] + "." + resPart
- }
- if xDecimals <= 2 {
- xDecimalsStr := xParts[1]
- x, _ = strconv.ParseFloat(xParts[0]+"."+xDecimalsStr, 64)
- res = xParts[0] + "." + xDecimalsStr
- }
- return
- }
|