sink.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 neuron
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/cast"
  21. "github.com/lf-edge/ekuiper/pkg/errorx"
  22. "sort"
  23. )
  24. type sink struct {
  25. url string
  26. c *c
  27. connected bool
  28. }
  29. type c struct {
  30. NodeName string `json:"nodeName"`
  31. GroupName string `json:"groupName"`
  32. Tags []string `json:"tags"`
  33. // If sent with the raw converted string or let us range over the result map
  34. Raw bool `json:"raw"`
  35. }
  36. type neuronTemplate struct {
  37. GroupName string `json:"group_name"`
  38. NodeName string `json:"node_name"`
  39. TagName string `json:"tag_name"`
  40. Value interface{} `json:"value"`
  41. }
  42. func (s *sink) Configure(props map[string]interface{}) error {
  43. s.url = NeuronUrl
  44. cc := &c{
  45. NodeName: "unknown",
  46. GroupName: "unknown",
  47. Raw: false,
  48. }
  49. err := cast.MapToStruct(props, cc)
  50. if err != nil {
  51. return err
  52. }
  53. s.c = cc
  54. return nil
  55. }
  56. func (s *sink) Open(ctx api.StreamContext) error {
  57. ctx.GetLogger().Debugf("Opening neuron sink")
  58. err := createOrGetConnection(ctx, s.url)
  59. if err != nil {
  60. return err
  61. }
  62. s.connected = true
  63. return nil
  64. }
  65. func (s *sink) Collect(ctx api.StreamContext, data interface{}) error {
  66. ctx.GetLogger().Debugf("receive %+v", data)
  67. if s.c.Raw {
  68. r, _, err := ctx.TransformOutput(data)
  69. if err != nil {
  70. return err
  71. }
  72. return publish(ctx, r)
  73. } else {
  74. switch d := data.(type) {
  75. case []map[string]interface{}:
  76. for _, el := range d {
  77. err := s.SendMapToNeuron(ctx, el)
  78. if err != nil {
  79. return err
  80. }
  81. }
  82. return nil
  83. case map[string]interface{}:
  84. return s.SendMapToNeuron(ctx, d)
  85. default:
  86. return fmt.Errorf("unrecognized format of %s", data)
  87. }
  88. }
  89. }
  90. func (s *sink) Close(ctx api.StreamContext) error {
  91. ctx.GetLogger().Debugf("closing neuron sink")
  92. if s.connected {
  93. return closeConnection(ctx, s.url)
  94. }
  95. return nil
  96. }
  97. func (s *sink) SendMapToNeuron(ctx api.StreamContext, el map[string]interface{}) error {
  98. n, err := ctx.ParseTemplate(s.c.NodeName, el)
  99. if err != nil {
  100. return err
  101. }
  102. g, err := ctx.ParseTemplate(s.c.GroupName, el)
  103. if err != nil {
  104. return err
  105. }
  106. t := &neuronTemplate{
  107. NodeName: n,
  108. GroupName: g,
  109. }
  110. var (
  111. ok bool
  112. )
  113. if len(s.c.Tags) == 0 {
  114. if conf.IsTesting {
  115. var keys []string
  116. for k := range el {
  117. keys = append(keys, k)
  118. }
  119. sort.Strings(keys)
  120. for _, k := range keys {
  121. t.TagName = k
  122. t.Value = el[k]
  123. err := doPublish(ctx, t)
  124. if err != nil {
  125. return err
  126. }
  127. }
  128. } else {
  129. for k, v := range el {
  130. t.TagName = k
  131. t.Value = v
  132. err := doPublish(ctx, t)
  133. if err != nil {
  134. return err
  135. }
  136. }
  137. }
  138. } else {
  139. // Send as many tags as possible in order and drop the tag if it is invalid
  140. for _, tag := range s.c.Tags {
  141. t.TagName, err = ctx.ParseTemplate(tag, el)
  142. if err != nil {
  143. ctx.GetLogger().Errorf("Error parsing tag %s: %v", tag, err)
  144. continue
  145. }
  146. t.Value, ok = el[t.TagName]
  147. if !ok {
  148. ctx.GetLogger().Errorf("Error get the value of tag %s: %v", t.TagName, err)
  149. continue
  150. }
  151. err := doPublish(ctx, t)
  152. if err != nil {
  153. return err
  154. }
  155. }
  156. }
  157. return nil
  158. }
  159. func doPublish(ctx api.StreamContext, t *neuronTemplate) error {
  160. r, err := json.Marshal(t)
  161. if err != nil {
  162. return fmt.Errorf("Error marshall the tag payload %v: %v", t, err)
  163. }
  164. err = publish(ctx, r)
  165. if err != nil {
  166. return fmt.Errorf("%s: Error publish the tag payload %s: %v", errorx.IOErr, t.TagName, err)
  167. }
  168. ctx.GetLogger().Debugf("Publish %s", r)
  169. return nil
  170. }
  171. func GetSink() *sink {
  172. return &sink{}
  173. }