master
wubin 5 years ago
parent c68310f90b
commit ead57dfa31

@ -5,8 +5,10 @@ import (
"dsBigData/Utils/CommonUtil" "dsBigData/Utils/CommonUtil"
"dsBigData/Utils/EsUtil" "dsBigData/Utils/EsUtil"
"encoding/json" "encoding/json"
"fmt"
"github.com/olivere/elastic/v7" "github.com/olivere/elastic/v7"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -45,12 +47,18 @@ func GetStudentCountAggsXb(schoolId string) ([]StudentModel.StudentCountXb, erro
var studentCountXbArr []StudentModel.StudentCountXb var studentCountXbArr []StudentModel.StudentCountXb
resCount := gjson.Get(resStr, "xb.buckets.#") resCount := gjson.Get(resStr, "xb.buckets.#")
totalCount := CommonUtil.ConvertInt64ToString(result.TotalHits())
totalCountFloat, _ := strconv.ParseFloat(totalCount, 64)
for i := 0; i < int(resCount.Num); i++ { for i := 0; i < int(resCount.Num); i++ {
var studentCountXb StudentModel.StudentCountXb var studentCountXb StudentModel.StudentCountXb
studentCountXb.XbName = gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".key").Str studentCountXb.XbName = gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".key").Str
studentCountXb.Count = int(gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num) studentCountXb.Count = int(gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num)
percent := gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num / totalCountFloat * 100
percentFloat, _ := strconv.ParseFloat(fmt.Sprintf("%.0f", percent), 64)
percentString := CommonUtil.ConverFloat64ToString(percentFloat)
studentCountXb.Percent = percentString + "%"
studentCountXbArr = append(studentCountXbArr, studentCountXb) studentCountXbArr = append(studentCountXbArr, studentCountXb)
} }
return studentCountXbArr, nil return studentCountXbArr, nil

@ -28,6 +28,7 @@ type StudentCountRxnf struct {
} }
type StudentCountXb struct { type StudentCountXb struct {
XbName string `json:"xb_name"` XbName string `json:"xb_name"`
Count int `json:"count"` Count int `json:"count"`
Percent string `json:"percent"`
} }

@ -5,8 +5,10 @@ import (
"dsBigData/Utils/CommonUtil" "dsBigData/Utils/CommonUtil"
"dsBigData/Utils/EsUtil" "dsBigData/Utils/EsUtil"
"encoding/json" "encoding/json"
"fmt"
"github.com/olivere/elastic/v7" "github.com/olivere/elastic/v7"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -42,14 +44,18 @@ func GetTeacherCountAggsXb(schoolId string) ([]TeacherModel.TeacherCountXb, erro
resByte, err := json.Marshal(result.Aggregations) resByte, err := json.Marshal(result.Aggregations)
resStr := string(resByte) resStr := string(resByte)
totalCount := CommonUtil.ConvertInt64ToString(result.TotalHits())
totalCountFloat, _ := strconv.ParseFloat(totalCount, 64)
var teacherCountXbArr []TeacherModel.TeacherCountXb var teacherCountXbArr []TeacherModel.TeacherCountXb
resCount := gjson.Get(resStr, "xb.buckets.#") resCount := gjson.Get(resStr, "xb.buckets.#")
for i := 0; i < int(resCount.Num); i++ { for i := 0; i < int(resCount.Num); i++ {
var teacherCountXb TeacherModel.TeacherCountXb var teacherCountXb TeacherModel.TeacherCountXb
teacherCountXb.XbName = gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".key").Str teacherCountXb.XbName = gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".key").Str
teacherCountXb.Count = int(gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num) teacherCountXb.Count = int(gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num)
percent := gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num / totalCountFloat * 100
percentFloat, _ := strconv.ParseFloat(fmt.Sprintf("%.0f", percent), 64)
percentString := CommonUtil.ConverFloat64ToString(percentFloat)
teacherCountXb.Percent = percentString + "%"
teacherCountXbArr = append(teacherCountXbArr, teacherCountXb) teacherCountXbArr = append(teacherCountXbArr, teacherCountXb)
} }
return teacherCountXbArr, nil return teacherCountXbArr, nil

@ -34,6 +34,7 @@ type TeacherCountBzlb struct {
} }
type TeacherCountXb struct { type TeacherCountXb struct {
XbName string `json:"xb_name"` XbName string `json:"xb_name"`
Count int `json:"count"` Count int `json:"count"`
Percent string `json:"percent"`
} }

Loading…
Cancel
Save