diff --git a/modules/repository/elk_pagedata.go b/modules/repository/elk_pagedata.go index fa4b61beb..f74324664 100644 --- a/modules/repository/elk_pagedata.go +++ b/modules/repository/elk_pagedata.go @@ -6,6 +6,8 @@ import ( "fmt" "io/ioutil" "net/http" + + "code.gitea.io/gitea/modules/setting" ) //输入elk的json结构begin @@ -98,7 +100,7 @@ type ResultInfo struct { //发送post请求到elk func SendReqToElk(jsonStr []byte) (content string) { - url := "http://192.168.207.35:5601/internal/bsearch" + url := setting.ElkUrl req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) req.Header.Set("Content-Type", "application/json") req.Header.Set("kbn-version", "7.13.2") @@ -126,17 +128,16 @@ func GetResultFromElk(resultinfo ResultInfo, jobResult string) (loaded int, tota func ProjectViewInit(User string, Project string, Gte string, Lte string) (projectViewInit InputInfo) { var inputStruct InputInfo inputStruct.Batch = make([]Batch, 1) - inputStruct.Batch[0].Request.Params.Index = "filebeat-7.3.2*" + inputStruct.Batch[0].Request.Params.Index = setting.Index 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.Fields[0].Field = setting.TimeField + inputStruct.Batch[0].Request.Params.Body.Fields[0].Format = setting.ElkTimeFormat 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 @@ -153,11 +154,11 @@ func ProjectViewInit(User string, Project string, Gte string, Lte string) (proje func AllProjectViewInit(MessageInfo string, NotProject string, Gte string, Lte string) (allProjectViewInit InputInfo) { var inputStruct InputInfo inputStruct.Batch = make([]Batch, 1) - inputStruct.Batch[0].Request.Params.Index = "filebeat-7.3.2*" + inputStruct.Batch[0].Request.Params.Index = setting.Index 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.Fields[0].Field = setting.TimeField + inputStruct.Batch[0].Request.Params.Body.Fields[0].Format = setting.ElkTimeFormat inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter = make([]Filter, 2) //限定message var bool Bool @@ -169,7 +170,6 @@ func AllProjectViewInit(MessageInfo string, NotProject string, Gte string, Lte s 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[1].Range = &timeRange //限定非项目 // var boolIn BoolIn @@ -182,11 +182,11 @@ func AllProjectViewInit(MessageInfo string, NotProject string, Gte string, Lte s func TagNameInit(MessageInfo string, Tagname string, Gte string, Lte string) (projectViewInit InputInfo) { var inputStruct InputInfo inputStruct.Batch = make([]Batch, 1) - inputStruct.Batch[0].Request.Params.Index = "filebeat-7.3.2*" + inputStruct.Batch[0].Request.Params.Index = setting.Index 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.Fields[0].Field = setting.TimeField + inputStruct.Batch[0].Request.Params.Body.Fields[0].Format = setting.ElkTimeFormat inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter = make([]Filter, 3) //限定message var bool Bool @@ -202,7 +202,6 @@ func TagNameInit(MessageInfo string, Tagname string, Gte string, Lte string) (pr 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[2].Range = &timeRange return inputStruct } @@ -242,7 +241,7 @@ func ViewInfo(viewInfo InputInfo) (totalView int) { // @param User string "用户名" // @param Project string "项目名" // @param Gte string "起始时间" 如time.Now().AddDate(0, 0, -1).Format(time.RFC3339) -// @param Lte string "结束时间" +// @param Lte string "结束时间" 如time.Now().Format(time.RFC3339) // @return totalView int "访问量" func AppointProjectView(User string, Project string, Gte string, Lte string) (totalView int) { InitInfo := ProjectViewInit(User, Project, Gte, Lte) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 0fbc9f909..1830deff4 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -482,6 +482,14 @@ var ( PoolInfos string Flavor string FlavorInfos string + + //obs config + ElkUrl string + ElkUser string + ElkPassword string + Index string + TimeField string + ElkTimeFormat string ) // DateLang transforms standard language locale name to corresponding value in datetime plugin. @@ -1201,6 +1209,14 @@ func NewContext() { PoolInfos = sec.Key("POOL_INFOS").MustString("") Flavor = sec.Key("FLAVOR").MustString("") FlavorInfos = sec.Key("FLAVOR_INFOS").MustString("") + + sec = Cfg.Section("elk") + ElkUrl = sec.Key("ELKURL").MustString("http://192.168.207.35:5601/internal/bsearch") + ElkUser = sec.Key("ELKUSER").MustString("Qizhi") + ElkPassword = sec.Key("ELKPASSWORD").MustString("Pcl2020") + Index = sec.Key("INDEX").MustString("filebeat-7.3.2*") + TimeField = sec.Key("TIMEFIELD").MustString(" @timestamptest") + ElkTimeFormat = sec.Key("ELKTIMEFORMAT").MustString("date_time") } func loadInternalToken(sec *ini.Section) string {