← back to Patternbank Archive

scripts/stats.js

33 lines

#!/usr/bin/env node
require('dotenv').config({ path: require('path').join(__dirname, '..', '.env') });
const db = require('../src/db');
(async () => {
  const sections = [
    ['queue',    `SELECT kind, COUNT(*) FILTER (WHERE done=FALSE) AS pending,
                          COUNT(*) FILTER (WHERE done=TRUE) AS done,
                          COUNT(*) AS total
                     FROM crawl_queue GROUP BY kind ORDER BY kind`],
    ['patterns', `SELECT COUNT(*) AS patterns,
                          COUNT(DISTINCT designer_slug) AS designers,
                          COUNT(*) FILTER (WHERE category IS NOT NULL) AS with_category,
                          COUNT(*) FILTER (WHERE cardinality(tags)>0) AS with_tags
                     FROM patterns`],
    ['images',   `SELECT COUNT(*) AS rows,
                          COUNT(*) FILTER (WHERE local_path IS NOT NULL) AS downloaded,
                          COALESCE(SUM(bytes),0) AS bytes
                     FROM pattern_images`],
    ['runs',     `SELECT mode, COUNT(*) AS runs,
                          MAX(finished_at) AS last_finish,
                          SUM(patterns_upserted) AS patterns_upserted,
                          SUM(images_downloaded) AS images_downloaded,
                          SUM(errors) AS errors
                     FROM ingest_runs GROUP BY mode ORDER BY mode`],
  ];
  for (const [name, sql] of sections) {
    console.log(`\n## ${name}`);
    const r = await db.q(sql);
    console.table(r.rows);
  }
  await db.pool.end();
})();