config.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package utils
  2. import (
  3. "fmt"
  4. "math"
  5. "strconv"
  6. "strings"
  7. beeLogger "github.com/beego/bee/v2/logger"
  8. "github.com/beego/beego/v2/server/web"
  9. "github.com/qiniu/qmgo"
  10. "github.com/shopspring/decimal"
  11. )
  12. var (
  13. RunMode string //运行模式
  14. MYSQL_URL string //数据库连接
  15. MYSQL_URL_DATA string
  16. MYSQL_URL_EDB string
  17. MgoUrlData string // mongodb数据库连接配置
  18. REDIS_CACHE string //缓存地址
  19. Re error //redis错误
  20. Rc RedisClient //redis缓存
  21. MgoDataCli *qmgo.Client // mongodb客户端连接
  22. MgoDataDbName string // mongodb指标数据的库名
  23. )
  24. var (
  25. APPNAME string
  26. APP_NAME_EN string
  27. Md5Key string
  28. EmailSendToUsers string
  29. AlarmMsgUrl string
  30. )
  31. var (
  32. EDB_LIB_URL string
  33. APP_EDB_LIB_NAME_EN string
  34. EDB_LIB_Md5_KEY string
  35. ETA_MINI_URL string
  36. ETA_MINI_DES_KEY string
  37. )
  38. var (
  39. LogPath string
  40. LogFile string
  41. BinLogPath string
  42. BinLogFile string
  43. ApiLogPath string
  44. ApiLogFile string
  45. LogMaxDays int //日志最大保留天数
  46. )
  47. var (
  48. UseMongo bool // 是否使用mongo
  49. )
  50. func init() {
  51. tmpRunMode, err := web.AppConfig.String("run_mode")
  52. if err != nil {
  53. panic("配置文件读取run_mode错误 " + err.Error())
  54. }
  55. RunMode = tmpRunMode
  56. if RunMode == "" {
  57. localIp, err := GetLocalIP()
  58. if localIp == "10.0.0.123" {
  59. RunMode = "debug"
  60. } else {
  61. RunMode = "release"
  62. }
  63. configPath := `/home/code/config/eta_gn/eta_chart_lib/conf/app.conf`
  64. err = web.LoadAppConfig("ini", configPath)
  65. if err != nil {
  66. fmt.Println("web.LoadAppConfig Err:" + err.Error())
  67. }
  68. }
  69. config, err := web.AppConfig.GetSection(RunMode)
  70. if err != nil {
  71. panic("配置文件读取错误 " + err.Error())
  72. }
  73. beeLogger.Log.Info(RunMode + " 模式")
  74. APP_NAME_EN = config["app_name_en"]
  75. Md5Key = config["md5_key"]
  76. MYSQL_URL = config["mysql_url"]
  77. MYSQL_URL_DATA = config["mysql_url_data"]
  78. MYSQL_URL_EDB = config["mysql_url_edb"]
  79. MgoUrlData = config["mgo_url_data"]
  80. REDIS_CACHE = config["beego_cache"]
  81. if len(REDIS_CACHE) <= 0 {
  82. panic("redis链接参数没有配置")
  83. }
  84. redisClient, err := initRedis(config["redis_type"], config["beego_cache"])
  85. if err != nil {
  86. fmt.Println("redis链接异常:", err)
  87. panic(any(Re))
  88. }
  89. Rc = redisClient
  90. appNameCn, err := web.AppConfig.String("app_name_cn")
  91. if err != nil {
  92. panic(any("配置文件读取app_name_cn错误 " + err.Error()))
  93. }
  94. APPNAME = appNameCn
  95. EmailSendToUsers = config["email_send_to_users"]
  96. AlarmMsgUrl = config["alarm_msg_url"]
  97. {
  98. EDB_LIB_URL = config["edb_lib_url"]
  99. APP_EDB_LIB_NAME_EN = config["app_edb_lib_name_en"]
  100. EDB_LIB_Md5_KEY = config["edb_lib_md5_key"]
  101. ETA_MINI_URL = config["eta_mini_url"]
  102. ETA_MINI_DES_KEY = config["eta_mini_des_key"]
  103. }
  104. {
  105. LogPath = config["log_path"]
  106. LogFile = config["log_file"]
  107. BinLogPath = config["binlog_path"]
  108. BinLogFile = config["binlog_file"]
  109. ApiLogPath = config["apilog_path"]
  110. ApiLogFile = config["apilog_file"]
  111. logMaxDaysStr := config["log_max_day"]
  112. LogMaxDays, _ = strconv.Atoi(logMaxDaysStr)
  113. }
  114. }
  115. func FormatMixTableDataShowValue(x float64) (res string) {
  116. res = strconv.FormatFloat(x, 'f', -1, 64)
  117. return
  118. }
  119. func FormatTableDataShowValue(x float64) (res string) {
  120. if x > 1 || x < -1 {
  121. res = decimal.NewFromFloat(x).Round(2).String()
  122. return
  123. }
  124. xStr := strconv.FormatFloat(x, 'f', -10, 64)
  125. xParts := strings.Split(xStr, ".")
  126. if len(xParts) < 2 {
  127. res = fmt.Sprint(x)
  128. return
  129. }
  130. xDecimals := len(xParts[1])
  131. if xDecimals > 2 {
  132. parts := xParts[1]
  133. partLen := len(xParts[1])
  134. if partLen <= 2 {
  135. res = xParts[0] + "." + parts
  136. return
  137. }
  138. one := 0
  139. for k, p := range parts {
  140. if p != 48 {
  141. if one == 0 {
  142. one = k + 1
  143. }
  144. }
  145. }
  146. n := partLen - one
  147. if n >= 1 {
  148. n -= 1
  149. } else {
  150. one -= 1
  151. }
  152. var partFloat float64
  153. partInt, _ := strconv.Atoi(parts)
  154. partFloat, _ = decimal.NewFromInt(int64(partInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(n)))).Float64()
  155. partFloat = math.Round(partFloat)
  156. partFloat, _ = decimal.NewFromFloat(partFloat).Div(decimal.NewFromFloat(math.Pow(10, float64(one+1)))).Float64()
  157. resParts := strings.Split(fmt.Sprint(partFloat), ".")
  158. resPart := ""
  159. if len(resParts) > 1 {
  160. resPart = resParts[1]
  161. } else {
  162. resPart = resParts[0]
  163. }
  164. res = xParts[0] + "." + resPart
  165. }
  166. if xDecimals <= 2 {
  167. xDecimalsStr := xParts[1]
  168. x, _ = strconv.ParseFloat(xParts[0]+"."+xDecimalsStr, 64)
  169. res = xParts[0] + "." + xDecimalsStr
  170. }
  171. return
  172. }