syncresults.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 compare
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. type SyncResult struct {
  21. AddCnt int `json:"add_cnt,omitzero"`
  22. AddErrCnt int `json:"add_err_cnt,omitzero"`
  23. UpdateCnt int `json:"update_cnt,omitzero"`
  24. UpdateErrCnt int `json:"update_err_cnt,omitzero"`
  25. DelCnt int `json:"del_cnt,omitzero"`
  26. DelErrCnt int `json:"del_err_cnt,omitzero"`
  27. errors []error
  28. }
  29. func (self *SyncResult) Error(msg error) {
  30. if self.errors == nil {
  31. self.errors = make([]error, 0)
  32. }
  33. self.errors = append(self.errors, msg)
  34. }
  35. func (self *SyncResult) Add() {
  36. self.AddCnt += 1
  37. }
  38. func (self *SyncResult) AddError(msg error) {
  39. self.AddErrCnt += 1
  40. self.Error(msg)
  41. }
  42. func (self *SyncResult) Update() {
  43. self.UpdateCnt += 1
  44. }
  45. func (self *SyncResult) UpdateError(msg error) {
  46. self.UpdateErrCnt += 1
  47. self.Error(msg)
  48. }
  49. func (self *SyncResult) Delete() {
  50. self.DelCnt += 1
  51. }
  52. func (self *SyncResult) DeleteError(msg error) {
  53. self.DelErrCnt += 1
  54. self.Error(msg)
  55. }
  56. func (self *SyncResult) AllError() error {
  57. msgs := make(map[string]bool)
  58. for _, e := range self.errors {
  59. msg := e.Error()
  60. msgs[msg] = true
  61. }
  62. ret := make([]string, len(msgs))
  63. i := 0
  64. for m := range msgs {
  65. ret[i] = m
  66. i += 1
  67. }
  68. return fmt.Errorf(strings.Join(ret, ";"))
  69. }
  70. func (self *SyncResult) IsError() bool {
  71. return self.errors != nil && len(self.errors) > 0
  72. }
  73. func (self *SyncResult) IsGenerateError() bool {
  74. for _, err := range self.errors {
  75. if e := errors.Cause(err); e != errors.ErrNotImplemented && e != errors.ErrNotSupported && e != errors.ErrNotEmpty {
  76. return true
  77. }
  78. }
  79. return false
  80. }
  81. func (self *SyncResult) Result() string {
  82. msg := fmt.Sprintf("removed %d failed %d updated %d failed %d added %d failed %d", self.DelCnt, self.DelErrCnt, self.UpdateCnt, self.UpdateErrCnt, self.AddCnt, self.AddErrCnt)
  83. if self.IsError() {
  84. msg = fmt.Sprintf("%s;%s", msg, self.AllError())
  85. }
  86. return msg
  87. }