mng.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strconv"
  6. "go-common/app/admin/main/search/model"
  7. "go-common/library/ecode"
  8. "go-common/library/sync/errgroup"
  9. )
  10. // BusinessList .
  11. func (s *Service) BusinessList(ctx context.Context, name string, pn, ps int) (list []*model.MngBusiness, total int64, err error) {
  12. offset := (pn - 1) * ps
  13. if list, err = s.dao.BusinessList(ctx, name, offset, ps); err != nil {
  14. return
  15. }
  16. total, err = s.dao.BusinessTotal(ctx, name)
  17. return
  18. }
  19. // BusinessAll .
  20. func (s *Service) BusinessAll(ctx context.Context) (list []*model.MngBusiness, err error) {
  21. list, err = s.dao.BusinessAll(ctx)
  22. return
  23. }
  24. // BusinessInfo .
  25. func (s *Service) BusinessInfo(ctx context.Context, id int64) (info *model.MngBusiness, err error) {
  26. info, err = s.dao.BusinessInfo(ctx, id)
  27. return
  28. }
  29. // AddBusiness .
  30. func (s *Service) AddBusiness(ctx context.Context, b *model.MngBusiness) (id int64, err error) {
  31. info, err := s.dao.BusinessInfoByName(ctx, b.Name)
  32. if err != nil {
  33. return
  34. }
  35. if info != nil {
  36. err = ecode.SearchBusinessExistErr
  37. return
  38. }
  39. id, err = s.dao.AddBusiness(ctx, b)
  40. return
  41. }
  42. // UpdateBusiness .
  43. func (s *Service) UpdateBusiness(ctx context.Context, b *model.MngBusiness) (err error) {
  44. err = s.dao.UpdateBusiness(ctx, b)
  45. return
  46. }
  47. // UpdateBusinessApp .
  48. func (s *Service) UpdateBusinessApp(ctx context.Context, business, app, incrWay string, isJob, incrOpen bool) (err error) {
  49. info, err := s.dao.BusinessInfoByName(ctx, business)
  50. if err != nil {
  51. return
  52. }
  53. var exist bool
  54. for k, v := range info.Apps {
  55. if v.AppID == app {
  56. exist = true
  57. if !isJob {
  58. info.Apps = append(info.Apps[:k], info.Apps[k+1:]...)
  59. break
  60. }
  61. v.IncrWay = incrWay
  62. v.IncrOpen = incrOpen
  63. }
  64. }
  65. if !exist {
  66. info.Apps = append(info.Apps, &model.MngBusinessApp{AppID: app, IncrWay: incrWay, IncrOpen: incrOpen})
  67. }
  68. bs, err := json.Marshal(info.Apps)
  69. if err != nil {
  70. return
  71. }
  72. info.AppsJSON = string(bs)
  73. err = s.dao.UpdateBusiness(ctx, info)
  74. return
  75. }
  76. // AssetList .
  77. func (s *Service) AssetList(ctx context.Context, typ int, name string, pn, ps int) (list []*model.MngAsset, total int64, err error) {
  78. offset := (pn - 1) * ps
  79. if list, err = s.dao.AssetList(ctx, typ, name, offset, ps); err != nil {
  80. return
  81. }
  82. total, err = s.dao.AssetTotal(ctx, typ, name)
  83. return
  84. }
  85. // AssetAll .
  86. func (s *Service) AssetAll(ctx context.Context) (list []*model.MngAsset, err error) {
  87. list, err = s.dao.AssetAll(ctx)
  88. return
  89. }
  90. // AssetInfo .
  91. func (s *Service) AssetInfo(ctx context.Context, id int64) (info *model.MngAsset, err error) {
  92. info, err = s.dao.AssetInfo(ctx, id)
  93. return
  94. }
  95. // AddAsset .
  96. func (s *Service) AddAsset(ctx context.Context, a *model.MngAsset) (id int64, err error) {
  97. info, err := s.dao.AssetInfoByName(ctx, a.Name)
  98. if err != nil {
  99. return
  100. }
  101. if info != nil {
  102. err = ecode.SearchAssetExistErr
  103. return
  104. }
  105. id, err = s.dao.AddAsset(ctx, a)
  106. return
  107. }
  108. // UpdateAsset .
  109. func (s *Service) UpdateAsset(ctx context.Context, a *model.MngAsset) (err error) {
  110. if err = s.dao.UpdateAsset(ctx, a); err != nil {
  111. return
  112. }
  113. if a.Type == model.MngAssetTypeDatabus {
  114. if a.Config == "" {
  115. return
  116. }
  117. v := new(model.MngAssetDatabus)
  118. if err = json.Unmarshal([]byte(a.Config), &v); err != nil {
  119. return
  120. }
  121. err = s.dao.UpdateAppAssetDatabus(ctx, a.Name, v)
  122. return
  123. }
  124. if a.Type == model.MngAssetTypeTable {
  125. if a.Config == "" {
  126. return
  127. }
  128. v := new(model.MngAssetTable)
  129. if err = json.Unmarshal([]byte(a.Config), &v); err != nil {
  130. return
  131. }
  132. err = s.dao.UpdateAppAssetTable(ctx, a.Name, v)
  133. return
  134. }
  135. return
  136. }
  137. // AppList .
  138. func (s *Service) AppList(ctx context.Context, business string) (list []*model.MngApp, err error) {
  139. list, err = s.dao.AppList(ctx, business)
  140. return
  141. }
  142. // AppInfo .
  143. func (s *Service) AppInfo(ctx context.Context, id int64) (info *model.MngApp, err error) {
  144. info, err = s.dao.AppInfo(ctx, id)
  145. return
  146. }
  147. // AddApp .
  148. func (s *Service) AddApp(ctx context.Context, a *model.MngApp) (id int64, err error) {
  149. info, err := s.dao.AppInfoByAppid(ctx, a.AppID)
  150. if err != nil {
  151. return
  152. }
  153. if info != nil {
  154. err = ecode.SearchAssetExistErr
  155. return
  156. }
  157. id, err = s.dao.AddApp(ctx, a)
  158. return
  159. }
  160. // UpdateApp .
  161. func (s *Service) UpdateApp(ctx context.Context, a *model.MngApp) (err error) {
  162. group := errgroup.Group{}
  163. group.Go(func() error {
  164. if a.TableName == "" {
  165. a.TableFormat = ""
  166. a.TablePrefix = ""
  167. return nil
  168. }
  169. tb, e := s.dao.AssetInfoByName(ctx, a.TableName)
  170. if e != nil {
  171. return e
  172. }
  173. if tb == nil || tb.Config == "" {
  174. return nil
  175. }
  176. val := new(model.MngAssetTable)
  177. if e := json.Unmarshal([]byte(tb.Config), val); e != nil {
  178. return e
  179. }
  180. a.TablePrefix = val.TablePrefix
  181. a.TableFormat = val.TableFormat
  182. return nil
  183. })
  184. group.Go(func() error {
  185. if a.DatabusName == "" {
  186. a.DatabusInfo = ""
  187. a.DatabusIndexID = ""
  188. return nil
  189. }
  190. dbus, e := s.dao.AssetInfoByName(ctx, a.DatabusName)
  191. if e != nil {
  192. return e
  193. }
  194. if dbus == nil || dbus.Config == "" {
  195. return nil
  196. }
  197. val := new(model.MngAssetDatabus)
  198. if e := json.Unmarshal([]byte(dbus.Config), val); e != nil {
  199. return e
  200. }
  201. a.DatabusInfo = val.DatabusInfo
  202. a.DatabusIndexID = val.DatabusIndexID
  203. return nil
  204. })
  205. if err = group.Wait(); err != nil {
  206. return
  207. }
  208. err = s.dao.UpdateApp(ctx, a)
  209. return
  210. }
  211. // MngCountList .
  212. func (s *Service) MngCountList(ctx context.Context) (list []*model.MngCount, err error) {
  213. daily := "每日增量"
  214. sum := "历史总量"
  215. list = []*model.MngCount{
  216. // 业务方
  217. {Business: "业务方", Type: sum, Name: "业务方历史总量", Chart: "line", Param: "business=app&type=all"},
  218. {Business: "业务方", Type: daily, Name: "业务方每日增量", Chart: "line", Param: "business=app&type=inc"},
  219. // 视频+稿件
  220. {Business: "视频稿件", Type: daily, Name: "archive每日增量", Chart: "line", Param: "business=archive&type=inc"},
  221. {Business: "视频稿件", Type: daily, Name: "video每日增量", Chart: "line", Param: "business=archive_video&type=inc"},
  222. {Business: "视频稿件", Type: sum, Name: "archive历史总量", Chart: "line", Param: "business=archive&type=all"},
  223. {Business: "视频稿件", Type: sum, Name: "video历史总量", Chart: "line", Param: "business=archive_video&type=all"},
  224. // 弹幕
  225. {Business: "弹幕", Type: daily, Name: "弹幕每日增量", Chart: "line", Param: "business=dm&type=inc"},
  226. {Business: "弹幕", Type: daily, Name: "弹幕举报每日增量", Chart: "line", Param: "business=dm_report&type=inc"},
  227. {Business: "弹幕", Type: daily, Name: "弹幕监控每日增量", Chart: "line", Param: "business=dm_monitor&type=inc"},
  228. {Business: "弹幕", Type: sum, Name: "弹幕历史总量", Chart: "line", Param: "business=dm&type=all"},
  229. {Business: "弹幕", Type: sum, Name: "弹幕举报历史总量", Chart: "line", Param: "business=dm_report&type=all"},
  230. {Business: "弹幕", Type: sum, Name: "弹幕监控历史总量", Chart: "line", Param: "business=dm_monitor&type=all"},
  231. // 评论
  232. {Business: "评论", Type: daily, Name: "评论每日增量", Chart: "line", Param: "business=reply&type=inc"},
  233. // 日志
  234. {Business: "日志", Type: "审核日志", Name: "审核日志每日查询量", Chart: "line", Param: "business=log_audit_access&type=inc"},
  235. {Business: "日志", Type: "审核日志", Name: "审核日志昨日查询情况 - 业务维度", Chart: "pie", Param: "business=log_audit_business&type=inc"},
  236. {Business: "日志", Type: "审核日志", Name: "审核日志昨日查询情况 - 用户维度", Chart: "pie", Param: "business=log_audit_uid&type=inc"},
  237. {Business: "日志", Type: "用户日志", Name: "用户日志每日查询量", Chart: "line", Param: "business=log_user_action_access&type=inc"},
  238. {Business: "日志", Type: "用户日志", Name: "用户日志昨日查询情况 - 业务维度", Chart: "pie", Param: "business=log_user_action_business&type=inc"},
  239. {Business: "日志", Type: "用户日志", Name: "用户日志昨日查询情况 - 用户维度", Chart: "pie", Param: "business=log_user_action_uid&type=inc"},
  240. // 用户
  241. {Business: "用户", Type: sum, Name: "用户历史总量", Chart: "line", Param: "business=user&type=all"},
  242. // 专栏
  243. {Business: "专栏", Type: daily, Name: "专栏每日增量", Chart: "line", Param: "business=article&type=inc"},
  244. {Business: "专栏", Type: sum, Name: "专栏历史总量", Chart: "line", Param: "business=article&type=all"},
  245. }
  246. return list, err
  247. }
  248. // MngCount .
  249. func (s *Service) MngCount(ctx context.Context, c *model.MngCount) (list []*model.MngCountRes, err error) {
  250. list, err = s.dao.MngCount(ctx, c)
  251. return
  252. }
  253. // MngCount .
  254. func (s *Service) MngPercent(ctx context.Context, c *model.MngCount) (list []*model.MngPercentRes, err error) {
  255. list, err = s.dao.MngPercent(ctx, c)
  256. switch c.Business {
  257. case "log_audit_business":
  258. for k, v := range list {
  259. if id, e := strconv.Atoi(v.Name); e == nil {
  260. if t, ok := s.dao.GetLogInfo("log_audit", id); ok {
  261. list[k].Name = t.Name
  262. }
  263. }
  264. }
  265. case "log_user_action_business":
  266. for k, v := range list {
  267. if id, e := strconv.Atoi(v.Name); e == nil {
  268. if t, ok := s.dao.GetLogInfo("log_user_action", id); ok {
  269. list[k].Name = t.Name
  270. }
  271. }
  272. }
  273. case "log_audit_uid", "log_user_action_uid":
  274. uid := []string{}
  275. for _, v := range list {
  276. uid = append(uid, v.Name)
  277. }
  278. if data, err := s.dao.Unames(ctx, uid); err == nil {
  279. for k, v := range list {
  280. if t, ok := data.Data[v.Name]; ok {
  281. list[k].Name = t
  282. }
  283. }
  284. }
  285. }
  286. return
  287. }