ruleset.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2022-2023 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. "io"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  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) ExportRuleSet() *Ruleset {
  58. all := &Ruleset{}
  59. allStreams, err := rs.s.GetAll()
  60. if err != nil {
  61. conf.Log.Errorf("fail to get all streams: %v", err)
  62. return nil
  63. }
  64. all.Streams = allStreams["streams"]
  65. all.Tables = allStreams["tables"]
  66. rules, err := rs.r.GetAllRulesJson()
  67. if err != nil {
  68. conf.Log.Errorf("fail to get all rules: %v", err)
  69. return nil
  70. }
  71. all.Rules = rules
  72. return all
  73. }
  74. func (rs *RulesetProcessor) ExportRuleSetStatus() *Ruleset {
  75. all := &Ruleset{}
  76. allStreams, err := rs.s.streamStatusDb.All()
  77. if err != nil {
  78. conf.Log.Errorf("fail to get all stream status: %v", err)
  79. return nil
  80. }
  81. allTables, err := rs.s.tableStatusDb.All()
  82. if err != nil {
  83. conf.Log.Errorf("fail to get all table status: %v", err)
  84. return nil
  85. }
  86. all.Streams = allStreams
  87. all.Tables = allTables
  88. rules, err := rs.r.ruleStatusDb.All()
  89. if err != nil {
  90. conf.Log.Errorf("fail to get all rule status: %v", err)
  91. return nil
  92. }
  93. all.Rules = rules
  94. return all
  95. }
  96. func (rs *RulesetProcessor) Import(content []byte) ([]string, []int, error) {
  97. all := &Ruleset{}
  98. err := json.Unmarshal(content, all)
  99. if err != nil {
  100. return nil, nil, fmt.Errorf("invalid import file: %v", err)
  101. }
  102. counts := make([]int, 3)
  103. // restore streams
  104. for k, v := range all.Streams {
  105. _, e := rs.s.ExecStreamSql(v)
  106. if e != nil {
  107. conf.Log.Errorf("Fail to import stream %s(%s) with error: %v", k, v, e)
  108. } else {
  109. counts[0]++
  110. }
  111. }
  112. // restore tables
  113. for k, v := range all.Tables {
  114. _, e := rs.s.ExecStreamSql(v)
  115. if e != nil {
  116. conf.Log.Errorf("Fail to import table %s(%s) with error: %v", k, v, e)
  117. } else {
  118. counts[1]++
  119. }
  120. }
  121. var rules []string
  122. // restore rules
  123. for k, v := range all.Rules {
  124. _, e := rs.r.ExecCreateWithValidation(k, v)
  125. if e != nil {
  126. conf.Log.Errorf("Fail to import rule %s(%s) with error: %v", k, v, e)
  127. } else {
  128. rules = append(rules, k)
  129. counts[2]++
  130. }
  131. }
  132. return rules, counts, nil
  133. }
  134. func (rs *RulesetProcessor) ImportRuleSet(all Ruleset) Ruleset {
  135. ruleSetRsp := Ruleset{
  136. Rules: map[string]string{},
  137. Streams: map[string]string{},
  138. Tables: map[string]string{},
  139. }
  140. _ = rs.s.streamStatusDb.Clean()
  141. _ = rs.s.tableStatusDb.Clean()
  142. _ = rs.r.ruleStatusDb.Clean()
  143. counts := make([]int, 3)
  144. // restore streams
  145. for k, v := range all.Streams {
  146. _, e := rs.s.ExecStreamSql(v)
  147. if e != nil {
  148. conf.Log.Errorf("Fail to import stream %s(%s) with error: %v", k, v, e)
  149. _ = rs.s.streamStatusDb.Set(k, e.Error())
  150. ruleSetRsp.Streams[k] = e.Error()
  151. continue
  152. }
  153. counts[0]++
  154. }
  155. // restore tables
  156. for k, v := range all.Tables {
  157. _, e := rs.s.ExecStreamSql(v)
  158. if e != nil {
  159. conf.Log.Errorf("Fail to import table %s(%s) with error: %v", k, v, e)
  160. _ = rs.s.tableStatusDb.Set(k, e.Error())
  161. ruleSetRsp.Tables[k] = e.Error()
  162. continue
  163. }
  164. counts[1]++
  165. }
  166. // restore rules
  167. for k, v := range all.Rules {
  168. _, e := rs.r.ExecCreateWithValidation(k, v)
  169. if e != nil {
  170. conf.Log.Errorf("Fail to import rule %s(%s) with error: %v", k, v, e)
  171. _ = rs.r.ruleStatusDb.Set(k, e.Error())
  172. ruleSetRsp.Rules[k] = e.Error()
  173. continue
  174. }
  175. counts[2]++
  176. }
  177. return ruleSetRsp
  178. }