databus.go 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  1. package http
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "math/rand"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/jinzhu/gorm"
  14. "go-common/app/admin/main/apm/conf"
  15. "go-common/app/admin/main/apm/model/databus"
  16. "go-common/app/admin/main/apm/service"
  17. "go-common/library/ecode"
  18. "go-common/library/log"
  19. bm "go-common/library/net/http/blademaster"
  20. )
  21. func databusProjects(c *bm.Context) {
  22. username := name(c)
  23. projects, err := apmSvc.Projects(c, username, c.Request.Header.Get("Cookie"))
  24. if err != nil {
  25. log.Error("%v", err)
  26. c.JSON(nil, err)
  27. return
  28. }
  29. c.JSON(projects, nil)
  30. }
  31. func databusClusters(c *bm.Context) {
  32. var clusters []string
  33. for k := range conf.Conf.Kafka {
  34. clusters = append(clusters, k)
  35. }
  36. c.JSON(clusters, nil)
  37. }
  38. func databusApps(c *bm.Context) {
  39. v := new(struct {
  40. Pn int `form:"pn" default:"1" validate:"min=1"`
  41. Ps int `form:"ps" default:"20" validate:"min=1"`
  42. Project string `form:"project"`
  43. })
  44. var err error
  45. if err = c.Bind(v); err != nil {
  46. return
  47. }
  48. username := name(c)
  49. projects, err := apmSvc.Projects(c, username, c.Request.Header.Get("Cookie"))
  50. if err != nil {
  51. log.Error("%v", err)
  52. c.JSON(nil, err)
  53. return
  54. }
  55. var (
  56. apps []*databus.App
  57. total int
  58. )
  59. if v.Project == "" {
  60. err = apmSvc.DBDatabus.Order("id").Offset((v.Pn-1)*v.Ps).Limit(v.Ps).Where("project in (?)", projects).Find(&apps).Error
  61. } else {
  62. err = apmSvc.DBDatabus.Order("id").Offset((v.Pn-1)*v.Ps).Limit(v.Ps).Where("project in (?) AND project LIKE ?", projects, "%"+v.Project+"%").Find(&apps).Error
  63. }
  64. if err != nil {
  65. log.Error("apmSvr.Apps error(%v)", err)
  66. c.JSON(nil, err)
  67. return
  68. }
  69. if err = apmSvc.DBDatabus.Model(&databus.App{}).Where("project in (?)", projects).Count(&total).Error; err != nil {
  70. log.Error("apmSvr.Apps count error(%v)", err)
  71. c.JSON(nil, err)
  72. return
  73. }
  74. data := &Paper{
  75. Pn: v.Pn,
  76. Ps: v.Ps,
  77. Items: apps,
  78. Total: total,
  79. }
  80. c.JSON(data, nil)
  81. }
  82. func databusAppAdd(c *bm.Context) {
  83. v := new(struct {
  84. Project string `form:"project" validate:"required"`
  85. Remark string `form:"remark"`
  86. })
  87. var err error
  88. if err = c.Bind(v); err != nil {
  89. return
  90. }
  91. m := &databus.App{
  92. AppKey: genKey(v.Project),
  93. AppSecret: genSecret(v.Project + strconv.FormatInt(rand.Int63(), 10)),
  94. Project: v.Project,
  95. Remark: v.Remark,
  96. }
  97. db := apmSvc.DBDatabus.Model(&databus.App{}).Create(m)
  98. if err = db.Error; err != nil {
  99. log.Error("apmSvc.appAdd error(%v)", err)
  100. c.JSON(nil, err)
  101. return
  102. }
  103. username := name(c)
  104. apmSvc.SendLog(*c, username, 0, 1, int64(m.ID), "apmSvc.appAdd", m)
  105. data := map[string]int{
  106. "id": db.Value.(*databus.App).ID,
  107. }
  108. c.JSON(data, nil)
  109. }
  110. func databusAppEdit(c *bm.Context) {
  111. v := new(struct {
  112. ID int64 `form:"id" validate:"required"`
  113. Remark string `form:"remark"`
  114. })
  115. var err error
  116. if err = c.Bind(v); err != nil {
  117. return
  118. }
  119. app := &databus.App{}
  120. if err = apmSvc.DBDatabus.Find(app, v.ID).Error; err != nil {
  121. c.JSON(nil, err)
  122. return
  123. }
  124. if err = apmSvc.DBDatabus.Model(&databus.App{}).Where("id = ?", v.ID).Omit("id").UpdateColumns(v).Error; err != nil {
  125. log.Error("apmSvc.serviceMod error(%v)", err)
  126. c.JSON(nil, err)
  127. return
  128. }
  129. sqlLog := &map[string]interface{}{
  130. "SQLType": "update",
  131. "Where": "id = ?",
  132. "Value1": v.ID,
  133. "Update": v,
  134. "Old": app,
  135. }
  136. username := name(c)
  137. apmSvc.SendLog(*c, username, 0, 2, v.ID, "apmSvc.serviceMod", sqlLog)
  138. c.JSON(nil, err)
  139. }
  140. func databusGroups(c *bm.Context) {
  141. v := new(struct {
  142. Topic string `form:"topic"`
  143. Group string `form:"group" default:""`
  144. Pn int `form:"pn" default:"1" validate:"min=1"`
  145. Ps int `form:"ps" default:"20" validate:"min=1"`
  146. })
  147. var err error
  148. if err = c.Bind(v); err != nil {
  149. return
  150. }
  151. username := name(c)
  152. projects, err := apmSvc.Projects(c, username, c.Request.Header.Get("Cookie"))
  153. if err != nil {
  154. c.JSON(nil, err)
  155. return
  156. }
  157. var (
  158. apps []*databus.App
  159. topics []*databus.Topic
  160. groups []*databus.Group
  161. total int
  162. )
  163. if err = apmSvc.DBDatabus.Model(&databus.App{}).Where("project in (?)", projects).Find(&apps).Error; err != nil {
  164. c.JSON(nil, err)
  165. return
  166. }
  167. var appIDs []int
  168. for _, val := range apps {
  169. appIDs = append(appIDs, val.ID)
  170. }
  171. if v.Topic != "" {
  172. err = apmSvc.DBDatabus.Where("topic = ?", v.Topic).Find(&topics).Error
  173. } else {
  174. err = apmSvc.DBDatabus.Find(&topics).Error
  175. }
  176. if err != nil {
  177. log.Error("apmSvc.groups error(%v)", err)
  178. c.JSON(nil, err)
  179. return
  180. }
  181. var topicIDS []int
  182. for _, value := range topics {
  183. topicIDS = append(topicIDS, value.ID)
  184. }
  185. if err = apmSvc.DBDatabus.Raw(`SELECT auth2.id,auth2.group,auth2.app_id,app2.app_key,app2.project,auth2.topic_id,auth2.operation,topic.cluster,topic.topic,auth2.remark,auth2.ctime,auth2.mtime,auth2.percentage,auth2.alarm, if(auth2.number=0,100,auth2.number) as number
  186. FROM auth2 LEFT JOIN app2 ON app2.id=auth2.app_id LEFT JOIN topic ON topic.id=auth2.topic_id WHERE auth2.is_delete=0 AND auth2.topic_id in (?) and auth2.app_id in (?) and auth2.group LIKE '%`+v.Group+`%' ORDER BY auth2.id LIMIT ?,?`,
  187. topicIDS, appIDs, (v.Pn-1)*v.Ps, v.Ps).Scan(&groups).Error; err != nil {
  188. log.Error("apmSvc.Groups error(%v)", err)
  189. c.JSON(nil, err)
  190. return
  191. }
  192. if err = apmSvc.DBDatabus.Model(&databus.Group{}).Joins("LEFT JOIN app2 ON app2.id=auth2.app_id LEFT JOIN topic ON topic.id=auth2.topic_id").
  193. Where("auth2.is_delete=0 AND auth2.topic_id in (?) AND auth2.app_id in (?) and auth2.group LIKE '%"+v.Group+"%'", topicIDS, appIDs).Count(&total).Error; err != nil {
  194. log.Error("apmSvc.Groups count error(%v)", err)
  195. c.JSON(nil, err)
  196. return
  197. }
  198. data := &Paper{
  199. Pn: v.Pn,
  200. Ps: v.Ps,
  201. Items: groups,
  202. Total: total,
  203. }
  204. c.JSON(data, nil)
  205. }
  206. func databusGroupProjects(c *bm.Context) {
  207. var apps []*databus.App
  208. var err error
  209. if err = apmSvc.DBDatabus.Find(&apps).Error; err != nil {
  210. log.Error("apmSvc.GroupProjects error(%v)", err)
  211. c.JSON(nil, err)
  212. return
  213. }
  214. username := name(c)
  215. allPs, err := apmSvc.Projects(c, username, c.Request.Header.Get("Cookie"))
  216. if err != nil {
  217. log.Error("apmSvc.Projects(%s) error(%v)", username, err)
  218. c.JSON(nil, err)
  219. return
  220. }
  221. ps := make([]string, 0, len(apps))
  222. for _, app := range apps {
  223. for _, pj := range allPs {
  224. if pj == app.Project {
  225. ps = append(ps, app.Project)
  226. break
  227. }
  228. }
  229. }
  230. c.JSON(ps, nil)
  231. }
  232. func databusGroupSubAdd(c *bm.Context) {
  233. v := new(struct {
  234. Project string `form:"project" validate:"required"`
  235. Topic string `form:"topic" validate:"required"`
  236. // Operation int8 `form:"operation" validate:"required"`
  237. Remark string `form:"remark"`
  238. })
  239. var err error
  240. if err = c.Bind(v); err != nil {
  241. return
  242. }
  243. topic := &databus.Topic{}
  244. app := &databus.App{}
  245. if err = apmSvc.DBDatabus.Where("topic = ?", v.Topic).First(topic).Error; err != nil {
  246. log.Error("apmSvc.databusGroupSubAdd topic error(%v)", err)
  247. c.JSON(nil, err)
  248. return
  249. }
  250. if err = apmSvc.DBDatabus.Where("project = ? ", v.Project).First(app).Error; err != nil {
  251. log.Error("apmSvc.databusGroupSubAdd project error(%v)", err)
  252. c.JSON(nil, err)
  253. return
  254. }
  255. m := &databus.Group{
  256. Group: genGroup(v.Topic, v.Project, 1),
  257. TopicID: topic.ID,
  258. AppID: app.ID,
  259. Operation: 1, // NOTE: sub
  260. Remark: v.Remark,
  261. Number: 100,
  262. Percentage: "80",
  263. }
  264. exist := 0
  265. if err = apmSvc.DBDatabus.Model(&databus.Group{}).Where("`group`=? AND topic_id=?", m.Group, m.TopicID).Count(&exist).Error; err != nil {
  266. log.Error("apmSvc.databusGroupSubAdd exist group error(%v)", err)
  267. c.JSON(nil, err)
  268. return
  269. }
  270. if exist > 0 {
  271. m.Group = genGroup(v.Topic, v.Project, 11)
  272. }
  273. db := apmSvc.DBDatabus.Create(m)
  274. if err = db.Error; err != nil {
  275. log.Error("apmSvc.databusGroupSubAdd error(%v)", err)
  276. c.JSON(nil, err)
  277. return
  278. }
  279. username := name(c)
  280. apmSvc.SendLog(*c, username, 0, 1, int64(m.ID), "apmSvc.databusGroupSubAdd", m)
  281. data := map[string]int{
  282. "id": db.Value.(*databus.Group).ID,
  283. }
  284. c.JSON(data, nil)
  285. }
  286. func databusGroupPubAdd(c *bm.Context) {
  287. v := new(struct {
  288. Project string `form:"project" validate:"required"`
  289. Topic string `form:"topic" validate:"required"`
  290. // Operation int8 `form:"operation" validate:"required"`
  291. Remark string `form:"remark"`
  292. })
  293. var err error
  294. if err = c.Bind(v); err != nil {
  295. return
  296. }
  297. topic := &databus.Topic{}
  298. app := &databus.App{}
  299. if err = apmSvc.DBDatabus.Where("topic = ?", v.Topic).First(topic).Error; err != nil {
  300. log.Error("apmSvc.databusGroupPubAdd topic error(%v)", err)
  301. c.JSON(nil, err)
  302. return
  303. }
  304. if err = apmSvc.DBDatabus.Where("project = ? ", v.Project).First(app).Error; err != nil {
  305. log.Error("apmSvc.databusGroupPubAdd project error(%v)", err)
  306. c.JSON(nil, err)
  307. return
  308. }
  309. m := &databus.Group{
  310. Group: genGroup(v.Topic, v.Project, 2),
  311. TopicID: topic.ID,
  312. AppID: app.ID,
  313. Operation: 2, // NOTE: pub
  314. Remark: v.Remark,
  315. }
  316. db := apmSvc.DBDatabus.Create(m)
  317. if err = db.Error; err != nil {
  318. log.Error("apmSvc.databusGroupPubAdd error(%v)", err)
  319. c.JSON(nil, err)
  320. return
  321. }
  322. username := name(c)
  323. apmSvc.SendLog(*c, username, 0, 1, int64(m.ID), "apmSvc.databusGroupPubAdd", m)
  324. data := map[string]int{
  325. "id": db.Value.(*databus.Group).ID,
  326. }
  327. c.JSON(data, nil)
  328. }
  329. func databusTopics(c *bm.Context) {
  330. v := new(struct {
  331. Pn int `form:"pn" default:"1" validate:"min=1"`
  332. Ps int `form:"ps" default:"20" validate:"min=1"`
  333. Cluster string `form:"cluster"`
  334. Topic string `form:"topic"`
  335. })
  336. var err error
  337. if err = c.Bind(v); err != nil {
  338. return
  339. }
  340. var (
  341. tps []*databus.Topic
  342. total int
  343. )
  344. if v.Cluster != "" && v.Topic == "" {
  345. err = apmSvc.DBDatabus.Where("cluster = ?", v.Cluster).Order("id").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&tps).Error
  346. } else if v.Cluster == "" && v.Topic != "" {
  347. err = apmSvc.DBDatabus.Where("topic LIKE ?", "%"+v.Topic+"%").Order("id").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&tps).Error
  348. } else if v.Cluster != "" && v.Topic != "" {
  349. err = apmSvc.DBDatabus.Where("cluster = ? AND topic LIKE ?", v.Cluster, "%"+v.Topic+"%").Order("id").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&tps).Error
  350. } else {
  351. err = apmSvc.DBDatabus.Order("id").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&tps).Error
  352. }
  353. if err != nil {
  354. log.Error("apmSvr.Topics error(%v)", err)
  355. c.JSON(nil, err)
  356. return
  357. }
  358. if v.Cluster != "" && v.Topic == "" {
  359. err = apmSvc.DBDatabus.Model(&databus.Topic{}).Where("cluster = ?", v.Cluster).Count(&total).Error
  360. } else if v.Cluster == "" && v.Topic != "" {
  361. err = apmSvc.DBDatabus.Model(&databus.Topic{}).Where("topic LIKE ?", "%"+v.Topic+"%").Count(&total).Error
  362. } else if v.Cluster != "" && v.Topic != "" {
  363. err = apmSvc.DBDatabus.Model(&databus.Topic{}).Where("cluster = ? AND topic LIKE ?", v.Cluster, "%"+v.Topic+"%").Count(&total).Error
  364. } else {
  365. err = apmSvc.DBDatabus.Model(&databus.Topic{}).Count(&total).Error
  366. }
  367. if err != nil {
  368. log.Error("apmSvr.Topics cluster(%s) count error(%v)", v.Cluster, err)
  369. c.JSON(nil, err)
  370. return
  371. }
  372. data := &Paper{
  373. Pn: v.Pn,
  374. Ps: v.Ps,
  375. Items: tps,
  376. Total: total,
  377. }
  378. c.JSON(data, nil)
  379. }
  380. func databusTopicNames(c *bm.Context) {
  381. v := new(struct {
  382. Cluster string `form:"cluster" validate:"required"`
  383. })
  384. var err error
  385. if err = c.Bind(v); err != nil {
  386. return
  387. }
  388. var tps []*databus.Topic
  389. if err = apmSvc.DBDatabus.Where("cluster = ?", v.Cluster).Find(&tps).Error; err != nil {
  390. log.Error("apmSvr.Topics error(%v)", err)
  391. c.JSON(nil, err)
  392. return
  393. }
  394. var ns []string
  395. for _, t := range tps {
  396. ns = append(ns, t.Topic)
  397. }
  398. c.JSON(ns, nil)
  399. }
  400. func databusTopicAdd(c *bm.Context) {
  401. res := make(map[string]interface{}, 1)
  402. v := new(struct {
  403. Topic string `form:"topic" validate:"required"`
  404. Cluster string `form:"cluster" validate:"required"`
  405. Remark string `form:"remark"`
  406. })
  407. var err error
  408. if err = c.Bind(v); err != nil {
  409. return
  410. }
  411. f := strings.HasSuffix(v.Topic, "-T")
  412. if !f {
  413. c.JSON(nil, ecode.RequestErr)
  414. return
  415. }
  416. topic := &databus.Topic{
  417. Topic: v.Topic,
  418. Cluster: v.Cluster,
  419. Remark: v.Remark,
  420. }
  421. db := apmSvc.DBDatabus.Create(topic)
  422. if err = db.Error; err != nil {
  423. log.Error("apmSvc.databusTopicAdd error(%v)", err)
  424. res["message"] = "DB创建topic失败"
  425. c.JSONMap(res, err)
  426. return
  427. }
  428. username := name(c)
  429. apmSvc.SendLog(*c, username, 0, 1, int64(topic.ID), "apmSvc.databusTopicAdd", topic)
  430. if conf.Conf.Kafka[topic.Cluster] == nil || !(len(conf.Conf.Kafka[v.Cluster].Brokers) > 0) {
  431. log.Error("apmSvc.topicAdd CreateTopic kafka cluster error(%v)", v.Cluster)
  432. res["message"] = "kafka集群(" + v.Cluster + ")未配置,请手动创建"
  433. c.JSONMap(res, err)
  434. return
  435. }
  436. if err = service.CreateTopic(conf.Conf.Kafka[v.Cluster].Brokers, v.Topic, conf.Conf.DatabusConfig.Partitions, conf.Conf.DatabusConfig.Factor); err != nil {
  437. log.Error("apmSvc.topicAdd CreateTopic kafka error(%v)", err)
  438. res["message"] = "kafka创建topic失败,请手动创建"
  439. c.JSONMap(res, err)
  440. return
  441. }
  442. data := map[string]int{
  443. "id": db.Value.(*databus.Topic).ID,
  444. }
  445. c.JSON(data, nil)
  446. }
  447. func databusTopicEdit(c *bm.Context) {
  448. res := map[string]interface{}{}
  449. v := new(struct {
  450. ID int64 `form:"id" validate:"required"`
  451. Cluster string `form:"cluster" validate:"required"`
  452. })
  453. var err error
  454. if err = c.Bind(v); err != nil {
  455. return
  456. }
  457. topic := &databus.Topic{}
  458. if err = apmSvc.DBDatabus.Find(topic, v.ID).Error; err != nil {
  459. c.JSON(nil, err)
  460. return
  461. }
  462. username := name(c)
  463. ups := map[string]interface{}{
  464. "cluster": v.Cluster,
  465. "operator": username,
  466. }
  467. if v.Cluster != topic.Cluster {
  468. if err = apmSvc.DBDatabus.Model(&databus.Topic{}).Where("`id` = ?", v.ID).Updates(ups).Error; err != nil {
  469. log.Error("apmSvc.databusTopicEdit error(%v)", err)
  470. c.JSON(nil, err)
  471. return
  472. }
  473. sqlLog := &map[string]interface{}{
  474. "SQLType": "update",
  475. "Where": "id = ?",
  476. "Value1": v.ID,
  477. "Update": ups,
  478. "Old": topic,
  479. }
  480. apmSvc.SendLog(*c, username, 0, 2, v.ID, "apmSvc.databusTopicEdit", sqlLog)
  481. auth := &databus.OldAuth{}
  482. if err = apmSvc.DBDatabus.Where("`topic` = ?", topic.Topic).First(auth).Error; err == nil {
  483. app := &databus.OldApp{}
  484. if err = apmSvc.DBDatabus.Where("`id` = ?", auth.AppID).First(app).Error; err != nil {
  485. log.Error("apmSvc.databusTopicEdit first app id(%v) error(%v)", auth.AppID, err)
  486. c.JSON(nil, err)
  487. return
  488. }
  489. update := map[string]interface{}{
  490. "cluster": v.Cluster,
  491. }
  492. if err = apmSvc.DBDatabus.Model(&databus.OldApp{}).Where("`id` = ?", auth.AppID).Updates(update).Error; err != nil {
  493. log.Error("apmSvc.databusTopicEdit error(%v)", err)
  494. c.JSON(nil, err)
  495. return
  496. }
  497. sqlLog := &map[string]interface{}{
  498. "SQLType": "update",
  499. "Where": "id = ?",
  500. "Value1": auth.AppID,
  501. "Update": update,
  502. "Old": app,
  503. }
  504. apmSvc.SendLog(*c, username, 0, 2, v.ID, "apmSvc.databusTopicEdit", sqlLog)
  505. }
  506. if conf.Conf.Kafka[topic.Cluster] == nil || !(len(conf.Conf.Kafka[v.Cluster].Brokers) > 0) {
  507. log.Error("apmSvc.topicAdd CreateTopic kafka cluster error(%v)", v.Cluster)
  508. res["message"] = "kafka集群(" + v.Cluster + ")未配置,请手动创建"
  509. c.JSONMap(res, err)
  510. return
  511. }
  512. if err = service.CreateTopic(conf.Conf.Kafka[v.Cluster].Brokers, topic.Topic, conf.Conf.DatabusConfig.Partitions, conf.Conf.DatabusConfig.Factor); err != nil {
  513. log.Error("apmSvc.topicAdd CreateTopic kafka error(%v)", err)
  514. res["message"] = "kafka创建topic失败,请手动创建"
  515. c.JSONMap(res, err)
  516. return
  517. }
  518. }
  519. c.JSON(nil, err)
  520. }
  521. func genKey(s string) string {
  522. m := md5.New()
  523. m.Write([]byte(s))
  524. cipherByte := m.Sum(nil)
  525. return hex.EncodeToString(cipherByte[:8])
  526. }
  527. func genSecret(s string) string {
  528. m := md5.New()
  529. m.Write([]byte(s))
  530. cipherByte := m.Sum(nil)
  531. return hex.EncodeToString(cipherByte)
  532. }
  533. func genGroup(t, p string, o int8) string {
  534. group := ""
  535. ts := []rune(t)
  536. topic := string(ts[:len(ts)-2])
  537. group += topic + "-"
  538. p = strings.Replace(p, "-", ".", -1)
  539. p = strings.Replace(p, "_", ".", -1)
  540. ps := strings.Split(p, ".")
  541. path := ""
  542. for _, v := range ps {
  543. path += strings.Title(v)
  544. }
  545. group += path
  546. switch o {
  547. case 1:
  548. group += "-S"
  549. case 2:
  550. group += "-P"
  551. case 3:
  552. group += "-PS"
  553. case 11:
  554. group += "-2-S"
  555. case 4:
  556. group += "-N"
  557. }
  558. return group
  559. }
  560. func renameGroup(t, p, r string, o int8) (group string) {
  561. group = ""
  562. ts := []rune(t)
  563. topic := string(ts[:len(ts)-2])
  564. group += topic + "-"
  565. p = strings.Replace(p, "-", ".", -1)
  566. p = strings.Replace(p, "_", ".", -1)
  567. ps := strings.Split(p, ".")
  568. path := ""
  569. for _, v := range ps {
  570. path += strings.Title(v)
  571. }
  572. group += path
  573. switch o {
  574. case 1:
  575. group += "-" + r + "-S"
  576. case 2:
  577. group += "-" + r + "-P"
  578. case 3:
  579. group += "-" + r + "-N"
  580. case 4:
  581. group += "-" + r + "-N"
  582. }
  583. return
  584. }
  585. func databusAlarm(c *bm.Context) {
  586. var (
  587. gps []*databus.Group
  588. gpMaps []*databus.Alarm
  589. )
  590. if err := apmSvc.DBDatabus.Select("auth2.group, app2.project, auth2.alarm, auth2.percentage").Joins("left join app2 on auth2.app_id = app2.id").Where("auth2.is_delete = 0").Find(&gps).Error; err != nil {
  591. log.Error("apmSvc.databusProject error(%v)", err)
  592. c.JSON(nil, err)
  593. return
  594. }
  595. for _, v := range gps {
  596. a := &databus.Alarm{}
  597. a.Group = v.Group
  598. a.Project = v.Project
  599. a.Alarm = v.Alarm
  600. if len(v.Percentage) == 0 {
  601. v.Percentage = "0"
  602. }
  603. a.Percentage = v.Percentage
  604. gpMaps = append(gpMaps, a)
  605. }
  606. c.JSON(gpMaps, nil)
  607. }
  608. func databusAlarms(c *bm.Context) {
  609. var (
  610. gps []*databus.Group
  611. gpMaps []*databus.Alarms
  612. records []*databus.Record
  613. count int
  614. err error
  615. )
  616. v := new(struct {
  617. Pn int `form:"pn" default:"1"`
  618. Ps int `form:"ps" default:"20"`
  619. })
  620. if err = c.Bind(v); err != nil {
  621. return
  622. }
  623. if err = apmSvc.DBDatabus.Select("auth2.group, app2.project, auth2.alarm, auth2.percentage, topic.topic, topic.cluster").Joins("left join app2 on auth2.app_id = app2.id").Joins("left join topic on topic.id = auth2.topic_id").Where("auth2.is_delete = 0").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&gps).Error; err != nil {
  624. log.Error("apmSvc.databusAlarms error(%v)", err)
  625. c.JSON(nil, err)
  626. }
  627. if err = apmSvc.DBDatabus.Select("auth2.group, app2.project, auth2.alarm, auth2.percentage, topic.topic, topic.cluster").Joins("left join app2 on auth2.app_id = app2.id").Joins("left join topic on topic.id = auth2.topic_id").Where("auth2.is_delete = 0").Model(&databus.Group{}).Count(&count).Error; err != nil {
  628. log.Error("apmSvc.databusAlarms count error(%v)", err)
  629. c.JSON(nil, err)
  630. }
  631. for _, v := range gps {
  632. a := &databus.Alarms{}
  633. a.Group = v.Group
  634. a.Project = v.Project
  635. a.Alarm = v.Alarm
  636. if len(v.Percentage) == 0 {
  637. v.Percentage = "0"
  638. }
  639. a.Percentage = v.Percentage
  640. a.Topic = v.Topic
  641. a.Cluster = v.Cluster
  642. records, err = service.Diff(v.Cluster, v.Topic, v.Group)
  643. if err != nil {
  644. log.Error("apmSvc.databusAlarms Diff error(%v)", err)
  645. }
  646. a.Diff = records
  647. gpMaps = append(gpMaps, a)
  648. }
  649. data := &Paper{
  650. Pn: v.Pn,
  651. Ps: v.Ps,
  652. Items: gpMaps,
  653. Total: count,
  654. }
  655. c.JSON(data, nil)
  656. }
  657. func databusGroupDelete(c *bm.Context) {
  658. v := new(struct {
  659. Group string `form:"group" validate:"required"`
  660. })
  661. var err error
  662. if err = c.Bind(v); err != nil {
  663. return
  664. }
  665. group := &databus.Group{}
  666. if err = apmSvc.DBDatabus.Where("`group` = ?", v.Group).Find(group).Error; err != nil {
  667. log.Error("apmSvc.databusGroupDelete select error(%v)", err)
  668. c.JSON(nil, err)
  669. return
  670. }
  671. if group.IsDelete == 1 {
  672. return
  673. }
  674. username := name(c)
  675. ups := map[string]interface{}{
  676. "is_delete": 1,
  677. "operator": username,
  678. }
  679. if err = apmSvc.DBDatabus.Model(group).Where("`group` = ?", v.Group).Updates(ups).Error; err != nil {
  680. log.Error("apmSvc.databusGroupDelete delete error(%v)", err)
  681. c.JSON(nil, err)
  682. return
  683. }
  684. apmSvc.SendLog(*c, username, 0, 4, int64(group.ID), "apmSvc.databusGroupDelete", group)
  685. c.JSON(nil, err)
  686. }
  687. func databusGroupRename(c *bm.Context) {
  688. v := new(struct {
  689. Group string `form:"group" validate:"required"`
  690. NewName string `form:"newname" validate:"required"`
  691. })
  692. var err error
  693. if err = c.Bind(v); err != nil {
  694. return
  695. }
  696. reg := regexp.MustCompile(`.+?-.+?-2-.+?`)
  697. match := reg.FindAllString(v.Group, -1)
  698. if len(match) == 0 {
  699. c.JSON(nil, ecode.RequestErr)
  700. return
  701. }
  702. newGroup := strings.Replace(v.Group, "-2-", "-"+v.NewName+"-", -1)
  703. group := &databus.Group{}
  704. if err = apmSvc.DBDatabus.Where("`group` = ?", v.Group).Find(group).Error; err != nil {
  705. log.Error("apmSvc.databusGroupRename select error(%v)", err)
  706. c.JSON(nil, err)
  707. return
  708. }
  709. username := name(c)
  710. ups := map[string]interface{}{
  711. "group": newGroup,
  712. "operator": username,
  713. }
  714. if err = apmSvc.DBDatabus.Model(&databus.Group{}).Where("`group` = ?", v.Group).Updates(ups).Error; err != nil {
  715. log.Error("apmSvc.databusGroupRename update error(%v)", err)
  716. c.JSON(nil, err)
  717. return
  718. }
  719. sqlLog := &map[string]interface{}{
  720. "SQLType": "update",
  721. "Where": "group = ?",
  722. "Value1": v.Group,
  723. "Update": ups,
  724. "Old": group,
  725. }
  726. apmSvc.SendLog(*c, username, 0, 2, 0, "apmSvc.databusGroupRename", sqlLog)
  727. c.JSON(nil, err)
  728. }
  729. func databusGroupOffset(c *bm.Context) {
  730. v := new(struct {
  731. Group string `form:"group" validate:"required"`
  732. })
  733. var err error
  734. if err = c.Bind(v); err != nil {
  735. return
  736. }
  737. group := &databus.Group{}
  738. if err = apmSvc.DBDatabus.Where("`group` = ?", v.Group).Find(group).Error; err != nil {
  739. log.Error("databusGroupOffset select group error(%v)", err)
  740. c.JSON(nil, err)
  741. return
  742. }
  743. topic := &databus.Topic{}
  744. if err = apmSvc.DBDatabus.Where("id = ?", group.TopicID).Find(topic).Error; err != nil {
  745. log.Error("databusGroupOffset select topic error(%v)", err)
  746. c.JSON(nil, err)
  747. return
  748. }
  749. client, err := service.NewClient(conf.Conf.Kafka[topic.Cluster].Brokers, topic.Topic, group.Group)
  750. if err != nil {
  751. log.Error("service.NewClient() error(%v)\n", err)
  752. c.JSON(nil, err)
  753. return
  754. }
  755. defer client.Close()
  756. marked, err := client.OffsetMarked()
  757. if err != nil {
  758. log.Error("client.OffsetMarked() error(%v)\n", err)
  759. c.JSON(nil, err)
  760. return
  761. }
  762. new, err := client.OffsetNew()
  763. if err != nil {
  764. log.Error("client.OffsetNew() error(%v)\n", err)
  765. c.JSON(nil, err)
  766. return
  767. }
  768. type record struct {
  769. Partition int32 `json:"partition"`
  770. Diff int64 `json:"diff"`
  771. New int64 `json:"new"`
  772. }
  773. records := make([]*record, len(new))
  774. for partition, offset := range new {
  775. r := &record{
  776. Partition: partition,
  777. New: offset,
  778. }
  779. if tmp, ok := marked[partition]; ok {
  780. if tmp == -1 {
  781. r.Diff = -1
  782. } else {
  783. r.Diff = offset - tmp
  784. }
  785. }
  786. records[partition] = r
  787. }
  788. c.JSON(records, nil)
  789. }
  790. func databusGroupMarked(c *bm.Context) {
  791. v := new(struct {
  792. Group string `form:"group" validate:"required"`
  793. })
  794. var err error
  795. if err = c.Bind(v); err != nil {
  796. return
  797. }
  798. group := &databus.Group{}
  799. if err = apmSvc.DBDatabus.Where("`group` = ?", v.Group).Find(group).Error; err != nil {
  800. log.Error("databusGroupMarked select group error(%v)", err)
  801. c.JSON(nil, err)
  802. return
  803. }
  804. topic := &databus.Topic{}
  805. if err = apmSvc.DBDatabus.Where("id = ?", group.TopicID).Find(topic).Error; err != nil {
  806. log.Error("databusGroupMarked select topic error(%v)", err)
  807. c.JSON(nil, err)
  808. return
  809. }
  810. client, err := service.NewClient(conf.Conf.Kafka[topic.Cluster].Brokers, topic.Topic, group.Group)
  811. if err != nil {
  812. log.Error("service.NewClient() error(%v)\n", err)
  813. c.JSON(nil, err)
  814. return
  815. }
  816. defer client.Close()
  817. err = client.SeekEnd()
  818. sqlLog := &map[string]interface{}{
  819. "SQLType": "kafka",
  820. "Cluster": conf.Conf.Kafka[topic.Cluster].Brokers,
  821. "Topic": topic,
  822. "Group": group,
  823. "action": "SeekEnd",
  824. }
  825. username := name(c)
  826. apmSvc.SendLog(*c, username, 0, 6, 0, "apmSvc.databusGroupMarked", sqlLog)
  827. if err != nil {
  828. log.Error("client.SeekEnd() error(%v)\n", err)
  829. c.JSON(nil, err)
  830. return
  831. }
  832. c.JSON(nil, err)
  833. }
  834. func databusAlarmEdit(c *bm.Context) {
  835. v := new(struct {
  836. ID int64 `form:"id" validate:"required"`
  837. Alarm int8 `form:"alarm" validate:"required"`
  838. Percentage string `form:"percentage" validate:"required"`
  839. Remark string `form:"remark"`
  840. Number int `form:"number"`
  841. })
  842. var err error
  843. if err = c.Bind(v); err != nil {
  844. return
  845. }
  846. group := &databus.Group{}
  847. if err = apmSvc.DBDatabus.Find(group, v.ID).Error; err != nil {
  848. c.JSON(nil, err)
  849. return
  850. }
  851. ups := map[string]interface{}{
  852. "alarm": v.Alarm,
  853. "percentage": v.Percentage,
  854. "remark": v.Remark,
  855. }
  856. if v.Number > 0 {
  857. ups["number"] = v.Number
  858. }
  859. if err = apmSvc.DBDatabus.Model(&databus.Group{}).Where("id = ?", v.ID).Updates(ups).Error; err != nil {
  860. log.Error("apmSvc.databusAlarmEdit error(%v)", err)
  861. c.JSON(nil, err)
  862. return
  863. }
  864. sqlLog := &map[string]interface{}{
  865. "SQLType": "update",
  866. "Where": "id = ?",
  867. "Value1": v.ID,
  868. "Update": ups,
  869. "Old": group,
  870. }
  871. username := name(c)
  872. apmSvc.SendLog(*c, username, 0, 2, v.ID, "apmSvc.databusAlarmEdit", sqlLog)
  873. c.JSON(nil, err)
  874. }
  875. func databusGroupBegin(c *bm.Context) {
  876. v := new(struct {
  877. Group string `form:"group" validate:"required"`
  878. })
  879. var err error
  880. if err = c.Bind(v); err != nil {
  881. return
  882. }
  883. group := &databus.Group{}
  884. if err = apmSvc.DBDatabus.Where("`group` = ?", v.Group).Find(group).Error; err != nil {
  885. log.Error("databusGroupMarked select group error(%v)", err)
  886. c.JSON(nil, err)
  887. return
  888. }
  889. topic := &databus.Topic{}
  890. if err = apmSvc.DBDatabus.Where("id = ?", group.TopicID).Find(topic).Error; err != nil {
  891. log.Error("databusGroupMarked select topic error(%v)", err)
  892. c.JSON(nil, err)
  893. return
  894. }
  895. client, err := service.NewClient(conf.Conf.Kafka[topic.Cluster].Brokers, topic.Topic, group.Group)
  896. if err != nil {
  897. log.Error("service.NewClient() error(%v)\n", err)
  898. c.JSON(nil, err)
  899. return
  900. }
  901. defer client.Close()
  902. err = client.SeekBegin()
  903. if err != nil {
  904. log.Error("client.SeekBegin() error(%v)\n", err)
  905. c.JSON(nil, err)
  906. return
  907. }
  908. c.JSON(nil, err)
  909. }
  910. func databusApplyPubAdd(c *bm.Context) {
  911. res := map[string]interface{}{}
  912. v := new(struct {
  913. Project string `form:"project" validate:"required"`
  914. Remark string `form:"remark" validate:"required"`
  915. TopicName string `form:"topic" validate:"required"`
  916. TopicRemark string `form:"topic_remark"`
  917. })
  918. var err error
  919. if err = c.Bind(v); err != nil {
  920. return
  921. }
  922. clstr := ""
  923. topic := &databus.Topic{}
  924. app := &databus.App{}
  925. if err = apmSvc.DBDatabus.Where("project = ? ", v.Project).First(app).Error; err != nil {
  926. log.Error("databusApplyPubAdd project error(%v)", err)
  927. res["message"] = "project不存在"
  928. c.JSONMap(res, err)
  929. return
  930. }
  931. f := strings.HasSuffix(v.TopicName, "-T")
  932. if !f {
  933. log.Error("databusApplyPubAdd topic_name not standard error(%v)", err)
  934. res["message"] = "topic名称不合法"
  935. c.JSONMap(res, ecode.RequestErr)
  936. return
  937. }
  938. if err = apmSvc.DBDatabus.Where("topic = ?", v.TopicName).First(topic).Error; err == nil {
  939. clstr = topic.Cluster
  940. // log.Error("databusApplyPubAdd cluster error(%v)", err)
  941. // res["message"] = "该topic不存在"
  942. // c.JSONMap(res, err)
  943. // return
  944. }
  945. username := name(c)
  946. m := &databus.Apply{
  947. Group: genGroup(v.TopicName, v.Project, 2),
  948. Cluster: clstr,
  949. TopicRemark: v.TopicRemark,
  950. TopicName: v.TopicName,
  951. AppID: app.ID,
  952. Project: app.Project,
  953. Operation: 2, // NOTE: pub
  954. State: 1,
  955. Operator: username,
  956. Remark: v.Remark,
  957. }
  958. //db := apmSvc.DBDatabus.Create(m)
  959. tmp := &databus.Apply{}
  960. db := apmSvc.DBDatabus.Where(databus.Apply{Group: m.Group, State: 4}).Assign(m).FirstOrCreate(tmp)
  961. if err = db.Error; err != nil {
  962. log.Error("apmSvc.databusApplyPubAdd create error(%v)", err)
  963. res["message"] = "创建失败"
  964. c.JSONMap(res, err)
  965. return
  966. }
  967. c.JSON(nil, err)
  968. }
  969. func databusApplySubAdd(c *bm.Context) {
  970. res := map[string]interface{}{}
  971. v := new(struct {
  972. Project string `form:"project" validate:"required"`
  973. Remark string `form:"remark" validate:"required"`
  974. TopicName string `form:"topic" validate:"required"`
  975. Rename string `form:"rename"`
  976. })
  977. var err error
  978. if err = c.Bind(v); err != nil {
  979. return
  980. }
  981. clstr := ""
  982. topic := &databus.Topic{}
  983. app := &databus.App{}
  984. if err = apmSvc.DBDatabus.Where("project = ? ", v.Project).First(app).Error; err != nil {
  985. log.Error("databusApplySubAdd project error(%v)", err)
  986. res["message"] = "project不存在"
  987. c.JSONMap(res, err)
  988. return
  989. }
  990. f := strings.HasSuffix(v.TopicName, "-T")
  991. if !f {
  992. log.Error("databusApplySubAdd topic_name not standard error(%v)", err)
  993. res["message"] = "topic不合法"
  994. c.JSONMap(res, ecode.RequestErr)
  995. return
  996. }
  997. if err = apmSvc.DBDatabus.Where("topic = ?", v.TopicName).First(topic).Error; err == nil {
  998. clstr = topic.Cluster
  999. // log.Error("databusApplySubAdd topic error(%v)", err)
  1000. // res["message"] = "不存在此topic"
  1001. // c.JSONMap(res, err)
  1002. // return
  1003. }
  1004. var groupName string
  1005. var message string
  1006. groupName, message, err = GroupName(v.TopicName, v.Project, v.Rename, 1)
  1007. if err != nil {
  1008. log.Error("databusApplySubAdd groupname error(%v)", err)
  1009. res["message"] = message
  1010. c.JSONMap(res, err)
  1011. return
  1012. }
  1013. username := name(c)
  1014. m := &databus.Apply{
  1015. Group: groupName,
  1016. Cluster: clstr,
  1017. TopicName: v.TopicName,
  1018. AppID: app.ID,
  1019. Project: app.Project,
  1020. Operation: 1, // NOTE: sub
  1021. State: 1,
  1022. Operator: username,
  1023. Remark: v.Remark,
  1024. }
  1025. db := apmSvc.DBDatabus.Create(m)
  1026. if err = db.Error; err != nil {
  1027. log.Error("apmSvc.databusApplySubAdd create error(%v)", err)
  1028. res["message"] = "创建失败"
  1029. c.JSONMap(res, err)
  1030. return
  1031. }
  1032. c.JSON(nil, err)
  1033. }
  1034. func databusApplyEdit(c *bm.Context) {
  1035. res := map[string]interface{}{}
  1036. v := new(struct {
  1037. ID int `form:"id" validate:"required"`
  1038. TopicName string `form:"topic" validate:"required"`
  1039. Project string `form:"project" validate:"required"`
  1040. Remark string `form:"remark" validate:"required"`
  1041. TopicRemark string `form:"topic_remark"`
  1042. Rename string `form:"rename"`
  1043. Cluster string `form:"cluster"`
  1044. })
  1045. var err error
  1046. if err = c.Bind(v); err != nil {
  1047. return
  1048. }
  1049. apply := &databus.Apply{}
  1050. if err = apmSvc.DBDatabus.Where("id = ?", v.ID).First(apply).Error; err != nil {
  1051. log.Error("databusApplyEdit id error(%v)", err)
  1052. res["message"] = "id有误"
  1053. c.JSONMap(res, err)
  1054. return
  1055. }
  1056. if !(apply.State == 1 || apply.State == 2) {
  1057. log.Error("databusApplyEdit apply state error(%v)", err)
  1058. res["message"] = "只有申请中和打回才可修改"
  1059. c.JSONMap(res, ecode.RequestErr)
  1060. return
  1061. }
  1062. clstr := v.Cluster
  1063. topic := &databus.Topic{}
  1064. app := &databus.App{}
  1065. if err = apmSvc.DBDatabus.Where("project = ? ", v.Project).First(app).Error; err != nil {
  1066. log.Error("databusApplyEdit project error(%v)", err)
  1067. res["message"] = "project不存在"
  1068. c.JSONMap(res, err)
  1069. return
  1070. }
  1071. f := strings.HasSuffix(v.TopicName, "-T")
  1072. if !f {
  1073. log.Error("databusApplyEdit topic_name not standard error(%v)", err)
  1074. res["message"] = "topic不合法"
  1075. c.JSONMap(res, ecode.RequestErr)
  1076. return
  1077. }
  1078. if err = apmSvc.DBDatabus.Where("topic = ?", v.TopicName).First(topic).Error; err == nil {
  1079. clstr = topic.Cluster
  1080. // log.Error("databusApplyEdit topic error(%v)", err)
  1081. // res["message"] = "不存在此topic"
  1082. // c.JSONMap(res, err)
  1083. // return
  1084. }
  1085. ups := map[string]interface{}{
  1086. "cluster": clstr,
  1087. "topic_name": v.TopicName,
  1088. "app_id": app.ID,
  1089. "project": v.Project,
  1090. "remark": v.Remark,
  1091. }
  1092. if apply.State == 2 {
  1093. ups["state"] = 1
  1094. }
  1095. if apply.Operation == 2 && apply.State != 3 {
  1096. ups["topic_remark"] = v.TopicRemark
  1097. }
  1098. var groupName string
  1099. var message string
  1100. if (v.TopicName != apply.TopicName) || (v.Project != apply.Project) || len(v.Rename) > 0 {
  1101. groupName, message, err = GroupName(v.TopicName, v.Project, v.Rename, apply.Operation)
  1102. if err != nil {
  1103. log.Error("databusApplySubAdd groupname error(%v)", err)
  1104. res["message"] = message
  1105. c.JSONMap(res, err)
  1106. return
  1107. }
  1108. ups["group"] = groupName
  1109. }
  1110. if err = apmSvc.DBDatabus.Model(apply).Where("id = ?", apply.ID).Updates(ups).Error; err != nil {
  1111. log.Error("databusApplyEdit update error(%v)", err)
  1112. res["message"] = "修改失败"
  1113. c.JSONMap(res, err)
  1114. return
  1115. }
  1116. c.JSON(nil, err)
  1117. }
  1118. func databusApplyList(c *bm.Context) {
  1119. v := new(struct {
  1120. TopicName string `form:"topic"`
  1121. Group string `form:"group"`
  1122. State int8 `form:"state"`
  1123. Pn int `form:"pn" default:"1"`
  1124. Ps int `form:"ps" default:"20"`
  1125. })
  1126. var err error
  1127. if err = c.Bind(v); err != nil {
  1128. return
  1129. }
  1130. var (
  1131. cs []*databus.Apply
  1132. count int
  1133. lk = "%" + v.Group + "%"
  1134. in []int8
  1135. )
  1136. if v.State > 0 {
  1137. in = append(in, v.State)
  1138. } else {
  1139. in = []int8{0, 1, 2, 3, 4}
  1140. }
  1141. if v.TopicName != "" {
  1142. err = apmSvc.DBDatabus.Where("topic_name = ? and `group` like ? and state in (?)", v.TopicName, lk, in).Order("mtime desc").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&cs).Error
  1143. } else {
  1144. err = apmSvc.DBDatabus.Select("group_apply.*,notify.id as nid,notify.gid,notify.offset,notify.state as nstate,notify.filter,notify.concurrent,notify.callback,notify.zone").Joins(
  1145. "left join notify on notify.gid=group_apply.id ").Where("group_apply.group like ? and group_apply.state in (?)",
  1146. lk, in).Order("mtime desc").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&cs).Error
  1147. }
  1148. if err != nil {
  1149. log.Error("apmSvc.databusApplyList error(%v)", err)
  1150. c.JSON(nil, err)
  1151. return
  1152. }
  1153. if v.TopicName != "" {
  1154. err = apmSvc.DBDatabus.Where("topic_name = ? and `group` like ? and state in (?)", v.TopicName, lk, in).Model(&databus.Apply{}).Count(&count).Error
  1155. } else {
  1156. err = apmSvc.DBDatabus.Joins("left join notify on group_apply.id = notify.gid ").Where("group_apply.group like ? and group_apply.state in (?)", lk, in).Model(&databus.NotifyGroup{}).Count(&count).Error
  1157. }
  1158. if err != nil {
  1159. log.Error("apmSvc.databusApplyList count error(%v)", err)
  1160. c.JSON(nil, err)
  1161. return
  1162. }
  1163. var ids []int64
  1164. for _, val := range cs {
  1165. ids = append(ids, val.Nid)
  1166. }
  1167. filter := []*databus.Filter{}
  1168. if err = apmSvc.DBDatabus.Where("nid in (?)", ids).Find(&filter).Error; err != nil && err != gorm.ErrRecordNotFound {
  1169. log.Error("apmSvc.databusApplyList filter error(%v)", err)
  1170. c.JSON(nil, err)
  1171. return
  1172. }
  1173. type result struct {
  1174. *databus.Apply
  1175. }
  1176. var results []*result
  1177. for _, val := range cs {
  1178. rs := new(result)
  1179. rs.Apply = val
  1180. for _, vv := range filter {
  1181. if val.Nid == int64(vv.Nid) {
  1182. err = json.Unmarshal([]byte(vv.Filters), &rs.FilterList)
  1183. }
  1184. }
  1185. results = append(results, rs)
  1186. }
  1187. data := &Paper{
  1188. Pn: v.Pn,
  1189. Ps: v.Ps,
  1190. Items: results,
  1191. Total: count,
  1192. }
  1193. c.JSON(data, nil)
  1194. }
  1195. func databusApplyApprovalProcess(c *bm.Context) {
  1196. res := map[string]interface{}{}
  1197. v := new(struct {
  1198. ID int `form:"id" validate:"required"`
  1199. State int8 `form:"state" validate:"required"`
  1200. })
  1201. var err error
  1202. if err = c.Bind(v); err != nil {
  1203. return
  1204. }
  1205. apply := &databus.Apply{}
  1206. if err = apmSvc.DBDatabus.Where("id = ?", v.ID).First(apply).Error; err != nil {
  1207. log.Error("databusApplyApprovalProcess id error(%v)", err)
  1208. res["message"] = "id有误"
  1209. c.JSONMap(res, err)
  1210. return
  1211. }
  1212. if !(apply.State == 1 || apply.State == 2) {
  1213. log.Error("databusApplyApprovalProcess apply.state error(%v)", apply.State)
  1214. res["message"] = "只有申请中和打回才可审核"
  1215. c.JSONMap(res, ecode.RequestErr)
  1216. return
  1217. }
  1218. if !(v.State == 2 || v.State == 3 || v.State == 4) {
  1219. log.Error("databusApplyApprovalProcess v.state error(%v)", v.State)
  1220. res["message"] = "state值范围为2,3,4"
  1221. c.JSONMap(res, ecode.RequestErr)
  1222. return
  1223. }
  1224. ups := map[string]interface{}{
  1225. "state": v.State,
  1226. }
  1227. tx := apmSvc.DBDatabus.Begin()
  1228. if v.State == 3 && apply.State != 3 {
  1229. if apply.Cluster == "" {
  1230. log.Error("databusApplyApprovalProcess apply.cluster err")
  1231. res["message"] = "新申请的Topic需填写对应Cluster才可审核"
  1232. c.JSONMap(res, ecode.RequestErr)
  1233. tx.Rollback()
  1234. return
  1235. }
  1236. app := &databus.App{}
  1237. if err = tx.Where("id = ?", apply.AppID).First(app).Error; err != nil {
  1238. log.Error("databusApplyApprovalProcess app first error(%v)", err)
  1239. res["message"] = "申请数据状态有误"
  1240. c.JSONMap(res, err)
  1241. tx.Rollback()
  1242. return
  1243. }
  1244. topic := &databus.Topic{}
  1245. //可能要创建topic,创建group,更新apply
  1246. if err = tx.Where("topic=? and cluster=?", apply.TopicName, apply.Cluster).First(topic).Error; err == gorm.ErrRecordNotFound {
  1247. //不存在就要创建
  1248. topic = &databus.Topic{
  1249. Topic: apply.TopicName,
  1250. Cluster: apply.Cluster,
  1251. Remark: apply.TopicRemark,
  1252. }
  1253. db := tx.Create(topic)
  1254. if err = db.Error; err != nil {
  1255. log.Error("apmSvc.databusApplyApprovalProcess create topic error(%v)", err)
  1256. res["message"] = "创建topic失败"
  1257. c.JSONMap(res, err)
  1258. tx.Rollback()
  1259. return
  1260. }
  1261. username := name(c)
  1262. //create topic on kafka cluster
  1263. apmSvc.SendLog(*c, username, 0, 1, int64(topic.ID), "apmSvc.databusTopicAdd", topic)
  1264. if conf.Conf.Kafka[topic.Cluster] == nil || !(len(conf.Conf.Kafka[topic.Cluster].Brokers) > 0) {
  1265. log.Error("apmSvc.topicAdd CreateTopic kafka cluster error(%v)", topic.Cluster)
  1266. res["message"] = "kafka集群(" + topic.Cluster + ")未配置,请手动创建"
  1267. c.JSONMap(res, err)
  1268. tx.Rollback()
  1269. return
  1270. }
  1271. if err = service.CreateTopic(conf.Conf.Kafka[topic.Cluster].Brokers, topic.Topic, conf.Conf.DatabusConfig.Partitions, conf.Conf.DatabusConfig.Factor); err != nil {
  1272. log.Error("apmSvc.topicAdd CreateTopic kafka error(%v)", err)
  1273. res["message"] = "kafka创建topic失败,请手动创建"
  1274. c.JSONMap(res, err)
  1275. tx.Rollback()
  1276. return
  1277. }
  1278. }
  1279. number := 0
  1280. if apply.Operation == 1 {
  1281. number = 100
  1282. }
  1283. //创建group
  1284. group := &databus.Group{
  1285. Group: apply.Group,
  1286. TopicID: topic.ID,
  1287. AppID: apply.AppID,
  1288. Operation: apply.Operation,
  1289. Remark: apply.Remark,
  1290. Number: number,
  1291. Percentage: "80",
  1292. }
  1293. db := tx.Create(group)
  1294. if err = db.Error; err != nil {
  1295. log.Error("apmSvc.databusApplyApprovalProcess create group error(%v)", err)
  1296. res["message"] = "创建group失败"
  1297. c.JSONMap(res, err)
  1298. tx.Rollback()
  1299. return
  1300. }
  1301. ups["group"] = group.Group
  1302. if strings.HasSuffix(group.Group, "-N") {
  1303. notify := &databus.Notify{}
  1304. nus := map[string]interface{}{
  1305. "gid": group.ID,
  1306. "state": 1,
  1307. }
  1308. if err = tx.Model(notify).Where("gid = ? ", apply.ID).Updates(nus).Error; err != nil {
  1309. log.Error("databusApplyApprovalProcess updates error(%v)", err)
  1310. res["message"] = "修改状态失败"
  1311. c.JSONMap(res, err)
  1312. tx.Rollback()
  1313. return
  1314. }
  1315. }
  1316. }
  1317. //更新apply
  1318. if err = tx.Model(apply).Where("id = ?", apply.ID).Updates(ups).Error; err != nil {
  1319. log.Error("databusApplyApprovalProcess updates error(%v)", err)
  1320. res["message"] = "修改状态失败"
  1321. c.JSONMap(res, err)
  1322. tx.Rollback()
  1323. return
  1324. }
  1325. tx.Commit()
  1326. c.JSON(nil, err)
  1327. }
  1328. // GroupName get group name
  1329. func GroupName(TopicName, Project, Rename string, operation int8) (groupName, message string, err error) {
  1330. auth := &databus.Group{}
  1331. if len(Rename) != 0 {
  1332. groupName = renameGroup(TopicName, Project, Rename, operation)
  1333. } else {
  1334. groupName = genGroup(TopicName, Project, operation)
  1335. }
  1336. if err = apmSvc.DBDatabus.Where("`group` = ?", groupName).First(auth).Error; err == nil {
  1337. if err = apmSvc.DBDatabus.Where("`group` = ?", groupName).First(auth).Error; err == nil {
  1338. log.Error("GroupName group2 error(%v)", err)
  1339. err = ecode.SvenRepeat
  1340. message = "该group(" + groupName + ")可能己存在,请重命名group或查看group列表"
  1341. return
  1342. }
  1343. }
  1344. apply := &databus.Apply{}
  1345. if err = apmSvc.DBDatabus.Where("`group` = ?", groupName).First(apply).Error; err == nil {
  1346. if apply.State == 4 {
  1347. ups := map[string]interface{}{
  1348. "state": 1,
  1349. }
  1350. if err = apmSvc.DBDatabus.Model(&databus.Apply{}).Where("id = ?", apply.ID).Updates(ups).Error; err != nil {
  1351. log.Error("GroupName update error(%v)", err)
  1352. message = "状态变更失败"
  1353. }
  1354. return
  1355. }
  1356. log.Error("GroupName group3 error(%v)", err)
  1357. err = ecode.SvenRepeat
  1358. message = "该group(" + groupName + ")己在申请中,请重命名group或查看group申请列表"
  1359. return
  1360. }
  1361. err = nil
  1362. return
  1363. }
  1364. func databusNotifyApplyAdd(c *bm.Context) {
  1365. res := map[string]interface{}{}
  1366. v := new(struct {
  1367. Project string `form:"project" validate:"required"`
  1368. Remark string `form:"remark" validate:"required"`
  1369. TopicName string `form:"topic" validate:"required"`
  1370. TopicRemark string `form:"topic_remark"`
  1371. Filter int8 `form:"filter"`
  1372. Concurrent int8 `form:"concurrent" validate:"required"`
  1373. Callback string `form:"callback" validate:"required"`
  1374. Filters string `form:"filters"`
  1375. Rename string `form:"rename"`
  1376. Zone string `form:"notify_zone"`
  1377. })
  1378. var err error
  1379. if err = c.Bind(v); err != nil {
  1380. return
  1381. }
  1382. if len(v.Zone) == 0 {
  1383. v.Zone = "sh001"
  1384. }
  1385. topic := &databus.Topic{}
  1386. app := &databus.App{}
  1387. if err = apmSvc.DBDatabus.Where("project = ? ", v.Project).First(app).Error; err != nil {
  1388. log.Error("databusNotifyApplyAdd project error(%v)", err)
  1389. res["message"] = "project不存在"
  1390. c.JSONMap(res, err)
  1391. return
  1392. }
  1393. f := strings.HasSuffix(v.TopicName, "-T")
  1394. if !f {
  1395. log.Error("databusNotifyApplyAdd topic_name not standard error(%v)", err)
  1396. res["message"] = "topic不合法"
  1397. c.JSONMap(res, ecode.RequestErr)
  1398. return
  1399. }
  1400. if err = apmSvc.DBDatabus.Where("topic = ?", v.TopicName).First(topic).Error; err != nil {
  1401. log.Error("databusNotifyApplyAdd topic error(%v)", err)
  1402. res["message"] = "不存在此topic"
  1403. c.JSONMap(res, err)
  1404. return
  1405. }
  1406. var groupName string
  1407. var message string
  1408. groupName, message, err = GroupName(v.TopicName, v.Project, v.Rename, 4)
  1409. if err != nil {
  1410. log.Error("databusNotifyApplyAdd groupname error(%v)", err)
  1411. res["message"] = message
  1412. c.JSONMap(res, err)
  1413. return
  1414. }
  1415. username := name(c)
  1416. m := &databus.Apply{
  1417. Group: groupName,
  1418. Cluster: topic.Cluster,
  1419. TopicRemark: v.TopicRemark,
  1420. TopicName: v.TopicName,
  1421. AppID: app.ID,
  1422. Operation: 4, // NOTE: sub
  1423. State: 1,
  1424. Operator: username,
  1425. Remark: v.Remark,
  1426. Project: v.Project,
  1427. }
  1428. tx := apmSvc.DBDatabus.Begin()
  1429. db := tx.Create(m)
  1430. if err = db.Error; err != nil {
  1431. tx.Rollback()
  1432. log.Error("apmSvc.databusNotifyApplyAdd create group error(%v)", err)
  1433. res["message"] = "创建group申请表失败"
  1434. c.JSONMap(res, err)
  1435. return
  1436. }
  1437. n := &databus.Notify{
  1438. Gid: m.ID,
  1439. State: 0,
  1440. Filter: v.Filter,
  1441. Concurrent: v.Concurrent,
  1442. Callback: v.Callback,
  1443. Zone: v.Zone,
  1444. }
  1445. db = tx.Create(n)
  1446. if err = db.Error; err != nil {
  1447. tx.Rollback()
  1448. log.Error("apmSvc.databusNotifyApplyAdd create notify error(%v)", err)
  1449. res["message"] = "notify表创建失败"
  1450. c.JSONMap(res, err)
  1451. return
  1452. }
  1453. filter := &databus.Filter{
  1454. Nid: n.ID,
  1455. Filters: v.Filters,
  1456. }
  1457. db = tx.Create(filter)
  1458. if err = db.Error; err != nil {
  1459. tx.Rollback()
  1460. log.Error("apmSvc.databusNotifyApplyAdd create filter error(%v)", err)
  1461. res["message"] = "filter表创建失败"
  1462. c.JSONMap(res, err)
  1463. return
  1464. }
  1465. tx.Commit()
  1466. c.JSON(nil, err)
  1467. }
  1468. func databusNotifyEdit(c *bm.Context) {
  1469. res := map[string]interface{}{}
  1470. v := new(struct {
  1471. ID int `form:"id" validate:"required"`
  1472. NID int `form:"n_id" validate:"required"`
  1473. // Cluster string `form:"cluster" validate:"required"`
  1474. TopicName string `form:"topic" validate:"required"`
  1475. Project string `form:"project" validate:"required"`
  1476. Rename string `form:"rename"`
  1477. Remark string `form:"remark"`
  1478. State int8 `form:"state"`
  1479. Filter int8 `form:"filter"`
  1480. Concurrent int8 `form:"concurrent"`
  1481. Callback string `form:"callback" validate:"required"`
  1482. Filters string `form:"filters"`
  1483. Status int8 `form:"status"` //1审核表,2auth2表
  1484. Zone string `form:"notify_zone" validate:"required"`
  1485. })
  1486. var err error
  1487. if err = c.Bind(v); err != nil {
  1488. return
  1489. }
  1490. app := &databus.App{}
  1491. if err = apmSvc.DBDatabus.Where("project = ? ", v.Project).First(app).Error; err != nil {
  1492. log.Error("databusNotifyEdit project error(%v)", err)
  1493. res["message"] = "project不存在"
  1494. c.JSONMap(res, err)
  1495. return
  1496. }
  1497. ups := map[string]interface{}{
  1498. "remark": v.Remark,
  1499. }
  1500. if len(v.Rename) != 0 {
  1501. var groupName, message string
  1502. groupName, message, err = GroupName(v.TopicName, v.Project, v.Rename, 4)
  1503. if err != nil {
  1504. log.Error("databusNotifyEdit groupname error(%v)", err)
  1505. res["message"] = message
  1506. c.JSONMap(res, err)
  1507. return
  1508. }
  1509. ups["group"] = groupName
  1510. }
  1511. if v.Status == 1 {
  1512. ups["topic_name"] = v.TopicName
  1513. ups["app_id"] = app.ID
  1514. ups["project"] = v.Project
  1515. apply := &databus.Apply{}
  1516. if err = apmSvc.DBDatabus.Model(apply).Where("id = ?", v.ID).Updates(ups).Error; err != nil {
  1517. log.Error("databusNotifyEdit appply update error(%v)", err)
  1518. res["message"] = "修改失败"
  1519. c.JSONMap(res, err)
  1520. return
  1521. }
  1522. } else {
  1523. group := &databus.Group{}
  1524. if err = apmSvc.DBDatabus.Model(group).Where("id = ?", v.ID).Updates(ups).Error; err != nil {
  1525. log.Error("databusNotifyEdit auth2 update error(%v)", err)
  1526. res["message"] = "修改失败"
  1527. c.JSONMap(res, err)
  1528. return
  1529. }
  1530. }
  1531. notify := &databus.Notify{}
  1532. if err = apmSvc.DBDatabus.Where("id = ?", v.NID).First(&notify).Error; err != nil {
  1533. log.Error("databusNotifyEdit notify first error(%v)", err)
  1534. c.JSON(nil, err)
  1535. return
  1536. }
  1537. nups := map[string]interface{}{
  1538. "filter": v.Filter,
  1539. "concurrent": v.Concurrent,
  1540. "callback": v.Callback,
  1541. "state": v.State,
  1542. "zone": v.Zone,
  1543. }
  1544. tx := apmSvc.DBDatabus.Begin()
  1545. if err = tx.Model(notify).Where("id = ?", v.NID).Updates(nups).Error; err != nil {
  1546. log.Error("databusNotifyEdit updates error(%v)", err)
  1547. c.JSON(nil, err)
  1548. tx.Rollback()
  1549. return
  1550. }
  1551. filter := &databus.Filter{}
  1552. fups := map[string]interface{}{
  1553. "filters": v.Filters,
  1554. }
  1555. if err = tx.Where("nid = ?", v.NID).First(&filter).Error; err != nil {
  1556. filter.Nid = v.NID
  1557. filter.Filters = v.Filters
  1558. tx.Create(filter)
  1559. } else {
  1560. if err = tx.Model(filter).Where("nid = ?", v.NID).Updates(fups).Error; err != nil {
  1561. c.JSON(nil, err)
  1562. tx.Rollback()
  1563. return
  1564. }
  1565. }
  1566. tx.Commit()
  1567. c.JSON(nil, err)
  1568. }
  1569. func databusNotifyList(c *bm.Context) {
  1570. v := new(struct {
  1571. Topic string `form:"topic"`
  1572. Group string `form:"group" default:""`
  1573. Pn int `form:"pn" default:"1" validate:"min=1"`
  1574. Ps int `form:"ps" default:"20" validate:"min=1"`
  1575. })
  1576. var err error
  1577. if err = c.Bind(v); err != nil {
  1578. return
  1579. }
  1580. username := name(c)
  1581. projects, err := apmSvc.Projects(c, username, c.Request.Header.Get("Cookie"))
  1582. if err != nil {
  1583. c.JSON(nil, err)
  1584. return
  1585. }
  1586. var (
  1587. apps []*databus.App
  1588. topics []*databus.Topic
  1589. groups []*databus.Group
  1590. total int
  1591. )
  1592. if err = apmSvc.DBDatabus.Model(&databus.App{}).Where("project in (?)", projects).Find(&apps).Error; err != nil {
  1593. c.JSON(nil, err)
  1594. return
  1595. }
  1596. var appIDs []int
  1597. for _, val := range apps {
  1598. appIDs = append(appIDs, val.ID)
  1599. }
  1600. if v.Topic != "" {
  1601. err = apmSvc.DBDatabus.Where("topic = ?", v.Topic).Find(&topics).Error
  1602. } else {
  1603. err = apmSvc.DBDatabus.Find(&topics).Error
  1604. }
  1605. if err != nil {
  1606. log.Error("apmSvc.groups error(%v)", err)
  1607. c.JSON(nil, err)
  1608. return
  1609. }
  1610. var topicIDS []int
  1611. for _, value := range topics {
  1612. topicIDS = append(topicIDS, value.ID)
  1613. }
  1614. if err = apmSvc.DBDatabus.Raw(`SELECT auth2.id,auth2.group,auth2.app_id,app2.app_key,app2.project,auth2.topic_id,topic.cluster,topic.topic,auth2.remark,auth2.ctime,
  1615. auth2.mtime,auth2.percentage,auth2.alarm, if(auth2.number=0,100,auth2.number) as number,
  1616. notify.id as nid, notify.gid,notify.callback,notify.concurrent,notify.filter,notify.state,filters.filters,notify.zone
  1617. FROM auth2 LEFT JOIN app2 ON app2.id=auth2.app_id LEFT JOIN topic ON topic.id=auth2.topic_id
  1618. INNER JOIN notify ON notify.gid=auth2.id
  1619. LEFT JOIN filters ON filters.nid = notify.id
  1620. WHERE auth2.is_delete=0 AND auth2.topic_id in (?) and auth2.app_id in (?) and auth2.group LIKE '%`+v.Group+`%' ORDER BY auth2.id LIMIT ?,?`,
  1621. topicIDS, appIDs, (v.Pn-1)*v.Ps, v.Ps).Scan(&groups).Error; err != nil {
  1622. log.Error("apmSvc.Groups error(%v)", err)
  1623. c.JSON(nil, err)
  1624. return
  1625. }
  1626. if err = apmSvc.DBDatabus.Model(&databus.Group{}).Joins(`LEFT JOIN app2 ON app2.id=auth2.app_id LEFT JOIN topic ON topic.id=auth2.topic_id
  1627. INNER JOIN notify ON notify.gid=auth2.id LEFT JOIN filters ON filters.nid = notify.id`).Where("auth2.is_delete=0 AND auth2.topic_id in (?) AND auth2.app_id in (?) and auth2.group LIKE '%"+v.Group+"%'", topicIDS, appIDs).Count(&total).Error; err != nil {
  1628. log.Error("apmSvc.Groups count error(%v)", err)
  1629. c.JSON(nil, err)
  1630. return
  1631. }
  1632. for _, g := range groups {
  1633. if g.Filters != "" {
  1634. json.Unmarshal([]byte(g.Filters), &g.FilterList)
  1635. }
  1636. }
  1637. data := &Paper{
  1638. Pn: v.Pn,
  1639. Ps: v.Ps,
  1640. Items: groups,
  1641. Total: total,
  1642. }
  1643. c.JSON(data, nil)
  1644. }
  1645. // func databusNotifyFilterAdd(c *bm.Context) {
  1646. //
  1647. // username, err := permit(c, user.DatabusNotifyEdit)
  1648. // if err != nil {
  1649. // log.Error("permit(%v, %s)", username, user.DatabusNotifyEdit)
  1650. // c.JSON(nil, err)
  1651. // return
  1652. // }
  1653. // type filt struct {
  1654. // Field string `json:"field"`
  1655. // Condition int8 `json:"conidtion"`
  1656. // Value string `json:"value"`
  1657. // }
  1658. // v := new(struct {
  1659. // ID int `form:"n_id" validate:"required"` //notify id
  1660. // Filters string `form:"filters" validate:"required"`
  1661. // })
  1662. // if err = c.Bind(v); err != nil {
  1663. // c.JSON(nil, ecode.RequestErr)
  1664. // res["message"] = "参数有误"
  1665. // return
  1666. // }
  1667. // filters := []filt{}
  1668. // if err = json.Unmarshal([]byte(v.Filters), &filters); err != nil {
  1669. // c.JSON(nil, ecode.RequestErr)
  1670. // res["message"] = "filters有误"
  1671. // return
  1672. // }
  1673. // notify := &databus.Notify{}
  1674. // if err = apmSvc.DBDatabus.Where("id = ?", v.ID).First(notify).Error; err != nil {
  1675. // c.JSON(nil, ecode.RequestErr)
  1676. // res["message"] = "未找到该notify"
  1677. // return
  1678. // }
  1679. // tx := apmSvc.DBDatabus.Begin()
  1680. // var db *gorm.DB
  1681. // var filter *databus.Filter
  1682. // for _, val := range filters {
  1683. // filter.Nid = v.ID
  1684. // filter.Field = val.Field
  1685. // filter.Value = val.Value
  1686. // filter.Condition = val.Condition
  1687. // db = tx.Create(filter)
  1688. // if err = db.Error; err != nil {
  1689. // tx.Rollback()
  1690. // log.Error("apmSvc.databusNotifyFilterAdd add filters error(%v)", err)
  1691. // c.JSON(nil, err)
  1692. // res["message"] = "filter表添加失败"
  1693. // return
  1694. // }
  1695. // }
  1696. // tx.Commit()
  1697. // }
  1698. // func databusNotifyFilterEdit(c *bm.Context) {
  1699. //
  1700. // username, err := permit(c, user.DatabusNotifyEdit)
  1701. // if err != nil {
  1702. // log.Error("permit(%v, %s)", username, user.DatabusNotifyEdit)
  1703. // c.JSON(nil, err)
  1704. // return
  1705. // }
  1706. // v := new(struct {
  1707. // ID int `form:"f_id" validate:"required"`
  1708. // Field string `form:"field" validate:"required"`
  1709. // Condition int8 `form:"conidtion" validate:"required"`
  1710. // Value string `form:"value" validate:"required"`
  1711. // })
  1712. // if err = c.Bind(v); err != nil {
  1713. // c.JSON(nil, ecode.RequestErr)
  1714. // res["message"] = "参数有误"
  1715. // return
  1716. // }
  1717. // filter := &databus.Filter{}
  1718. // if err = apmSvc.DBDatabus.Where("id = ?", v.ID).First(&filter).Error; err != nil {
  1719. // log.Error("apmSvc.databusNotifyFilterEdit filter first error(%v)", err)
  1720. // c.JSON(nil, err)
  1721. // return
  1722. // }
  1723. // ups := map[string]interface{}{
  1724. // "field": v.Field,
  1725. // "conidtion": v.Condition,
  1726. // "value": v.Value,
  1727. // }
  1728. // if err = apmSvc.DBDatabus.Model(filter).Where("id = ?", v.ID).Updates(ups).Error; err != nil {
  1729. // log.Error("databusNotifyFilterEdit updates error(%v)", err)
  1730. // c.JSON(nil, err)
  1731. // }
  1732. // }
  1733. func databusTopicAll(c *bm.Context) {
  1734. var tps []*databus.Topic
  1735. var err error
  1736. if err = apmSvc.DBDatabus.Find(&tps).Error; err != nil {
  1737. log.Error("apmSvr.Topics error(%v)", err)
  1738. c.JSON(nil, err)
  1739. return
  1740. }
  1741. var ns []string
  1742. for _, t := range tps {
  1743. ns = append(ns, t.Topic)
  1744. }
  1745. c.JSON(ns, nil)
  1746. }
  1747. func databusGroupNewOffset(c *bm.Context) {
  1748. v := new(struct {
  1749. Group string `form:"group" validate:"required"`
  1750. Partition int32 `form:"partition" validate:"required"`
  1751. Offset int64 `form:"offset" validate:"required"`
  1752. })
  1753. var err error
  1754. if err = c.Bind(v); err != nil {
  1755. return
  1756. }
  1757. group := &databus.Group{}
  1758. if err = apmSvc.DBDatabus.Where("`group` = ?", v.Group).Find(group).Error; err != nil {
  1759. log.Error("databusGroupNewOffset select group error(%v)", err)
  1760. c.JSON(nil, err)
  1761. return
  1762. }
  1763. topic := &databus.Topic{}
  1764. if err = apmSvc.DBDatabus.Where("id = ?", group.TopicID).Find(topic).Error; err != nil {
  1765. log.Error("databusGroupNewOffset select topic error(%v)", err)
  1766. c.JSON(nil, err)
  1767. return
  1768. }
  1769. client, err := service.NewClient(conf.Conf.Kafka[topic.Cluster].Brokers, topic.Topic, group.Group)
  1770. if err != nil {
  1771. log.Error("service.NewClient() error(%v)\n", err)
  1772. c.JSON(nil, err)
  1773. return
  1774. }
  1775. defer client.Close()
  1776. err = client.NewOffset(v.Partition, v.Offset)
  1777. sqlLog := &map[string]interface{}{
  1778. "SQLType": "kafka",
  1779. "Cluster": conf.Conf.Kafka[topic.Cluster].Brokers,
  1780. "Topic": topic,
  1781. "Group": group,
  1782. "action": "NewOffset",
  1783. }
  1784. username := name(c)
  1785. apmSvc.SendLog(*c, username, 0, 6, 0, "apmSvc.databusGroupNewOffset", sqlLog)
  1786. if err != nil {
  1787. log.Error("client.NewOffset() error(%v)\n", err)
  1788. c.JSON(nil, err)
  1789. return
  1790. }
  1791. c.JSON(nil, err)
  1792. }
  1793. func databusGroupTime(c *bm.Context) {
  1794. v := new(struct {
  1795. Group string `form:"group" validate:"required"`
  1796. Time int64 `form:"time" validate:"required"`
  1797. })
  1798. var err error
  1799. if err = c.Bind(v); err != nil {
  1800. return
  1801. }
  1802. group := &databus.Group{}
  1803. if err = apmSvc.DBDatabus.Where("`group` = ?", v.Group).Find(group).Error; err != nil {
  1804. log.Error("databusGroupTime select group error(%v)", err)
  1805. c.JSON(nil, err)
  1806. return
  1807. }
  1808. topic := &databus.Topic{}
  1809. if err = apmSvc.DBDatabus.Where("id = ?", group.TopicID).Find(topic).Error; err != nil {
  1810. log.Error("databusGroupTime select topic error(%v)", err)
  1811. c.JSON(nil, err)
  1812. return
  1813. }
  1814. client, err := service.NewClient(conf.Conf.Kafka[topic.Cluster].Brokers, topic.Topic, group.Group)
  1815. if err != nil {
  1816. log.Error("service.NewClient() error(%v)\n", err)
  1817. c.JSON(nil, err)
  1818. return
  1819. }
  1820. defer client.Close()
  1821. err = client.NewTime(v.Time)
  1822. sqlLog := &map[string]interface{}{
  1823. "SQLType": "kafka",
  1824. "Cluster": conf.Conf.Kafka[topic.Cluster].Brokers,
  1825. "Topic": topic,
  1826. "Group": group,
  1827. "action": "NewTime",
  1828. }
  1829. username := name(c)
  1830. apmSvc.SendLog(*c, username, 0, 6, 0, "apmSvc.databusGroupTime", sqlLog)
  1831. if err != nil {
  1832. log.Error("client.NewTime() error(%v)\n", err)
  1833. c.JSON(nil, err)
  1834. return
  1835. }
  1836. c.JSON(nil, err)
  1837. }
  1838. func databusOpsmind(c *bm.Context) {
  1839. v := new(struct {
  1840. Group string `form:"group" validate:"required"`
  1841. Percentage int64 `form:"percentage" default:"0"`
  1842. Owner string `form:"owner"`
  1843. ForTime int64 `form:"for_time" default:"300"`
  1844. Silence bool `form:"silence" default:"false"`
  1845. AdjustID string `form:"adjust_id"`
  1846. })
  1847. var err error
  1848. if err = c.Bind(v); err != nil {
  1849. return
  1850. }
  1851. group := &databus.Group{}
  1852. if err = apmSvc.DBDatabus.Where("`group`=?", v.Group).First(group).Error; err != nil {
  1853. c.JSON(nil, ecode.RequestErr)
  1854. return
  1855. }
  1856. app := &databus.App{}
  1857. if err = apmSvc.DBDatabus.Where("`id`=?", group.AppID).First(app).Error; err != nil {
  1858. c.JSON(nil, ecode.RequestErr)
  1859. return
  1860. }
  1861. res, err := apmSvc.Opsmind(c, app.Project, v.Group, "OpsMindAdjustCreate", v.Owner, v.Percentage, v.ForTime, v.Silence)
  1862. if res.RetCode == 290 {
  1863. res, err = apmSvc.OpsmindRemove(c, res.Data.AdjustID, "OpsMindPolicyAdjustRemove")
  1864. if err != nil || res.RetCode != 0 {
  1865. c.JSON(nil, ecode.RequestErr)
  1866. return
  1867. }
  1868. res, err = apmSvc.Opsmind(c, app.Project, v.Group, "OpsMindAdjustCreate", v.Owner, v.Percentage, v.ForTime, v.Silence)
  1869. }
  1870. if res.RetCode != 0 {
  1871. err = ecode.RequestErr
  1872. c.JSON(nil, err)
  1873. return
  1874. }
  1875. if len(v.AdjustID) > 0 {
  1876. apmSvc.OpsmindRemove(c, v.AdjustID, "OpsMindPolicyAdjustRemove")
  1877. }
  1878. c.JSON(res, err)
  1879. }
  1880. func databusQuery(c *bm.Context) {
  1881. v := new(struct {
  1882. Group string `form:"group" validate:"required"`
  1883. })
  1884. var err error
  1885. if err = c.Bind(v); err != nil {
  1886. return
  1887. }
  1888. res, err := apmSvc.OpsmindQuery(c, v.Group, "OpsMindAdjustQuery")
  1889. if res.RetCode != 0 {
  1890. err = ecode.RequestErr
  1891. c.JSON(nil, err)
  1892. return
  1893. }
  1894. c.JSON(res, err)
  1895. }
  1896. func databusOpsmindRemove(c *bm.Context) {
  1897. v := new(struct {
  1898. AdjustID string `form:"adjust_id" validate:"required"`
  1899. })
  1900. var err error
  1901. if err = c.Bind(v); err != nil {
  1902. return
  1903. }
  1904. res, err := apmSvc.OpsmindRemove(c, v.AdjustID, "OpsMindPolicyAdjustRemove")
  1905. if err != nil || res.RetCode != 0 {
  1906. c.JSON(nil, ecode.RequestErr)
  1907. return
  1908. }
  1909. c.JSON(nil, err)
  1910. }
  1911. func databusMsgFetch(c *bm.Context) {
  1912. v := new(struct {
  1913. Cluster string `form:"cluster" validate:"required"`
  1914. Topic string `form:"topic" validate:"required"`
  1915. Key string `form:"key"`
  1916. Start int64 `form:"start"`
  1917. End int64 `form:"end"`
  1918. Limit int `form:"limit"`
  1919. })
  1920. if err := c.Bind(v); err != nil {
  1921. return
  1922. }
  1923. if v.Limit <= 0 {
  1924. v.Limit = 20
  1925. } else if v.Limit > 100 {
  1926. v.Limit = 100
  1927. }
  1928. // {Topic}APM-MainCommonArch-S
  1929. group := fmt.Sprintf("%sAPM-MainCommonArch-S", strings.Replace(v.Topic, "-T", "", -1))
  1930. if v.Start > 0 {
  1931. kc, ok := conf.Conf.Kafka[v.Cluster]
  1932. if !ok {
  1933. c.JSON(nil, ecode.NothingFound)
  1934. return
  1935. }
  1936. client, err := service.NewClient(kc.Brokers, v.Topic, group)
  1937. if err != nil {
  1938. log.Error("service.NewClient() error(%v)\n", err)
  1939. c.JSON(nil, err)
  1940. return
  1941. }
  1942. if err = client.NewTime(v.Start); err != nil {
  1943. client.Close()
  1944. log.Error("client.NewTime(%d) error(%v)\n", v.Start, err)
  1945. c.JSON(nil, err)
  1946. return
  1947. }
  1948. client.Close()
  1949. time.Sleep(time.Millisecond * 200)
  1950. }
  1951. res, err := service.FetchMessage(c, v.Cluster, v.Topic, group, v.Key, v.Start, v.End, v.Limit)
  1952. if err != nil {
  1953. c.JSON(nil, ecode.RequestErr)
  1954. return
  1955. }
  1956. c.JSON(res, err)
  1957. }
  1958. func databusAlarmInit(c *bm.Context) {
  1959. v := new(struct {
  1960. Name string `form:"name"`
  1961. MS int `form:"ms"`
  1962. })
  1963. var err error
  1964. if err = c.Bind(v); err != nil {
  1965. return
  1966. }
  1967. group := []*databus.Group{}
  1968. if len(v.Name) > 0 {
  1969. err = apmSvc.DBDatabus.Where("`group` LIKE ?", v.Name+"%").Find(&group).Error
  1970. } else {
  1971. err = apmSvc.DBDatabus.Find(&group).Error
  1972. }
  1973. if err != nil {
  1974. log.Error("databusAlarmInit group error(%v)", err)
  1975. c.JSON(nil, err)
  1976. return
  1977. }
  1978. app := []*databus.App{}
  1979. if err = apmSvc.DBDatabus.Find(&app).Error; err != nil {
  1980. log.Error("databusAlarmInit app error(%v)", err)
  1981. c.JSON(nil, err)
  1982. return
  1983. }
  1984. project := make(map[int]string)
  1985. for _, val := range app {
  1986. project[val.ID] = val.Project
  1987. }
  1988. data := make(map[string]int64)
  1989. go func() {
  1990. for _, val := range group {
  1991. res, _ := apmSvc.Opsmind(context.Background(), project[val.AppID], val.Group, "OpsMindAdjustCreate", "", 80, 300, false)
  1992. data[val.Group] = res.RetCode
  1993. if v.MS > 0 {
  1994. time.Sleep(time.Millisecond * time.Duration(v.MS))
  1995. }
  1996. }
  1997. log.Info("databusAlarmInit data info(%v)", data)
  1998. }()
  1999. c.JSON(nil, err)
  2000. }
  2001. func databusAlarmAllEdit(c *bm.Context) {
  2002. v := new(struct {
  2003. Alarm int8 `form:"alarm" validate:"required"`
  2004. Percentage string `form:"percentage" validate:"required"`
  2005. })
  2006. var err error
  2007. if err = c.Bind(v); err != nil {
  2008. return
  2009. }
  2010. ups := map[string]interface{}{
  2011. "percentage": v.Percentage,
  2012. }
  2013. if err = apmSvc.DBDatabus.Model(&databus.Group{}).Where("alarm = ?", v.Alarm).Updates(ups).Error; err != nil {
  2014. log.Error("apmSvc.databusAlarmAllEdit error(%v)", err)
  2015. c.JSON(nil, err)
  2016. return
  2017. }
  2018. sqlLog := &map[string]interface{}{
  2019. "SQLType": "update",
  2020. "Where": "alarm = ?",
  2021. "Value1": v.Alarm,
  2022. "Update": ups,
  2023. }
  2024. username := name(c)
  2025. apmSvc.SendLog(*c, username, 0, 2, 0, "apmSvc.databusAlarmAllEdit", sqlLog)
  2026. c.JSON(nil, err)
  2027. }
  2028. func databusConsumerAddrs(c *bm.Context) {
  2029. v := new(struct {
  2030. Group string `form:"group" validate:"required"`
  2031. })
  2032. var err error
  2033. if err = c.Bind(v); err != nil {
  2034. return
  2035. }
  2036. c.JSON(apmSvc.DatabusConsumerAddrs(c, v.Group))
  2037. }