Mobile-responsive personal website, generated using Hugo. https://kevin-mok.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.7 KiB

  1. const log = console.log
  2. const headerKeys = {
  3. "status": "Status",
  4. "url": "URL",
  5. "description": "Description",
  6. "repo": "Repository"
  7. }
  8. const keyOrder = ["status", "url", "description", "repo"]
  9. const generateTableHead = table => {
  10. let thead = table.createTHead();
  11. let row = thead.insertRow();
  12. keyOrder.forEach(key => {
  13. let th = document.createElement("th");
  14. th.textContent = headerKeys[key];
  15. row.appendChild(th);
  16. })
  17. }
  18. const generateTable = (table, data) => {
  19. data.forEach(elem => {
  20. let row = table.insertRow();
  21. keyOrder.forEach(key => {
  22. const cell = row.insertCell();
  23. cell.className = key;
  24. const aElem = document.createElement('a')
  25. aElem.target = '_blank'
  26. switch (key) {
  27. case 'status':
  28. // cell.textContent = ((elem[key] == 'up') ? '🗸' : '✗')
  29. const statusIcon = document.createElement('img')
  30. statusIcon.src = '/img/server/' + ((elem[key] == 'up') ? 'check' : 'x') + '.svg'
  31. cell.appendChild(statusIcon)
  32. break
  33. case 'url':
  34. aElem.href = elem[key]
  35. aElem.textContent = elem['shortUrl']
  36. cell.appendChild(aElem)
  37. break
  38. case 'repo':
  39. aElem.href = elem['repoUrl']
  40. aElem.textContent = elem[key]
  41. cell.appendChild(aElem)
  42. break
  43. default:
  44. cell.textContent = elem[key]
  45. }
  46. })
  47. })
  48. }
  49. const getDurationSince = (date, verbose) => {
  50. const milli = Math.abs(Date.now() - date)
  51. let mins = Math.floor(milli/1000/60)
  52. const days = Math.floor(mins/60/24)
  53. mins %= 60*24
  54. const hours = Math.floor(mins/60)
  55. mins %= 60
  56. let dayString = ''
  57. let hourString = ''
  58. let minString = ''
  59. if (days > 0) {
  60. dayString += days
  61. if (!verbose) {
  62. dayString += 'd '
  63. } else {
  64. dayString += (days == 1) ? ' day' : ' days'
  65. dayString += ', '
  66. }
  67. }
  68. if (hours > 0) {
  69. hourString += hours
  70. if (!verbose) {
  71. hourString += 'h '
  72. } else {
  73. hourString += (hours == 1) ? ' hour' : ' hours'
  74. hourString += ', '
  75. }
  76. }
  77. minString += (mins > 0) ? mins: '0'
  78. if (!verbose) {
  79. minString += 'm'
  80. } else {
  81. minString += (mins == 1) ? ' minute' : ' minutes'
  82. }
  83. return dayString + hourString + minString
  84. }
  85. fetch("/server-apps.json")
  86. .then(response => response.json())
  87. .then(json => {
  88. let table = document.querySelector("table");
  89. generateTable(table, json['sites']);
  90. generateTableHead(table);
  91. document.querySelector('#last-updated-time').textContent =
  92. getDurationSince(new Date(json['updateTime']), false) + ' ago.'
  93. document.querySelector('#server-uptime-time').textContent =
  94. getDurationSince(new Date(json['upSince']), true) + '.'
  95. });