Skip to content

Commit 9910ca4

Browse files
Merge pull request #3 from artefactory/refactor/dataPatchEmissions/patch-experimental-data
refactor(dataPatchEmissions): refactor the script to inject carbon footprint data in database
2 parents 3fa13aa + a02b6d8 commit 9910ca4

3 files changed

Lines changed: 519 additions & 74 deletions

File tree

src/cmd/dataPatchEmissions.ts

Lines changed: 45 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,57 @@
11
import ProgressBar from 'progress';
22
import config from '../config';
3+
import emissionsCoef from '../data/emissions/coefficients.json';
34
import { PoolClient } from 'pg';
45

5-
const batchSize = 10000;
6-
76
async function run(): Promise<void> {
8-
const pool = await config.pg();
9-
10-
const client = await pool.connect();
11-
try {
12-
// Fetch all product hashes
13-
config.logger.info('Fetching all product hashes');
14-
const productHashes = await getProductHashes(client);
15-
16-
const progressBar = new ProgressBar(
17-
'-> loading [:bar] :percent (:etas remaining)',
18-
{
19-
width: 40,
20-
complete: '=',
21-
incomplete: ' ',
22-
renderThrottle: 500,
23-
total: productHashes.length,
24-
}
25-
);
26-
27-
// For each product hash, insert by batch the emissions data into the product table
28-
config.logger.info('Updating product emissions');
29-
for (let i = 0; i < productHashes.length; i += batchSize) {
30-
const batch = productHashes.slice(i, i + batchSize);
31-
32-
await client.query('BEGIN');
33-
34-
await Promise.all(batch.map((productHash) => {
35-
updateProductEmissions(client, productHash);
36-
progressBar.tick();
37-
}));
38-
39-
await client.query('COMMIT');
40-
}
41-
42-
} catch (e) {
43-
await client.query('ROLLBACK');
44-
45-
throw e;
7+
const pool = await config.pg();
8+
9+
const client = await pool.connect();
10+
11+
const progressBar = new ProgressBar(
12+
'-> loading [:bar] :percent (:etas remaining)',
13+
{
14+
width: 40,
15+
complete: '=',
16+
incomplete: ' ',
17+
renderThrottle: 500,
18+
total: Object.keys(emissionsCoef).length,
4619
}
20+
);
4721

48-
}
49-
50-
async function getProductHashes(client: PoolClient): Promise<string[]> {
51-
const result = await client.query(
52-
`
53-
SELECT "productHash"
54-
FROM "products"
55-
`
56-
);
57-
58-
return result.rows.map((row) => row.productHash);
59-
}
22+
// For each product hash, insert by batch the emissions data into the product table
23+
config.logger.info('Updating product emissions');
6024

61-
async function updateProductEmissions(client: PoolClient, productHash: string): Promise<void> {
62-
const emissionsData = generateEmissionsData();
63-
await client.query(
64-
`
65-
UPDATE "products"
66-
SET "emissions" = $1
67-
WHERE "productHash" = $2
68-
`,
69-
[emissionsData, productHash]
70-
);
25+
for (const [key, coefficient] of Object.entries(emissionsCoef)) {
26+
const [_, skuId, region] = key.split('_');
27+
await updateProductEmissions(client, skuId, region, coefficient);
28+
progressBar.tick();
29+
}
7130
}
7231

73-
const generateEmissionsData = () => {
74-
return JSON.stringify([{
75-
emissionHash: 'SampleEmissionHash',
76-
unit: 'kgCO2e',
77-
emissions: Math.random() * 100,
78-
effectiveDateStart: '2020-01-01',
79-
effectiveDateEnd: '2024-12-31',
80-
startUsageAmount: 0,
81-
endUsageAmount: 100,
82-
description: 'Sample Description',
83-
}]);
32+
const updateProductEmissions = async (
33+
client: PoolClient,
34+
skuId: string,
35+
region: string,
36+
coefficient: number
37+
): Promise<void> => {
38+
const emissionData = JSON.stringify([
39+
{
40+
emissionHash: 'emissionHash',
41+
unit: 'kgCO2e',
42+
emissions: coefficient,
43+
startUsageAmount: 0,
44+
},
45+
]);
46+
47+
await client.query(
48+
`
49+
UPDATE "products"
50+
SET "emissions" = $1
51+
WHERE "sku" = $2 AND "region" = $3
52+
`,
53+
[emissionData, skuId, region]
54+
);
8455
};
8556

8657
config.logger.info('Starting: loading data into DB');
@@ -92,4 +63,4 @@ run()
9263
.catch((err) => {
9364
config.logger.error(err);
9465
process.exit(1);
95-
});
66+
});

0 commit comments

Comments
 (0)