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
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 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. refresh3DInfo(record);
  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"){
  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. function getTaskLink(record){
  174. var re = getRepoLink(record);
  175. if(record.OpType == 24){
  176. re = re + "/datasets";
  177. }else if(record.OpType == 25){
  178. re = re + "/cloudbrain/" + record.Content;
  179. }else if(record.OpType == 26){
  180. re = re + "/modelarts/notebook/" + record.Content;
  181. }else if(record.OpType == 27){
  182. re = re + "/modelarts/train-job/" + record.Content;
  183. }else if(record.OpType == 28){
  184. re = re + "/modelarts/inference-job/" + record.Content;
  185. }else if(record.OpType == 29){
  186. re = re + "/cloudbrain/benchmark/" + record.Content;
  187. }else if(record.OpType == 30){
  188. re = re + "/modelmanage/show_model_info?name=" + record.RefName;
  189. }else if(record.OpType == 31){
  190. re = re + "/cloudbrain/train-job/" + record.Content;
  191. }
  192. re = encodeURI(re);
  193. return re;
  194. }
  195. function refresh3DInfo(record){
  196. if(record.OpType == "25" || record.OpType == "29" || record.OpType == "31"){
  197. //cloudbrain one
  198. var lines = $('.rotation3D__line');
  199. var span = $('.rotation3D__line').find("span")[0];
  200. //console.log(span);
  201. span.innerText =record.RefName;
  202. //$('.rotation3D__line').find("span").eq(0).text(record.RefName)
  203. //console.log("cloudbrain one line length=" + lines.length);
  204. //lines[0].find("span").text(record.RefName);
  205. }else if(record.OpType == "26" || record.OpType == "27" || record.OpType == "28"){
  206. //cloudbrain two
  207. var lines = $('.rotation3D__line');
  208. //console.log("cloudbrain two line length=" + lines.length);
  209. var span = $('.rotation3D__line').find("span")[1];
  210. //console.log(span);
  211. if(span != null){
  212. span.innerText =record.RefName;
  213. }
  214. }
  215. }
  216. function getMsg(record){
  217. var html ="";
  218. html += "<div class=\"swiper-slide item\">";
  219. var name = "";
  220. if(record.ActUser != null){
  221. name = record.ActUser.Name;
  222. }else{
  223. console.log("act user is null.");
  224. }
  225. html += " <img class=\"ui avatar image\" src=\"/user/avatar/" + name + "/-1\" alt=\"\">"
  226. html += " <div class=\"middle aligned content nowrap\">"
  227. html += " <a href=\"/" + encodeURI(name) + "\" title=\"\">" + name + "</a>"
  228. return html;
  229. }
  230. function getRepotext(record){
  231. if(record.Repo.Alias){
  232. return record.Repo.OwnerName + "/" + record.Repo.Alias;
  233. }else{
  234. return record.Repo.OwnerName + "/" + record.Repo.Name;
  235. }
  236. }
  237. function getRepoLink(record){
  238. return encodeURI(record.Repo.OwnerName + "/" + record.Repo.Name);
  239. }
  240. function getTime(UpdatedUnix,currentTime){
  241. UpdatedUnix = UpdatedUnix;
  242. currentTime = currentTime / 1000;
  243. var timeEscSecond = currentTime - UpdatedUnix;
  244. if( timeEscSecond < 0){
  245. timeEscSecond = 1;
  246. }
  247. var hours= Math.floor(timeEscSecond / 3600);
  248. //计算相差分钟数
  249. var leave2 = Math.floor(timeEscSecond % (3600)); //计算小时数后剩余的秒数
  250. var minutes= Math.floor(leave2 / 60);//计算相差分钟数
  251. var leave3=Math.floor(leave2 % 60); //计算分钟数后剩余的秒数
  252. var seconds= leave3;
  253. if(hours == 0 && minutes == 0){
  254. return seconds + getRepoOrOrg(6,isZh,seconds);
  255. }else{
  256. if(hours > 0){
  257. return hours + getRepoOrOrg(4,isZh,hours);
  258. }else{
  259. return minutes + getRepoOrOrg(5,isZh,minutes);
  260. }
  261. }
  262. }
  263. function getPRLink(record){
  264. return encodeURI("/" + record.Repo.OwnerName + "/" + record.Repo.Name + "/pulls/" + getIssueId(record));
  265. }
  266. function getPRText(record){
  267. if(record.Repo.Alias){
  268. return record.Repo.OwnerName + "/" + record.Repo.Alias + "#" + getIssueId(record);
  269. }else{
  270. return record.Repo.OwnerName + "/" + record.Repo.Name + "#" + getIssueId(record);
  271. }
  272. }
  273. function getIssueLink(record){
  274. return encodeURI("/" + record.Repo.OwnerName + "/" + record.Repo.Name + "/issues/" + getIssueId(record));
  275. }
  276. function getIssueId(record){
  277. var Id = "1";
  278. if(!isEmpty(record.Comment) && !isEmpty(record.Comment.Issue)){
  279. Id = record.Comment.Issue.Index;
  280. }else{
  281. if(!isEmpty(record.Content)){
  282. var content = record.Content;
  283. var index = content.indexOf("|");
  284. if(index != -1){
  285. Id = content.substring(0,index);
  286. }
  287. }
  288. }
  289. return Id;
  290. }
  291. function getIssueText(record){
  292. if(record.Repo.Alias){
  293. return record.Repo.OwnerName + "/" + record.Repo.Alias + "#" + getIssueId(record);
  294. }else{
  295. return record.Repo.OwnerName + "/" + record.Repo.Name + "#" + getIssueId(record);
  296. }
  297. }
  298. /*
  299. ActionCreateRepo ActionType = iota + 1 // 1
  300. ActionRenameRepo // 2
  301. ActionStarRepo // 3
  302. ActionWatchRepo // 4
  303. ActionCommitRepo // 5
  304. ActionCreateIssue // 6
  305. ActionCreatePullRequest // 7
  306. ActionTransferRepo // 8
  307. ActionPushTag // 9
  308. ActionCommentIssue // 10
  309. ActionMergePullRequest // 11
  310. ActionCloseIssue // 12
  311. ActionReopenIssue // 13
  312. ActionClosePullRequest // 14
  313. ActionReopenPullRequest // 15
  314. ActionDeleteTag // 16
  315. ActionDeleteBranch // 17
  316. ActionMirrorSyncPush // 18
  317. ActionMirrorSyncCreate // 19
  318. ActionMirrorSyncDelete // 20
  319. ActionApprovePullRequest // 21
  320. ActionRejectPullRequest // 22
  321. ActionCommentPull // 23
  322. */
  323. var actionNameZH={
  324. "1":"创建了项目",
  325. "2":"重命名项目 {oldRepoName} 为",
  326. "5":"推送了 {branch} 分支的代码到",
  327. "6":"创建了任务",
  328. "7":"创建了合并请求",
  329. "9":"推送了标签 {branch} 到",
  330. "10":"评论了任务",
  331. "11":"合并了合并请求",
  332. "12":"关闭了任务",
  333. "13":"重新开启了任务",
  334. "14":"关闭了合并请求",
  335. "15":"重新开启了合并请求",
  336. "17":"从 {repoName} 删除分支 {deleteBranchName}",
  337. "22":"建议变更",
  338. "23":"评论了合并请求",
  339. "24":"上传了数据集文件",
  340. "25":"创建了CPU/GPU类型调试任务",
  341. "26":"创建了NPU类型调试任务",
  342. "27":"创建了NPU类型训练任务",
  343. "28":"创建了推理任务",
  344. "29":"创建了评测任务",
  345. "30":"导入了新模型",
  346. "31":"创建了CPU/GPU类型训练任务"
  347. };
  348. var actionNameEN={
  349. "1":" created repository",
  350. "2":" renamed repository from {oldRepoName} to ",
  351. "5":" pushed to {branch} at",
  352. "6":" opened issue",
  353. "7":" created pull request",
  354. "9":" pushed tag {branch} to ",
  355. "10":" commented on issue",
  356. "11":" merged pull request",
  357. "12":" closed issue",
  358. "13":" reopened issue",
  359. "14":" closed pull request",
  360. "15":" reopened pull request",
  361. "17":" deleted branch {deleteBranchName} from {repoName}",
  362. "22":" proposed changes",
  363. "23":" commented on pull request",
  364. "24":" upload dataset ",
  365. "25":" created CPU/GPU type debugging task ",
  366. "26":" created NPU type debugging task ",
  367. "27":" created NPU type training task",
  368. "28":" created reasoning task",
  369. "29":" created profiling task",
  370. "30":" created new model",
  371. "31":" created CPU/GPU type training task",
  372. };
  373. var repoAndOrgZH={
  374. "1":"项目",
  375. "2":"成员",
  376. "3":"团队",
  377. "11":"项目",
  378. "21":"成员",
  379. "31":"团队",
  380. "4":"小时前",
  381. "5":"分钟前",
  382. "6":"秒前",
  383. "41":"小时前",
  384. "51":"分钟前",
  385. "61":"秒前"
  386. };
  387. var repoAndOrgEN={
  388. "1":"Repository",
  389. "2":"Member ",
  390. "3":"Team",
  391. "11":"Repositories",
  392. "21":"Members ",
  393. "31":"Teams",
  394. "4":" hour ago",
  395. "5":" minute ago",
  396. "6":" second ago",
  397. "41":" hours ago",
  398. "51":" minutes ago",
  399. "61":" seconds ago"
  400. };
  401. function getAction(opType,isZh){
  402. if(isZh){
  403. return actionNameZH[opType]
  404. }else{
  405. return actionNameEN[opType]
  406. }
  407. }
  408. queryRecommendData();
  409. function queryRecommendData(){
  410. $.ajax({
  411. type:"GET",
  412. url:"/recommend/home",
  413. headers: {
  414. authorization:token,
  415. },
  416. dataType:"json",
  417. async:false,
  418. success:function(json){
  419. displayOrg(json.org);
  420. displayRepo(json.repo);
  421. displayActivity(json.image);
  422. },
  423. error:function(response) {
  424. }
  425. });
  426. }
  427. function displayActivity(json){
  428. var activityDiv = document.getElementById("recommendactivity");
  429. var html = "";
  430. if (json != null && json.length > 0){
  431. for(var i = 0; i < json.length;i++){
  432. var record = json[i]
  433. html += "<div class=\"swiper-slide\">";
  434. html += "<a href=\"" + record["image_link"] + "\" class=\"ui fluid card\">";
  435. html += " <div class=\"image\"><img src=\"" + record["url"] + "\"></div>"
  436. html += "</a>";
  437. html += "</div>";
  438. }
  439. }
  440. activityDiv.innerHTML = html;
  441. swiperEvent.updateSlides();
  442. swiperEvent.updateProgress();
  443. }
  444. function displayRepo(json){
  445. var orgRepo = document.getElementById("recommendrepo");
  446. var html = "";
  447. if (json != null && json.length > 0){
  448. for(var i = 0; i < json.length;i++){
  449. var record = json[i]
  450. html += "<div class=\"swiper-slide\">";
  451. html += " <div class=\"ui fluid card\">";
  452. html += " <div class=\"content\">";
  453. html += " <span class=\"right floated meta\">";
  454. html += " <i class=\"ri-star-line\"></i>" + record["NumStars"] + "<i class=\"ri-git-branch-line am-ml-10\"></i>" + record["NumForks"];
  455. html += " </span>";
  456. html += " <img class=\"left floated mini ui image\" src=\"" + record["Avatar"] + "\">";
  457. html += " <a class=\"header nowrap\" href=\"/" + record["OwnerName"] + "/" + record["Name"] + "\">" + record["Alias"] +"</a>";
  458. html += " <div class=\"description nowrap-2\">" + record["Description"] + " </div>";
  459. html += " <div class=\"ui tags nowrap am-mt-10\">"
  460. if(record["Topics"] != null){
  461. for(var j = 0; j < record["Topics"].length; j++){
  462. topic = record["Topics"][j];
  463. url = "/explore/repos?q=" + (topic) + "&amp;topic="
  464. html += "<a class=\"ui small label topic\" href=\"" + url + "\">" + topic + "</a>";
  465. }
  466. }
  467. html += " </div>";
  468. html += " </div>";
  469. html += " </div>";
  470. html += "</div>";
  471. }
  472. }
  473. orgRepo.innerHTML = html;
  474. swiperRepo.updateSlides();
  475. swiperRepo.updateProgress();
  476. }
  477. function getRepoOrOrg(key,isZhLang,numbers=1){
  478. if(numbers > 1){
  479. key+="1";
  480. }
  481. if(isZhLang){
  482. return repoAndOrgZH[key];
  483. }else{
  484. return repoAndOrgEN[key];
  485. }
  486. }
  487. function displayOrg(json){
  488. var orgDiv = document.getElementById("recommendorg");
  489. var html = "";
  490. if (json != null && json.length > 0){
  491. for(var i = 0; i < json.length;i++){
  492. var record = json[i]
  493. html += "<div class=\"swiper-slide\">";
  494. html += " <a href=\"/" + record["Name"] + "\" class=\"ui fluid card\">";
  495. html += " <div class=\"content\">";
  496. html += " <div class=\"ui small header\">";
  497. html += " <img class=\"ui image\" src=\"" + record["Avatar"] + "\">";
  498. html += " <div class=\"content nowrap\">";
  499. html += " <span class=\"ui blue\">" + record["Name"] + "</span> " + record["FullName"];
  500. 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>";
  501. html += " </div>";
  502. html += " </div>";
  503. html += " </div>";
  504. html += " </a>";
  505. html += "</div>";
  506. }
  507. }
  508. orgDiv.innerHTML = html;
  509. swiperOrg.updateSlides();
  510. }