-
Notifications
You must be signed in to change notification settings - Fork 0
/
backlog.js
62 lines (58 loc) · 2.62 KB
/
backlog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { Pool } = require("pg");
const Cursor = require("pg-cursor");
const df = require("date-fns");
const { processAndInsert, batchSize } = require("./common.js");
const dotenv = require("dotenv");
dotenv.config();
const args = process.argv.slice(2);
function getDatesInRange(startDate, endDate) {
const date = new Date(startDate.getTime());
const dates = [];
while (date <= endDate) {
dates.push(new Date(date));
date.setDate(date.getDate() + 1);
}
return dates;
}
const pool = new Pool({
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
host: process.env.PG_HOST,
port: process.env.PG_PORT,
database: process.env.PG_DATABASE,
});
const processData = async () => {
const dates = getDatesInRange(new Date(args[0]), new Date(args[1]))
.sort(df.compareAsc)
.map((d) => [
df.format(d, "yyyy-MM-dd"),
df.format(df.addDays(d, 1), "yyyy-MM-dd"),
]);
const client = await pool.connect();
try {
for (const [start, end] of dates) {
console.log(`Working on ${start}`);
const cursor = client.query(
new Cursor(
`select o.uid ou,o.name,o.path,psi.programstageinstanceid::text,psi.uid,to_char(psi.created,'YYYY-MM-DD') created,to_char(psi.created,'MM') m,to_char(psi.lastupdated,'YYYY-MM-DD') lastupdated,programinstanceid::text,programstageid::text,attributeoptioncomboid::text,psi.deleted,psi.storedby,to_char(duedate,'YYYY-MM-DD') duedate,to_char(executiondate,'YYYY-MM-DD') executiondate,psi.organisationunitid::text,status,completedby,to_char(completeddate,'YYYY-MM-DD') completeddate,eventdatavalues->'bbnyNYD1wgS'->>'value' as vaccine,eventdatavalues->'LUIsbsm3okG'->>'value' as dose,assigneduserid::text,psi.createdbyuserinfo,psi.lastupdatedbyuserinfo from programstageinstance psi inner join organisationunit o using(organisationunitid) where psi.created >= '${start}' and psi.created < '${end}' and programstageid = 12715`
)
);
let rows = await cursor.read(batchSize);
if (rows.length > 0) {
await processAndInsert("programstageinstance", rows);
}
while (rows.length > 0) {
rows = await cursor.read(batchSize);
if (rows.length > 0) {
await processAndInsert("programstageinstance", rows);
}
}
console.log(`Finished working on ${start}`);
}
} catch (error) {
console.log(error.message);
} finally {
client.release();
}
};
processData().then(() => console.log("Done"));