This commit is contained in:
Zheng Kai
2023-06-13 15:04:56 +08:00
parent f0c0db8e94
commit 408c8c3b6e
8 changed files with 47 additions and 9 deletions

View File

@@ -8,6 +8,9 @@ build: git
run: build run: build
sudo docker run \ sudo docker run \
--env "OPENAI_API_KEY=$(OPENAI_API_KEY)" \ --env "OPENAI_API_KEY=$(OPENAI_API_KEY)" \
--env "ORCA_ES_ADDR=$(ORCA_ES_ADDR)" \
--env "ORCA_ES_USER=$(ORCA_ES_USER)" \
--env "ORCA_ES_PASS=$(ORCA_ES_PASS)" \
--mount type=bind,source=/www/orca/static,target=/tmp \ --mount type=bind,source=/www/orca/static,target=/tmp \
--mount type=bind,source=/www/orca/server/dist/prod/log,target=/log \ --mount type=bind,source=/www/orca/server/dist/prod/log,target=/log \
-p 127.0.0.1:21035:80 \ -p 127.0.0.1:21035:80 \

View File

@@ -12,6 +12,7 @@ message EsMetrics {
uint32 reqBytes = 7; uint32 reqBytes = 7;
uint32 rspBytes = 8; uint32 rspBytes = 8;
uint64 ts = 9; uint64 ts = 9;
string hash = 10;
} }
message EsMetricsToken { message EsMetricsToken {

View File

@@ -14,6 +14,7 @@ var (
OpenAIKey = `` OpenAIKey = ``
ESAddr = ``
ESUser = `` ESUser = ``
ESPass = `` ESPass = ``
) )

View File

@@ -17,6 +17,7 @@ func init() {
`STATIC_DIR`: &StaticDir, `STATIC_DIR`: &StaticDir,
`ORCA_WEB`: &WebAddr, `ORCA_WEB`: &WebAddr,
`ORCA_LOG`: &LogDir, `ORCA_LOG`: &LogDir,
`ORCA_ES_ADDR`: &ESAddr,
`ORCA_ES_USER`: &ESUser, `ORCA_ES_USER`: &ESUser,
`ORCA_ES_PASS`: &ESPass, `ORCA_ES_PASS`: &ESPass,
} }

View File

@@ -12,10 +12,15 @@ var theClient *elasticsearch.Client
// Init ... // Init ...
func Init() (err error) { func Init() (err error) {
theClient, err = elasticsearch.NewClient(elasticsearch.Config{ cfg := elasticsearch.Config{
Username: config.ESUser, Username: config.ESUser,
Password: config.ESPass, Password: config.ESPass,
}) }
if config.ESAddr != `` {
cfg.Addresses = []string{config.ESAddr}
}
theClient, err = elasticsearch.NewClient(cfg)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -2,8 +2,9 @@ package es
import ( import (
"bytes" "bytes"
"encoding/json"
"project/pb" "project/pb"
"google.golang.org/protobuf/encoding/protojson"
) )
// Insert ... // Insert ...
@@ -13,7 +14,10 @@ func Insert(d *pb.EsMetrics) {
return return
} }
ab, err := json.Marshal(d) ab, err := protojson.MarshalOptions{
EmitUnpopulated: true,
AllowPartial: true,
}.Marshal(d)
if err != nil { if err != nil {
return return
} }

View File

@@ -1,6 +1,7 @@
package es package es
import ( import (
"crypto/md5"
"fmt" "fmt"
"math/rand" "math/rand"
"project/pb" "project/pb"
@@ -14,6 +15,8 @@ func Test() {
for { for {
ts := zu.MS()
minute := time.Now().Minute() - 30 minute := time.Now().Minute() - 30
if minute < 0 { if minute < 0 {
minute = -minute minute = -minute
@@ -27,20 +30,37 @@ func Test() {
} }
model := modelChoose[rand.Intn(len(modelChoose))] model := modelChoose[rand.Intn(len(modelChoose))]
ipChoose := []string{
`10.0.32.43`,
`10.0.84.231`,
`10.0.84.49`,
`10.2.197.197`,
}
ip := ipChoose[rand.Intn(len(ipChoose))]
keyChoose := []string{
`zhengkai.orca`,
`zhengkai.debug`,
}
key := keyChoose[rand.Intn(len(keyChoose))]
hash := md5.Sum([]byte(fmt.Sprintf(`%s-%s-%s-%d`, model, ip, key, ts)))
d := &pb.EsMetrics{ d := &pb.EsMetrics{
ID: fmt.Sprintf(`chatcmpl-%d`, zu.MS()), ID: fmt.Sprintf(`chatcmpl-%d`, ts),
Token: &pb.EsMetricsToken{ Token: &pb.EsMetricsToken{
Total: uint32(rand.Intn(minute * 10)), Total: uint32(rand.Intn(minute * 10)),
Completion: uint32(rand.Intn(minute * 10)), Completion: uint32(rand.Intn(minute * 10)),
Prompt: uint32(rand.Intn(minute * 10)), Prompt: uint32(rand.Intn(minute * 10)),
}, },
Cached: true, Cached: rand.Intn(2) == 1,
Ip: `127.0.0.1`, Ip: ip,
Model: model, Model: model,
Key: `zhengkai.orca`, Key: key,
ReqBytes: uint32(rand.Intn(minute * 57)), ReqBytes: uint32(rand.Intn(minute * 57)),
RspBytes: uint32(rand.Intn(minute * 21)), RspBytes: uint32(rand.Intn(minute * 21)),
Ts: zu.MS(), Ts: ts,
Hash: fmt.Sprintf(`%x`, hash),
} }
Insert(d) Insert(d)

View File

@@ -37,6 +37,9 @@
}, },
"ts": { "ts": {
"type": "date" "type": "date"
},
"hash": {
"type": "keyword"
} }
} }
} }