-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·168 lines (151 loc) · 5.24 KB
/
index.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
#!/usr/bin/env node
const fs = require("fs");
const csvParse = require("csv-parser");
const moment = require("moment");
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const { writeToPath } = require("fast-csv");
const argv = yargs(hideBin(process.argv))
.option("input", {
alias: "i",
describe: "Input CSV file path",
type: "string",
demandOption: true,
})
.option("output", {
alias: "o",
describe: "Output CSV file path",
type: "string",
default: "meter_output.csv",
})
.option("cost", {
alias: "c",
describe: "Cost multiplier for total cost calculation",
type: "number",
default: 0.6007,
}).argv;
const inputFile = argv.input;
const outputFile = argv.output;
const costMultiplier = argv.cost;
const processedRows = [];
const totals = {};
const processRow = (row) => {
const datetime = `${row[0]} ${row[1]}`;
const dateTime = moment(datetime, "DD/MM/YYYY HH:mm", true);
const kwh = parseFloat(row[2]);
if (!dateTime.isValid() || isNaN(kwh)) {
return null;
}
const dayOfWeek = dateTime.isoWeekday();
const hour = dateTime.hour();
const month = dateTime.month() + 1;
return {
DateTime: datetime,
KWH: kwh,
Pazgas24: kwh * 0.93,
ElectraPower: kwh * 0.95,
ElectraHitech: hour >= 23 || hour < 17 ? kwh * 0.92 : kwh,
ElectraNight: hour >= 23 || hour < 7 ? kwh * 0.8 : kwh,
Amisragaz24: kwh * 0.93,
Cellcom24: kwh * 0.95,
CellcomDay: dayOfWeek <= 5 && hour >= 7 && hour < 17 ? kwh * 0.85 : kwh,
CellcomFamily: hour >= 14 && hour < 20 ? kwh * 0.82 : kwh,
CellcomNight: hour >= 23 || hour < 7 ? kwh * 0.8 : kwh,
Bezeq24: kwh * 0.93,
BezeqDay: dayOfWeek <= 5 && hour >= 7 && hour < 17 ? kwh * 0.85 : kwh,
BezeqNight: dayOfWeek <= 5 && (hour >= 23 || hour < 7) ? kwh * 0.8 : kwh,
Taoz:
((month >= 6 && month <= 9
? dayOfWeek === 6 ||
dayOfWeek === 7 ||
(hour >= 0 && hour < 17) ||
hour === 23
? 0.4815
: 1.6533
: month === 12 || month <= 2
? hour >= 17 && hour < 22
? 1.1478
: 0.4184
: dayOfWeek <= 5 && hour >= 17 && hour < 22
? 0.4583
: 0.4084) /
costMultiplier) *
kwh,
};
};
fs.createReadStream(inputFile)
.pipe(csvParse({ headers: false }))
.on("data", (row) => {
const processed = processRow(row);
if (processed) {
processedRows.push(processed);
Object.keys(processed).forEach((key) => {
if (key !== "DateTime") {
// Exclude DateTime from totals
totals[key] = (totals[key] || 0) + processed[key];
}
});
}
})
.on("end", () => {
const totalKwhCost = totals["KWH"] * costMultiplier;
const totalCosts = {};
const totalDiscounts = {};
const discountPercentage = {};
Object.keys(totals).forEach((key) => {
if (key !== "DateTime") {
// Exclude DateTime from totals
// Calculate total cost for each key
totalCosts[key] = totals[key] * costMultiplier;
}
});
Object.keys(totalCosts).forEach((key) => {
if (key !== "KWH" && key !== "DateTime") {
// Exclude KWH and DateTime from discounts
// Calculate total discounts for each key
totalDiscounts[key] = totalKwhCost - totalCosts[key];
// Calculate discount percent for each key
discountPercentage[key] = (totalDiscounts[key] / totalKwhCost) * 100;
} else {
totalDiscounts[key] = 0; // No discount for KWH itself
discountPercentage[key] = 0; // No discount percent for KWH itself
}
});
// Add total rows for kWh, costs, discounts, and discount percentage
totals["DateTime"] = "Total KWH";
totalCosts["DateTime"] = "Total Costs";
totalDiscounts["DateTime"] = "Total Discounts";
discountPercentage["DateTime"] = "Discount Percentage";
processedRows.push(totals);
processedRows.push(totalCosts);
processedRows.push(totalDiscounts);
processedRows.push(discountPercentage);
// Write processed rows to output file
writeToPath(outputFile, processedRows, { headers: true })
.on("finish", () => {
console.log(`Processing complete. Output saved to ${outputFile}`);
// Calculate and print summary of all discounted plans
const discountArray = Object.keys(discountPercentage).map((key) => ({
plan: key,
discountPercentage: discountPercentage[key],
}));
// Remove 'DateTime' and 'KWH' from the list for sorting
const discountsExcludingDateTimeAndKWH = discountArray.filter(
(item) => item.plan !== "DateTime" && item.plan !== "KWH"
);
// Sort in descending order of discount percentages
discountsExcludingDateTimeAndKWH.sort(
(a, b) => b.discountPercentage - a.discountPercentage
);
console.log("All plans, sorted by discount:");
discountsExcludingDateTimeAndKWH.forEach((plan, index) => {
console.log(
`${index + 1}. ${plan.plan}: ${plan.discountPercentage.toFixed(2)}%`
);
});
})
.on("error", (err) =>
console.error("Error writing processed data to CSV:", err)
);
})
.on("error", (err) => console.error("Error reading input CSV:", err));