1
0
mirror of https://github.com/beestat/app.git synced 2025-05-24 02:14:03 -04:00

Fixed #320 - Profiles don't always generate and sometimes break GUI

This commit is contained in:
Jon Ziebell 2021-02-03 22:35:50 -05:00
parent 2ae9a05963
commit ae3a72c791
4 changed files with 79 additions and 23 deletions

View File

@ -243,7 +243,7 @@ class thermostat extends cora\crud {
* @param int $thermostat_id
*/
public function generate_profile($thermostat_id) {
return $this->update([
$this->update([
'thermostat_id' => $thermostat_id,
'profile' => $this->api('profile', 'generate', $thermostat_id)
]);

View File

@ -88,16 +88,24 @@ beestat.component.card.comparison_settings.prototype.decorate_contents_ = functi
api.send();
}, 10000);
} else if (beestat.cache.data.metrics === undefined) {
this.show_loading_('Fetching');
} else {
if (thermostat.profile === null) {
this.show_loading_('Fetching');
new beestat.api()
.add_call(
'thermostat',
'generate_profile',
{
'thermostat_id': this.thermostat_id_
}
)
.add_call(
'thermostat',
'read_id',
{
'attributes': {
'inactive': 0
}
},
'thermostat'
)
@ -105,6 +113,21 @@ beestat.component.card.comparison_settings.prototype.decorate_contents_ = functi
beestat.cache.set('thermostat', response.thermostat);
})
.send();
} else if (beestat.cache.data.metrics === undefined) {
this.show_loading_('Fetching');
new beestat.api()
.add_call(
'thermostat',
'get_metrics',
{
'thermostat_id': this.thermostat_id_,
'attributes': beestat.comparisons.get_attributes()
}
)
.set_callback(function(response) {
beestat.cache.set('data.metrics', response);
})
.send();
}
}
};

View File

@ -27,20 +27,6 @@ beestat.component.card.metrics = function(thermostat_id) {
);
beestat.component.card.apply(this, arguments);
new beestat.api()
.add_call(
'thermostat',
'get_metrics',
{
'thermostat_id': this.thermostat_id_,
'attributes': beestat.comparisons.get_attributes()
}
)
.set_callback(function(response) {
beestat.cache.set('data.metrics', response);
})
.send();
};
beestat.extend(beestat.component.card.metrics, beestat.component.card);
@ -196,6 +182,11 @@ beestat.component.card.metrics.prototype.decorate_top_right_ = function(parent)
beestat.component.card.metrics.prototype.get_subtitle_ = function() {
const thermostat = beestat.cache.thermostat[this.thermostat_id_];
// If the profile has not yet been generated.
if (thermostat.profile === null) {
return null;
}
const generated_at_m = moment(
thermostat.profile.metadata.generated_at
);
@ -214,7 +205,7 @@ beestat.component.card.metrics.prototype.get_subtitle_ = function() {
} else {
duration_text += duration_weeks + ' weeks';
}
duration_text += ' of data.';
duration_text += ' of data';
return 'Generated ' + generated_at_m.format('MMM Do @ h a') + ' (updated weekly) ' + duration_text;
return 'Generated ' + generated_at_m.format('MMM Do @ h a') + duration_text + ' (updated weekly).';
};

View File

@ -1,5 +1,5 @@
/**
* Temperature profiles.
* Temperature Profiles.
*
* @param {number} thermostat_id The thermostat_id this card is displaying
* data for.
@ -7,6 +7,22 @@
beestat.component.card.temperature_profiles = function(thermostat_id) {
this.thermostat_id_ = thermostat_id;
var self = this;
/*
* Debounce so that multiple setting changes don't re-trigger the same
* event. This fires on the trailing edge so that all changes are accounted
* for when rerendering.
*/
var change_function = beestat.debounce(function() {
self.rerender();
}, 10);
beestat.dispatcher.addEventListener(
['cache.thermostat'],
change_function
);
beestat.component.card.apply(this, arguments);
};
beestat.extend(beestat.component.card.temperature_profiles, beestat.component.card);
@ -49,8 +65,29 @@ beestat.component.card.temperature_profiles.prototype.get_data_ = function() {
if (
thermostat.profile === null
) {
this.chart_.render(parent);
this.show_loading_('Fetching');
new beestat.api()
.add_call(
'thermostat',
'generate_profile',
{
'thermostat_id': this.thermostat_id_
}
)
.add_call(
'thermostat',
'read_id',
{
'attributes': {
'inactive': 0
}
},
'thermostat'
)
.set_callback(function(response) {
beestat.cache.set('thermostat', response.thermostat);
})
.send();
} else {
// Global x range.
var x_min = Infinity;
@ -202,6 +239,11 @@ beestat.component.card.temperature_profiles.prototype.get_title_ = function() {
beestat.component.card.temperature_profiles.prototype.get_subtitle_ = function() {
const thermostat = beestat.cache.thermostat[this.thermostat_id_];
// If the profile has not yet been generated.
if (thermostat.profile === null) {
return null;
}
const generated_at_m = moment(
thermostat.profile.metadata.generated_at
);
@ -220,9 +262,9 @@ beestat.component.card.temperature_profiles.prototype.get_subtitle_ = function()
} else {
duration_text += duration_weeks + ' weeks';
}
duration_text += ' of data.';
duration_text += ' of data';
return 'Generated ' + generated_at_m.format('MMM Do @ h a') + ' (updated weekly) ' + duration_text;
return 'Generated ' + generated_at_m.format('MMM Do @ h a') + duration_text + ' (updated weekly).';
};
/**