metadata.go 7.5 KB

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