export function getItems(data) { let filteredData = data.filter((metric) => { return metric.MetricValue !== 'nan'; }); let transform = filteredData.map((metric) => { let date = new Date(metric.Timestamp); return { ...metric, Timestamp: { time: date.getHours() + ':' + String(date.getMinutes()).padStart(2, '0'), date: formatDate(date), }, MetricValue: Math.round(metric.MetricValue), }; }); let group = transform.reduce(function (rv, x) { (rv[x['MetricProperty']] = rv[x['MetricProperty']] || []).push(x); return rv; }, {}); return Object.keys(group).map((metric) => { return { name: metric, current: group[metric][group[metric].length - 1].MetricValue, middle: findAverage(group[metric]), min: findMin(group[metric]), minDate: findDateOfMin(group[metric]), max: findMax(group[metric]), maxDate: findDateOfMax(group[metric]), }; }); } function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return [ padTo2Digits(date.getDate()), padTo2Digits(date.getMonth() + 1), date.getFullYear(), ].join('.'); } function findAverage(arr) { const { length } = arr; return Math.round( arr.reduce((acc, val) => { return acc + val.MetricValue / length; }, 0) ); } function findMin(arr) { return arr.reduce( (min, p) => (p.MetricValue < min ? p.MetricValue : min), arr[0].MetricValue ); } function findMax(arr) { return arr.reduce( (max, p) => (p.MetricValue > max ? p.MetricValue : max), arr[0].MetricValue ); } function findDateOfMin(arr) { return arr.reduce((res, obj) => obj.MetricValue < res.MetricValue ? obj : res ).Timestamp; } function findDateOfMax(arr) { return arr.reduce((res, obj) => obj.MetricValue > res.MetricValue ? obj : res ).Timestamp; }