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