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