alertrecord.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "time"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. )
  22. type AlertRecordListOptions struct {
  23. options.BaseListOptions
  24. AlertId string `help:"id of alert"`
  25. Level string `help:"alert level"`
  26. State string `help:"alert state"`
  27. ResTypes []string `json:"res_types"`
  28. ResId string `json:"res_id"`
  29. ResName string `json:"res_name"`
  30. Alerting bool `json:"alerting"`
  31. AlertName string `json:"alert_name"`
  32. }
  33. func (o *AlertRecordListOptions) Params() (jsonutils.JSONObject, error) {
  34. return options.ListStructToParams(o)
  35. }
  36. type AlertRecordShowOptions struct {
  37. ID string `help:"ID of Metric " json:"-"`
  38. }
  39. func (o *AlertRecordShowOptions) Params() (jsonutils.JSONObject, error) {
  40. return options.StructToParams(o)
  41. }
  42. func (o *AlertRecordShowOptions) GetId() string {
  43. return o.ID
  44. }
  45. type AlertRecordTotalOptions struct {
  46. options.BaseListOptions
  47. }
  48. func (o *AlertRecordTotalOptions) Params() (jsonutils.JSONObject, error) {
  49. return options.ListStructToParams(o)
  50. }
  51. func (o *AlertRecordTotalOptions) Property() string {
  52. return "total-alert"
  53. }
  54. type AlertRecordHistoryAlertOptions struct {
  55. options.BaseListOptions
  56. }
  57. func (o *AlertRecordHistoryAlertOptions) Params() (jsonutils.JSONObject, error) {
  58. return options.ListStructToParams(o)
  59. }
  60. func (o *AlertRecordHistoryAlertOptions) Property() string {
  61. return "history-alert"
  62. }
  63. type AlertRecordProjectAlertResourceCountOptions struct {
  64. StartTime time.Time `help:"start time (RFC3339 format)" json:"start_time" default:"2025-01-01 00:00:00"`
  65. EndTime time.Time `help:"end time (RFC3339 format)" json:"end_time" default:"2025-01-01 00:00:00"`
  66. ResType string `help:"resource type" json:"res_type"`
  67. AlertId string `help:"alert id" json:"alert_id"`
  68. Scope string `help:"scope" json:"scope" choices:"system|domain|project"`
  69. }
  70. func (o *AlertRecordProjectAlertResourceCountOptions) Params() (jsonutils.JSONObject, error) {
  71. return options.StructToParams(o)
  72. }
  73. func (o *AlertRecordProjectAlertResourceCountOptions) Property() string {
  74. return "project-alert-resource-count"
  75. }
  76. type AlertRecordShieldListOptions struct {
  77. options.BaseListOptions
  78. AlertId string `help:"id of alert"`
  79. Alertname string `json:"alertname"`
  80. ResName string `json:"res_name"`
  81. ResTypes []string `json:"res_types"`
  82. }
  83. func (o *AlertRecordShieldListOptions) Params() (jsonutils.JSONObject, error) {
  84. return options.ListStructToParams(o)
  85. }
  86. type AlertRecordShieldShowOptions struct {
  87. ID string `help:"ID of Metric " json:"-"`
  88. }
  89. func (o *AlertRecordShieldShowOptions) Params() (jsonutils.JSONObject, error) {
  90. return options.StructToParams(o)
  91. }
  92. func (o *AlertRecordShieldShowOptions) GetId() string {
  93. return o.ID
  94. }
  95. type AlertRecordShieldDeleteOptions struct {
  96. ID string `json:"-"`
  97. }
  98. func (o *AlertRecordShieldDeleteOptions) GetId() string {
  99. return o.ID
  100. }
  101. func (o *AlertRecordShieldDeleteOptions) Params() (jsonutils.JSONObject, error) {
  102. return options.StructToParams(o)
  103. }
  104. type AlertRecordShieldCreateOptions struct {
  105. apis.ScopedResourceCreateInput
  106. AlertId string `json:"alert_id" help:"common alert Id" required:"true"`
  107. ResType string `json:"res_type" help:"resource tyge" choices:"host|guest|redis|oss|rds|cloudaccount"`
  108. ResName string `json:"res_name" help:"resource name" required:"true"`
  109. ResId string `json:"res_id" help:"resource id" required:"true"`
  110. ShieldPeriod string `json:"shield_period" help:"shield time eg:'1m,2h'" required:"true"`
  111. }
  112. func (o *AlertRecordShieldCreateOptions) Params() (jsonutils.JSONObject, error) {
  113. params, err := options.StructToParams(o)
  114. if err != nil {
  115. return nil, err
  116. }
  117. duration, err := time.ParseDuration(o.ShieldPeriod)
  118. if err != nil {
  119. return nil, errors.Wrap(err, "parse shield_period err")
  120. }
  121. startTime := time.Now()
  122. endTime := startTime.Add(duration)
  123. params.Add(jsonutils.NewTimeString(startTime), "start_time")
  124. params.Add(jsonutils.NewTimeString(endTime), "end_time")
  125. return params, nil
  126. }