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.

home.js 19 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. var token;
  2. if(isEmpty(token)){
  3. var meta = $("meta[name=_uid]");
  4. if(!isEmpty(meta)){
  5. token = meta.attr("content");
  6. }
  7. }
  8. var swiperNewMessage = new Swiper(".newslist", {
  9. direction: "vertical",
  10. slidesPerView: 9,
  11. loop: true,
  12. autoplay: {
  13. delay: 2500,
  14. disableOnInteraction: false,
  15. },
  16. });
  17. var swiperEvent = new Swiper(".event-list", {
  18. slidesPerView: 3,
  19. spaceBetween: 30,
  20. pagination: {
  21. el: ".swiper-pagination",
  22. clickable: true,
  23. },
  24. autoplay: {
  25. delay: 2500,
  26. disableOnInteraction: false,
  27. },
  28. });
  29. var swiperRepo = new Swiper(".homepro-list", {
  30. slidesPerView: 1,
  31. slidesPerColumn: 2,
  32. slidesPerColumnFill:'row',
  33. spaceBetween: 30,
  34. pagination: {
  35. el: ".swiper-pagination",
  36. clickable: true,
  37. },
  38. autoplay: {
  39. delay: 2500,
  40. disableOnInteraction: false,
  41. },
  42. breakpoints: {
  43. 768: {
  44. slidesPerView: 2,
  45. },
  46. 1024: {
  47. slidesPerView: 3,
  48. },
  49. },
  50. });
  51. var swiperOrg = new Swiper(".homeorg-list", {
  52. slidesPerView: 1,
  53. slidesPerColumn: 4,
  54. slidesPerColumnFill:'row',
  55. spaceBetween: 15,
  56. pagination: {
  57. el: ".swiper-pagination",
  58. clickable: true,
  59. },
  60. autoplay: {
  61. delay: 4500,
  62. disableOnInteraction: false,
  63. },
  64. breakpoints: {
  65. 768: {
  66. slidesPerView: 2,
  67. },
  68. 1024: {
  69. slidesPerView: 3,
  70. },
  71. },
  72. });
  73. var maxSize = 20;
  74. var html =document.documentElement;
  75. var lang = html.attributes["lang"]
  76. var isZh = true;
  77. if(lang != null && lang.nodeValue =="en-US" ){
  78. isZh=false;
  79. }
  80. document.onreadystatechange = function () {
  81. queryRecommendData();
  82. var output = document.getElementById("newmessage");
  83. var url = "ws://" + document.location.host + "/action/notification";
  84. if(document.location.host == "git.openi.org.cn" || document.URL.startsWith("https")){
  85. url = "wss://" + document.location.host + "/action/notification"
  86. }
  87. var socket = new WebSocket(url);
  88. socket.onopen = function () {
  89. messageQueue = [];
  90. console.log("message has connected.");
  91. };
  92. socket.onmessage = function (e) {
  93. var data =JSON.parse(e.data)
  94. var html = "";
  95. if (data != null){
  96. if(messageQueue.length > maxSize){
  97. delete messageQueue[0];
  98. }else{
  99. messageQueue.push(data);
  100. }
  101. var currentTime = new Date().getTime();
  102. for(var i = 0; i < messageQueue.length; i++){
  103. var record = messageQueue[i];
  104. var actionName = getAction(record.OpType,isZh);
  105. if(record.ActUser == null){
  106. console.log("receive action type=" + record.OpType + " name=" + actionName + " but user is null.");
  107. continue;
  108. }
  109. if(record.OpType == "24"){
  110. if(record.Content.indexOf("true") != -1){
  111. continue;
  112. }
  113. }
  114. var recordPrefix = getMsg(record);
  115. if(record.OpType == "6" || record.OpType == "10" || record.OpType == "12" || record.OpType == "13"){
  116. html += recordPrefix + actionName;
  117. html += " <a href=\"" + getIssueLink(record) + "\" rel=\"nofollow\">" + getIssueText(record) + "</a>"
  118. }
  119. else if(record.OpType == "7" || record.OpType == "11" || record.OpType == "14" || record.OpType == "15" || record.OpType == "22"
  120. || record.OpType == "23"){
  121. html += recordPrefix + actionName;
  122. html += " <a href=\"" + getPRLink(record) + "\" rel=\"nofollow\">" + getPRText(record) + "</a>"
  123. }
  124. else if(record.OpType == "1"){
  125. html += recordPrefix + actionName;
  126. html += " <a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" +getRepotext(record) + "</a>"
  127. }
  128. else if(record.OpType == "5"){
  129. branch = "<a href=\"" + getRepoLink(record) + "/src/branch/" + encodeURI(record.RefName) + "\" rel=\"nofollow\">" + record.RefName + "</a>"
  130. actionName = actionName.replace("{branch}",branch);
  131. html += recordPrefix + actionName;
  132. html += " <a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepotext(record) + "</a>"
  133. }else if(record.OpType == "9"){
  134. branch = "<a href=\"" + getRepoLink(record) + "/src/tag/" + encodeURI(record.RefName) + "\" rel=\"nofollow\">" + record.RefName + "</a>"
  135. actionName = actionName.replace("{branch}",branch);
  136. html += recordPrefix + actionName;
  137. html += " <a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepotext(record) + "</a>"
  138. }
  139. else if(record.OpType == "17"){
  140. actionName = actionName.replace("{deleteBranchName}",record.RefName);
  141. var repoLink = "<a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepotext(record) + "</a>"
  142. actionName = actionName.replace("{repoName}",repoLink);
  143. html += recordPrefix + actionName;
  144. }
  145. else if(record.OpType == "2"){
  146. actionName = actionName.replace("{oldRepoName}",record.Content);
  147. html += recordPrefix + actionName;
  148. html += " <a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepotext(record) + "</a>"
  149. }
  150. else if(record.OpType == "24" || record.OpType == "26" || record.OpType == "27" || record.OpType == "28" || record.OpType == "30" || record.OpType == "31" || record.OpType == "32" || record.OpType == "33"){
  151. html += recordPrefix + actionName;
  152. html += " <a href=\"" + getTaskLink(record) + "\" rel=\"nofollow\">" + record.RefName + "</a>"
  153. }
  154. else if(record.OpType == "25" || record.OpType == "29"){
  155. html += recordPrefix + actionName;
  156. html += " <a href=\"" + getTaskLink(record) + "\" rel=\"nofollow\">" + record.RefName + "</a>"
  157. }
  158. else{
  159. continue;
  160. }
  161. if(record.Repo != null){
  162. var time = getTime(record.CreatedUnix,currentTime);
  163. html += " " + time;
  164. }
  165. html += "</div>";
  166. html += "</div>";
  167. }
  168. }
  169. output.innerHTML = html;
  170. swiperNewMessage.updateSlides();
  171. swiperNewMessage.updateProgress();
  172. };
  173. }
  174. function getTaskLink(record){
  175. var re = getRepoLink(record);
  176. if(record.OpType == 24){
  177. re = re + "/datasets";
  178. }else if(record.OpType == 25){
  179. re = re + "/cloudbrain/" + record.Content;
  180. }else if(record.OpType == 26){
  181. re = re + "/modelarts/notebook/" + record.Content;
  182. }else if(record.OpType == 27){
  183. re = re + "/modelarts/train-job/" + record.Content;
  184. }else if(record.OpType == 28){
  185. re = re + "/modelarts/inference-job/" + record.Content;
  186. }else if(record.OpType == 29){
  187. re = re + "/cloudbrain/benchmark/" + record.Content;
  188. }else if(record.OpType == 30){
  189. re = re + "/modelmanage/show_model_info?name=" + record.RefName;
  190. }else if(record.OpType == 31){
  191. re = re + "/cloudbrain/train-job/" + record.Content;
  192. }else if(record.OpType == 32 || record.OpType == 33){
  193. re = re + "/grampus/train-job/" + record.Content;
  194. }
  195. re = encodeURI(re);
  196. return re;
  197. }
  198. function getMsg(record){
  199. var html ="";
  200. html += "<div class=\"swiper-slide item\">";
  201. var name = "";
  202. if(record.ActUser != null){
  203. name = record.ActUser.Name;
  204. }else{
  205. console.log("act user is null.");
  206. }
  207. html += " <img class=\"ui avatar image\" src=\"/user/avatar/" + name + "/-1\" alt=\"\">"
  208. html += " <div class=\"middle aligned content nowrap\">"
  209. html += " <a href=\"/" + encodeURI(name) + "\" title=\"\">" + name + "</a>"
  210. return html;
  211. }
  212. function getRepotext(record){
  213. if(record.Repo.Alias){
  214. return record.Repo.OwnerName + "/" + record.Repo.Alias;
  215. }else{
  216. return record.Repo.OwnerName + "/" + record.Repo.Name;
  217. }
  218. }
  219. function getRepoLink(record){
  220. return encodeURI(record.Repo.OwnerName + "/" + record.Repo.Name);
  221. }
  222. function getTime(UpdatedUnix,currentTime){
  223. UpdatedUnix = UpdatedUnix;
  224. currentTime = currentTime / 1000;
  225. var timeEscSecond = currentTime - UpdatedUnix;
  226. if( timeEscSecond < 0){
  227. timeEscSecond = 1;
  228. }
  229. var hours= Math.floor(timeEscSecond / 3600);
  230. //计算相差分钟数
  231. var leave2 = Math.floor(timeEscSecond % (3600)); //计算小时数后剩余的秒数
  232. var minutes= Math.floor(leave2 / 60);//计算相差分钟数
  233. var leave3=Math.floor(leave2 % 60); //计算分钟数后剩余的秒数
  234. var seconds= leave3;
  235. if(hours == 0 && minutes == 0){
  236. return seconds + getRepoOrOrg(6,isZh,seconds);
  237. }else{
  238. if(hours > 0){
  239. return hours + getRepoOrOrg(4,isZh,hours);
  240. }else{
  241. return minutes + getRepoOrOrg(5,isZh,minutes);
  242. }
  243. }
  244. }
  245. function getPRLink(record){
  246. return encodeURI("/" + record.Repo.OwnerName + "/" + record.Repo.Name + "/pulls/" + getIssueId(record));
  247. }
  248. function getPRText(record){
  249. if(record.Repo.Alias){
  250. return record.Repo.OwnerName + "/" + record.Repo.Alias + "#" + getIssueId(record);
  251. }else{
  252. return record.Repo.OwnerName + "/" + record.Repo.Name + "#" + getIssueId(record);
  253. }
  254. }
  255. function getIssueLink(record){
  256. return encodeURI("/" + record.Repo.OwnerName + "/" + record.Repo.Name + "/issues/" + getIssueId(record));
  257. }
  258. function getIssueId(record){
  259. var Id = "1";
  260. if(!isEmpty(record.Comment) && !isEmpty(record.Comment.Issue)){
  261. Id = record.Comment.Issue.Index;
  262. }else{
  263. if(!isEmpty(record.Content)){
  264. var content = record.Content;
  265. var index = content.indexOf("|");
  266. if(index != -1){
  267. Id = content.substring(0,index);
  268. }
  269. }
  270. }
  271. return Id;
  272. }
  273. function getIssueText(record){
  274. if(record.Repo.Alias){
  275. return record.Repo.OwnerName + "/" + record.Repo.Alias + "#" + getIssueId(record);
  276. }else{
  277. return record.Repo.OwnerName + "/" + record.Repo.Name + "#" + getIssueId(record);
  278. }
  279. }
  280. /*
  281. ActionCreateRepo ActionType = iota + 1 // 1
  282. ActionRenameRepo // 2
  283. ActionStarRepo // 3
  284. ActionWatchRepo // 4
  285. ActionCommitRepo // 5
  286. ActionCreateIssue // 6
  287. ActionCreatePullRequest // 7
  288. ActionTransferRepo // 8
  289. ActionPushTag // 9
  290. ActionCommentIssue // 10
  291. ActionMergePullRequest // 11
  292. ActionCloseIssue // 12
  293. ActionReopenIssue // 13
  294. ActionClosePullRequest // 14
  295. ActionReopenPullRequest // 15
  296. ActionDeleteTag // 16
  297. ActionDeleteBranch // 17
  298. ActionMirrorSyncPush // 18
  299. ActionMirrorSyncCreate // 19
  300. ActionMirrorSyncDelete // 20
  301. ActionApprovePullRequest // 21
  302. ActionRejectPullRequest // 22
  303. ActionCommentPull // 23
  304. */
  305. var actionNameZH={
  306. "1":"创建了项目",
  307. "2":"重命名项目 {oldRepoName} 为",
  308. "5":"推送了 {branch} 分支的代码到",
  309. "6":"创建了任务",
  310. "7":"创建了合并请求",
  311. "9":"推送了标签 {branch} 到",
  312. "10":"评论了任务",
  313. "11":"合并了合并请求",
  314. "12":"关闭了任务",
  315. "13":"重新开启了任务",
  316. "14":"关闭了合并请求",
  317. "15":"重新开启了合并请求",
  318. "17":"从 {repoName} 删除分支 {deleteBranchName}",
  319. "22":"建议变更",
  320. "23":"评论了合并请求",
  321. "24":"上传了数据集文件",
  322. "25":"创建了CPU/GPU类型调试任务",
  323. "26":"创建了NPU类型调试任务",
  324. "27":"创建了NPU类型训练任务",
  325. "28":"创建了推理任务",
  326. "29":"创建了评测任务",
  327. "30":"导入了新模型",
  328. "31":"创建了CPU/GPU类型训练任务",
  329. "32":"创建了NPU类型训练任务",
  330. "33":"创建了CPU/GPU类型训练任务"
  331. };
  332. var actionNameEN={
  333. "1":" created repository",
  334. "2":" renamed repository from {oldRepoName} to ",
  335. "5":" pushed to {branch} at",
  336. "6":" opened issue",
  337. "7":" created pull request",
  338. "9":" pushed tag {branch} to ",
  339. "10":" commented on issue",
  340. "11":" merged pull request",
  341. "12":" closed issue",
  342. "13":" reopened issue",
  343. "14":" closed pull request",
  344. "15":" reopened pull request",
  345. "17":" deleted branch {deleteBranchName} from {repoName}",
  346. "22":" proposed changes",
  347. "23":" commented on pull request",
  348. "24":" upload dataset ",
  349. "25":" created CPU/GPU type debugging task ",
  350. "26":" created NPU type debugging task ",
  351. "27":" created NPU type training task",
  352. "28":" created reasoning task",
  353. "29":" created profiling task",
  354. "30":" created new model",
  355. "31":" created CPU/GPU type training task",
  356. "32":" created NPU type training task",
  357. "33":" created CPU/GPU type training task"
  358. };
  359. var repoAndOrgZH={
  360. "1":"项目",
  361. "2":"成员",
  362. "3":"团队",
  363. "11":"项目",
  364. "21":"成员",
  365. "31":"团队",
  366. "4":"小时前",
  367. "5":"分钟前",
  368. "6":"秒前",
  369. "41":"小时前",
  370. "51":"分钟前",
  371. "61":"秒前"
  372. };
  373. var repoAndOrgEN={
  374. "1":"Repository",
  375. "2":"Member ",
  376. "3":"Team",
  377. "11":"Repositories",
  378. "21":"Members ",
  379. "31":"Teams",
  380. "4":" hour ago",
  381. "5":" minute ago",
  382. "6":" second ago",
  383. "41":" hours ago",
  384. "51":" minutes ago",
  385. "61":" seconds ago"
  386. };
  387. function getAction(opType,isZh){
  388. if(isZh){
  389. return actionNameZH[opType]
  390. }else{
  391. return actionNameEN[opType]
  392. }
  393. }
  394. function queryRecommendData(){
  395. $.ajax({
  396. type:"GET",
  397. url:"/recommend/home",
  398. headers: {
  399. authorization:token,
  400. },
  401. dataType:"json",
  402. async:false,
  403. success:function(json){
  404. displayOrg(json.org);
  405. displayRepo(json.repo);
  406. displayActivity(json.image);
  407. },
  408. error:function(response) {
  409. }
  410. });
  411. }
  412. function displayActivity(json){
  413. var activityDiv = document.getElementById("recommendactivity");
  414. var html = "";
  415. if (json != null && json.length > 0){
  416. for(var i = 0; i < json.length;i++){
  417. var record = json[i]
  418. html += "<div class=\"swiper-slide\">";
  419. html += "<a href=\"" + record["image_link"] + "\" class=\"ui fluid card\">";
  420. html += " <div class=\"image\"><img src=\"" + record["url"] + "\"></div>"
  421. html += "</a>";
  422. html += "</div>";
  423. }
  424. }
  425. activityDiv.innerHTML = html;
  426. swiperEvent.updateSlides();
  427. swiperEvent.updateProgress();
  428. }
  429. function displayRepo(json){
  430. var orgRepo = document.getElementById("recommendrepo");
  431. var html = "";
  432. if (json != null && json.length > 0){
  433. for(var i = 0; i < json.length;i++){
  434. var record = json[i]
  435. html += "<div class=\"swiper-slide\">";
  436. html += " <div class=\"ui fluid card\">";
  437. html += " <div class=\"content\">";
  438. html += " <span class=\"right floated meta\">";
  439. html += " <i class=\"ri-star-line\"></i>" + record["NumStars"] + "<i class=\"ri-git-branch-line am-ml-10\"></i>" + record["NumForks"];
  440. html += " </span>";
  441. html += " <img class=\"left floated mini ui image\" src=\"" + record["Avatar"] + "\">";
  442. html += " <a class=\"header nowrap\" href=\"/" + record["OwnerName"] + "/" + record["Name"] + "\">" + record["Alias"] +"</a>";
  443. html += " <div class=\"description nowrap-2\">" + record["Description"] + " </div>";
  444. html += " <div class=\"ui tags nowrap am-mt-10\">"
  445. if(record["Topics"] != null){
  446. for(var j = 0; j < record["Topics"].length; j++){
  447. topic = record["Topics"][j];
  448. url = "/explore/repos?q=" + (topic) + "&amp;topic="
  449. html += "<a class=\"ui small label topic\" href=\"" + url + "\">" + topic + "</a>";
  450. }
  451. }
  452. html += " </div>";
  453. html += " </div>";
  454. html += " </div>";
  455. html += "</div>";
  456. }
  457. }
  458. orgRepo.innerHTML = html;
  459. swiperRepo.updateSlides();
  460. swiperRepo.updateProgress();
  461. }
  462. function getRepoOrOrg(key,isZhLang,numbers=1){
  463. if(numbers > 1){
  464. key+="1";
  465. }
  466. if(isZhLang){
  467. return repoAndOrgZH[key];
  468. }else{
  469. return repoAndOrgEN[key];
  470. }
  471. }
  472. function displayOrg(json){
  473. var orgDiv = document.getElementById("recommendorg");
  474. var html = "";
  475. if (json != null && json.length > 0){
  476. for(var i = 0; i < json.length;i++){
  477. var record = json[i]
  478. html += "<div class=\"swiper-slide\">";
  479. html += " <a href=\"/" + record["Name"] + "\" class=\"ui fluid card\">";
  480. html += " <div class=\"content\">";
  481. html += " <div class=\"ui small header\">";
  482. html += " <img class=\"ui image\" src=\"" + record["Avatar"] + "\">";
  483. html += " <div class=\"content nowrap\">";
  484. html += " <span class=\"ui blue\">" + record["Name"] + "</span> " + record["FullName"];
  485. html += " <div class=\"sub header\">" + record["NumRepos"] +" " + getRepoOrOrg(1,isZh,record["NumRepos"]) + " ・ " + record["NumMembers"] +" " + getRepoOrOrg(2,isZh,record["NumMembers"]) + " ・ " + record["NumTeams"] + " " + getRepoOrOrg(3,isZh,record["NumTeams"]) + "</div>";
  486. html += " </div>";
  487. html += " </div>";
  488. html += " </div>";
  489. html += " </a>";
  490. html += "</div>";
  491. }
  492. }
  493. orgDiv.innerHTML = html;
  494. swiperOrg.updateSlides();
  495. }