config.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. DM_URL string
  19. DM_URL_DATA string
  20. REDIS_CACHE string //缓存地址
  21. //Rc *cache.Cache //redis缓存
  22. Re error //redis错误
  23. Rc RedisClient //redis缓存
  24. MgoDataCli *qmgo.Client // mongodb客户端连接
  25. MgoDataDbName string // mongodb指标数据的库名
  26. )
  27. // 基础配置
  28. var (
  29. // APPNAME 项目中文名称
  30. APPNAME string
  31. // APP_NAME_EN 项目英文名称
  32. APP_NAME_EN string
  33. // Md5Key 签名秘钥
  34. Md5Key string
  35. // EmailSendToUsers 邮件提醒人员
  36. EmailSendToUsers string
  37. // AlarmMsgUrl 报警服务地址
  38. AlarmMsgUrl string
  39. )
  40. // 公共api内部服务调用
  41. var (
  42. // EDB_LIB_URL 公共指标库
  43. EDB_LIB_URL string
  44. APP_EDB_LIB_NAME_EN string
  45. EDB_LIB_Md5_KEY string
  46. ETA_MINI_URL string
  47. ETA_MINI_DES_KEY string
  48. )
  49. var (
  50. LogPath string
  51. LogFile string
  52. BinLogPath string
  53. BinLogFile string
  54. ApiLogPath string
  55. ApiLogFile string
  56. LogMaxDays int //日志最大保留天数
  57. )
  58. var (
  59. UseMongo bool // 是否使用mongo
  60. )
  61. func init() {
  62. tmpRunMode, err := web.AppConfig.String("run_mode")
  63. if err != nil {
  64. panic("配置文件读取run_mode错误 " + err.Error())
  65. }
  66. RunMode = tmpRunMode
  67. if RunMode == "" {
  68. localIp, err := GetLocalIP()
  69. if localIp == "10.0.0.123" {
  70. RunMode = "debug"
  71. } else {
  72. RunMode = "release"
  73. }
  74. configPath := `/home/code/config/eta_gn/eta_chart_lib/conf/app.conf`
  75. err = web.LoadAppConfig("ini", configPath)
  76. if err != nil {
  77. fmt.Println("web.LoadAppConfig Err:" + err.Error())
  78. }
  79. }
  80. config, err := web.AppConfig.GetSection(RunMode)
  81. if err != nil {
  82. panic("配置文件读取错误 " + err.Error())
  83. }
  84. beeLogger.Log.Info(RunMode + " 模式")
  85. APP_NAME_EN = config["app_name_en"]
  86. // 签名秘钥
  87. Md5Key = config["md5_key"]
  88. MYSQL_URL = config["mysql_url"]
  89. MYSQL_URL_DATA = config["mysql_url_data"]
  90. MYSQL_URL_EDB = config["mysql_url_edb"]
  91. // mongodb数据库连接配置
  92. MgoUrlData = config["mgo_url_data"]
  93. // 达梦数据库连接配置
  94. DM_URL = config["dm_url"]
  95. DM_URL_DATA = config["dm_url_data"]
  96. REDIS_CACHE = config["beego_cache"]
  97. if len(REDIS_CACHE) <= 0 {
  98. panic("redis链接参数没有配置")
  99. }
  100. //Rc, Re = cache.NewCache(REDIS_CACHE) //初始化缓存
  101. //if Re != nil {
  102. // fmt.Println(Re)
  103. // panic(Re)
  104. //}
  105. // 初始化缓存
  106. redisClient, err := initRedis(config["redis_type"], config["beego_cache"])
  107. if err != nil {
  108. fmt.Println("redis链接异常:", err)
  109. panic(any(Re))
  110. }
  111. Rc = redisClient
  112. // 项目中文名称
  113. appNameCn, err := web.AppConfig.String("app_name_cn")
  114. if err != nil {
  115. panic(any("配置文件读取app_name_cn错误 " + err.Error()))
  116. }
  117. APPNAME = appNameCn
  118. // 邮件提醒人员
  119. EmailSendToUsers = config["email_send_to_users"]
  120. // 报警服务地址
  121. AlarmMsgUrl = config["alarm_msg_url"]
  122. // 公共api内部服务调用
  123. {
  124. // 公共指标库相关
  125. EDB_LIB_URL = config["edb_lib_url"]
  126. APP_EDB_LIB_NAME_EN = config["app_edb_lib_name_en"]
  127. EDB_LIB_Md5_KEY = config["edb_lib_md5_key"]
  128. ETA_MINI_URL = config["eta_mini_url"]
  129. ETA_MINI_DES_KEY = config["eta_mini_des_key"]
  130. }
  131. //日志配置
  132. {
  133. LogPath = config["log_path"]
  134. LogFile = config["log_file"]
  135. BinLogPath = config["binlog_path"]
  136. BinLogFile = config["binlog_file"]
  137. ApiLogPath = config["apilog_path"]
  138. ApiLogFile = config["apilog_file"]
  139. logMaxDaysStr := config["log_max_day"]
  140. LogMaxDays, _ = strconv.Atoi(logMaxDaysStr)
  141. }
  142. }
  143. // FormatMixTableDataShowValue 格式化自定表格显示数据
  144. func FormatMixTableDataShowValue(x float64) (res string) {
  145. res = strconv.FormatFloat(x, 'f', -1, 64)
  146. return
  147. }
  148. //http://8.136.199.33:8608/swagger/
  149. //http://8.136.199.33:8608/v1
  150. // FormatTableDataShowValue 格式化自定表格显示数据
  151. func FormatTableDataShowValue(x float64) (res string) {
  152. if x > 1 || x < -1 {
  153. res = decimal.NewFromFloat(x).Round(2).String()
  154. return
  155. }
  156. // 介于-1到1之间
  157. xStr := strconv.FormatFloat(x, 'f', -10, 64)
  158. // 使用 strings.Split 函数将小数拆分为整数部分和小数部分
  159. xParts := strings.Split(xStr, ".")
  160. if len(xParts) < 2 {
  161. res = fmt.Sprint(x)
  162. return
  163. }
  164. // 计算小数部分的长度,即小数点后面的位数
  165. xDecimals := len(xParts[1])
  166. if xDecimals > 2 {
  167. parts := xParts[1]
  168. // 小数点后小于等于两位, 直接拼接返回
  169. partLen := len(xParts[1])
  170. if partLen <= 2 {
  171. res = xParts[0] + "." + parts
  172. return
  173. }
  174. // 找出第一个有效数字, 算出n
  175. one := 0
  176. for k, p := range parts {
  177. // 48->0
  178. if p != 48 {
  179. if one == 0 {
  180. one = k + 1
  181. }
  182. }
  183. }
  184. n := partLen - one
  185. if n >= 1 {
  186. n -= 1
  187. } else {
  188. one -= 1
  189. }
  190. var partFloat float64
  191. partInt, _ := strconv.Atoi(parts)
  192. partFloat, _ = decimal.NewFromInt(int64(partInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(n)))).Float64()
  193. partFloat = math.Round(partFloat)
  194. partFloat, _ = decimal.NewFromFloat(partFloat).Div(decimal.NewFromFloat(math.Pow(10, float64(one+1)))).Float64()
  195. resParts := strings.Split(fmt.Sprint(partFloat), ".")
  196. resPart := ""
  197. if len(resParts) > 1 {
  198. resPart = resParts[1]
  199. } else {
  200. resPart = resParts[0]
  201. }
  202. res = xParts[0] + "." + resPart
  203. }
  204. if xDecimals <= 2 {
  205. xDecimalsStr := xParts[1]
  206. x, _ = strconv.ParseFloat(xParts[0]+"."+xDecimalsStr, 64)
  207. res = xParts[0] + "." + xDecimalsStr
  208. }
  209. return
  210. }