EMqmyd 4 år sedan
förälder
incheckning
9e77151186
4 ändrade filer med 93 tillägg och 21 borttagningar
  1. 9 9
      docs/zh_CN/plugins/overview.md
  2. 73 12
      plugins/funcMeta.go
  3. 1 0
      plugins/manager.go
  4. 10 0
      xstream/server/server/rest.go

+ 9 - 9
docs/zh_CN/plugins/overview.md

@@ -265,15 +265,15 @@ source 的大部分属性用户通过对应的配置文件指定,用户无法
 以下为样例元数据文件。
 
 ```json
-{
-   "name": "avg",
-   "control": "text",
-   "eximple": "avg(col1)",
-   "hint": {
-     "en_US": "The file path for saving the result",
-     "zh_CN": "组中的平均值。空值不参与计算。"
-   }   
- }
+[{
+	"name": "avg",
+	"control": "text",
+	"example": "avg(col1)",
+	"hint": {
+		"en_US": "The file path for saving the result",
+		"zh_CN": "组中的平均值。空值不参与计算。"
+	}
+}]
 ```
 
 

+ 73 - 12
plugins/funcMeta.go

@@ -1,25 +1,86 @@
 package plugins
+
 import (
-	"encoding/json"
 	"fmt"
 	"github.com/emqx/kuiper/common"
 	"io/ioutil"
 	"path"
-	"reflect"
 	"strings"
 )
 
+type (
+	fileFunc struct {
+		Name    string        `json:"name"`
+		Control string        `json:"control"`
+		Example string        `json:"example"`
+		Hint    *fileLanguage `json:"hint"`
+	}
+	uiFunc struct {
+		Name    string    `json:"name"`
+		Control string    `json:"control"`
+		Example string    `json:"example"`
+		Hint    *language `json:"hint"`
+	}
+)
+
+func newUiFunc(fi *fileFunc) *uiFunc {
+	if nil == fi {
+		return nil
+	}
+	ui := new(uiFunc)
+	ui.Name = fi.Name
+	ui.Control = fi.Control
+	ui.Example = fi.Example
+	ui.Hint = newLanguage(fi.Hint)
+	return ui
+}
+
+var g_funcMetadata []*uiFunc
+
+func readfuncMetaDir() error {
+	confDir, err := common.GetConfLoc()
+	if nil != err {
+		return err
+	}
 
+	dir := path.Join(confDir, "functions")
+	files, err := ioutil.ReadDir(dir)
+	if nil != err {
+		return err
+	}
+	for _, file := range files {
+		fname := file.Name()
+		if !strings.HasSuffix(fname, ".json") {
+			continue
+		}
 
+		filePath := path.Join(dir, fname)
+		var fis []*fileFunc
+		err = common.ReadJsonUnmarshal(filePath, &fis)
+		if nil != err {
+			return fmt.Errorf("fname:%s err:%v", fname, err)
+		}
+		common.Log.Infof("funcMeta file : %s", fname)
+		for _, fi := range fis {
+			g_funcMetadata = append(g_funcMetadata, newUiFunc(fi))
+		}
+	}
+	return nil
+}
 
+func readFuncMetaFile(filePath string) error {
+	var fis []*fileFunc
+	err := common.ReadJsonUnmarshal(filePath, &fis)
+	if nil != err {
+		return fmt.Errorf("filePath:%s err:%v", filePath, err)
+	}
+	for _, fi := range fis {
+		g_funcMetadata = append(g_funcMetadata, newUiFunc(fi))
+	}
 
-{
-   "name": "path",
-   "control": "text",
-   "eximple": "text",
-   "type": "string",
-   "hint": {
-     "en_US": "The file path for saving the result",
-     "zh_CN": "保存结果的文件路径"
-   }
- }
+	common.Log.Infof("funcMeta file : %s", path.Base(filePath))
+	return nil
+}
+func GetFunctions() []*uiFunc {
+	return g_funcMetadata
+}

+ 1 - 0
plugins/manager.go

@@ -214,6 +214,7 @@ func NewPluginManager() (*Manager, error) {
 		}
 		readSourceMetaDir()
 		readSinkMetaDir()
+		readFuncMetaDir()
 	})
 	return singleton, outerErr
 }

+ 10 - 0
xstream/server/server/rest.go

@@ -88,6 +88,8 @@ func createRestServer(port int) *http.Server {
 	r.HandleFunc("/plugins/functions", functionsHandler).Methods(http.MethodGet, http.MethodPost)
 	r.HandleFunc("/plugins/functions/{name}", functionHandler).Methods(http.MethodDelete, http.MethodGet)
 
+	r.HandleFunc("/metadata/functions", functionsMetaHandler).Methods(http.MethodGet)
+
 	r.HandleFunc("/metadata/sinks", sinksMetaHandler).Methods(http.MethodGet)
 	r.HandleFunc("/metadata/sinks/{name}", newSinkMetaHandler).Methods(http.MethodGet)
 	r.HandleFunc("/metadata/sinks/rule/{id}", showSinkMetaHandler).Methods(http.MethodGet)
@@ -453,6 +455,14 @@ func showSinkMetaHandler(w http.ResponseWriter, r *http.Request) {
 	jsonResponse(ptrMetadata, w, logger)
 }
 
+//list functions
+func functionsMetaHandler(w http.ResponseWriter, r *http.Request) {
+	defer r.Body.Close()
+	sinks := plugins.GetFunctions()
+	jsonResponse(sinks, w, logger)
+	return
+}
+
 //list source plugin
 func sourcesMetaHandler(w http.ResponseWriter, r *http.Request) {
 	defer r.Body.Close()