quintel / etmoses

Online decision support tool to create local energy situations for neighbourhoods, cities and regions with a time resolution of 15 minutes created and maintained by Quintel – Not maintained
https://moses.energytransitionmodel.com
MIT License
11 stars 3 forks source link

Adding initial templates #1515

Closed grdw closed 7 years ago

grdw commented 7 years ago

Pull request to extract topology templates and market model templates from used market models and topologies from unqiue LES's. A market model or topology in this setup is a clone from a template.

LES -> Topology     -> Topology Template
    -> Market Model -> Market Model Template

This PR is done for now.

Before merging; make backup of staging database.


Todo:

antw commented 7 years ago

I think theres an issue with the migratiosn. I took a copy of the staging DB, migrated it, and a lot of the market models reference templates (via market_model_template_id) which don't exist.

For example, a lot of the recent market models refer to a template with ID=325, but there is no such template; there is a 324 and 326, but no 325.

SELECT id, market_model_template_id
FROM market_models
WHERE market_model_template_id NOT IN (SELECT id FROM market_model_templates)

# 116 rows

This is also true of topologies:

SELECT id, topology_template_id
FROM topologies
WHERE topology_template_id NOT IN (SELECT id FROM topology_templates)

# 101 rows

I suppose it is acceptable for the market models and topologies to reference templates which no longer exist since the owner may have deleted them (although I would still expect the template_id keys to be NULL), but the views need to account for this. For example:

- if @testing_ground.market_model
  %span.sep
    = link_to @testing_ground.market_model.market_model_template.name, market_model_templates_path(@testing_ground.market_model)

This will raise undefined method 'name' for nil:NilClass because the market model exists, but the template does not. Perhaps MarketModel should have delegation methods to deal with this?

class MarketModel < ActiveRecord::Base
  delegate :name, to: :market_model_template, allow_nil: true
end
grdw commented 7 years ago

I would still expect the template_id keys to be NULL

I fixed this. When somebody removes a template, the associated market_models and topologies get 'detached' from their previously associated template. I also fixed the the submitting of featured with pundit policies. It took a bit of fiddling but it works fine 👍

The empty specs are removed.

antw commented 7 years ago

Nice work @grdw! Database backup has been made; I've merged this.