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