dispatcher.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package service
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "time"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/appctx"
  23. "yunion.io/x/onecloud/pkg/appsrv"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  25. "yunion.io/x/onecloud/pkg/mcclient/auth"
  26. sub "yunion.io/x/onecloud/pkg/monitor/influxdbsubscribe"
  27. "yunion.io/x/onecloud/pkg/monitor/subscriptionmodel"
  28. )
  29. var (
  30. SubscriptionWorkerManager *appsrv.SWorkerManager
  31. )
  32. func init() {
  33. SubscriptionWorkerManager = appsrv.NewWorkerManager("SubscriptionWorkerManager", 4, 1024, false)
  34. }
  35. func addCommonAlertDispatcher(prefix string, app *appsrv.Application) {
  36. manager := db.NewModelHandler(subscriptionmodel.SubscriptionManager)
  37. metadata := map[string]interface{}{"manager": manager}
  38. tags := map[string]string{"resource": subscriptionmodel.SubscriptionManager.KeywordPlural()}
  39. app.AddHandler2("POST",
  40. fmt.Sprintf("%s/%s/<subscription>", prefix, manager.KeywordPlural()),
  41. performHandler, metadata, "perform_class_subscription", tags)
  42. }
  43. type subscriptionTask struct {
  44. ctx context.Context
  45. query jsonutils.JSONObject
  46. body []sub.Point
  47. }
  48. func (t *subscriptionTask) Run() {
  49. t.ctx = context.WithValue(context.Background(), appctx.APP_CONTEXT_KEY_AUTH_TOKEN, auth.AdminCredential())
  50. subscriptionmodel.SubscriptionManager.PerformWrite(t.ctx, auth.AdminCredential(), t.query, t.body)
  51. }
  52. func (t *subscriptionTask) Dump() string {
  53. return ""
  54. }
  55. func performHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  56. _, query, body := fetchEnv(ctx, w, r)
  57. appsrv.SendJSON(w, wrap(jsonutils.NewDict(), "subscription"))
  58. task := &subscriptionTask{
  59. ctx: ctx,
  60. query: query,
  61. body: body,
  62. }
  63. SubscriptionWorkerManager.Run(task, nil, nil)
  64. }
  65. // fetchEnv fetch handler, params, query and body from ctx(context.Context)
  66. func fetchEnv(ctx context.Context, w http.ResponseWriter, r *http.Request) (map[string]string,
  67. jsonutils.JSONObject, []sub.Point) {
  68. params, query, body := FetchEnv(ctx, w, r)
  69. return params, query, body
  70. }
  71. func FetchEnv(ctx context.Context, w http.ResponseWriter, r *http.Request) (params map[string]string,
  72. query jsonutils.JSONObject, body []sub.Point) {
  73. var err error
  74. params = appctx.AppContextParams(ctx)
  75. query, err = jsonutils.ParseQueryString(r.URL.RawQuery)
  76. if err != nil {
  77. log.Errorf("Parse query string %s failed: %s", r.URL.RawQuery, err)
  78. }
  79. //var body jsonutils.JSONObject = nil
  80. if (r.Method == "PUT" || r.Method == "POST" || r.Method == "DELETE" || r.Method == "PATCH") && r.ContentLength > 0 {
  81. body, err = FetchRequest(r)
  82. if err != nil {
  83. log.Errorln(err)
  84. }
  85. }
  86. return params, query, body
  87. }
  88. func FetchRequest(req *http.Request) ([]sub.Point, error) {
  89. body, err := appsrv.Fetch(req)
  90. if err != nil {
  91. return nil, err
  92. }
  93. precision := req.FormValue("precision")
  94. if precision == "" {
  95. precision = "n"
  96. }
  97. points, err := sub.ParsePointsWithPrecision(body, time.Now().UTC(), precision)
  98. if err != nil {
  99. return nil, err
  100. }
  101. return points, nil
  102. }
  103. func mergeQueryParams(params map[string]string, query jsonutils.JSONObject, excludes ...string) jsonutils.JSONObject {
  104. if query == nil {
  105. query = jsonutils.NewDict()
  106. }
  107. queryDict := query.(*jsonutils.JSONDict)
  108. for k, v := range params {
  109. queryDict.Add(jsonutils.NewString(v), k[1:len(k)-1])
  110. }
  111. return queryDict
  112. }
  113. func wrap(data jsonutils.JSONObject, key string) jsonutils.JSONObject {
  114. ret := jsonutils.NewDict()
  115. ret.Add(data, key)
  116. return ret
  117. }