alertpanel.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 monitor
  15. import (
  16. "strings"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. "yunion.io/x/onecloud/pkg/apis/monitor"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/mcclient/options"
  23. )
  24. type AlertPanelCreateOptions struct {
  25. apis.ScopedResourceCreateInput
  26. DashboardId string `help:"id of attached dashboard"`
  27. NAME string `help:"Name of bashboard"`
  28. Metric string `help:"Metric name, include measurement and field, e.g. vm_cpu.usage_active" required:"true"`
  29. Database string
  30. Interval string `help:"query aggregation interval e.g. 1m|5s"`
  31. From string `help:"query start time e.g. 5m|6h"`
  32. To string `help:"query end time"`
  33. }
  34. func (o *AlertPanelCreateOptions) Params() (jsonutils.JSONObject, error) {
  35. createInput := new(monitor.AlertPanelCreateInput)
  36. createInput.Name = o.NAME
  37. createInput.Scope = o.Scope
  38. createInput.From = o.From
  39. createInput.To = o.To
  40. createInput.DashboardId = o.DashboardId
  41. createInput.Interval = o.Interval
  42. alertQuery := new(monitor.CommonAlertQuery)
  43. metrics := strings.Split(o.Metric, ".")
  44. if len(metrics) != 2 {
  45. return nil, errors.Wrap(httperrors.ErrBadRequest, "metric")
  46. }
  47. measurement := metrics[0]
  48. field := metrics[1]
  49. sels := make([]monitor.MetricQuerySelect, 0)
  50. sels = append(sels, monitor.NewMetricQuerySelect(
  51. monitor.MetricQueryPart{
  52. Type: "field",
  53. Params: []string{field},
  54. }))
  55. q := monitor.MetricQuery{
  56. Database: o.Database,
  57. Measurement: measurement,
  58. Selects: sels,
  59. }
  60. tmp := new(monitor.AlertQuery)
  61. tmp.Model = q
  62. alertQuery.AlertQuery = tmp
  63. createInput.MetricQuery = make([]*monitor.CommonAlertQuery, 0)
  64. createInput.MetricQuery = append(createInput.MetricQuery, alertQuery)
  65. return jsonutils.Marshal(createInput), nil
  66. }
  67. type AlertPanelListOptions struct {
  68. options.BaseListOptions
  69. DashboardId string `help:"id of attached dashboard"`
  70. }
  71. func (o *AlertPanelListOptions) Params() (jsonutils.JSONObject, error) {
  72. return options.ListStructToParams(o)
  73. }
  74. type AlertPanelShowOptions struct {
  75. ID string `help:"ID of Metric " json:"-"`
  76. }
  77. func (o *AlertPanelShowOptions) Params() (jsonutils.JSONObject, error) {
  78. return options.StructToParams(o)
  79. }
  80. func (o *AlertPanelShowOptions) GetId() string {
  81. return o.ID
  82. }
  83. type AlertPanelDeleteOptions struct {
  84. ID string `json:"-"`
  85. }
  86. func (o *AlertPanelDeleteOptions) GetId() string {
  87. return o.ID
  88. }
  89. func (o *AlertPanelDeleteOptions) Params() (jsonutils.JSONObject, error) {
  90. return options.StructToParams(o)
  91. }