<commit_before>package utils

import (
	"crypto/md5"
	"encoding/hex"
	"flag"
	"hash/fnv"
	"net"
	"strconv"
	"strings"
	"time"

	"github.com/golang/glog"
	"github.com/nu7hatch/gouuid"
	"golang.org/x/net/context"

	"github.com/cloudstax/firecamp/common"
)

func SetLogToStd() {
	flag.Set("logtostderr", "true")
	flag.Set("stderrthreshold", "INFO")
}

func SetLogDir() {
	// glog has log_dir flag to specify the log dir
	f := flag.Lookup("log_dir")
	if f.Value.String() == "" {
		// set the default log dir
		flag.Set("log_dir", common.DefaultLogDir)
	}

	err := CreateDirIfNotExist(f.Value.String())
	if err != nil {
		glog.Fatalln("create/check controldb log dir error", err, f.Value.String())
	}
}

func Hash(s string) uint32 {
	h := fnv.New32a()
	h.Write([]byte(s))
	return h.Sum32()
}

func GenMD5(content string) string {
	data := md5.Sum([]byte(content))
	return hex.EncodeToString(data[:])
}

// GenUUID returns a 32 bytes uuid string
func GenUUID() string {
	u, err := uuid.NewV4()
	if err != nil {
		// uuid gen error, switch to use timestamp
		glog.Errorln("uuid.NewV4 error", err)
		tmstr := strconv.FormatInt(time.Now().UnixNano(), 10)
		return GenMD5(tmstr)
	}
	return hex.EncodeToString(u[:])
}

func GenRequestUUID() string {
	return "req-" + GenUUID()
}

// timestamp + uuid
func GenCallerID() string {
	sep := "-"
	t := time.Now().Unix()
	uuid := strconv.FormatInt(t, 10) + sep + GenUUID()
	return uuid
}

func GetServiceJournalVolumeName(serviceUUID string) string {
	return common.JournalVolumeNamePrefix + common.NameSeparator + serviceUUID
}

func GenServiceMemberName(serviceName string, index int64) string {
	return serviceName + common.NameSeparator + strconv.FormatInt(index, 10)
}

func GenMemberConfigFileID(memberName string, configFileName string, version int64) string {
	return memberName + common.NameSeparator + configFileName + common.NameSeparator + strconv.FormatInt(version, 10)
}

func GetConfigFileVersion(fileID string) (version int64, err error) {
	fields := strings.Split(fileID, common.NameSeparator)
	versionStr := fields[len(fields)-1]
	return strconv.ParseInt(versionStr, 10, 64)
}

func GenControlDBServiceUUID(volID string) string {
	return common.ControlDBUUIDPrefix + volID
}

func IsControlDBService(serviceUUID string) bool {
	return strings.HasPrefix(serviceUUID, common.ControlDBUUIDPrefix)
}

// GetControlDBVolumeID extracts the volume id from the controldb serviceUUID
func GetControlDBVolumeID(controldbServiceUUID string) string {
	return strings.TrimPrefix(controldbServiceUUID, common.ControlDBUUIDPrefix)
}
<commit_msg>fix the underscore char in config file id<commit_after>package utils

import (
	"crypto/md5"
	"encoding/hex"
	"flag"
	"hash/fnv"
	"net"
	"strconv"
	"strings"
	"time"

	"github.com/golang/glog"
	"github.com/nu7hatch/gouuid"
	"golang.org/x/net/context"

	"github.com/cloudstax/firecamp/common"
)

func SetLogToStd() {
	flag.Set("logtostderr", "true")
	flag.Set("stderrthreshold", "INFO")
}

func SetLogDir() {
	// glog has log_dir flag to specify the log dir
	f := flag.Lookup("log_dir")
	if f.Value.String() == "" {
		// set the default log dir
		flag.Set("log_dir", common.DefaultLogDir)
	}

	err := CreateDirIfNotExist(f.Value.String())
	if err != nil {
		glog.Fatalln("create/check controldb log dir error", err, f.Value.String())
	}
}

func Hash(s string) uint32 {
	h := fnv.New32a()
	h.Write([]byte(s))
	return h.Sum32()
}

func GenMD5(content string) string {
	data := md5.Sum([]byte(content))
	return hex.EncodeToString(data[:])
}

// GenUUID returns a 32 bytes uuid string
func GenUUID() string {
	u, err := uuid.NewV4()
	if err != nil {
		// uuid gen error, switch to use timestamp
		glog.Errorln("uuid.NewV4 error", err)
		tmstr := strconv.FormatInt(time.Now().UnixNano(), 10)
		return GenMD5(tmstr)
	}
	return hex.EncodeToString(u[:])
}

func GenRequestUUID() string {
	return "req-" + GenUUID()
}

// timestamp + uuid
func GenCallerID() string {
	sep := "-"
	t := time.Now().Unix()
	uuid := strconv.FormatInt(t, 10) + sep + GenUUID()
	return uuid
}

func GetServiceJournalVolumeName(serviceUUID string) string {
	return common.JournalVolumeNamePrefix + common.NameSeparator + serviceUUID
}

func GenServiceMemberName(serviceName string, index int64) string {
	return serviceName + common.NameSeparator + strconv.FormatInt(index, 10)
}

func GenMemberConfigFileID(memberName string, configFileName string, version int64) string {
	// configFileName may include the '_', such as pg_hba.conf for postgres.
	// The '_' is not supported as k8s configmap name. convert configFileName to md5.
	fileID := GenMD5(configFileName)
	return memberName + common.NameSeparator + fileID + common.NameSeparator + strconv.FormatInt(version, 10)
}

func GetConfigFileVersion(fileID string) (version int64, err error) {
	fields := strings.Split(fileID, common.NameSeparator)
	versionStr := fields[len(fields)-1]
	return strconv.ParseInt(versionStr, 10, 64)
}

func GenControlDBServiceUUID(volID string) string {
	return common.ControlDBUUIDPrefix + volID
}

func IsControlDBService(serviceUUID string) bool {
	return strings.HasPrefix(serviceUUID, common.ControlDBUUIDPrefix)
}

// GetControlDBVolumeID extracts the volume id from the controldb serviceUUID
func GetControlDBVolumeID(controldbServiceUUID string) string {
	return strings.TrimPrefix(controldbServiceUUID, common.ControlDBUUIDPrefix)
}

{'fraction_non_alphanumeric': 0.1275367715509216, 'fraction_numerical': 0.013219139824986036, 'mean_word_length': 4.94905869324474, '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': '4068097', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1881, 'n_words': 442}