forked from electricitymaps/electricitymaps-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
128 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FROM node:8.5.0 | ||
WORKDIR /home | ||
EXPOSE 9000 | ||
RUN npm install http-server -g |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"data": {"exchanges": {"BR-S->UY": {"sortedCountryCodes": "BR-S->UY", "netFlow": 0.2, "datetime": "2017-09-24T10:17:00-03:00", "co2intensity": 475.7995468281372, "source": "ons.org.br"}, "BR-CS->BR-S": {"sortedCountryCodes": "BR-CS->BR-S", "netFlow": 3291.799, "datetime": "2017-09-24T10:17:00-03:00", "co2intensity": null, "source": "ons.org.br"}}, "datetime": "2017-09-24T10:17:00-03:00", "countries": {"UY": {"exchange": {"BR-S": 0.2}}, "BR-S": {"countryCode": "BR-S", "exchange": {"UY": -0.2, "BR-CS": 3291.799}, "storage": {"hydro": null}, "datetime": "2017-09-24T10:17:00-03:00", "source": "ons.org.br", "production": {"nuclear": 0.0, "hydro": 3366.93042, "solar": 0.0, "wind": 576.1216, "unknown": 1304.81372}, "co2intensity": 475.7995468281372}, "BR-CS": {"exchange": {"BR-S": -3291.799}}}}} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# This script should be run from the root directory | ||
import arrow, importlib, json, pprint, sys | ||
from random import random | ||
pp = pprint.PrettyPrinter(indent=2) | ||
|
||
# Read parser import list from config jsons | ||
zones_config = json.load(open('config/zones.json')) | ||
exchanges_config = json.load(open('config/exchanges.json')) | ||
|
||
# Read zone_name from commandline | ||
if not len(sys.argv) > 1: | ||
raise Exception('Missing argument <zone_name>') | ||
zone_name = sys.argv[1] | ||
zone_config = zones_config[zone_name] | ||
|
||
# Find parsers | ||
production_parser = zone_config['parsers']['production'] | ||
exchange_parser_keys = [] | ||
for k in exchanges_config.keys(): | ||
zones = k.split('->') | ||
if zone_name in zones: exchange_parser_keys.append(k) | ||
|
||
# Import / run production parser | ||
print 'Finding and executing %s production parser %s..' % (zone_name, production_parser) | ||
mod_name, fun_name = production_parser.split('.') | ||
mod = importlib.import_module('parsers.%s' % mod_name) | ||
production = getattr(mod, fun_name)(zone_name) | ||
pp.pprint(production) | ||
|
||
# Import / run exchange parser(s) | ||
exchanges = [] | ||
for k in exchange_parser_keys: | ||
exchange_parser = exchanges_config[k]['parsers']['exchange'] | ||
print 'Finding and executing %s exchange parser %s..' % (k, exchange_parser) | ||
mod_name, fun_name = exchange_parser.split('.') | ||
mod = importlib.import_module('parsers.%s' % mod_name) | ||
sorted_zone_names = sorted(k.split('->')) | ||
exchange = getattr(mod, fun_name)(sorted_zone_names[0], sorted_zone_names[1]) | ||
exchanges.append(exchange) | ||
pp.pprint(exchange) | ||
|
||
# Load and update state | ||
print 'Updating and writing state..' | ||
with open('mockserver/public/v3/state', 'r') as f: | ||
obj = json.load(f)['data'] | ||
obj['countries'][zone_name] = {} | ||
# Update production | ||
obj['countries'][zone_name] = production | ||
production['datetime'] = arrow.get(production['datetime']).isoformat() | ||
# Set random co2 value | ||
production['co2intensity'] = random() * 500 | ||
# Update exchanges | ||
for e in exchanges: | ||
zones = e['sortedCountryCodes'].split('->') | ||
e['datetime'] = arrow.get(e['datetime']).isoformat() | ||
obj['exchanges'][e['sortedCountryCodes']] = e.copy() | ||
origin_zone = zones[0] if e['netFlow'] >= 0 else zones[1] | ||
obj['exchanges'][e['sortedCountryCodes']]['co2intensity'] = \ | ||
obj['countries'].get(origin_zone, {}).get('co2intensity') | ||
for z in zones: | ||
other_zone = zones[(zones.index(z) + 1) % 2] | ||
if not z in obj['countries']: | ||
obj['countries'][z] = {} | ||
if not 'exchange' in obj['countries'][z]: | ||
obj['countries'][z]['exchange'] = {} | ||
obj['countries'][z]['exchange'][other_zone] = e['netFlow'] | ||
print z, other_zone | ||
if z == zones[0]: | ||
obj['countries'][z]['exchange'][other_zone] *= -1 | ||
|
||
# Set state datetime | ||
obj['datetime'] = production['datetime'] | ||
# Save | ||
with open('mockserver/public/v3/state', 'w') as f: | ||
json.dump({'data': obj}, f) | ||
print '..done' |
This file contains 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
This file contains 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