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