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