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.

104 lines
2.8 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 'description':
  39. cell.innerHTML = elem[key]
  40. break
  41. case 'repo':
  42. aElem.href = elem['repoUrl']
  43. aElem.textContent = elem[key]
  44. cell.appendChild(aElem)
  45. break
  46. default:
  47. cell.textContent = elem[key]
  48. }
  49. })
  50. })
  51. }
  52. const getDurationSince = (date, verbose) => {
  53. const milli = Math.abs(Date.now() - date)
  54. let mins = Math.floor(milli/1000/60)
  55. const days = Math.floor(mins/60/24)
  56. mins %= 60*24
  57. const hours = Math.floor(mins/60)
  58. mins %= 60
  59. let dayString = ''
  60. let hourString = ''
  61. let minString = ''
  62. if (days > 0) {
  63. dayString += days
  64. if (!verbose) {
  65. dayString += 'd '
  66. } else {
  67. dayString += (days == 1) ? ' day' : ' days'
  68. dayString += ', '
  69. }
  70. }
  71. if (hours > 0) {
  72. hourString += hours
  73. if (!verbose) {
  74. hourString += 'h '
  75. } else {
  76. hourString += (hours == 1) ? ' hour' : ' hours'
  77. hourString += ', '
  78. }
  79. }
  80. minString += (mins > 0) ? mins: '0'
  81. if (!verbose) {
  82. minString += 'm'
  83. } else {
  84. minString += (mins == 1) ? ' minute' : ' minutes'
  85. }
  86. return dayString + hourString + minString
  87. }
  88. fetch("/server-apps.json")
  89. .then(response => response.json())
  90. .then(json => {
  91. let table = document.querySelector("table");
  92. generateTable(table, json['sites']);
  93. generateTableHead(table);
  94. document.querySelector('#last-updated-time').textContent =
  95. getDurationSince(new Date(json['updateTime']), false) + ' ago.'
  96. document.querySelector('#server-uptime-time').textContent =
  97. getDurationSince(new Date(json['upSince']), true) + '.'
  98. });