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