tree.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/main/apm/conf"
  10. "go-common/app/admin/main/apm/model/tree"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. )
  14. const (
  15. _discoveryIDKey = "%s_discoveryIDKey"
  16. )
  17. // Appids get appid by username.
  18. func (s *Service) Appids(c context.Context, username, cookie string) (appids []string, err error) {
  19. s.treeLock.RLock()
  20. nodem, ok := s.treeCache[username]
  21. tmp := make(map[string]struct{})
  22. if !ok {
  23. s.treeLock.RUnlock()
  24. s.TreeSync(c, username, cookie)
  25. s.treeLock.RLock()
  26. nodem = s.treeCache[username]
  27. }
  28. for _, v := range nodem {
  29. nameArr := strings.Split(v.Path, ".")
  30. newName := nameArr[1] + "." + nameArr[2] + "." + nameArr[3]
  31. if _, ok := tmp[newName]; !ok {
  32. appids = append(appids, newName)
  33. tmp[newName] = struct{}{}
  34. }
  35. }
  36. s.treeLock.RUnlock()
  37. return
  38. }
  39. // Projects get projects by username.
  40. func (s *Service) Projects(c context.Context, username, cookie string) (projects []string, err error) {
  41. s.treeLock.RLock()
  42. nodem, ok := s.treeCache[username]
  43. mm := make(map[string]struct{})
  44. if !ok {
  45. s.treeLock.RUnlock()
  46. s.TreeSync(c, username, cookie)
  47. s.treeLock.RLock()
  48. nodem = s.treeCache[username]
  49. }
  50. for _, v := range nodem {
  51. nameArr := strings.Split(v.Path, ".")
  52. newName := nameArr[1] + "." + nameArr[2]
  53. if _, ok := mm[newName]; ok {
  54. continue
  55. }
  56. mm[newName] = struct{}{}
  57. projects = append(projects, newName)
  58. }
  59. s.treeLock.RUnlock()
  60. return
  61. }
  62. // TreeSync sync tree cache by username.
  63. func (s *Service) TreeSync(c context.Context, username, cookie string) {
  64. nodem, err := s.roleTrees(c, username, cookie)
  65. if err != nil {
  66. log.Error("TreeSync(%s) error(%v)", username, err)
  67. return
  68. }
  69. s.treeLock.Lock()
  70. s.treeCache[username] = nodem
  71. s.treeLock.Unlock()
  72. }
  73. // trees get service trees by username.
  74. func (s *Service) roleTrees(c context.Context, username, cookie string) (trees []*tree.Node, err error) {
  75. url := "http://easyst.bilibili.co/v1/auth"
  76. req, err := http.NewRequest("GET", url, nil)
  77. if err != nil {
  78. err = ecode.RequestErr
  79. return
  80. }
  81. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  82. req.Header.Set("Cookie", cookie)
  83. var result = &tree.TokenResult{}
  84. if err = s.client.Do(c, req, result); err != nil {
  85. log.Error("TreeSync(%s) error(%v)", username, err)
  86. err = ecode.RequestErr
  87. return
  88. }
  89. url = "http://easyst.bilibili.co/v1/node/role/app"
  90. if req, err = http.NewRequest("GET", url, nil); err != nil {
  91. log.Error("TreeSync(%s) get token error(%v)", username, err)
  92. err = ecode.RequestErr
  93. return
  94. }
  95. req.Header.Set("Content-Type", "application/json")
  96. req.Header.Set("X-Authorization-Token", result.Data.Token)
  97. var dat = &tree.Resp{}
  98. if err = s.client.Do(c, req, dat); err != nil {
  99. log.Error("TreeSync(%s) token(%s) error(%v)", username, result.Data.Token, err)
  100. err = ecode.RequestErr
  101. return
  102. }
  103. if len(dat.Data) == 0 {
  104. log.Error("TreeSync(%s) no data", username)
  105. return
  106. }
  107. trees = dat.Data
  108. log.Info("TreeSync(%s) data(%v)", username, trees)
  109. return
  110. }
  111. // Trees tree list
  112. func (s *Service) Trees(c context.Context, username, cookie string) (nodem []*tree.Node, err error) {
  113. s.treeLock.RLock()
  114. nodem, ok := s.treeCache[username]
  115. if !ok {
  116. s.treeLock.RUnlock()
  117. s.TreeSync(c, username, cookie)
  118. s.treeLock.RLock()
  119. nodem = s.treeCache[username]
  120. }
  121. s.treeLock.RUnlock()
  122. return
  123. }
  124. // AllTrees AllTrees
  125. // func (s *Service) AllTrees(c context.Context, username, cookie string) (trees []*tree.Info, err error) {
  126. // var (
  127. // jsonBytes []byte
  128. // url = "http://easyst.bilibili.co/v1/token"
  129. // )
  130. // body := &struct {
  131. // Username string `json:"user_name"`
  132. // PlatformID string `json:"platform_id"`
  133. // }{
  134. // Username: "main",
  135. // PlatformID: conf.Conf.Tree.PlatformID,
  136. // }
  137. // if jsonBytes, err = json.Marshal(body); err != nil {
  138. // log.Error("json.Marshal(body) error(%v)", err)
  139. // return
  140. // }
  141. // req, err := http.NewRequest("POST", url, strings.NewReader(string(jsonBytes)))
  142. // if err != nil {
  143. // log.Error("http.NewRequest failed", err)
  144. // return
  145. // }
  146. // req.Header.Set("Content-Type", "application/json")
  147. // result := &struct {
  148. // Code int `json:"code"`
  149. // Data *model.ServiceTreeToken `json:"data"`
  150. // Message string `json:"message"`
  151. // Status int `json:"status"`
  152. // }{}
  153. // if err = s.client.Do(c, req, result); err != nil {
  154. // log.Error("TreesAll(%s) get token error(%v)", username, err)
  155. // err = ecode.RequestErr
  156. // return
  157. // }
  158. // url = "http://easyst.bilibili.co/v1/node/app/extendinfo"
  159. // if req, err = http.NewRequest("GET", url, nil); err != nil {
  160. // log.Error("TreesAll(%s) error(%v)", username, err)
  161. // err = ecode.RequestErr
  162. // return
  163. // }
  164. // req.Header.Set("Content-Type", "application/json")
  165. // req.Header.Set("X-Authorization-Token", result.Data.Token)
  166. // var dat = &tree.Rest{}
  167. // if err = s.client.Do(c, req, dat); err != nil {
  168. // log.Error("TreesAll(%s) token(%s) error(%v)", username, result.Data.Token, err)
  169. // err = ecode.RequestErr
  170. // return
  171. // }
  172. // if len(dat.Data) == 0 {
  173. // log.Error("TreesAll(%s) no data", username)
  174. // return
  175. // }
  176. // trees = dat.Data
  177. // log.Info("TreesAll(%s) data(%v)", username, trees)
  178. // return
  179. // }
  180. // DiscoveryID get appid by username.
  181. func (s *Service) DiscoveryID(c context.Context, username, cookie string) (appids []string, err error) {
  182. keyName := discoveryIDKey(username)
  183. s.discoveryIDLock.RLock()
  184. nodem, ok := s.discoveryIDCache[keyName]
  185. if !ok || (time.Since(nodem.CTime) > 60*time.Second) {
  186. s.discoveryIDLock.RUnlock()
  187. s.DiscoveryTreeSync(c, username, cookie)
  188. s.discoveryIDLock.RLock()
  189. nodem = s.discoveryIDCache[keyName]
  190. }
  191. s.discoveryIDLock.RUnlock()
  192. tmp := make(map[string]struct{})
  193. for _, v := range nodem.Data {
  194. var newName string
  195. if v.DiscoveryID != "" {
  196. newName = v.DiscoveryID
  197. } else {
  198. nameArr := strings.Split(v.AppID, ".")
  199. newName = nameArr[0] + "." + nameArr[1] + "." + nameArr[2]
  200. }
  201. if _, ok := tmp[newName]; !ok {
  202. appids = append(appids, newName)
  203. tmp[newName] = struct{}{}
  204. }
  205. }
  206. return
  207. }
  208. // discoveryAllTrees ...
  209. func (s *Service) discoveryAllTrees(c context.Context, username, cookie string) (dat *tree.Resd, err error) {
  210. url := "http://easyst.bilibili.co/v1/token"
  211. var jsonBytes []byte
  212. body := &struct {
  213. Username string `json:"user_name"`
  214. PlatformID string `json:"platform_id"`
  215. }{
  216. Username: "msm",
  217. PlatformID: conf.Conf.Tree.MsmPlatformID,
  218. }
  219. if jsonBytes, err = json.Marshal(body); err != nil {
  220. log.Error("json.Marshal(body) error(%v)", err)
  221. return
  222. }
  223. req, err := http.NewRequest("POST", url, strings.NewReader(string(jsonBytes)))
  224. if err != nil {
  225. err = ecode.RequestErr
  226. return
  227. }
  228. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  229. req.Header.Set("Cookie", cookie)
  230. var result = &tree.TokenResult{}
  231. if err = s.client.Do(c, req, result); err != nil {
  232. log.Error("TreeSync(%s) error(%v)", username, err)
  233. err = ecode.RequestErr
  234. return
  235. }
  236. dat = &tree.Resd{}
  237. url = "http://easyst.bilibili.co/v1/node/app/secretinfo/prod"
  238. if req, err = http.NewRequest("GET", url, nil); err != nil {
  239. log.Error("TreeSync(%s) get token error(%v)", username, err)
  240. err = ecode.RequestErr
  241. return
  242. }
  243. req.Header.Set("Content-Type", "application/json")
  244. req.Header.Set("X-Authorization-Token", result.Data.Token)
  245. if err = s.client.Do(c, req, dat); err != nil {
  246. log.Error("TreeSync(%s) token(%s) error(%v)", username, result.Data.Token, err)
  247. err = ecode.RequestErr
  248. return
  249. }
  250. if len(dat.Data) == 0 {
  251. log.Error("TreeSync(%s) no data", username)
  252. return
  253. }
  254. dat.CTime = time.Now()
  255. log.Info("TreeSync(%s) data(%v)", username, dat)
  256. return
  257. }
  258. func discoveryIDKey(username string) string {
  259. return fmt.Sprintf(_discoveryIDKey, username)
  260. }
  261. // DiscoveryTreeSync sync tree cache by username discoverykey.
  262. func (s *Service) DiscoveryTreeSync(c context.Context, username, cookie string) {
  263. keyName := discoveryIDKey(username)
  264. nodem, err := s.discoveryAllTrees(c, username, cookie)
  265. if err != nil {
  266. log.Error("DiscoveryTreeSync(%s) error(%v)", username, err)
  267. return
  268. }
  269. s.discoveryIDLock.Lock()
  270. s.discoveryIDCache[keyName] = nodem
  271. s.discoveryIDLock.Unlock()
  272. }