Calculated variables

This commit is contained in:
2022-09-23 02:14:10 -04:00
parent cc1b8341d6
commit 2c28b85a10
3 changed files with 68 additions and 9 deletions

View File

@@ -79,7 +79,7 @@
</style>
<script>
import { uniq, groupByArray, quantile_sorted } from './util.mjs';
import { uniq, groupByArray, quantile_sorted, inherit } from './util.mjs';
function isNumeric(x) {
return (x !== '') && (x !== false) && (x !== null) && (!isNaN(x));
@@ -133,6 +133,14 @@
type: Boolean,
default: false
},
constants: {
type: Object,
default: {}
},
calculations: {
type: Array,
default: []
},
reports: {
type: Array,
default: []
@@ -142,8 +150,32 @@
return {};
},
computed: {
resultset_all() {
return this.resultset_calculated.length > 0 ? this.resultset.concat(this.resultset_calculated) : this.resultset;
},
resultset_calculated() {
var self = this, snapshots = [], results = [], history, update, item;
groupByArray(this.resultset, x => x.time).map(group => group.values.reduce(((acc, x) => (acc.values[x.name] = x, acc)), { key: group.key, datehdr: date_header(group.key), values: {}})).sort((a, b) => (a.key > b.key) - (a.key < b.key)).forEach(function(group) {
snapshots.push({ key: group.key, values: history = Object.assign(snapshots.length > 0 ? inherit(snapshots[snapshots.length - 1].values) : inherit(self.constants), update = group.values) });
history['Time'] = update['Time'] = { time: group.key, value: group.key };
for(var i = 0; i < self.calculations.length; ++i) {
var calculation = self.calculations[i], deps = calculation.deps;
for(var j = deps.length - 1, satisfied = true, updated = false; j >= 0; --j) if(!history[deps[j]]) { satisfied = false; break; }
else if(update[deps[j]]) updated = true;
if((satisfied) && (updated)) {
item = calculation.calc(...calculation.deps.map(x => history[x].value), history[calculation.name] && history[calculation.name].value);
if((item !== undefined) && (item !== null)) {
results.push(history[calculation.name] = update[calculation.name] = Object.assign({ time: group.key, value: item }, calculation));
if((item.hasOwnProperty('rangeL')) && (item.value < item.rangeL)) item.flag = 'L';
else if((item.hasOwnProperty('rangeH')) && (item.value > item.rangeH)) item.flag = 'H';
}
}
}
});
return results;
},
groups() {
if(this.daily) return groupByArray(this.resultset, x => new Date(x.time.getFullYear(), x.time.getMonth(), x.time.getDate())).map(function(group) {
if(this.daily) return groupByArray(this.resultset_all, x => new Date(x.time.getFullYear(), x.time.getMonth(), x.time.getDate())).map(function(group) {
group = group.values.reduce(((acc, x) => ((acc.values[x.name] || (acc.values[x.name] = [])).push(x), acc)), { key: group.key, datehdr: date_header(group.key), values: {}});
for(var k in group.values) if(group.values.hasOwnProperty(k)) {
var items = group.values[k].sort((a, b) => a.time - b.time);
@@ -165,10 +197,10 @@
}
return group;
}).sort((a, b) => (a.key > b.key) - (a.key < b.key));
else return groupByArray(this.resultset, x => x.time).map(group => group.values.reduce(((acc, x) => (acc.values[x.name] = x, acc)), { key: group.key, datehdr: date_header(group.key), values: {}})).sort((a, b) => (a.key > b.key) - (a.key < b.key));
else return groupByArray(this.resultset_all, x => x.time).map(group => group.values.reduce(((acc, x) => (acc.values[x.name] = x, acc)), { key: group.key, datehdr: date_header(group.key), values: {}})).sort((a, b) => (a.key > b.key) - (a.key < b.key));
},
names() {
var res = uniq(this.resultset.map(x => x.name));
var res = uniq(this.resultset_all.map(x => x.name));
return res.reduce((acc, x) => (acc[x] = true, acc), res);
},
names_excluded() {
@@ -177,7 +209,7 @@
return res;
},
statistics() {
return statistics(this.resultset);
return statistics(this.resultset_all);
}
},
watch: {