sinkMeta.go 8.2 KB

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