104 lines
3.4 KiB
JavaScript
104 lines
3.4 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.pool = void 0;
|
|
exports.initDb = initDb;
|
|
exports.saveAntigravityLatest = saveAntigravityLatest;
|
|
exports.loadAntigravityLatest = loadAntigravityLatest;
|
|
exports.insertQuota = insertQuota;
|
|
exports.getLatestQuotas = getLatestQuotas;
|
|
exports.getTodayStartQuota = getTodayStartQuota;
|
|
exports.getQuotaHistory = getQuotaHistory;
|
|
const pg_1 = require("pg");
|
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
dotenv_1.default.config();
|
|
exports.pool = new pg_1.Pool({
|
|
user: process.env.DB_USER,
|
|
host: process.env.DB_HOST,
|
|
database: process.env.DB_NAME,
|
|
password: process.env.DB_PASSWORD,
|
|
port: parseInt(process.env.DB_PORT || '5432'),
|
|
});
|
|
async function initDb() {
|
|
const client = await exports.pool.connect();
|
|
try {
|
|
await client.query(`
|
|
CREATE TABLE IF NOT EXISTS quotas (
|
|
id SERIAL PRIMARY KEY,
|
|
provider VARCHAR(50) NOT NULL,
|
|
used NUMERIC NOT NULL,
|
|
total NUMERIC NOT NULL,
|
|
percentage NUMERIC NOT NULL,
|
|
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
`);
|
|
await client.query(`
|
|
CREATE TABLE IF NOT EXISTS antigravity_latest (
|
|
key VARCHAR(100) PRIMARY KEY,
|
|
value JSONB NOT NULL,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
`);
|
|
console.log('Database initialized');
|
|
}
|
|
finally {
|
|
client.release();
|
|
}
|
|
}
|
|
async function saveAntigravityLatest(data) {
|
|
await exports.pool.query(`
|
|
INSERT INTO antigravity_latest (key, value, updated_at)
|
|
VALUES ('quota', $1, NOW())
|
|
ON CONFLICT (key) DO UPDATE SET value = $1, updated_at = NOW();
|
|
`, [JSON.stringify(data)]);
|
|
}
|
|
async function loadAntigravityLatest() {
|
|
const res = await exports.pool.query(`SELECT value FROM antigravity_latest WHERE key = 'quota';`);
|
|
if (res.rows.length === 0)
|
|
return null;
|
|
return res.rows[0].value;
|
|
}
|
|
async function insertQuota(entry) {
|
|
const query = `
|
|
INSERT INTO quotas (provider, used, total, percentage)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING *;
|
|
`;
|
|
const values = [entry.provider, entry.used, entry.total, entry.percentage];
|
|
const res = await exports.pool.query(query, values);
|
|
return res.rows[0];
|
|
}
|
|
async function getLatestQuotas() {
|
|
const query = `
|
|
SELECT DISTINCT ON (provider) *
|
|
FROM quotas
|
|
ORDER BY provider, timestamp DESC;
|
|
`;
|
|
const res = await exports.pool.query(query);
|
|
return res.rows;
|
|
}
|
|
async function getTodayStartQuota(provider) {
|
|
const query = `
|
|
SELECT *
|
|
FROM quotas
|
|
WHERE provider = $1
|
|
AND timestamp >= date_trunc('day', NOW() AT TIME ZONE 'UTC')
|
|
ORDER BY timestamp ASC
|
|
LIMIT 1;
|
|
`;
|
|
const res = await exports.pool.query(query, [provider]);
|
|
return res.rows[0];
|
|
}
|
|
async function getQuotaHistory(days = 7) {
|
|
const query = `
|
|
SELECT *
|
|
FROM quotas
|
|
WHERE timestamp > CURRENT_DATE - $1::integer
|
|
ORDER BY timestamp ASC;
|
|
`;
|
|
const res = await exports.pool.query(query, [days]);
|
|
return res.rows;
|
|
}
|
|
//# sourceMappingURL=db.js.map
|