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