property.eta 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <% layout("/en/panel/stations/history/layout", { title: it.meteostanica.name }) %>
  2. <%~ include("/en/panel/partials/navbar") %>
  3. <% const backIcon = `
  4. <svg class="icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
  5. <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m12 19l-7-7l7-7m7 7H5" />
  6. </svg>
  7. ` %>
  8. <%~ include("/en/panel/stations/partials/details", { meteostanica: it.meteostanica }) %>
  9. <div class="container-row">
  10. <a role="button" href="/en/panel/stations/<%= it.meteostanica.id %>/history"><%~ backIcon %></a>
  11. <h2><%~ it?.lang?.stations?.history.properties?.[it?.property]() %></h2>
  12. </div>
  13. <div class="historyLinks">
  14. <a role="button" href="/en/panel/stations/<%= it.meteostanica.id %>/history/<%= it.property %>/daily">daily</a>
  15. <a role="button" href="/en/panel/stations/<%= it.meteostanica.id %>/history/<%= it.property %>/monthly">monthly</a>
  16. <a role="button" href="/en/panel/stations/<%= it.meteostanica.id %>/history/<%= it.property %>/yearly">yearly</a>
  17. <a role="button" href="/en/panel/stations/<%= it.meteostanica.id %>/history/<%= it.property %>/allTime">all time</a>
  18. </div>
  19. <% if (it?.dateMap?.years) { %>
  20. <form id="historyForm">
  21. <% if (it?.dateMap?.days) { %>
  22. <div>
  23. <label for="day">day</label>
  24. <select id="day" name="day">
  25. <% for (const item of it?.dateMap?.days) { %>
  26. <option value="<%= item %>" <%= item === it?.selected?.day ? `selected` : `` %> ><%= item %></option>
  27. <% } %>
  28. </select>
  29. </div>
  30. <% } %>
  31. <% if (it?.dateMap?.months) { %>
  32. <div>
  33. <label for="month">month</label>
  34. <select id="month" name="month">
  35. <% for (const item of it?.dateMap?.months) { %>
  36. <option value="<%= item %>" <%= item === it?.selected?.month ? `selected` : `` %> > <%= it?.lang?.general.dateFormats.months?.[item]() %></option>
  37. <% } %>
  38. </select>
  39. </div>
  40. <% } %>
  41. <div>
  42. <label for="year">year</label>
  43. <select id="year" name="year">
  44. <% for (const item of it?.dateMap?.years) { %>
  45. <option value="<%= item %>" <%= item === it?.selected?.year ? `selected` : `` %> > <%= item %></option>
  46. <% } %>
  47. </select>
  48. </div>
  49. <button type="submit">load</button>
  50. </form>
  51. <% } %>
  52. <% const time = [] %>
  53. <% const data = [] %>
  54. <% for (const item of it.data) { %>
  55. <% time.push(item.timeMark) %>
  56. <% if (it.property === "outdoorConnected") {%>
  57. <% data.push(item.value) %>
  58. <% continue %>
  59. <% } %>
  60. <% data.push(item.value / 100) %>
  61. <% } %>
  62. <div>
  63. <canvas id="historyChart"></canvas>
  64. </div>
  65. <script>
  66. const dateMap = <%~ JSON.stringify(it?.dateMap.raw, null, 2) %>
  67. const monthLang = <%~ serializeToCode(it?.lang?.general.dateFormats.months) %>
  68. const daySelect = document.querySelector("#historyForm select#day")
  69. const monthSelect = document.querySelector("#historyForm select#month")
  70. const yearSelect = document.querySelector("#historyForm select#year")
  71. monthSelect.addEventListener('change', resetHistoryForm)
  72. yearSelect.addEventListener('change', resetHistoryForm)
  73. function resetHistoryForm(e) {
  74. daySelect.textContent = ''
  75. if (e.target.id === "year") monthSelect.textContent = ''
  76. const years = Object.keys(dateMap)
  77. const selectedYear = years.find(i => i === yearSelect.value) ?? years[years.length - 1]
  78. const months = Object.keys(dateMap[selectedYear])
  79. const selectedMonth = months.find(i => i === monthSelect.value) ?? months[months.length - 1]
  80. const days = dateMap[selectedYear][selectedMonth]
  81. for (const day of days) {
  82. const option = document.createElement('option')
  83. option.textContent = day
  84. option.value = day
  85. daySelect.append(option)
  86. }
  87. if (e.target.id === "year") {
  88. for (const [key, value] of Object.entries(months)) {
  89. const option = document.createElement('option')
  90. option.textContent = monthLang[key]()
  91. option.value = key
  92. monthSelect.append(option)
  93. }
  94. }
  95. }
  96. const ctx = document.querySelector('#historyChart');
  97. new Chart(ctx, {
  98. type: 'bar',
  99. data: {
  100. labels: <%~ JSON.stringify(time, null, 2) %>,
  101. datasets: [{
  102. label: `<%= it.property %>`,
  103. data: <%~ JSON.stringify(data, null, 2) %>,
  104. borderWidth: 1
  105. }]
  106. },
  107. options: {
  108. scales: {
  109. y: {
  110. beginAtZero: true
  111. }
  112. }
  113. }
  114. });
  115. </script>
  116. <% function serializeToCode(obj) { %>
  117. <% // Handle null/primitive types %>
  118. <% if (obj === null) return 'null'; %>
  119. <% if (typeof obj === 'string') return `'${obj.replace(/'/g, "\\'")}'`; %>
  120. <% if (typeof obj !== 'object') return obj.toString(); %>
  121. <% // Handle Arrays %>
  122. <% if (Array.isArray(obj)) { %>
  123. <% return `[${obj.map(item => serializeToCode(item)).join(',')}]`; %>
  124. <% } %>
  125. <% // Handle Objects %>
  126. <% const entries = Object.entries(obj).map(([key, value]) => { %>
  127. <% // Ensure keys are safe (quoted if they contain special characters) %>
  128. <% const safeKey = /^[a-z$_][a-z0-9$_]*$/i.test(key) ? key : `'${key}'`; %>
  129. <% return `${safeKey}: ${serializeToCode(value)}`; %>
  130. <% }); %>
  131. <% return `{${entries.join(',')}}`; %>
  132. <% } %>