sinkMeta.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package plugins
  2. import (
  3. "fmt"
  4. "github.com/emqx/kuiper/common"
  5. "github.com/emqx/kuiper/xstream/api"
  6. "io/ioutil"
  7. "path"
  8. "sort"
  9. "strings"
  10. )
  11. const (
  12. baseProperty = `properties`
  13. baseOption = `options`
  14. )
  15. type (
  16. author struct {
  17. Name string `json:"name"`
  18. Email string `json:"email"`
  19. Company string `json:"company"`
  20. Website string `json:"website"`
  21. }
  22. language struct {
  23. English string `json:"en_US"`
  24. Chinese string `json:"zh_CN"`
  25. }
  26. field struct {
  27. Name string `json:"name"`
  28. Default interface{} `json:"default"`
  29. Type string `json:"type"`
  30. Control string `json:"control"`
  31. Optional bool `json:"optional"`
  32. Values interface{} `json:"values"`
  33. Hint *language `json:"hint"`
  34. Label *language `json:"label"`
  35. }
  36. sinkMeta struct {
  37. Author *author `json:"author"`
  38. HelpUrl *language `json:"helpUrl"`
  39. Libs []string `json:"libs"`
  40. Fields []*field `json:"properties"`
  41. }
  42. )
  43. var g_sinkMetadata map[string]*sinkMeta //map[fileName]
  44. //const internal sinks
  45. var InternalSinks = [...]string{"log", "mqtt", "rest", "nop", "edgex"}
  46. func (this *Manager) readSinkMetaDir() error {
  47. confDir, err := common.GetConfLoc()
  48. if nil != err {
  49. return err
  50. }
  51. dir := path.Join(confDir, "sinks")
  52. tmpMap := make(map[string]*sinkMeta)
  53. //The internal support sinks
  54. for _, sink := range InternalSinks {
  55. file := path.Join(confDir, "sinks", "internal", sink + ".json")
  56. common.Log.Infof("Loading metadata file for sink: %s", file)
  57. meta := new(sinkMeta)
  58. err := common.ReadJsonUnmarshal(file, meta)
  59. if nil != err {
  60. return fmt.Errorf("Failed to load internal sink plugin:%s with err:%v", file, err)
  61. }
  62. tmpMap[sink + ".json"] = meta
  63. }
  64. files, err := ioutil.ReadDir(dir)
  65. if nil != err {
  66. return err
  67. }
  68. for _, file := range files {
  69. fname := file.Name()
  70. if !strings.HasSuffix(fname, ".json") {
  71. continue
  72. }
  73. filePath := path.Join(dir, fname)
  74. metadata := new(sinkMeta)
  75. err = common.ReadJsonUnmarshal(filePath, metadata)
  76. if nil != err {
  77. return fmt.Errorf("fname:%s err:%v", fname, err)
  78. }
  79. common.Log.Infof("sinkMeta file : %s", fname)
  80. tmpMap[fname] = metadata
  81. }
  82. g_sinkMetadata = tmpMap
  83. return nil
  84. }
  85. func (this *Manager) readSinkMetaFile(filePath string) error {
  86. metadata := new(sinkMeta)
  87. err := common.ReadJsonUnmarshal(filePath, metadata)
  88. if nil != err {
  89. return fmt.Errorf("filePath:%s err:%v", filePath, err)
  90. }
  91. sinkMetadata := g_sinkMetadata
  92. tmpMap := make(map[string]*sinkMeta)
  93. for k, v := range sinkMetadata {
  94. tmpMap[k] = v
  95. }
  96. fileName := path.Base(filePath)
  97. common.Log.Infof("sinkMeta file : %s", fileName)
  98. tmpMap[fileName] = metadata
  99. g_sinkMetadata = tmpMap
  100. return nil
  101. }
  102. type (
  103. hintLanguage struct {
  104. English string `json:"en"`
  105. Chinese string `json:"zh"`
  106. }
  107. hintField struct {
  108. Name string `json:"name"`
  109. Default interface{} `json:"default"`
  110. Control string `json:"control"`
  111. Optional bool `json:"optional"`
  112. Type string `json:"type"`
  113. Hint *hintLanguage `json:"hint"`
  114. Label *hintLanguage `json:"label"`
  115. Values interface{} `json:"values"`
  116. }
  117. sinkPropertyNode struct {
  118. Fields []*hintField `json:"properties"`
  119. HelpUrl *hintLanguage `json:"helpUrl"`
  120. Libs []string `json:"libs"`
  121. }
  122. sinkProperty struct {
  123. CustomProperty map[string]*sinkPropertyNode `json:"customProperty"`
  124. BaseProperty map[string]*sinkPropertyNode `json:"baseProperty"`
  125. BaseOption *sinkPropertyNode `json:"baseOption"`
  126. }
  127. )
  128. func (this *hintLanguage) set(l *language) {
  129. this.English = l.English
  130. this.Chinese = l.Chinese
  131. }
  132. func (this *hintField) setSinkField(v *field) {
  133. this.Name = v.Name
  134. this.Type = v.Type
  135. this.Default = v.Default
  136. this.Values = v.Values
  137. this.Control = v.Control
  138. this.Optional = v.Optional
  139. this.Hint = new(hintLanguage)
  140. this.Hint.set(v.Hint)
  141. this.Label = new(hintLanguage)
  142. this.Label.set(v.Label)
  143. }
  144. func (this *sinkPropertyNode) setNodeFromMetal(data *sinkMeta) {
  145. this.Libs = data.Libs
  146. if nil != data.HelpUrl {
  147. this.HelpUrl = new(hintLanguage)
  148. this.HelpUrl.set(data.HelpUrl)
  149. }
  150. for _, v := range data.Fields {
  151. field := new(hintField)
  152. field.setSinkField(v)
  153. this.Fields = append(this.Fields, field)
  154. }
  155. }
  156. func (this *sinkProperty) setCustomProperty(pluginName string) error {
  157. fileName := pluginName + `.json`
  158. sinkMetadata := g_sinkMetadata
  159. data := sinkMetadata[fileName]
  160. if nil == data {
  161. return fmt.Errorf(`not found pligin:%s`, fileName)
  162. }
  163. node := new(sinkPropertyNode)
  164. node.setNodeFromMetal(data)
  165. if 0 == len(this.CustomProperty) {
  166. this.CustomProperty = make(map[string]*sinkPropertyNode)
  167. }
  168. this.CustomProperty[pluginName] = node
  169. return nil
  170. }
  171. func (this *sinkProperty) setBasePropertry(pluginName string) error {
  172. sinkMetadata := g_sinkMetadata
  173. data := sinkMetadata[baseProperty+".json"]
  174. if nil == data {
  175. return fmt.Errorf(`not found pligin:%s`, baseProperty)
  176. }
  177. node := new(sinkPropertyNode)
  178. node.setNodeFromMetal(data)
  179. if 0 == len(this.BaseProperty) {
  180. this.BaseProperty = make(map[string]*sinkPropertyNode)
  181. }
  182. this.BaseProperty[pluginName] = node
  183. return nil
  184. }
  185. func (this *sinkProperty) setBaseOption() error {
  186. sinkMetadata := g_sinkMetadata
  187. data := sinkMetadata[baseOption+".json"]
  188. if nil == data {
  189. return fmt.Errorf(`not found pligin:%s`, baseOption)
  190. }
  191. node := new(sinkPropertyNode)
  192. node.setNodeFromMetal(data)
  193. this.BaseOption = node
  194. return nil
  195. }
  196. func (this *sinkProperty) hintWhenNewSink(pluginName string) (err error) {
  197. err = this.setCustomProperty(pluginName)
  198. if nil != err {
  199. return err
  200. }
  201. err = this.setBasePropertry(pluginName)
  202. if nil != err {
  203. return err
  204. }
  205. err = this.setBaseOption()
  206. return err
  207. }
  208. func (this *sinkPropertyNode) modifyPropertyNode(mapFields map[string]interface{}) (err error) {
  209. for i, field := range this.Fields {
  210. fieldVal := mapFields[field.Name]
  211. if nil != fieldVal {
  212. this.Fields[i].Default = fieldVal
  213. }
  214. }
  215. return nil
  216. }
  217. func (this *sinkProperty) modifyProperty(pluginName string, mapFields map[string]interface{}) (err error) {
  218. customProperty := this.CustomProperty[pluginName]
  219. if nil != customProperty {
  220. customProperty.modifyPropertyNode(mapFields)
  221. }
  222. baseProperty := this.BaseProperty[pluginName]
  223. if nil != baseProperty {
  224. baseProperty.modifyPropertyNode(mapFields)
  225. }
  226. return nil
  227. }
  228. func (this *sinkProperty) modifyOption(option *api.RuleOption) {
  229. baseOption := this.BaseOption
  230. if nil == baseOption {
  231. return
  232. }
  233. for i, field := range baseOption.Fields {
  234. switch field.Name {
  235. case `isEventTime`:
  236. baseOption.Fields[i].Default = option.IsEventTime
  237. case `lateTol`:
  238. baseOption.Fields[i].Default = option.LateTol
  239. case `concurrency`:
  240. baseOption.Fields[i].Default = option.Concurrency
  241. case `bufferLength`:
  242. baseOption.Fields[i].Default = option.BufferLength
  243. case `sendMetaToSink`:
  244. baseOption.Fields[i].Default = option.SendMetaToSink
  245. case `qos`:
  246. baseOption.Fields[i].Default = option.Qos
  247. case `checkpointInterval`:
  248. baseOption.Fields[i].Default = option.CheckpointInterval
  249. }
  250. }
  251. }
  252. func (this *sinkProperty) hintWhenModifySink(rule *api.Rule) (err error) {
  253. for _, m := range rule.Actions {
  254. for pluginName, sink := range m {
  255. mapFields, _ := sink.(map[string]interface{})
  256. err = this.hintWhenNewSink(pluginName)
  257. if nil != err {
  258. return err
  259. }
  260. this.modifyProperty(pluginName, mapFields)
  261. }
  262. }
  263. this.modifyOption(rule.Options)
  264. return nil
  265. }
  266. func (this *Manager) GetSinkMeta(pluginName string, rule *api.Rule) (ptrSinkProperty *sinkProperty, err error) {
  267. ptrSinkProperty = new(sinkProperty)
  268. if nil == rule {
  269. err = ptrSinkProperty.hintWhenNewSink(pluginName)
  270. } else {
  271. err = ptrSinkProperty.hintWhenModifySink(rule)
  272. }
  273. return ptrSinkProperty, err
  274. }
  275. func (this *Manager) GetSinks() (sinks []string) {
  276. sinkMeta := g_sinkMetadata
  277. for fileName, _ := range sinkMeta {
  278. if fileName == baseProperty+".json" || fileName == baseOption+".json" {
  279. continue
  280. }
  281. sinks = append(sinks, strings.TrimSuffix(fileName, `.json`))
  282. }
  283. sort.Sort(sort.StringSlice(sinks))
  284. return sinks
  285. }