Add scheme-based SECTOR/MSECTOR lookups and mapped EMISSIONS form#1777
Draft
louispt1 wants to merge 3 commits into
Draft
Add scheme-based SECTOR/MSECTOR lookups and mapped EMISSIONS form#1777louispt1 wants to merge 3 commits into
louispt1 wants to merge 3 commits into
Conversation
This was referenced Jul 8, 2026
noracato
reviewed
Jul 10, 2026
|
|
||
| # Public: The inverted mapping index, keyed by scheme then normalized value. | ||
| def mapping | ||
| Rails.cache.fetch('sector_mapping_hash') do |
Member
There was a problem hiding this comment.
Depending on how much the new sector is used, we might want to use process memory (NastyCache) vs the Rails cache
noracato
reviewed
Jul 10, 2026
noracato
left a comment
Member
There was a problem hiding this comment.
Nice work! Already left a few questions for you to think about.
I was also contemplating if the Qernel::Sector could benefit from Dry, what do you think about that?
|
|
||
| # Scheme-based sector mapping lookups. `energy`/`molecule` select the | ||
| # graph whose live nodes are returned; the mapping itself is shared. | ||
| def energy_sector_map(scheme, values) |
Member
There was a problem hiding this comment.
Suggested change
| def energy_sector_map(scheme, values) | |
| def energy_sector_map_nodes(scheme, values) |
This method is returning nodes, not a map. It's more clear to put that already in the methods name
Comment on lines
+403
to
+421
| # Internal: Sums the emissions store over every (sector label, use) pair | ||
| # the mapping resolves for `scheme`/`value`. Present year is implicit; | ||
| # only 1990 is suffixed. Missing store keys count as zero. | ||
| def mapped_emissions(keys) | ||
| scheme, value, ghg, year = keys | ||
|
|
||
| if ghg.nil? | ||
| raise Gql::GqlError, "EMISSIONS(#{scheme.inspect}, ...) needs a GHG argument, e.g. " \ | ||
| "EMISSIONS(#{scheme.inspect}, #{value.inspect}, co2)" | ||
| end | ||
|
|
||
| suffix = ('1990' if year.to_i == 1990) | ||
| emissions = scope.graph.emissions | ||
|
|
||
| scope.sector_resolver.pairs(scheme, value).sum do |label, use| | ||
| key = [label, use, ghg, suffix].compact.join('_').to_sym | ||
| emissions[key] || 0.0 | ||
| end | ||
| end |
Member
There was a problem hiding this comment.
Let's think about if summing logic should live in GQL lookup. Did you consider a (class) method on emissions?
This was referenced Jul 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Adds the ETEngine surface for the sector mapping feature: Enabling SECTOR lookup by external classification schemes as recorded in the sector_mapping.csv config in ETSource.
Implemented changes
Etsource::Sectors: Rails-cached mapping hash and per-graph-type(label, use) -> node keysindex (mirrors the merit order import).Qernel::Sectors: per-graph-instance resolver —scheme?,pairs(scheme, values),lookup(scheme, values)— memoized per instance, reached via a newGraph#sector_mapaccessor.SECTOR/MSECTORnow dispatch on arity: one argument is the legacy namespace-sector filter (byte-for-byte unchanged); two or more isscheme, value(s), returning the union of matching live nodes. Works identically on the molecule graph viaMSECTOR.EMISSIONSdispatches on whether the first argument is a known scheme:EMISSIONS(scheme, value, ghg[, 1990])sums the emissions store over the mapping's resolved(label, use)pairs (missing store keys treated as 0); all legacyEMISSIONSforms are unchanged.Gql::UnknownSectorSchemeError/UnknownSectorValueError) naming the valid options; a valid value with no labelled nodes returns[].spec/models/gql/runtime/functions/lookup_spec.rbcovering legacy back-compat, multi-value union, pair-correctness (energetic vs non-energetic), unknown scheme/value errors, blank/-cells, and the mapped EMISSIONS form.Related
atlas
etengine
etsource
Checklist