You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

elk_pagedata.go 12 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package repository
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. //输入elk的json结构begin
  12. type InputInfo struct {
  13. Batch []Batch `json:"batch"`
  14. }
  15. type Fields struct {
  16. Field string `json:"field"`
  17. Format string `json:"format"`
  18. }
  19. type MatchPhrase struct {
  20. Message string `json:"message"`
  21. }
  22. type Should struct {
  23. MatchPhrase MatchPhrase `json:"match_phrase"`
  24. }
  25. type Bool struct {
  26. Should []Should `json:"should"`
  27. MinimumShouldMatch int `json:"minimum_should_match"`
  28. }
  29. type Timestamptest struct {
  30. // Gte time.Time `json:"gte"`
  31. Gte string `json:"gte"`
  32. Lte string `json:"lte"`
  33. Format string `json:"format"`
  34. }
  35. type Range struct {
  36. Timestamptest Timestamptest `json:"@timestamptest"`
  37. }
  38. type FilterMatchPhrase struct {
  39. UserName string `json:"userName.keyword,omitempty"`
  40. ProjectName string `json:"projectName.keyword,omitempty"`
  41. TagName string `json:"tagName.keyword,omitempty"`
  42. }
  43. type Filter struct {
  44. Bool *Bool `json:"bool,omitempty"`
  45. Range *Range `json:"range,omitempty"`
  46. FilterMatchPhrase *FilterMatchPhrase `json:"match_phrase,omitempty"`
  47. }
  48. type MustNotMatchPhrase struct {
  49. ProjectName string `json:"projectName"`
  50. }
  51. type MustNot struct {
  52. MustNotMatchPhrase MustNotMatchPhrase `json:"match_phrase"`
  53. }
  54. type BoolIn struct {
  55. Filter []Filter `json:"filter"`
  56. MustNot []MustNot `json:"must_not"`
  57. }
  58. type Query struct {
  59. BoolIn BoolIn `json:"bool"`
  60. }
  61. type Body struct {
  62. Size int `json:"size"`
  63. Fields []Fields `json:"fields"`
  64. Query Query `json:"query"`
  65. }
  66. type Params struct {
  67. Index string `json:"index"`
  68. Body Body `json:"body"`
  69. }
  70. type Request struct {
  71. Params Params `json:"params"`
  72. }
  73. type Batch struct {
  74. Request Request `json:"request"`
  75. }
  76. //输入elk的json结构end
  77. //elk输出的json结构begin
  78. type Hits struct {
  79. Total int `json:"total"`
  80. }
  81. type RawResponse struct {
  82. Hits Hits `json:"hits"`
  83. }
  84. type Result struct {
  85. RawResponse RawResponse `json:"rawResponse"`
  86. Loaded int `json:"loaded"`
  87. }
  88. type ResultInfo struct {
  89. Id int `json:"id"`
  90. Result Result `json:"result"`
  91. }
  92. //elk输出的json结构end
  93. //发送post请求到elk
  94. func SendReqToElk(jsonStr []byte) (content string) {
  95. ElkBase64Init := setting.ElkUser + ":" + setting.ElkPassword
  96. ElkBase64 := base64.StdEncoding.EncodeToString([]byte(ElkBase64Init))
  97. BasicElkBase64 := "Basic" + " " + ElkBase64
  98. url := setting.ElkUrl
  99. req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  100. req.Header.Set("Content-Type", "application/json")
  101. req.Header.Set("kbn-version", "7.13.2")
  102. req.Header.Set("Authorization", BasicElkBase64)
  103. client := &http.Client{}
  104. resp, err := client.Do(req)
  105. if err != nil {
  106. panic(err)
  107. }
  108. defer resp.Body.Close()
  109. body, _ := ioutil.ReadAll(resp.Body)
  110. return string(body)
  111. }
  112. //处理返回的elk数据,只保留totalView,即访问量;loaded是分片载入次数,用来判断返回的数据是否准确
  113. func GetResultFromElk(resultinfo ResultInfo, jobResult string) (loaded int, totalView int) {
  114. var resultTest ResultInfo
  115. errs := json.Unmarshal([]byte(jobResult), &resultTest)
  116. fmt.Println(errs)
  117. return resultTest.Result.Loaded, resultTest.Result.RawResponse.Hits.Total
  118. }
  119. //初始化传给elk的数据结构,给定用户名和项目名,查询的起止时间,返回初始化后的结构
  120. func ProjectViewInit(User string, Project string, Gte string, Lte string) (projectViewInit InputInfo) {
  121. var inputStruct InputInfo
  122. inputStruct.Batch = make([]Batch, 1)
  123. inputStruct.Batch[0].Request.Params.Index = setting.Index
  124. inputStruct.Batch[0].Request.Params.Body.Size = 0
  125. inputStruct.Batch[0].Request.Params.Body.Fields = make([]Fields, 1)
  126. inputStruct.Batch[0].Request.Params.Body.Fields[0].Field = setting.TimeField
  127. inputStruct.Batch[0].Request.Params.Body.Fields[0].Format = setting.ElkTimeFormat
  128. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter = make([]Filter, 3)
  129. //限定查询时间
  130. var timeRange Range
  131. timeRange.Timestamptest.Gte = Gte
  132. timeRange.Timestamptest.Lte = Lte
  133. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[0].Range = &timeRange
  134. //限定用户
  135. var userName FilterMatchPhrase
  136. userName.UserName = User
  137. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[1].FilterMatchPhrase = &userName
  138. //限定项目
  139. var projectName FilterMatchPhrase
  140. projectName.ProjectName = Project
  141. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[2].FilterMatchPhrase = &projectName
  142. return inputStruct
  143. }
  144. //初始化传给elk的数据结构,给定查询信息和非项目名,查询的起止时间,返回初始化后的结构
  145. func AllProjectViewInit(MessageInfo string, NotProject string, Gte string, Lte string) (allProjectViewInit InputInfo) {
  146. var inputStruct InputInfo
  147. inputStruct.Batch = make([]Batch, 1)
  148. inputStruct.Batch[0].Request.Params.Index = setting.Index
  149. inputStruct.Batch[0].Request.Params.Body.Size = 0
  150. inputStruct.Batch[0].Request.Params.Body.Fields = make([]Fields, 1)
  151. inputStruct.Batch[0].Request.Params.Body.Fields[0].Field = setting.TimeField
  152. inputStruct.Batch[0].Request.Params.Body.Fields[0].Format = setting.ElkTimeFormat
  153. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter = make([]Filter, 2)
  154. //限定message
  155. var bool Bool
  156. bool.Should = make([]Should, 1)
  157. bool.Should[0].MatchPhrase.Message = MessageInfo
  158. bool.MinimumShouldMatch = 1
  159. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[0].Bool = &bool
  160. //限定查询时间
  161. var timeRange Range
  162. timeRange.Timestamptest.Gte = Gte
  163. timeRange.Timestamptest.Lte = Lte
  164. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[1].Range = &timeRange
  165. //限定非项目
  166. // var boolIn BoolIn
  167. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.MustNot = make([]MustNot, 1)
  168. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.MustNot[0].MustNotMatchPhrase.ProjectName = NotProject
  169. return inputStruct
  170. }
  171. //初始化传给elk的数据结构,给定查询信息和tagName,查询的起止时间,返回初始化后的结构
  172. func TagNameInit(MessageInfo string, Tagname string, Gte string, Lte string) (projectViewInit InputInfo) {
  173. var inputStruct InputInfo
  174. inputStruct.Batch = make([]Batch, 1)
  175. inputStruct.Batch[0].Request.Params.Index = setting.Index
  176. inputStruct.Batch[0].Request.Params.Body.Size = 0
  177. inputStruct.Batch[0].Request.Params.Body.Fields = make([]Fields, 1)
  178. inputStruct.Batch[0].Request.Params.Body.Fields[0].Field = setting.TimeField
  179. inputStruct.Batch[0].Request.Params.Body.Fields[0].Format = setting.ElkTimeFormat
  180. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter = make([]Filter, 3)
  181. //限定message
  182. var bool Bool
  183. bool.Should = make([]Should, 1)
  184. bool.Should[0].MatchPhrase.Message = MessageInfo
  185. bool.MinimumShouldMatch = 1
  186. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[0].Bool = &bool
  187. //限定tagName
  188. var tagName FilterMatchPhrase
  189. tagName.TagName = Tagname
  190. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[1].FilterMatchPhrase = &tagName
  191. //限定查询时间
  192. var timeRange Range
  193. timeRange.Timestamptest.Gte = Gte
  194. timeRange.Timestamptest.Lte = Lte
  195. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[2].Range = &timeRange
  196. return inputStruct
  197. }
  198. //向elk发送请求,将获取的结果只保留访问量,输入是初始化后的数据结构,返回访问量
  199. func ViewInfo(viewInfo InputInfo) (totalView int) {
  200. jsons, errs := json.Marshal(viewInfo)
  201. if errs != nil {
  202. fmt.Println("errs:", errs.Error())
  203. }
  204. // fmt.Println("viewInfoInit:",string(jsons))
  205. var jsonStr = []byte(jsons)
  206. var resultInfo ResultInfo
  207. loaded, totalView := GetResultFromElk(resultInfo, SendReqToElk(jsonStr))
  208. time := 0
  209. for {
  210. if loaded == 0 {
  211. loaded_next, totalView := GetResultFromElk(resultInfo, SendReqToElk(jsonStr))
  212. time++
  213. if loaded_next != 0 && time < 100 {
  214. fmt.Println("totalView:", totalView)
  215. return totalView
  216. }
  217. if time > 100 {
  218. break
  219. }
  220. } else {
  221. break
  222. }
  223. }
  224. fmt.Println("loaded:", loaded)
  225. return totalView
  226. }
  227. // @title ProjectView
  228. // @description 获取指定用户和项目的访问量
  229. // @param User string "用户名"
  230. // @param Project string "项目名"
  231. // @param Gte string "起始时间" 如time.Now().AddDate(0, 0, -1).Format(time.RFC3339)
  232. // @param Lte string "结束时间" 如time.Now().Format(time.RFC3339)
  233. // @return totalView int "访问量"
  234. func AppointProjectView(User string, Project string, Gte string, Lte string) (totalView int) {
  235. InitInfo := ProjectViewInit(User, Project, Gte, Lte)
  236. return ViewInfo(InitInfo)
  237. }
  238. //统计项目相关页面的访问量
  239. type ProjectInfo struct {
  240. /* 统计所有项目中该页面的浏览情况,不需要区分项目。以aiforge项目为例 */
  241. //地址:https://git.openi.org.cn/OpenI/aiforge/datasets?type=0
  242. Project_dataset_type_0 int
  243. //地址:https://git.openi.org.cn/OpenI/aiforge/datasets?type=1
  244. Project_dataset_type_1 int
  245. //地址:https://git.openi.org.cn/OpenI/aiforge/issues
  246. Project_issues int
  247. //地址:https://git.openi.org.cn/OpenI/aiforge/labels
  248. Project_labels int
  249. //地址:https://git.openi.org.cn/OpenI/aiforge/milestones
  250. Project_milestones int
  251. //地址:https://git.openi.org.cn/OpenI/aiforge/pulls
  252. Project_pulls int
  253. //地址:https://git.openi.org.cn/OpenI/aiforge/release
  254. Project_release int
  255. //地址:https://git.openi.org.cn/OpenI/aiforge/wiki
  256. Project_wiki int
  257. //地址:https://git.openi.org.cn/OpenI/aiforge/activity
  258. Project_activity int
  259. //地址:https://git.openi.org.cn/OpenI/aiforge/cloudbrain
  260. Project_cloudbrain int
  261. //地址:https://git.openi.org.cn/OpenI/aiforge/modelarts
  262. Project_modelarts int
  263. //地址:https://git.openi.org.cn/OpenI/aiforge/blockchain
  264. Project_blockchain int
  265. //地址:https://git.openi.org.cn/OpenI/aiforge/watchers
  266. Project_watchers int
  267. //地址:https://git.openi.org.cn/OpenI/aiforge/stars
  268. Project_stars int
  269. //地址:https://git.openi.org.cn/OpenI/aiforge/forks
  270. Project_forks int
  271. }
  272. // @title AllProjectView
  273. // @description 获取指定用户和项目的访问量
  274. // @param Gte string "起始时间" 如time.Now().AddDate(0, 0, -1).Format(time.RFC3339)
  275. // @param Lte string "结束时间"
  276. // @return projectInfo ProjectInfo "统计所有项目中页面的浏览情况,不需要区分项目"
  277. func AllProjectView(Gte string, Lte string) (projectInfo ProjectInfo) {
  278. projectInfo.Project_dataset_type_0 = ViewInfo(AllProjectViewInit("/datasets?type=0", "%{[request][2]}", Gte, Lte))
  279. projectInfo.Project_dataset_type_1 = ViewInfo(AllProjectViewInit("/datasets?type=1", "%{[request][2]}", Gte, Lte))
  280. projectInfo.Project_issues = ViewInfo(AllProjectViewInit("/issues HTTP/2.0", "%{[request][2]}", Gte, Lte))
  281. projectInfo.Project_labels = ViewInfo(TagNameInit("/labels HTTP/2.0", "labels", Gte, Lte))
  282. projectInfo.Project_milestones = ViewInfo(AllProjectViewInit("/milestones HTTP/2.0", "%{[request][2]}", Gte, Lte))
  283. projectInfo.Project_pulls = ViewInfo(AllProjectViewInit("/pulls HTTP/2.0", "%{[request][2]}", Gte, Lte))
  284. projectInfo.Project_release = ViewInfo(AllProjectViewInit("/release HTTP/2.0", "%{[request][2]}", Gte, Lte))
  285. projectInfo.Project_wiki = ViewInfo(AllProjectViewInit("/wiki HTTP/2.0", "%{[request][2]}", Gte, Lte))
  286. projectInfo.Project_activity = ViewInfo(AllProjectViewInit("/activity HTTP/2.0", "%{[request][2]}", Gte, Lte))
  287. projectInfo.Project_cloudbrain = ViewInfo(AllProjectViewInit("/cloudbrain HTTP/2.0", "%{[request][2]}", Gte, Lte))
  288. projectInfo.Project_modelarts = ViewInfo(AllProjectViewInit("/modelarts HTTP/2.0", "%{[request][2]}", Gte, Lte))
  289. projectInfo.Project_blockchain = ViewInfo(AllProjectViewInit("/blockchain HTTP/2.0", "%{[request][2]}", Gte, Lte))
  290. projectInfo.Project_watchers = ViewInfo(AllProjectViewInit("/watchers HTTP/2.0", "%{[request][2]}", Gte, Lte))
  291. projectInfo.Project_stars = ViewInfo(AllProjectViewInit("/stars HTTP/2.0", "%{[request][2]}", Gte, Lte))
  292. projectInfo.Project_forks = ViewInfo(AllProjectViewInit("/forks HTTP/2.0", "%{[request][2]}", Gte, Lte))
  293. return projectInfo
  294. }