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