ruleset.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  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 processor
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "io"
  21. )
  22. type RulesetProcessor struct {
  23. r *RuleProcessor
  24. s *StreamProcessor
  25. }
  26. type ruleset struct {
  27. Streams map[string]string `json:"streams"`
  28. Tables map[string]string `json:"tables"`
  29. Rules map[string]string `json:"rules"`
  30. }
  31. func NewRulesetProcessor(r *RuleProcessor, s *StreamProcessor) *RulesetProcessor {
  32. return &RulesetProcessor{
  33. r: r,
  34. s: s,
  35. }
  36. }
  37. func (rs *RulesetProcessor) Export() (io.ReadSeeker, []int, error) {
  38. var all ruleset
  39. allStreams, err := rs.s.GetAll()
  40. if err != nil {
  41. return nil, nil, fmt.Errorf("fail to get all streams: %v", err)
  42. }
  43. all.Streams = allStreams["streams"]
  44. all.Tables = allStreams["tables"]
  45. rules, err := rs.r.GetAllRulesJson()
  46. if err != nil {
  47. return nil, nil, fmt.Errorf("fail to get all rules: %v", err)
  48. }
  49. all.Rules = rules
  50. jsonBytes, err := json.Marshal(all)
  51. if err != nil {
  52. return nil, nil, err
  53. }
  54. counts := []int{len(all.Streams), len(all.Tables), len(all.Rules)}
  55. return bytes.NewReader(jsonBytes), counts, nil
  56. }
  57. func (rs *RulesetProcessor) Import(content []byte) ([]string, []int, error) {
  58. all := &ruleset{}
  59. err := json.Unmarshal(content, all)
  60. if err != nil {
  61. return nil, nil, fmt.Errorf("invalid import file: %v", err)
  62. }
  63. counts := make([]int, 3)
  64. // restore streams
  65. for k, v := range all.Streams {
  66. _, e := rs.s.ExecStreamSql(v)
  67. if e != nil {
  68. conf.Log.Errorf("Fail to import stream %s(%s) with error: %v", k, v, e)
  69. } else {
  70. counts[0]++
  71. }
  72. }
  73. // restore tables
  74. for k, v := range all.Tables {
  75. _, e := rs.s.ExecStreamSql(v)
  76. if e != nil {
  77. conf.Log.Errorf("Fail to import table %s(%s) with error: %v", k, v, e)
  78. } else {
  79. counts[1]++
  80. }
  81. }
  82. var rules []string
  83. // restore rules
  84. for k, v := range all.Rules {
  85. _, e := rs.r.ExecCreate(k, v)
  86. if e != nil {
  87. conf.Log.Errorf("Fail to import rule %s(%s) with error: %v", k, v, e)
  88. } else {
  89. rules = append(rules, k)
  90. counts[2]++
  91. }
  92. }
  93. return rules, counts, nil
  94. }