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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. //处理返回的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. fmt.Println(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. fmt.Println("errs:", errs.Error())
  198. }
  199. fmt.Println("viewInfoInit:", string(jsons))
  200. var jsonStr = []byte(jsons)
  201. var resultInfo ResultInfo
  202. loaded, totalView, err := GetResultFromElk(resultInfo, jsonStr)
  203. time := 0
  204. for {
  205. if loaded == 0 {
  206. loaded_next, totalView, err := GetResultFromElk(resultInfo, jsonStr)
  207. time++
  208. fmt.Println("time:", time)
  209. if loaded_next != 0 && time < 100 {
  210. return totalView, err
  211. }
  212. if time > 100 {
  213. break
  214. }
  215. } else {
  216. break
  217. }
  218. }
  219. return totalView, err
  220. }
  221. // @title AppointProjectView
  222. // @description 获取指定用户和项目的访问量
  223. // @param User string "用户名"
  224. // @param Project string "项目名"
  225. // @param Gte string "起始时间" 如time.Now().AddDate(0, 0, -1).Format(time.RFC3339)
  226. // @param Lte string "结束时间" 如time.Now().Format(time.RFC3339)
  227. // @return totalView int "访问量"
  228. func AppointProjectView(User string, Project string, Gte string, Lte string) (totalView int, err error) {
  229. ProjectViewInitInfo := ProjectViewInit(User, Project, Gte, Lte)
  230. ProjectTotalView, err := ViewInfo(ProjectViewInitInfo)
  231. return ProjectTotalView, err
  232. }
  233. //统计项目相关页面的访问量
  234. type ProjectInfo struct {
  235. /* 统计所有项目中该页面的浏览情况,不需要区分项目。以aiforge项目为例 */
  236. //地址:https://git.openi.org.cn/OpenI/aiforge/datasets?type=0
  237. Project_dataset_type_0 int
  238. //地址:https://git.openi.org.cn/OpenI/aiforge/datasets?type=1
  239. Project_dataset_type_1 int
  240. //地址:https://git.openi.org.cn/OpenI/aiforge/issues
  241. Project_issues int
  242. //地址:https://git.openi.org.cn/OpenI/aiforge/labels
  243. Project_labels int
  244. //地址:https://git.openi.org.cn/OpenI/aiforge/milestones
  245. Project_milestones int
  246. //地址:https://git.openi.org.cn/OpenI/aiforge/pulls
  247. Project_pulls int
  248. //地址:https://git.openi.org.cn/OpenI/aiforge/release
  249. Project_release int
  250. //地址:https://git.openi.org.cn/OpenI/aiforge/wiki
  251. Project_wiki int
  252. //地址:https://git.openi.org.cn/OpenI/aiforge/activity
  253. Project_activity int
  254. //地址:https://git.openi.org.cn/OpenI/aiforge/cloudbrain
  255. Project_cloudbrain int
  256. //地址:https://git.openi.org.cn/OpenI/aiforge/modelarts
  257. Project_modelarts int
  258. //地址:https://git.openi.org.cn/OpenI/aiforge/blockchain
  259. Project_blockchain int
  260. //地址:https://git.openi.org.cn/OpenI/aiforge/watchers
  261. Project_watchers int
  262. //地址:https://git.openi.org.cn/OpenI/aiforge/stars
  263. Project_stars int
  264. //地址:https://git.openi.org.cn/OpenI/aiforge/forks
  265. Project_forks int
  266. }
  267. type ErrorInfo struct {
  268. Project_dataset_type_0 error
  269. Project_dataset_type_1 error
  270. Project_issues error
  271. Project_labels error
  272. Project_milestones error
  273. Project_pulls error
  274. Project_release error
  275. Project_wiki error
  276. Project_activity error
  277. Project_cloudbrain error
  278. Project_modelarts error
  279. Project_blockchain error
  280. Project_watchers error
  281. Project_stars error
  282. Project_forks error
  283. }
  284. // @title AllProjectView
  285. // @description 获取指定用户和项目的访问量
  286. // @param Gte string "起始时间" 如time.Now().AddDate(0, 0, -1).Format(time.RFC3339)
  287. // @param Lte string "结束时间"
  288. // @return projectInfo ProjectInfo "统计所有项目中页面的浏览情况,不需要区分项目"
  289. func AllProjectView(Gte string, Lte string) (projectViewInfo ProjectInfo, errorInfo ErrorInfo) {
  290. projectViewInfo.Project_dataset_type_0, errorInfo.Project_dataset_type_0 = ViewInfo(AllProjectViewInit("/datasets?type=0", "%{[request][2]}", Gte, Lte))
  291. projectViewInfo.Project_dataset_type_1, errorInfo.Project_dataset_type_1 = ViewInfo(AllProjectViewInit("/datasets?type=1", "%{[request][2]}", Gte, Lte))
  292. projectViewInfo.Project_issues, errorInfo.Project_issues = ViewInfo(AllProjectViewInit("/issues HTTP/2.0", "%{[request][2]}", Gte, Lte))
  293. projectViewInfo.Project_labels, errorInfo.Project_labels = ViewInfo(TagNameInit("/labels HTTP/2.0", "labels", Gte, Lte))
  294. projectViewInfo.Project_milestones, errorInfo.Project_milestones = ViewInfo(AllProjectViewInit("/milestones HTTP/2.0", "%{[request][2]}", Gte, Lte))
  295. projectViewInfo.Project_pulls, errorInfo.Project_pulls = ViewInfo(AllProjectViewInit("/pulls HTTP/2.0", "%{[request][2]}", Gte, Lte))
  296. projectViewInfo.Project_release, errorInfo.Project_release = ViewInfo(AllProjectViewInit("/release HTTP/2.0", "%{[request][2]}", Gte, Lte))
  297. projectViewInfo.Project_wiki, errorInfo.Project_wiki = ViewInfo(AllProjectViewInit("/wiki HTTP/2.0", "%{[request][2]}", Gte, Lte))
  298. projectViewInfo.Project_activity, errorInfo.Project_activity = ViewInfo(AllProjectViewInit("/activity HTTP/2.0", "%{[request][2]}", Gte, Lte))
  299. projectViewInfo.Project_cloudbrain, errorInfo.Project_cloudbrain = ViewInfo(AllProjectViewInit("/cloudbrain HTTP/2.0", "%{[request][2]}", Gte, Lte))
  300. projectViewInfo.Project_modelarts, errorInfo.Project_modelarts = ViewInfo(AllProjectViewInit("/modelarts HTTP/2.0", "%{[request][2]}", Gte, Lte))
  301. projectViewInfo.Project_blockchain, errorInfo.Project_blockchain = ViewInfo(AllProjectViewInit("/blockchain HTTP/2.0", "%{[request][2]}", Gte, Lte))
  302. projectViewInfo.Project_watchers, errorInfo.Project_watchers = ViewInfo(AllProjectViewInit("/watchers HTTP/2.0", "%{[request][2]}", Gte, Lte))
  303. projectViewInfo.Project_stars, errorInfo.Project_stars = ViewInfo(AllProjectViewInit("/stars HTTP/2.0", "%{[request][2]}", Gte, Lte))
  304. projectViewInfo.Project_forks, errorInfo.Project_forks = ViewInfo(AllProjectViewInit("/forks HTTP/2.0", "%{[request][2]}", Gte, Lte))
  305. return projectViewInfo, errorInfo
  306. }