|
@@ -0,0 +1,180 @@ |
|
|
|
|
|
package models |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
|
"bytes" |
|
|
|
|
|
"encoding/json" |
|
|
|
|
|
"fmt" |
|
|
|
|
|
"io/ioutil" |
|
|
|
|
|
"net/http" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
//输出的json结构begin |
|
|
|
|
|
type Hits struct { |
|
|
|
|
|
Total int `json:"total"` |
|
|
|
|
|
} |
|
|
|
|
|
type RawResponse struct { |
|
|
|
|
|
Hits Hits `json:"hits"` |
|
|
|
|
|
} |
|
|
|
|
|
type Result struct { |
|
|
|
|
|
RawResponse RawResponse `json:"rawResponse"` |
|
|
|
|
|
Loaded int `json:"loaded"` |
|
|
|
|
|
} |
|
|
|
|
|
type ResultInfo struct { |
|
|
|
|
|
Id int `json:"id"` |
|
|
|
|
|
Result Result `json:"result"` |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//输出的json结构end |
|
|
|
|
|
|
|
|
|
|
|
// //输入的json结构end |
|
|
|
|
|
type InputInfo struct { |
|
|
|
|
|
Batch []Batch `json:"batch"` |
|
|
|
|
|
} |
|
|
|
|
|
type Fields struct { |
|
|
|
|
|
Field string `json:"field"` |
|
|
|
|
|
Format string `json:"format"` |
|
|
|
|
|
} |
|
|
|
|
|
type MatchPhrase struct { |
|
|
|
|
|
Message string `json:"message"` |
|
|
|
|
|
} |
|
|
|
|
|
type Should struct { |
|
|
|
|
|
MatchPhrase MatchPhrase `json:"match_phrase"` |
|
|
|
|
|
} |
|
|
|
|
|
type Bool struct { |
|
|
|
|
|
Should []Should `json:"should"` |
|
|
|
|
|
MinimumShouldMatch int `json:"minimum_should_match"` |
|
|
|
|
|
} |
|
|
|
|
|
type Timestamptest struct { |
|
|
|
|
|
// Gte time.Time `json:"gte"` |
|
|
|
|
|
Gte string `json:"gte"` |
|
|
|
|
|
Lte string `json:"lte"` |
|
|
|
|
|
Format string `json:"format"` |
|
|
|
|
|
} |
|
|
|
|
|
type Range struct { |
|
|
|
|
|
Timestamptest Timestamptest `json:"@timestamptest"` |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type FilterMatchPhrase struct { |
|
|
|
|
|
UserName string `json:"userName.keyword,omitempty"` |
|
|
|
|
|
ProjectName string `json:"projectName.keyword,omitempty"` |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type Filter struct { |
|
|
|
|
|
Bool *Bool `json:"bool,omitempty"` |
|
|
|
|
|
Range *Range `json:"range,omitempty"` |
|
|
|
|
|
FilterMatchPhrase *FilterMatchPhrase `json:"match_phrase,omitempty"` |
|
|
|
|
|
} |
|
|
|
|
|
type MatchPhraseIn struct { |
|
|
|
|
|
ProjectName string `json:"projectName"` |
|
|
|
|
|
} |
|
|
|
|
|
type MustNot struct { |
|
|
|
|
|
MatchPhraseIn MatchPhraseIn `json:"match_phrase"` |
|
|
|
|
|
} |
|
|
|
|
|
type BoolIn struct { |
|
|
|
|
|
Filter []Filter `json:"filter"` |
|
|
|
|
|
MustNot []MustNot `json:"must_not"` |
|
|
|
|
|
} |
|
|
|
|
|
type Query struct { |
|
|
|
|
|
BoolIn BoolIn `json:"bool"` |
|
|
|
|
|
} |
|
|
|
|
|
type Body struct { |
|
|
|
|
|
Size int `json:"size"` |
|
|
|
|
|
Fields []Fields `json:"fields"` |
|
|
|
|
|
Query Query `json:"query"` |
|
|
|
|
|
} |
|
|
|
|
|
type Params struct { |
|
|
|
|
|
Index string `json:"index"` |
|
|
|
|
|
Body Body `json:"body"` |
|
|
|
|
|
} |
|
|
|
|
|
type Request struct { |
|
|
|
|
|
Params Params `json:"params"` |
|
|
|
|
|
} |
|
|
|
|
|
type Batch struct { |
|
|
|
|
|
Request Request `json:"request"` |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func SendData(jsonStr []byte) (content string) { |
|
|
|
|
|
url := "http://192.168.207.35:5601/internal/bsearch" |
|
|
|
|
|
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) |
|
|
|
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
|
|
|
req.Header.Set("kbn-version", "7.13.2") |
|
|
|
|
|
req.Header.Set("Authorization", "Basic cWl6aGk6UGNsMjAyMA==") |
|
|
|
|
|
client := &http.Client{} |
|
|
|
|
|
resp, err := client.Do(req) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
panic(err) |
|
|
|
|
|
} |
|
|
|
|
|
defer resp.Body.Close() |
|
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body) |
|
|
|
|
|
return string(body) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getdata(resultinfo ResultInfo, jobResult string) (value1 int, value2 int) { |
|
|
|
|
|
var resultTest ResultInfo |
|
|
|
|
|
errs := json.Unmarshal([]byte(jobResult), &resultTest) |
|
|
|
|
|
fmt.Println(errs) |
|
|
|
|
|
return resultTest.Result.Loaded, resultTest.Result.RawResponse.Hits.Total |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func inputStructInit(User string, Project string, Gte string, Lte string) (inputStructInit InputInfo) { |
|
|
|
|
|
var inputStruct InputInfo |
|
|
|
|
|
inputStruct.Batch = make([]Batch, 1) |
|
|
|
|
|
inputStruct.Batch[0].Request.Params.Index = "filebeat-7.3.2*" |
|
|
|
|
|
inputStruct.Batch[0].Request.Params.Body.Size = 0 |
|
|
|
|
|
inputStruct.Batch[0].Request.Params.Body.Fields = make([]Fields, 1) |
|
|
|
|
|
inputStruct.Batch[0].Request.Params.Body.Fields[0].Field = "@timestamptest" |
|
|
|
|
|
inputStruct.Batch[0].Request.Params.Body.Fields[0].Format = "date_time" |
|
|
|
|
|
inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter = make([]Filter, 3) |
|
|
|
|
|
|
|
|
|
|
|
var timeRange Range |
|
|
|
|
|
timeRange.Timestamptest.Gte = Gte |
|
|
|
|
|
timeRange.Timestamptest.Lte = Lte |
|
|
|
|
|
timeRange.Timestamptest.Format = "strict_date_optional_time" |
|
|
|
|
|
inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[0].Range = &timeRange |
|
|
|
|
|
|
|
|
|
|
|
var userName FilterMatchPhrase |
|
|
|
|
|
userName.UserName = User |
|
|
|
|
|
inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[1].FilterMatchPhrase = &userName |
|
|
|
|
|
|
|
|
|
|
|
var projectName FilterMatchPhrase |
|
|
|
|
|
projectName.ProjectName = Project |
|
|
|
|
|
inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[2].FilterMatchPhrase = &projectName |
|
|
|
|
|
|
|
|
|
|
|
return inputStruct |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// @title ProjectViewData |
|
|
|
|
|
// @description 获取指定用户和项目的访问量 |
|
|
|
|
|
// @param User string "用户名" |
|
|
|
|
|
// @param Project string "项目名" |
|
|
|
|
|
// @param Gte string "起始时间" 如 time.Now().AddDate(0, 0, -1).Format(time.RFC3339) |
|
|
|
|
|
// @param Lte string "结束时间" 如 time.Now().Format(time.RFC3339) |
|
|
|
|
|
// @return value int "访问量" |
|
|
|
|
|
func ProjectViewData(User string, Project string, Gte string, Lte string) (value int) { |
|
|
|
|
|
InitInfo := inputStructInit(User, Project, Gte, Lte) |
|
|
|
|
|
jsons, errs := json.Marshal(InitInfo) |
|
|
|
|
|
if errs != nil { |
|
|
|
|
|
fmt.Println("errs:", errs.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
var jsonStr = []byte(jsons) |
|
|
|
|
|
var resultTest ResultInfo |
|
|
|
|
|
loaded, total := getdata(resultTest, SendData(jsonStr)) |
|
|
|
|
|
time := 0 |
|
|
|
|
|
for { |
|
|
|
|
|
if loaded == 0 { |
|
|
|
|
|
value1, total := getdata(resultTest, SendData(jsonStr)) |
|
|
|
|
|
time++ |
|
|
|
|
|
if value1 != 0 && time < 100 { |
|
|
|
|
|
fmt.Println("total:", total) |
|
|
|
|
|
return total |
|
|
|
|
|
} |
|
|
|
|
|
if time > 100 { |
|
|
|
|
|
break |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
break |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
fmt.Println("loaded:", loaded) |
|
|
|
|
|
return total |
|
|
|
|
|
} |