mirror of
https://github.com/zhengkai/orca.git
synced 2026-02-04 15:02:26 +08:00
es index
This commit is contained in:
51
server/src/es/index.go
Normal file
51
server/src/es/index.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package es
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"project/config"
|
||||
"project/zj"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "embed" //
|
||||
|
||||
"github.com/zhengkai/zu"
|
||||
)
|
||||
|
||||
//go:embed tpl/mapping.json
|
||||
var indexMapping string
|
||||
|
||||
func indexName(ts uint32) string {
|
||||
|
||||
index := `orca-metrics`
|
||||
if !config.Prod {
|
||||
index = `dev-` + index
|
||||
}
|
||||
|
||||
index = fmt.Sprintf(`%s-%s`, index, time.Unix(int64(ts), 0).Format(`2006-01-02`))
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
func createIndex() {
|
||||
|
||||
ts := zu.TS()
|
||||
|
||||
zj.J(`index name:`, indexName(ts))
|
||||
|
||||
mapping(ts)
|
||||
mapping(ts + 86400)
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Hour * 3)
|
||||
mapping(zu.TS() + 86400)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func mapping(ts uint32) {
|
||||
theClient.Indices.Create(
|
||||
indexName(ts),
|
||||
theClient.Indices.Create.WithBody(strings.NewReader(indexMapping)),
|
||||
)
|
||||
}
|
||||
@@ -28,5 +28,7 @@ func Init() (err error) {
|
||||
|
||||
zj.J(`elasticsearch`, res.String())
|
||||
|
||||
createIndex()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -22,7 +22,9 @@ func Insert(d *pb.EsMetrics) {
|
||||
|
||||
// theClient.Create(`orca-metrics`, d.ID, bytes.NewReader(ab))
|
||||
|
||||
re, err := theClient.Index(`orca-metrics`, bytes.NewReader(ab))
|
||||
index := indexName(uint32(d.Ts / 1000))
|
||||
|
||||
re, err := theClient.Index(index, bytes.NewReader(ab))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
43
server/src/es/tpl/mapping.json
Normal file
43
server/src/es/tpl/mapping.json
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"mappings": {
|
||||
"properties": {
|
||||
"ID": {
|
||||
"type": "keyword"
|
||||
},
|
||||
"token": {
|
||||
"properties": {
|
||||
"total": {
|
||||
"type": "long"
|
||||
},
|
||||
"completion": {
|
||||
"type": "long"
|
||||
},
|
||||
"prompt": {
|
||||
"type": "long"
|
||||
}
|
||||
}
|
||||
},
|
||||
"cached": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"ip": {
|
||||
"type": "ip"
|
||||
},
|
||||
"model": {
|
||||
"type": "keyword"
|
||||
},
|
||||
"key": {
|
||||
"type": "keyword"
|
||||
},
|
||||
"reqBytes": {
|
||||
"type": "long"
|
||||
},
|
||||
"rspBytes": {
|
||||
"type": "long"
|
||||
},
|
||||
"ts": {
|
||||
"type": "date"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user