| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import { Database } from "bun:sqlite";
- const meteostanicaDB = new Database("./data/meteostanica.sqlite");
- meteostanicaDB.run(`create table if not exists data (
- timestamp datetime default current_timestamp primary key,
- indoorTemp text not null,
- indoorPressure text not null,
- indoorHumidity text not null,
- indoorAltitude text not null,
- outdoorConnected integer not null,
- outdoorTemp text not null,
- outdoorPressure text not null,
- outdoorHumidity text not null,
- outdoorAltitude text not null
- );`)
- export default class Meteostanica {
- static getData() {
- const statement = meteostanicaDB.prepare(`
- SELECT *
- FROM data
- ORDER BY timestamp DESC;
- `)
- const result = statement.all();
- return result
- }
- static getDataProperty(property) {
- const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
- if (!tableNames.find(i => i.name === property)) return null
- const statement = meteostanicaDB.prepare(`
- SELECT ${property}
- FROM data
- ORDER BY timestamp DESC
- LIMIT 10;
- `)
- const result = statement.all();
- return result
- }
- static getDataPropertyDaily(property, date) {
- const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
- if (!tableNames.find(i => i.name === property)) return null
- const statement = meteostanicaDB.prepare(`
- SELECT strftime('%Y-%m-%d %H:00:00', timestamp) AS timeMark,
- AVG(${property}) AS value
- FROM data
- WHERE date(timestamp) = ? -- Pass 'YYYY-MM-DD' here
- GROUP BY timeMark
- ORDER BY timeMark;
- `)
-
- const result = statement.all(date);
- return result
- }
- static getDataPropertyMonthly(property, yearMonth) {
- const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
- if (!tableNames.find(i => i.name === property)) return null
- const statement = meteostanicaDB.prepare(`
- SELECT date(timestamp) AS timeMark,
- AVG(${property}) AS value
- FROM data
- WHERE strftime('%Y-%m', timestamp) = ? -- Pass 'YYYY-MM' here
- GROUP BY timeMark
- ORDER BY timeMark;
- `)
-
- const result = statement.all(yearMonth);
- return result
- }
- static getDataPropertyYearly(property, year) {
- const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
- if (!tableNames.find(i => i.name === property)) return null
- const statement = meteostanicaDB.prepare(`
- SELECT strftime('%Y-%m', timestamp) AS timeMark,
- AVG(${property}) AS value
- FROM data
- WHERE strftime('%Y', timestamp) = ? -- Pass 'YYYY' here
- GROUP BY timeMark
- ORDER BY timeMark;
- `)
-
- const result = statement.all(year);
- return result
- }
- static getDataPropertyAllTime(property) {
- const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
- if (!tableNames.find(i => i.name === property)) return null
- const statement = meteostanicaDB.prepare(`
- SELECT strftime('%Y', timestamp) AS timeMark,
- AVG(${property}) AS value
- FROM data
- GROUP BY timeMark
- ORDER BY timeMark;
- `)
-
- const result = statement.all();
- return result
- }
- static getDateMap() {
- const statement = meteostanicaDB.query("SELECT DISTINCT date(timestamp) as d FROM data ORDER BY d ASC");
- const rows = statement.all();
- return rows.reduce((acc, row) => {
- const [year, month, day] = row.d.split("-");
- if (!acc[year]) acc[year] = {};
- if (!acc[year][month]) acc[year][month] = [];
- acc[year][month].push(day);
- return acc;
- }, {});
- }
- }
|