config.go 5.7 KB

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