forked from GeorgeVasiliadis/CleanEmon-Populator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.json
20 lines (20 loc) · 3.16 KB
/
views.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"_id": "_design/api",
"views": {
"daily_consumption": {
"map": "function(doc) {\n if (doc.energy_data) {\n var firstPower = null;\n var lastPower = null;\n var kwh;\n for (var i = 0; i < doc.energy_data.length; i++) {\n kwh = doc.energy_data[i].kwh;\n if (kwh !== null) {\n if (firstPower === null) {\n firstPower = kwh;\n break;\n }\n }\n }\n for (i = doc.energy_data.length - 1; i >= 0; i--) {\n kwh = doc.energy_data[i].kwh;\n if (kwh !== null) {\n lastPower = kwh;\n break;\n }\n }\n if (firstPower !== null && lastPower !== null) {\n emit(doc.date, lastPower - firstPower);\n }\n }\n}\n",
"reduce": "_sum"
},
"last_energy_data_record": {
"map": "function (doc) {\n if (doc.energy_data && doc.energy_data.length > 0) {\n emit(doc.date, doc.energy_data[doc.energy_data.length - 1]);\n }\n}\n"
},
"lower_res": {
"map": "function(doc) {\n if (doc.date && doc.energy_data) {\n var numDataPoints = doc.energy_data.length;\n var originalSamplingRate = doc.energy_data[1].timestamp - doc.energy_data[0].timestamp; // Original sampling rate in seconds\n var targetSamplingRate = 120; // Target sampling rate in seconds\n\n var downsampleFactor = Math.ceil(targetSamplingRate / originalSamplingRate);\n\n var lower_resolution_data = [];\n\n for (var i = 0; i < numDataPoints; i += downsampleFactor) {\n lower_resolution_data.push(doc.energy_data[i]);\n }\n \n var response = {\n \"date\": doc.date,\n \"energy_data\": lower_resolution_data\n };\n\n emit(doc._id, response);\n }\n}\n"
},
"pred_consumption": {
"map": "function(doc) {\n if (doc.energy_data) {\n var consumption = {};\n var timeIntervalSeconds = doc.energy_data[1].timestamp - doc.energy_data[0].timestamp; // Time interval in seconds\n var timeIntervalHours = timeIntervalSeconds / 3600; // Convert seconds to hours\n for (var i = 0; i < doc.energy_data.length; i++) {\n var record = doc.energy_data[i];\n for (var key in record) {\n if (key.startsWith(\"pred_\") || key === 'noise') {\n var power = record[key];\n if (power !== null) {\n if (!consumption[key]) {\n consumption[key] = 0;\n }\n consumption[key] += (power * timeIntervalHours) / 1000; // Convert watts to kilowatt-hours\n }\n }\n }\n }\n \n var firstPower = null;\n var lastPower = null;\n var kwh;\n for (var i = 0; i < doc.energy_data.length; i++) {\n kwh = doc.energy_data[i].kwh;\n if (kwh !== null) {\n if (firstPower === null) {\n firstPower = kwh;\n break;\n }\n }\n }\n for (i = doc.energy_data.length - 1; i >= 0; i--) {\n kwh = doc.energy_data[i].kwh;\n if (kwh !== null) {\n lastPower = kwh;\n break;\n }\n }\n if (firstPower !== null && lastPower !== null) {\n consumption['real_consumption'] = lastPower - firstPower;\n }\n \n \n emit(doc._id, consumption);\n }\n}\n",
"reduce": "_sum"
}
},
"language": "javascript"
}