<commit_before>package conf

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"reflect"
	"sync"

	"github.com/Cepave/open-falcon-backend/modules/query/utils"
)

type Gconfig struct {
	Funcations []FunConfig
}

type FunConfig struct {
	FuncationName string   `json:"funcation_name"`
	FilePath      string   `json:"file_path"`
	Params        []string `json:"params"`
	Description   string   `json:"description"`
	Codes         string   `json:"-"`
}

var (
	gconfig     []*FunConfig
	configLock  = new(sync.RWMutex)
	confpath    *string
	FunctionMap map[string]*FunConfig
)

func Config() map[string]*FunConfig {
	configLock.RLock()
	defer configLock.RUnlock()
	return FunctionMap
}

func functionMapGen() {
	f := "./"
	currentPath, _ := os.Getwd()
	if _, err := os.Stat(fmt.Sprintf("%s/js", currentPath)); err == nil {
		f = fmt.Sprintf("%s/js", currentPath)
	} else if _, err := os.Stat(fmt.Sprintf("%s/../js", currentPath)); err == nil {
		f = fmt.Sprintf("%s/../js", currentPath)
	} else if _, err := os.Stat(fmt.Sprintf("%s/../conf/js", currentPath)); err == nil {
		f = fmt.Sprintf("%s/../conf/js", currentPath)
	} else {
		log.Fatalf("load js files got error, currentPaht: %s , please check your code tree and make is correct!", currentPath)
	}
	FunctionMap = map[string]*FunConfig{}
	for _, v := range gconfig {
		contain := jsFileReader(fmt.Sprintf("%s/%s", f, v.FilePath))
		v.Codes = contain
		FunctionMap[v.FuncationName] = v
	}
}

func ReadConf() {
	var f string
	currentPath, _ := os.Getwd()
	if _, err := os.Stat(fmt.Sprintf("%s/conf/lambdaSetup.json", currentPath)); err == nil {
		f = fmt.Sprintf("%s/conf/lambdaSetup.json", currentPath)
	} else if _, err := os.Stat(fmt.Sprintf("%s/../conf/lambdaSetup.json", currentPath)); err == nil {
		f = fmt.Sprintf("%s/../conf/lambdaSetup.json", currentPath)
	} else {
		log.Fatalf("lambdaStup.json not found, currentPaht: %s", currentPath)
	}
	confpath = &f
	dat, err := ioutil.ReadFile(f)
	if err != nil {
		log.Println(err)
	}
	var myconf []*FunConfig
	json.Unmarshal(dat, &myconf)
	if len(myconf) == 0 {
		log.Println("conf file is empty or format is wrong, please check it!")
	}
	gconfig = myconf
	functionMapGen()
}

func Reload() {
	configLock.RLock()
	ReadConf()
	defer configLock.RUnlock()
}

func GetFunc(key string) *FunConfig {
	return FunctionMap[key]
}

func GetAvaibleFun() []string {
	return utils.GetMapKeys(reflect.ValueOf(FunctionMap).MapKeys())
}<commit_msg>fixed wrong config path of query<commit_after>package conf

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"reflect"
	"sync"

	"github.com/Cepave/open-falcon-backend/modules/query/utils"
)

type Gconfig struct {
	Funcations []FunConfig
}

type FunConfig struct {
	FuncationName string   `json:"funcation_name"`
	FilePath      string   `json:"file_path"`
	Params        []string `json:"params"`
	Description   string   `json:"description"`
	Codes         string   `json:"-"`
}

var (
	gconfig     []*FunConfig
	configLock  = new(sync.RWMutex)
	confpath    *string
	FunctionMap map[string]*FunConfig
)

func Config() map[string]*FunConfig {
	configLock.RLock()
	defer configLock.RUnlock()
	return FunctionMap
}

func functionMapGen() {
	f := "./"
	currentPath, _ := os.Getwd()
	if _, err := os.Stat(fmt.Sprintf("%s/js", currentPath)); err == nil {
		f = fmt.Sprintf("%s/js", currentPath)
	} else if _, err := os.Stat(fmt.Sprintf("%s/../js", currentPath)); err == nil {
		f = fmt.Sprintf("%s/../js", currentPath)
	} else if _, err := os.Stat(fmt.Sprintf("%s/../config/js", currentPath)); err == nil {
		f = fmt.Sprintf("%s/../config/js", currentPath)
	} else {
		log.Fatalf("load js files got error, currentPaht: %s , please check your code tree and make is correct!", currentPath)
	}
	FunctionMap = map[string]*FunConfig{}
	for _, v := range gconfig {
		contain := jsFileReader(fmt.Sprintf("%s/%s", f, v.FilePath))
		v.Codes = contain
		FunctionMap[v.FuncationName] = v
	}
}

func ReadConf() {
	var f string
	currentPath, _ := os.Getwd()
	if _, err := os.Stat(fmt.Sprintf("%s/conf/lambdaSetup.json", currentPath)); err == nil {
		f = fmt.Sprintf("%s/conf/lambdaSetup.json", currentPath)
	} else if _, err := os.Stat(fmt.Sprintf("%s/../config/lambdaSetup.json", currentPath)); err == nil {
		f = fmt.Sprintf("%s/../config/lambdaSetup.json", currentPath)
	} else {
		log.Fatalf("lambdaSetup.json not found, currentPaht: %s", currentPath)
	}
	confpath = &f
	dat, err := ioutil.ReadFile(f)
	if err != nil {
		log.Println(err)
	}
	var myconf []*FunConfig
	json.Unmarshal(dat, &myconf)
	if len(myconf) == 0 {
		log.Println("conf file is empty or format is wrong, please check it!")
	}
	gconfig = myconf
	functionMapGen()
}

func Reload() {
	configLock.RLock()
	ReadConf()
	defer configLock.RUnlock()
}

func GetFunc(key string) *FunConfig {
	return FunctionMap[key]
}

func GetAvaibleFun() []string {
	return utils.GetMapKeys(reflect.ValueOf(FunctionMap).MapKeys())
}
{'fraction_non_alphanumeric': 0.16883645240032547, 'fraction_numerical': 0.0004068348250610252, 'mean_word_length': 3.9969512195121952, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6319999', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1771, 'n_words': 401}