|
|
@@ -81,6 +81,13 @@ const DDL = [
|
|
|
report jsonb NOT NULL
|
|
|
)`,
|
|
|
`CREATE INDEX IF NOT EXISTS ops_report_created_idx ON ops_report (created_at DESC)`,
|
|
|
+ `CREATE TABLE IF NOT EXISTS ops_report_cache (
|
|
|
+ cache_key text PRIMARY KEY,
|
|
|
+ updated_at timestamptz NOT NULL,
|
|
|
+ data_version text NOT NULL,
|
|
|
+ source text NOT NULL DEFAULT 'rules',
|
|
|
+ report jsonb NOT NULL
|
|
|
+ )`,
|
|
|
];
|
|
|
|
|
|
function rowToApi(row) {
|
|
|
@@ -367,6 +374,25 @@ function createPgStore({ connectionString, ssl = true, max = 5, schema = 'public
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ async function getReportCache(cacheKey, dataVersion) {
|
|
|
+ const r = await getPool().query('SELECT report, source, data_version FROM ops_report_cache WHERE cache_key = $1', [cacheKey]);
|
|
|
+ if (!r.rows.length || r.rows[0].data_version !== dataVersion) return null;
|
|
|
+ return { report: r.rows[0].report, source: r.rows[0].source };
|
|
|
+ }
|
|
|
+
|
|
|
+ async function saveReportCache(record) {
|
|
|
+ await getPool().query(
|
|
|
+ `INSERT INTO ops_report_cache (cache_key, updated_at, data_version, source, report)
|
|
|
+ VALUES ($1,$2,$3,$4,$5)
|
|
|
+ ON CONFLICT (cache_key) DO UPDATE SET
|
|
|
+ updated_at = EXCLUDED.updated_at,
|
|
|
+ data_version = EXCLUDED.data_version,
|
|
|
+ source = EXCLUDED.source,
|
|
|
+ report = EXCLUDED.report`,
|
|
|
+ [record.cacheKey, record.updatedAt, record.dataVersion, record.source, JSON.stringify(record.report)],
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
async function insertReport(record) {
|
|
|
await getPool().query(
|
|
|
`INSERT INTO ops_report (id, created_at, period_from, period_to, schools, shops, source, model, title, report)
|
|
|
@@ -420,6 +446,8 @@ function createPgStore({ connectionString, ssl = true, max = 5, schema = 'public
|
|
|
findUploadBySha,
|
|
|
queryPlatformRows,
|
|
|
insertReport,
|
|
|
+ getReportCache,
|
|
|
+ saveReportCache,
|
|
|
listReports,
|
|
|
getReport,
|
|
|
deleteReport,
|