app.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. package http
  2. import (
  3. "fmt"
  4. "go-common/app/admin/main/apm/model/app"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. "strconv"
  8. "strings"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. func appList(c *bm.Context) {
  12. var err error
  13. v := new(struct {
  14. AppID string `form:"app_id"`
  15. Pn int `form:"pn" default:"1"`
  16. Ps int `form:"ps" default:"20"`
  17. })
  18. if err = c.Bind(v); err != nil {
  19. return
  20. }
  21. var (
  22. aa []*app.App
  23. count int
  24. lk = "%" + v.AppID + "%"
  25. )
  26. if v.AppID != "" {
  27. err = apmSvc.DB.Where("app_id LIKE ?", lk).Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
  28. } else {
  29. err = apmSvc.DB.Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
  30. }
  31. if err != nil {
  32. log.Error("apmSvc.AppList error(%v)", err)
  33. c.JSON(nil, err)
  34. return
  35. }
  36. if v.AppID != "" {
  37. err = apmSvc.DB.Where("app_id LIKE ?", lk).Model(&app.App{}).Count(&count).Error
  38. } else {
  39. err = apmSvc.DB.Model(&app.App{}).Count(&count).Error
  40. }
  41. if err != nil {
  42. log.Error("apmSvc.AppList count error(%v)", err)
  43. c.JSON(nil, err)
  44. return
  45. }
  46. data := &Paper{
  47. Pn: v.Pn,
  48. Ps: v.Ps,
  49. Items: aa,
  50. Total: count,
  51. }
  52. c.JSON(data, nil)
  53. }
  54. func appAdd(c *bm.Context) {
  55. var err error
  56. username := name(c)
  57. v := new(struct {
  58. AppTreeID int64 `form:"app_tree_id" validate:"required"`
  59. AppID string `form:"app_id" validate:"required"`
  60. Limit int64 `form:"limit"`
  61. })
  62. if err = c.Bind(v); err != nil {
  63. return
  64. }
  65. if err = apmSvc.AppAdd(c, username, v.AppTreeID, v.AppID, v.Limit); err != nil {
  66. log.Error("apmSvc.appAdd error(%v)", err)
  67. c.JSON(nil, err)
  68. return
  69. }
  70. c.JSON(nil, err)
  71. }
  72. func appEdit(c *bm.Context) {
  73. var err error
  74. v := new(struct {
  75. ID int `form:"id" validate:"required"`
  76. Limit int `form:"limit" validate:"required"`
  77. })
  78. if err = c.Bind(v); err != nil {
  79. return
  80. }
  81. a := &app.App{}
  82. if err = apmSvc.DB.Where("id = ?", v.ID).Find(a).Error; err != nil {
  83. log.Error("apmSvc.appEdit find(%d) error(%v)", v.ID, err)
  84. c.JSON(nil, err)
  85. return
  86. }
  87. ups := map[string]interface{}{
  88. "limit": v.Limit,
  89. }
  90. if err = apmSvc.DB.Model(&app.App{}).Where("id = ?", v.ID).Updates(ups).Error; err != nil {
  91. log.Error("apmSvc.appEdit updates error(%v)", err)
  92. c.JSON(nil, err)
  93. return
  94. }
  95. sqlLog := &map[string]interface{}{
  96. "SQLType": "update",
  97. "Where": "id = ?",
  98. "Value1": v.ID,
  99. "Update": ups,
  100. "Old": a,
  101. }
  102. username := name(c)
  103. apmSvc.SendLog(*c, username, 0, 2, int64(v.ID), "apmSvc.appEdit", sqlLog)
  104. c.JSON(nil, err)
  105. }
  106. func appDelete(c *bm.Context) {
  107. v := new(struct {
  108. ID int `form:"id" validate:"required"`
  109. })
  110. username := name(c)
  111. var err error
  112. if err = c.Bind(v); err != nil {
  113. return
  114. }
  115. a := &app.App{}
  116. if err = apmSvc.DB.Where("id = ?", v.ID).Find(a).Error; err != nil {
  117. log.Error("apmSvc.appDelete find(%d) error(%v)", v.ID, err)
  118. c.JSON(nil, err)
  119. return
  120. }
  121. if err = apmSvc.DB.Delete(a).Error; err != nil {
  122. log.Error("apmSvc.appDelete delete(%d) error(%v)", v.ID, err)
  123. c.JSON(nil, err)
  124. return
  125. }
  126. apmSvc.SendLog(*c, username, 0, 3, int64(v.ID), "apmSvc.appDelete", a)
  127. c.JSON(nil, err)
  128. }
  129. func appAuthList(c *bm.Context) {
  130. v := new(struct {
  131. AppID string `form:"app_id"`
  132. ServiceID string `form:"service_id"`
  133. Pn int `form:"pn" default:"1"`
  134. Ps int `form:"ps" default:"20"`
  135. })
  136. var err error
  137. if err = c.Bind(v); err != nil {
  138. return
  139. }
  140. var (
  141. aa []*app.Auth
  142. count int
  143. )
  144. if v.AppID != "" && v.ServiceID != "" {
  145. err = apmSvc.DB.Where("app_id=? and service_id=?", v.AppID, v.ServiceID).Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
  146. } else if v.AppID != "" {
  147. err = apmSvc.DB.Where("app_id=?", v.AppID).Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
  148. } else if v.ServiceID != "" {
  149. err = apmSvc.DB.Where("service_id=?", v.ServiceID).Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
  150. } else {
  151. err = apmSvc.DB.Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
  152. }
  153. if err != nil {
  154. log.Error("apmSvc.appAuthList error(%v)", err)
  155. c.JSON(nil, err)
  156. return
  157. }
  158. if v.AppID != "" {
  159. err = apmSvc.DB.Where("app_id=?", v.AppID).Model(&app.Auth{}).Count(&count).Error
  160. } else {
  161. err = apmSvc.DB.Model(&app.Auth{}).Count(&count).Error
  162. }
  163. if err != nil {
  164. log.Error("apmSvc.appAuthList count error(%v)", err)
  165. c.JSON(nil, err)
  166. return
  167. }
  168. data := &Paper{
  169. Pn: v.Pn,
  170. Ps: v.Ps,
  171. Items: aa,
  172. Total: count,
  173. }
  174. c.JSON(data, nil)
  175. }
  176. func appAuthAdd(c *bm.Context) {
  177. v := new(struct {
  178. AppTreeID int64 `form:"app_tree_id" validate:"required"`
  179. AppID string `form:"app_id" validate:"required"`
  180. ServiceTreeID int64 `form:"service_tree_id" validate:"required"`
  181. ServiceID string `form:"service_id" validate:"required"`
  182. RPCMethod string `form:"rpc_method"`
  183. HTTPMethod string `form:"http_method"`
  184. Quota int64 `form:"quota"`
  185. })
  186. var err error
  187. if err = c.Bind(v); err != nil {
  188. return
  189. }
  190. if v.ServiceTreeID == v.AppTreeID {
  191. log.Error("apmSvc.appAuthAdd service_tree_id=app_tree_id error(%v)", v.ServiceTreeID)
  192. c.JSON(nil, ecode.RequestErr)
  193. return
  194. }
  195. cnt := 0
  196. if err = apmSvc.DB.Model(&app.Auth{}).Where("service_tree_id=? and app_tree_id=?", v.ServiceTreeID, v.AppTreeID).Count(&cnt).Error; err != nil {
  197. log.Error("apmSvc.appAuthAdd count error(%v)", err)
  198. c.JSON(nil, ecode.RequestErr)
  199. return
  200. }
  201. if cnt > 0 {
  202. log.Error("apmSvc.appAuthAdd count (%v)", cnt)
  203. c.JSON(nil, ecode.RequestErr)
  204. return
  205. }
  206. a := &app.Auth{
  207. AppTreeID: v.AppTreeID,
  208. AppID: v.AppID,
  209. ServiceTreeID: v.ServiceTreeID,
  210. ServiceID: v.ServiceID,
  211. RPCMethod: v.RPCMethod,
  212. HTTPMethod: v.HTTPMethod,
  213. Quota: v.Quota,
  214. }
  215. if err = apmSvc.DB.Create(a).Error; err != nil {
  216. log.Error("apmSvc.appAuthAdd create error(%v)", err)
  217. c.JSON(nil, err)
  218. return
  219. }
  220. username := name(c)
  221. apmSvc.SendLog(*c, username, 0, 1, a.ID, "apmSvc.appAuthAdd", a)
  222. c.JSON(nil, err)
  223. }
  224. func appAuthEdit(c *bm.Context) {
  225. v := new(struct {
  226. ID int `form:"id" validate:"required"`
  227. RPCMethod string `form:"rpc_method"`
  228. HTTPMethod string `form:"http_method"`
  229. Quota int `form:"quota" validate:"required"`
  230. })
  231. var err error
  232. if err = c.Bind(v); err != nil {
  233. return
  234. }
  235. a := &app.Auth{}
  236. if err = apmSvc.DB.Where("id = ?", v.ID).Find(a).Error; err != nil {
  237. log.Error("apmSvc.appAuthEdit find(%d) error(%v)", v.ID, err)
  238. c.JSON(nil, err)
  239. return
  240. }
  241. ups := map[string]interface{}{
  242. "rpc_method": v.RPCMethod,
  243. "http_method": v.HTTPMethod,
  244. "quota": v.Quota,
  245. }
  246. if err = apmSvc.DB.Model(&app.Auth{}).Where("id = ?", v.ID).Updates(ups).Error; err != nil {
  247. log.Error("apmSvc.appAuthEdit updates error(%v)", err)
  248. c.JSON(nil, err)
  249. return
  250. }
  251. sqlLog := &map[string]interface{}{
  252. "SQLType": "update",
  253. "Where": "id = ?",
  254. "Value1": v.ID,
  255. "Update": ups,
  256. "Old": a,
  257. }
  258. username := name(c)
  259. apmSvc.SendLog(*c, username, 0, 2, int64(v.ID), "apmSvc.appAuthEdit", sqlLog)
  260. c.JSON(nil, err)
  261. }
  262. func appAuthDelete(c *bm.Context) {
  263. v := new(struct {
  264. ID int `form:"id" validate:"required"`
  265. })
  266. var err error
  267. if err = c.Bind(v); err != nil {
  268. return
  269. }
  270. a := &app.Auth{}
  271. if err = apmSvc.DB.Where("id = ?", v.ID).Find(a).Error; err != nil {
  272. log.Error("apmSvc.appAuthDelete find(%d) error(%v)", v.ID, err)
  273. c.JSON(nil, err)
  274. return
  275. }
  276. if err = apmSvc.DB.Delete(a).Error; err != nil {
  277. log.Error("apmSvc.appAuthDelete delete(%d) error(%v)", v.ID, err)
  278. c.JSON(nil, err)
  279. return
  280. }
  281. username := name(c)
  282. apmSvc.SendLog(*c, username, 0, 3, int64(v.ID), "apmSvc.appAuthDelete", a)
  283. c.JSON(nil, err)
  284. }
  285. func appTree(c *bm.Context) {
  286. v := new(struct {
  287. Bu string `form:"bu"`
  288. Team string `form:"team"`
  289. Name string `form:"name"`
  290. })
  291. var err error
  292. if err = c.Bind(v); err != nil {
  293. return
  294. }
  295. username := name(c)
  296. trees, err := apmSvc.Trees(c, username, c.Request.Header.Get("Cookie"))
  297. if err != nil {
  298. log.Error("apmSvc.appTree error(%v)", err)
  299. c.JSON(nil, err)
  300. return
  301. }
  302. var data []string
  303. temp := make(map[string]string)
  304. if v.Bu == "" && v.Team == "" {
  305. for _, val := range trees {
  306. nameArr := strings.Split(val.Path, ".")
  307. newName := nameArr[1]
  308. if f := temp[newName]; f == "" {
  309. data = append(data, newName)
  310. temp[newName] = newName
  311. }
  312. }
  313. } else if v.Bu != "" && v.Team != "" && v.Name != "" {
  314. for _, val := range trees {
  315. nameArr := strings.Split(val.Path, ".")
  316. if v.Bu == nameArr[1] && v.Team == nameArr[2] && v.Name == nameArr[3] {
  317. data = append(data, strconv.Itoa(val.TreeID))
  318. }
  319. }
  320. } else if v.Bu != "" && v.Team != "" {
  321. for _, val := range trees {
  322. nameArr := strings.Split(val.Path, ".")
  323. if v.Bu == nameArr[1] && v.Team == nameArr[2] {
  324. newName := nameArr[1] + "." + nameArr[2] + "." + nameArr[3]
  325. if f := temp[newName]; f == "" {
  326. data = append(data, nameArr[3])
  327. temp[newName] = newName
  328. }
  329. }
  330. }
  331. } else if v.Bu != "" {
  332. for _, val := range trees {
  333. nameArr := strings.Split(val.Path, ".")
  334. if v.Bu == nameArr[1] {
  335. newName := v.Bu + "." + nameArr[2]
  336. if f := temp[newName]; f == "" {
  337. data = append(data, nameArr[2])
  338. temp[newName] = newName
  339. }
  340. }
  341. }
  342. }
  343. c.JSON(data, nil)
  344. }
  345. func appCallerSearch(c *bm.Context) {
  346. v := new(struct {
  347. AppID string `form:"app_id"`
  348. Pn int `form:"pn" default:"1"`
  349. Ps int `form:"ps" default:"20"`
  350. })
  351. var err error
  352. if err = c.Bind(v); err != nil {
  353. return
  354. }
  355. var (
  356. aa []*app.Auth
  357. count int
  358. lk = "%" + v.AppID + "%"
  359. )
  360. if v.AppID != "" {
  361. err = apmSvc.DB.Where("app_id LIKE ?", lk).Group("app_tree_id").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
  362. } else {
  363. err = apmSvc.DB.Group("app_tree_id").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
  364. }
  365. if err != nil {
  366. log.Error("apmSvc.appCallerSearch error(%v)", err)
  367. c.JSON(nil, err)
  368. return
  369. }
  370. if v.AppID != "" {
  371. err = apmSvc.DB.Select("count(distinct(app_tree_id))").Where("app_id LIKE ?", lk).Model(&app.Auth{}).Count(&count).Error
  372. } else {
  373. err = apmSvc.DB.Select("count(distinct(app_tree_id))").Model(&app.Auth{}).Count(&count).Error
  374. }
  375. if err != nil {
  376. log.Error("apmSvc.appCallerSearch count error(%v)", err)
  377. c.JSON(nil, err)
  378. return
  379. }
  380. type result struct {
  381. AppTreeID int64 `gorm:"column:app_tree_id" json:"app_tree_id"`
  382. AppID string `gorm:"column:app_id" json:"app_id"`
  383. Services []*app.Auth
  384. }
  385. var results []*result
  386. if count > 0 {
  387. var in []int64
  388. auth := []*app.Auth{}
  389. for _, val := range aa {
  390. in = append(in, val.AppTreeID)
  391. fmt.Printf("apptreeid=%v", val.AppTreeID)
  392. }
  393. apmSvc.DB.Where("app_tree_id in (?)", in).Find(&auth)
  394. for _, vv := range aa {
  395. rs := new(result)
  396. rs.AppTreeID = vv.AppTreeID
  397. rs.AppID = vv.AppID
  398. for _, vvv := range auth {
  399. if vv.AppTreeID == vvv.AppTreeID {
  400. rs.Services = append(rs.Services, vvv)
  401. }
  402. }
  403. results = append(results, rs)
  404. }
  405. }
  406. data := &Paper{
  407. Pn: v.Pn,
  408. Ps: v.Ps,
  409. Items: results,
  410. Total: count,
  411. }
  412. c.JSON(data, nil)
  413. }