metric_joint.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 models
  15. import (
  16. "context"
  17. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. "yunion.io/x/onecloud/pkg/mcclient/auth"
  20. )
  21. type SMetricManager struct {
  22. db.SJointResourceBaseManager
  23. }
  24. type SMetric struct {
  25. db.SVirtualJointResourceBase
  26. MeasurementId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required"`
  27. FieldId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required"`
  28. }
  29. var MetricManager *SMetricManager
  30. func init() {
  31. MetricManager = &SMetricManager{
  32. SJointResourceBaseManager: db.NewJointResourceBaseManager(
  33. SMetric{},
  34. "metric_tbl",
  35. "metric",
  36. "metrics",
  37. MetricMeasurementManager,
  38. MetricFieldManager,
  39. ),
  40. }
  41. MetricManager.SetVirtualObject(MetricManager)
  42. MetricManager.TableSpec().AddIndex(true, MetricManager.GetMasterFieldName(), MetricManager.GetSlaveFieldName())
  43. }
  44. func (man *SMetricManager) GetMasterFieldName() string {
  45. return "measurement_id"
  46. }
  47. func (man *SMetricManager) GetSlaveFieldName() string {
  48. return "field_id"
  49. }
  50. func (metric *SMetric) DoSave(ctx context.Context) error {
  51. if err := MetricManager.TableSpec().Insert(ctx, metric); err != nil {
  52. return err
  53. }
  54. metric.SetModelManager(MetricManager, metric)
  55. return nil
  56. }
  57. func (self *SMetric) GetMetricField(ctx context.Context) (*SMetricField, error) {
  58. return MetricFieldManager.GetFieldByIdOrName(ctx, self.FieldId, auth.AdminCredential())
  59. }
  60. func (joint *SMetric) Detach(ctx context.Context, userCred mcclient.TokenCredential) error {
  61. return db.DetachJoint(ctx, userCred, joint)
  62. }