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 13 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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package repository
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "io/ioutil"
  7. "net/http"
  8. "code.gitea.io/gitea/modules/log"
  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. //处理返回的elk数据,只保留totalView,即访问量;loaded是分片载入次数,用来判断返回的数据是否准确
  94. func GetResultFromElk(resultInfo ResultInfo, jsonStr []byte) (loaded int, totalView int, err error) {
  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. errs := json.Unmarshal([]byte(string(body)), &resultInfo)
  111. log.Info("Get resultJson failed", errs)
  112. return resultInfo.Result.Loaded, resultInfo.Result.RawResponse.Hits.Total, err
  113. }
  114. //初始化传给elk的数据结构,给定用户名和项目名,查询的起止时间,返回初始化后的结构
  115. func ProjectViewInit(User string, Project string, Gte string, Lte string) (projectViewInit InputInfo) {
  116. var inputStruct InputInfo
  117. inputStruct.Batch = make([]Batch, 1)
  118. inputStruct.Batch[0].Request.Params.Index = setting.Index
  119. inputStruct.Batch[0].Request.Params.Body.Size = 0
  120. inputStruct.Batch[0].Request.Params.Body.Fields = make([]Fields, 1)
  121. inputStruct.Batch[0].Request.Params.Body.Fields[0].Field = setting.TimeField
  122. inputStruct.Batch[0].Request.Params.Body.Fields[0].Format = setting.ElkTimeFormat
  123. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter = make([]Filter, 3)
  124. //限定查询时间
  125. var timeRange Range
  126. timeRange.Timestamptest.Gte = Gte
  127. timeRange.Timestamptest.Lte = Lte
  128. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[0].Range = &timeRange
  129. //限定用户
  130. var userName FilterMatchPhrase
  131. userName.UserName = User
  132. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[1].FilterMatchPhrase = &userName
  133. //限定项目
  134. var projectName FilterMatchPhrase
  135. projectName.ProjectName = Project
  136. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[2].FilterMatchPhrase = &projectName
  137. return inputStruct
  138. }
  139. //初始化传给elk的数据结构,给定查询信息和非项目名,查询的起止时间,返回初始化后的结构
  140. func AllProjectViewInit(MessageInfo string, NotProject string, Gte string, Lte string) (allProjectViewInit InputInfo) {
  141. var inputStruct InputInfo
  142. inputStruct.Batch = make([]Batch, 1)
  143. inputStruct.Batch[0].Request.Params.Index = setting.Index
  144. inputStruct.Batch[0].Request.Params.Body.Size = 0
  145. inputStruct.Batch[0].Request.Params.Body.Fields = make([]Fields, 1)
  146. inputStruct.Batch[0].Request.Params.Body.Fields[0].Field = setting.TimeField
  147. inputStruct.Batch[0].Request.Params.Body.Fields[0].Format = setting.ElkTimeFormat
  148. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter = make([]Filter, 2)
  149. //限定message
  150. var bool Bool
  151. bool.Should = make([]Should, 1)
  152. bool.Should[0].MatchPhrase.Message = MessageInfo
  153. bool.MinimumShouldMatch = 1
  154. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[0].Bool = &bool
  155. //限定查询时间
  156. var timeRange Range
  157. timeRange.Timestamptest.Gte = Gte
  158. timeRange.Timestamptest.Lte = Lte
  159. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[1].Range = &timeRange
  160. //限定非项目
  161. // var boolIn BoolIn
  162. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.MustNot = make([]MustNot, 1)
  163. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.MustNot[0].MustNotMatchPhrase.ProjectName = NotProject
  164. return inputStruct
  165. }
  166. //初始化传给elk的数据结构,给定查询信息和tagName,查询的起止时间,返回初始化后的结构
  167. func TagNameInit(MessageInfo string, Tagname string, Gte string, Lte string) (projectViewInit InputInfo) {
  168. var inputStruct InputInfo
  169. inputStruct.Batch = make([]Batch, 1)
  170. inputStruct.Batch[0].Request.Params.Index = setting.Index
  171. inputStruct.Batch[0].Request.Params.Body.Size = 0
  172. inputStruct.Batch[0].Request.Params.Body.Fields = make([]Fields, 1)
  173. inputStruct.Batch[0].Request.Params.Body.Fields[0].Field = setting.TimeField
  174. inputStruct.Batch[0].Request.Params.Body.Fields[0].Format = setting.ElkTimeFormat
  175. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter = make([]Filter, 3)
  176. //限定message
  177. var bool Bool
  178. bool.Should = make([]Should, 1)
  179. bool.Should[0].MatchPhrase.Message = MessageInfo
  180. bool.MinimumShouldMatch = 1
  181. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[0].Bool = &bool
  182. //限定tagName
  183. var tagName FilterMatchPhrase
  184. tagName.TagName = Tagname
  185. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[1].FilterMatchPhrase = &tagName
  186. //限定查询时间
  187. var timeRange Range
  188. timeRange.Timestamptest.Gte = Gte
  189. timeRange.Timestamptest.Lte = Lte
  190. inputStruct.Batch[0].Request.Params.Body.Query.BoolIn.Filter[2].Range = &timeRange
  191. return inputStruct
  192. }
  193. //向elk发送请求,将获取的结果只保留访问量,输入是初始化后的数据结构,返回访问量
  194. func ViewInfo(viewInfo InputInfo) (totalView int, err error) {
  195. jsons, errs := json.Marshal(viewInfo)
  196. if errs != nil {
  197. log.Info("errs:", errs)
  198. }
  199. var jsonStr = []byte(jsons)
  200. var resultInfo ResultInfo
  201. loaded, totalView, err := GetResultFromElk(resultInfo, jsonStr)
  202. time := 0
  203. for {
  204. if loaded == 0 {
  205. loaded_next, totalView, err := GetResultFromElk(resultInfo, jsonStr)
  206. time++
  207. log.Info("time:", time)
  208. if loaded_next != 0 && time < 100 {
  209. return totalView, err
  210. }
  211. if time > 100 {
  212. break
  213. }
  214. } else {
  215. break
  216. }
  217. }
  218. return totalView, err
  219. }
  220. // @title AppointProjectView
  221. // @description 获取指定用户和项目的访问量
  222. // @param User string "用户名"
  223. // @param Project string "项目名"
  224. // @param Gte string "起始时间" 如time.Now().AddDate(0, 0, -1).Format(time.RFC3339)
  225. // @param Lte string "结束时间" 如time.Now().Format(time.RFC3339)
  226. // @return totalView int "访问量"
  227. func AppointProjectView(User string, Project string, Gte string, Lte string) (totalView int, err error) {
  228. ProjectViewInitInfo := ProjectViewInit(User, Project, Gte, Lte)
  229. ProjectTotalView, err := ViewInfo(ProjectViewInitInfo)
  230. return ProjectTotalView, err
  231. }
  232. //统计项目相关页面的访问量
  233. type ProjectInfo struct {
  234. /* 统计所有项目中该页面的浏览情况,不需要区分项目。以aiforge项目为例 */
  235. //地址:https://git.openi.org.cn/OpenI/aiforge/datasets?type=0
  236. Project_dataset_type_0 int
  237. //地址:https://git.openi.org.cn/OpenI/aiforge/datasets?type=1
  238. Project_dataset_type_1 int
  239. //地址:https://git.openi.org.cn/OpenI/aiforge/issues
  240. Project_issues int
  241. //地址:https://git.openi.org.cn/OpenI/aiforge/labels
  242. Project_labels int
  243. //地址:https://git.openi.org.cn/OpenI/aiforge/milestones
  244. Project_milestones int
  245. //地址:https://git.openi.org.cn/OpenI/aiforge/pulls
  246. Project_pulls int
  247. //地址:https://git.openi.org.cn/OpenI/aiforge/release
  248. Project_release int
  249. //地址:https://git.openi.org.cn/OpenI/aiforge/wiki
  250. Project_wiki int
  251. //地址:https://git.openi.org.cn/OpenI/aiforge/activity
  252. Project_activity int
  253. //地址:https://git.openi.org.cn/OpenI/aiforge/cloudbrain
  254. Project_cloudbrain int
  255. //地址:https://git.openi.org.cn/OpenI/aiforge/modelarts
  256. Project_modelarts int
  257. //地址:https://git.openi.org.cn/OpenI/aiforge/blockchain
  258. Project_blockchain int
  259. //地址:https://git.openi.org.cn/OpenI/aiforge/watchers
  260. Project_watchers int
  261. //地址:https://git.openi.org.cn/OpenI/aiforge/stars
  262. Project_stars int
  263. //地址:https://git.openi.org.cn/OpenI/aiforge/forks
  264. Project_forks int
  265. }
  266. type ErrorInfo struct {
  267. Project_dataset_type_0 error
  268. Project_dataset_type_1 error
  269. Project_issues error
  270. Project_labels error
  271. Project_milestones error
  272. Project_pulls error
  273. Project_release error
  274. Project_wiki error
  275. Project_activity error
  276. Project_cloudbrain error
  277. Project_modelarts error
  278. Project_blockchain error
  279. Project_watchers error
  280. Project_stars error
  281. Project_forks error
  282. }
  283. // @title AllProjectView
  284. // @description 获取指定用户和项目的访问量
  285. // @param Gte string "起始时间" 如time.Now().AddDate(0, 0, -1).Format(time.RFC3339)
  286. // @param Lte string "结束时间"
  287. // @return projectInfo ProjectInfo "统计所有项目中页面的浏览情况,不需要区分项目"
  288. func AllProjectView(Gte string, Lte string) (projectViewInfo ProjectInfo, errorInfo ErrorInfo) {
  289. projectViewInfo.Project_dataset_type_0, errorInfo.Project_dataset_type_0 = ViewInfo(AllProjectViewInit("/datasets?type=0", "%{[request][2]}", Gte, Lte))
  290. projectViewInfo.Project_dataset_type_1, errorInfo.Project_dataset_type_1 = ViewInfo(AllProjectViewInit("/datasets?type=1", "%{[request][2]}", Gte, Lte))
  291. projectViewInfo.Project_issues, errorInfo.Project_issues = ViewInfo(AllProjectViewInit("/issues HTTP/2.0", "%{[request][2]}", Gte, Lte))
  292. projectViewInfo.Project_labels, errorInfo.Project_labels = ViewInfo(TagNameInit("/labels HTTP/2.0", "labels", Gte, Lte))
  293. projectViewInfo.Project_milestones, errorInfo.Project_milestones = ViewInfo(AllProjectViewInit("/milestones HTTP/2.0", "%{[request][2]}", Gte, Lte))
  294. projectViewInfo.Project_pulls, errorInfo.Project_pulls = ViewInfo(AllProjectViewInit("/pulls HTTP/2.0", "%{[request][2]}", Gte, Lte))
  295. projectViewInfo.Project_release, errorInfo.Project_release = ViewInfo(AllProjectViewInit("/release HTTP/2.0", "%{[request][2]}", Gte, Lte))
  296. projectViewInfo.Project_wiki, errorInfo.Project_wiki = ViewInfo(AllProjectViewInit("/wiki HTTP/2.0", "%{[request][2]}", Gte, Lte))
  297. projectViewInfo.Project_activity, errorInfo.Project_activity = ViewInfo(AllProjectViewInit("/activity HTTP/2.0", "%{[request][2]}", Gte, Lte))
  298. projectViewInfo.Project_cloudbrain, errorInfo.Project_cloudbrain = ViewInfo(AllProjectViewInit("/cloudbrain HTTP/2.0", "%{[request][2]}", Gte, Lte))
  299. projectViewInfo.Project_modelarts, errorInfo.Project_modelarts = ViewInfo(AllProjectViewInit("/modelarts HTTP/2.0", "%{[request][2]}", Gte, Lte))
  300. projectViewInfo.Project_blockchain, errorInfo.Project_blockchain = ViewInfo(AllProjectViewInit("/blockchain HTTP/2.0", "%{[request][2]}", Gte, Lte))
  301. projectViewInfo.Project_watchers, errorInfo.Project_watchers = ViewInfo(AllProjectViewInit("/watchers HTTP/2.0", "%{[request][2]}", Gte, Lte))
  302. projectViewInfo.Project_stars, errorInfo.Project_stars = ViewInfo(AllProjectViewInit("/stars HTTP/2.0", "%{[request][2]}", Gte, Lte))
  303. projectViewInfo.Project_forks, errorInfo.Project_forks = ViewInfo(AllProjectViewInit("/forks HTTP/2.0", "%{[request][2]}", Gte, Lte))
  304. return projectViewInfo, errorInfo
  305. }