sinkMeta.go 7.4 KB

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