-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
311 lines (278 loc) · 10.1 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const axios = require("axios");
const fs = require("fs").promises;
const csv = require("csv-parse");
const { createReadStream } = require("fs");
const dotenv = require("dotenv");
const { chunk } = require("lodash");
dotenv.config();
class DHIS2DataTransfer {
constructor(sourceConfig, destConfig, batchSize = 1000) {
this.sourceApi = axios.create({
baseURL: sourceConfig.url.replace(/\/$/, ""),
auth: {
username: sourceConfig.username,
password: sourceConfig.password,
},
responseType: "arraybuffer",
});
this.destApi = axios.create({
baseURL: destConfig.url.replace(/\/$/, ""),
auth: {
username: destConfig.username,
password: destConfig.password,
},
});
this.batchSize = batchSize;
}
async downloadCSV(dataset, orgUnit, startDate, endDate) {
try {
console.log(
`Downloading CSV data for dataset ${dataset} and orgUnit ${orgUnit.name}...`,
);
const url = `/api/dataValueSets.csv?${dataset
.map((id) => `dataSet=${id}`)
.join("&")}&orgUnit=${
orgUnit.id
}&startDate=${startDate}&endDate=${endDate}`;
const response = await this.sourceApi.get(url);
const filename = `dhis2_data_${Date.now()}.csv`;
await fs.writeFile(filename, response.data);
console.log(`CSV downloaded successfully to ${filename}`);
return filename;
} catch (error) {
console.error("Error downloading CSV:", error.message);
throw error;
}
}
async getOrganisations() {
try {
console.log("Downloading organisation data...");
const {
data: { organisationUnits: ous1 },
} = await this.destApi.get(
`/api/organisationUnits.json?fields=id,name&paging=false&level=5`,
);
const {
data: { organisationUnits: ous2 },
} = await this.destApi.get(
`/api/organisationUnits.json?fields=id,name&paging=false&level=6`,
);
return [...ous1, ...ous2];
} catch (error) {
console.error("Error downloading CSV:", error.message);
throw error;
}
}
async processCSVStream(filePath) {
return new Promise((resolve, reject) => {
let currentBatch = [];
let totalProcessed = 0;
let batchNumber = 1;
let headers = null;
// Create read stream
const fileStream = fs.createReadStream(filePath);
// Create parser
const parser = csv.parse({
delimiter: ",",
trim: true,
skip_empty_lines: true,
});
// Handle stream events
const processRow = async (row) => {
try {
if (!headers) {
headers = row;
console.log("Headers:", headers);
return;
}
// Convert row to object using headers
const dataValue = {};
headers.forEach((header, index) => {
if (row[index]) {
dataValue[header] = row[index];
}
});
// Validate required fields
if (this.validateDataValue(dataValue)) {
currentBatch.push(dataValue);
totalProcessed++;
// Process batch if it reaches batch size
if (currentBatch.length >= this.batchSize) {
parser.pause(); // Pause parsing while uploading
await this.processBatch(currentBatch, batchNumber);
currentBatch = [];
batchNumber++;
parser.resume(); // Resume parsing
}
}
} catch (error) {
parser.destroy(error);
}
};
// Stream pipeline
fileStream
.pipe(parser)
.on("data", async (row) => {
try {
await processRow(row);
} catch (error) {
parser.destroy(error);
}
})
.on("end", async () => {
try {
// Process any remaining data in the last batch
if (currentBatch.length > 0) {
await this.processBatch(currentBatch, batchNumber);
}
console.log(
`Completed processing ${totalProcessed} records in ${batchNumber} batches`,
);
resolve({
status: "success",
totalProcessed,
batches: batchNumber,
});
} catch (error) {
reject(error);
}
})
.on("error", (error) => {
console.error("Error processing CSV:", error);
reject(error);
});
});
}
async uploadJSONBatch(batch, batchNumber, totalBatches) {
try {
console.log(
`Uploading batch ${batchNumber}/${totalBatches} (${batch.length} records)...`,
);
const response = await this.destApi.post(
"/api/dataValueSets",
{ dataValues: batch },
{
headers: {
"Content-Type": "application/json",
},
},
);
console.log(
`Batch ${batchNumber} upload completed:`,
response.data,
);
return response.data;
} catch (error) {
console.error(
`Error uploading batch ${batchNumber}:`,
error.message,
);
if (error.response) {
console.error("Response:", error.response.data);
}
throw error;
}
}
async transferData(dataset, startDate, endDate) {
let csvFile = null;
// try {
const orgs = await this.getOrganisations();
for (const orgUnit of orgs) {
// Step 1: Download CSV
csvFile = await this.downloadCSV(
dataset,
orgUnit,
startDate,
endDate,
);
// Step 2: Stream and convert CSV to JSON
console.log("Converting CSV to JSON...");
const dataValues = await this.streamCSVToJSON(csvFile);
// console.log(dataValues);
// if (dataValues.length === 0) {
// console.log("No valid data values found in CSV");
// // return {
// // status: "completed",
// // message: "No data to transfer",
// // };
// }
// Step 3: Upload in batches
// const batches = chunk(dataValues, this.batchSize);
// for (let i = 0; i < dataValues.length; i += this.batchSize) {
// batches.push(dataValues.slice(i, i + this.batchSize));
// }
// console.log(`Preparing to upload ${batches.length} batches...`);
// const results = [];
// for (let i = 0; i < batches.length; i++) {
// const result = await this.uploadJSONBatch(
// batches[i],
// i + 1,
// batches.length,
// );
// results.push(result);
// }
// // Step 4: Cleanup
// await fs.unlink(csvFile);
// console.log({
// status: "completed",
// totalRecords: dataValues.length,
// totalBatches: batches.length,
// results: results,
// });
}
// } catch (error) {
// console.error("Transfer failed:", error.message);
// if (csvFile) {
// try {
// await fs.unlink(csvFile);
// } catch (cleanupError) {
// console.error(
// "Error cleaning up file:",
// cleanupError.message,
// );
// }
// }
// throw error;
// }
}
}
// Example usage
async function main() {
try {
const sourceConfig = {
url: process.env.SOURCE_DHIS2_URL,
username: process.env.SOURCE_DHIS2_USERNAME,
password: process.env.SOURCE_DHIS2_PASSWORD,
};
const destConfig = {
url: process.env.DEST_DHIS2_URL,
username: process.env.DEST_DHIS2_USERNAME,
password: process.env.DEST_DHIS2_PASSWORD,
};
const transfer = new DHIS2DataTransfer(sourceConfig, destConfig, 1000);
const result = await transfer.transferData(
[
"onFoQ4ko74y",
"RtEYsASU7PG",
"ic1BSWhGOso",
"nGkMm2VBT4G",
"VDhwrW9DiC1",
"quMWqLxzcfO",
"dFRD2A5fdvn",
"DFMoIONIalm",
"EBqVAQRmiPm",
],
"2024-01-01",
"2024-10-31",
);
console.log("Transfer completed:", result);
} catch (error) {
console.error("Main process error:", error.message);
process.exit(1);
}
}
// Run the script if called directly
if (require.main === module) {
main();
}
module.exports = DHIS2DataTransfer;