meteostanica.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { Database } from "bun:sqlite";
  2. const meteostanicaDB = new Database("./data/meteostanica.sqlite");
  3. meteostanicaDB.run(`create table if not exists data (
  4. timestamp datetime default current_timestamp primary key,
  5. indoorTemp text not null,
  6. indoorPressure text not null,
  7. indoorHumidity text not null,
  8. indoorAltitude text not null,
  9. outdoorConnected integer not null,
  10. outdoorTemp text not null,
  11. outdoorPressure text not null,
  12. outdoorHumidity text not null,
  13. outdoorAltitude text not null
  14. );`)
  15. export default class Meteostanica {
  16. static getData() {
  17. const statement = meteostanicaDB.prepare(`
  18. SELECT *
  19. FROM data
  20. ORDER BY timestamp DESC;
  21. `)
  22. const result = statement.all();
  23. return result
  24. }
  25. static getDataProperty(property) {
  26. const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
  27. if (!tableNames.find(i => i.name === property)) return null
  28. const statement = meteostanicaDB.prepare(`
  29. SELECT ${property}
  30. FROM data
  31. ORDER BY timestamp DESC
  32. LIMIT 10;
  33. `)
  34. const result = statement.all();
  35. return result
  36. }
  37. static getDataPropertyDaily(property, date) {
  38. const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
  39. if (!tableNames.find(i => i.name === property)) return null
  40. const statement = meteostanicaDB.prepare(`
  41. SELECT strftime('%Y-%m-%d %H:00:00', timestamp) AS timeMark,
  42. AVG(${property}) AS value
  43. FROM data
  44. WHERE date(timestamp) = ? -- Pass 'YYYY-MM-DD' here
  45. GROUP BY timeMark
  46. ORDER BY timeMark;
  47. `)
  48. const result = statement.all(date);
  49. return result
  50. }
  51. static getDataPropertyMonthly(property, yearMonth) {
  52. const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
  53. if (!tableNames.find(i => i.name === property)) return null
  54. const statement = meteostanicaDB.prepare(`
  55. SELECT date(timestamp) AS timeMark,
  56. AVG(${property}) AS value
  57. FROM data
  58. WHERE strftime('%Y-%m', timestamp) = ? -- Pass 'YYYY-MM' here
  59. GROUP BY timeMark
  60. ORDER BY timeMark;
  61. `)
  62. const result = statement.all(yearMonth);
  63. return result
  64. }
  65. static getDataPropertyYearly(property, year) {
  66. const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
  67. if (!tableNames.find(i => i.name === property)) return null
  68. const statement = meteostanicaDB.prepare(`
  69. SELECT strftime('%Y-%m', timestamp) AS timeMark,
  70. AVG(${property}) AS value
  71. FROM data
  72. WHERE strftime('%Y', timestamp) = ? -- Pass 'YYYY' here
  73. GROUP BY timeMark
  74. ORDER BY timeMark;
  75. `)
  76. const result = statement.all(year);
  77. return result
  78. }
  79. static getDataPropertyAllTime(property) {
  80. const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
  81. if (!tableNames.find(i => i.name === property)) return null
  82. const statement = meteostanicaDB.prepare(`
  83. SELECT strftime('%Y', timestamp) AS timeMark,
  84. AVG(${property}) AS value
  85. FROM data
  86. GROUP BY timeMark
  87. ORDER BY timeMark;
  88. `)
  89. const result = statement.all();
  90. return result
  91. }
  92. static getDateMap() {
  93. const statement = meteostanicaDB.query("SELECT DISTINCT date(timestamp) as d FROM data ORDER BY d ASC");
  94. const rows = statement.all();
  95. return rows.reduce((acc, row) => {
  96. const [year, month, day] = row.d.split("-");
  97. if (!acc[year]) acc[year] = {};
  98. if (!acc[year][month]) acc[year][month] = [];
  99. acc[year][month].push(day);
  100. return acc;
  101. }, {});
  102. }
  103. }