config.go 5.2 KB

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