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