From 19ea51f121481272e1deba57bb3b04d80995f71e Mon Sep 17 00:00:00 2001 From: Jon Ziebell Date: Thu, 20 Jun 2019 21:37:19 -0400 Subject: [PATCH] Fixed #18 - Aggregate runtime - grouping by week causes odd spacing for months. I did a terrible job of group by week; fixing it. --- api/ecobee_runtime_thermostat.php | 23 +++++++++++++--- js/component/card/aggregate_runtime.js | 37 +++++++++++++++++++++----- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/api/ecobee_runtime_thermostat.php b/api/ecobee_runtime_thermostat.php index fea71b5..fec7c73 100644 --- a/api/ecobee_runtime_thermostat.php +++ b/api/ecobee_runtime_thermostat.php @@ -328,15 +328,26 @@ class ecobee_runtime_thermostat extends cora\crud { $select = []; $group_by_order_by = []; switch($group_by) { + case 'week': + /** + * Week is a special case. If grouping by week, month, year, you can + * get one week listed twice if it spans across months. So the month + * group by is undesirable in this case. + * + * The second argument of 3 to the yearweek() function will cause + * MySQL to return the ISO 8601 week of the year. + * + * https://stackoverflow.com/a/11804076 + */ + $select[] = 'yearweek(`timestamp`, 3) `yearweek`'; + $group_by_order_by[] = 'yearweek(`timestamp`, 3)'; + break; case 'hour': $select[] = 'hour(`timestamp`) `hour`'; $group_by_order_by[] = 'hour(`timestamp`)'; case 'day': $select[] = 'day(`timestamp`) `day`'; $group_by_order_by[] = 'day(`timestamp`)'; - case 'week': - $select[] = 'week(`timestamp`) `week`'; - $group_by_order_by[] = 'week(`timestamp`)'; case 'month': $select[] = 'month(`timestamp`) `month`'; $group_by_order_by[] = 'month(`timestamp`)'; @@ -397,6 +408,12 @@ class ecobee_runtime_thermostat extends cora\crud { } } + if(isset($row['yearweek']) === true) { + $row['year'] = (int) substr($row['yearweek'], 0, 4); + $row['week'] = (int) substr($row['yearweek'], 4); + unset($row['yearweek']); + } + $return[] = $row; } diff --git a/js/component/card/aggregate_runtime.js b/js/component/card/aggregate_runtime.js index 2a3d40c..b038d97 100644 --- a/js/component/card/aggregate_runtime.js +++ b/js/component/card/aggregate_runtime.js @@ -70,9 +70,20 @@ beestat.component.card.aggregate_runtime.prototype.decorate_contents_ = function var date_parts = this.value.match(/(?:h(\d+))?(?:d(\d+))?(?:w(\d+))?(?:m(\d+))?(?:y(\d+))?/); var hour = moment(date_parts[1], 'H').format('ha'); var day = date_parts[2]; - var week = date_parts[3]; var month = moment(date_parts[4], 'M').format('MMM'); - var year = date_parts[5]; + + var year; + var week; + if (beestat.setting('aggregate_runtime_group_by') === 'week') { + // ISO 8601 week of the year. + var yearweek_m = moment().isoWeek(date_parts[3]) + .year(date_parts[5]) + .day('Monday'); + week = yearweek_m.format('MMM D'); + year = yearweek_m.format('YYYY'); + } else { + year = date_parts[5]; + } var label_parts = []; switch (beestat.setting('aggregate_runtime_group_by')) { @@ -86,8 +97,8 @@ beestat.component.card.aggregate_runtime.prototype.decorate_contents_ = function } break; case 'week': - if (month !== current_month) { - label_parts.push(month); + if (week !== current_week) { + label_parts.push(week); } if (year !== current_year) { label_parts.push(year); @@ -211,9 +222,20 @@ beestat.component.card.aggregate_runtime.prototype.decorate_contents_ = function var date_parts = this.x.match(/(?:h(\d+))?(?:d(\d+))?(?:w(\d+))?(?:m(\d+))?(?:y(\d+))?/); var hour = moment(date_parts[1], 'H').format('ha'); var day = date_parts[2]; - var week = date_parts[3]; var month = moment(date_parts[4], 'M').format('MMM'); - var year = date_parts[5]; + + var year; + var week; + if (beestat.setting('aggregate_runtime_group_by') === 'week') { + // ISO 8601 week of the year. + var yearweek_m = moment().isoWeek(date_parts[3]) + .year(date_parts[5]) + .day('Monday'); + week = yearweek_m.format('MMM D'); + year = yearweek_m.format('YYYY'); + } else { + year = date_parts[5]; + } var label_parts = []; switch (beestat.setting('aggregate_runtime_group_by')) { @@ -225,7 +247,8 @@ beestat.component.card.aggregate_runtime.prototype.decorate_contents_ = function label_parts.push(year); break; case 'week': - label_parts.push(month); + label_parts.push('Week of'); + label_parts.push(week + ','); label_parts.push(year); break; case 'day':