members.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package service
  2. import (
  3. "context"
  4. "net/http"
  5. "go-common/app/admin/main/apm/model/monitor"
  6. "go-common/library/log"
  7. )
  8. const chatURL = "https://api.bilibili.com/x/web-interface/online"
  9. // ChatResult .
  10. type ChatResult struct {
  11. Code int `json:"code"`
  12. Data struct {
  13. WebOnline int64 `json:"web_online"`
  14. } `json:"data"`
  15. }
  16. // ChatProxy chat proxy
  17. func (s *Service) ChatProxy(c context.Context) (ret *ChatResult, err error) {
  18. var (
  19. req *http.Request
  20. )
  21. if req, err = s.client.NewRequest(http.MethodGet, chatURL, "", nil); err != nil {
  22. log.Error("s.ChatProxy.client.NewRequest err(%v)", err)
  23. return
  24. }
  25. if err = s.client.Do(c, req, &ret); err != nil {
  26. log.Error("s.ChatProxy.client.DO err(%v)", err)
  27. return
  28. }
  29. if ret.Code != 0 {
  30. log.Error("s.ChatProxy.client http_status(%d)", ret.Code)
  31. return
  32. }
  33. return
  34. }
  35. // Members get current online members
  36. func (s *Service) Members(c context.Context) (mt *monitor.Monitor, err error) {
  37. var (
  38. ret = &ChatResult{}
  39. )
  40. mt = &monitor.Monitor{}
  41. if ret, err = s.ChatProxy(c); err != nil {
  42. return nil, err
  43. }
  44. mt.AppID = "online"
  45. mt.Interface = "online-count"
  46. mt = &monitor.Monitor{
  47. AppID: "online",
  48. Interface: "online-count",
  49. Count: ret.Data.WebOnline,
  50. }
  51. return
  52. }