config.go 6.1 KB

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