Giter Site home page Giter Site logo

somenergia-opendata's Introduction

somenergia-opendata

CI Status Coverage Status

Public API to access open data information about the cooperative

Example queries

Numerical data

Follow the following structure /<metric>[/by/<geolevel>][/on/<date>|[/monthly|/yearly][/from/<date>][/to/<date>]]

  • /contracts All current contracts

  • /members All current members (instead of contracts).

  • /newcontracts New contracts last month (same for members)

  • /canceledcontracts Leaving contracts last month (same will do for members)

  • /contracts/on/2018-02-01 Contracts on a given date (just firsts of month allowed)

  • /contracts/by/city Current contracts aggregated by city

  • /contracts/by/city/on/2018-02-01 Contracts aggregated by city at a given date

  • /contracts/by/state/monthly Contracts aggregated by state every available month

  • /contracts/by/ccaa/yearly Contracts aggregated by CCAA every available year

  • /contracts/by/city/monthly/from/2018-02-01 Contracts aggregated by state every week from a date

  • /contracts/by/city/yearly/from/2018-02-01/to/2018-05-01 Contracts aggregated by city every week from a date until a date

  • /contracts/by/city/weekly/to/2018-05-01 Contracts aggregated by city every week until a date

Using filters

You can append repeatedly query params to filter data just including some geografical regions:

  • /contracts/by/city?city=23423&city=89545 Include just cities with INE code 23423 and 89545

  • /contracts/by/city?city=23423&city=89545&ccaa=04 Include just cities with INE code 23423 and 89545 and also all cities from CCAA 04 (Catalonia)

  • /contracts/by/city?state=04 Include just cities in state 04

Response format

Data is returned by default in YAML. It has a hierarchical structure from the world geographical level to the requested geographical level of detail (country in this example) Each region has as many numbers in list as the top level dates.

dates:
- 2014-01-01
- 2015-01-01
- 2016-01-01
values:
- 11905
- 17896
- 23749
countries:
  CL:
    name: Chile
    values:
    - 1
    - 1
    - 1
  ES:
    name: España
    values:
    - 11896
    - 17885
    - 23738
  FR:
    name: France
    values:
    - 1
    - 1
    - 1
  DE:
    name: Germany
    values:
    - 1
    - 2
    - 2
  GR:
    name: Greece
    values:
    - 0
    - 0
    - 0
  NL:
    name: Netherlands
    values:
    - 3
    - 3
    - 3
  PT:
    name: Portugal
    values:
    - 1
    - 1
    - 1
  UK:
    name: United Kingdom
    values:
    - 2
    - 2
    - 2
  None:
    name: None
    values:
    - 0
    - 1
    - 1

Maps

All maps are returned as SVG files. Follow the following structure /map/<metric>[/per/<relative>][/by/<geolevel>][/on/<date>|[/monthly|/yearly][/from/<date>][/to/<date>]]

  • /map/contracts All current contracts by ccaa

  • /map/contracts?lang=es All current contracts by ccaa in Spanish

  • /map/members All current members by ccaa

  • /map/contracts/on/2018-02-03 Contracts on a given date

  • /map/contracts/by/state Current contracts aggregated by state

  • /map/contracts/per/population/by/state Current contracts relatives to population aggregated by state

  • /map/contracts/by/state/on/2018-02-03 Contracts aggregated by state at a given date

  • /map/contracts/by/state/monthly Animation with contracts by state every available month

  • /map/contracts/by/ccaa/yearly Animation with contracts aggregated by CCAA every available year

  • /map/contracts/by/state/monthly/from/2018-02-01 Animation with contracts aggregated by state every month from a date

  • /map/contracts/by/state/monthly/from/2018-02-03/to/2018-05-01 Animation with contracts aggregated by state every month from a date until a date

  • /map/contracts/by/state/monthly/to/2018-05-01 Animation with contracts aggregated by state every month until a date

  • /map/contracts/by/city/monthly/to/2018-05-01?localgroup=BaixMontseny Animation with contracts aggregated by city within localgroup BaixMontseny every month until a date

Discovery

  • /discover/metrics Shows all suported metrics as a list named metrics with

    • id the id used to refer the metric
    • text the translated text to display users
    • description translated text with detailed explanation of the meaning, source and errors of the metric
  • /discover/geolevel Returns a list geolevels with the supported geolevels and related info

    • id is the id used to refer it (its a mnemonic id)
    • text is the translated text to display users
    • plural is the pluralization of id used in yaml's as key when many are given
    • parent tells which other geolevel fully contains its subdivisions.
    • detailable: false tells that a geolevel is not supported as statistics detail level (optional, default true)
    • mapable: false tells that a geolevel is not supported as map detail level (optional, default true)
  • /discover/geolevel/ccaa Returns a list of divisions at the ccaa level as a map options with id -> text

  • /discover/geolevel/city?localgroup=BaixLlobregat Limits the list of cities to the ones included in the localgroup BaixLlobregat

  • /discover/geolevel/localgroup?ccaa=09 Lists all the localgroups working on areas covering cities in Catalonia (09)

Language

Whenever human readable strings are returned, browser language is used by default (accept-language http header). Language can be forced by using lang query parameter.

If the language is not specified in either form or the one selected is not supported, spanish is chosen. But that could be changed in the future to english. So, if you want spanish, please, specify it.

Supported languages are:

  • Catalan (ca)
  • Spanish (es)
  • Euskara (eu)
  • Galician (gl)

Deploy

Requirements

sudo apt install libmagickwand-dev inkscape libyaml-dev
python setup.py develop

Compile Language Translations

WARNING: Some back-to-back tests will fail if language files are not generated.

./setup.py compile_catalog

Default options are specified in setup.cfg

In order to incorporate new strings from code:

./setup.py extract_messages # generates a new pot file with all extracted strings
./setup.py update_catalog # Merges all translations with new strings

More info about translation management on http://babel.pocoo.org/en/latest/setup.html

Generate documentation

npm install
npm run redocs

Testing

Tests are unittest based, using either nosetests or pytest as runner is recommended.

TRAVIS=1 pytest som_opendata

TRAVIS=1 will disable tests for code directly using SomEnergia databases. Mainly queries to extract annonymized by aggregation from non-annonymized original data.

Notice: Some tests require translation catalog to be generated. See above.

Profiling and optimizing

In order to optimize the api, first profile the entry point to detect the bottle necks, you that your efforts are better targeted and to have an objective measure of your progress.

sudo apt install pyprof2calltree kcachegrind
pip install pytest-profiling
PROFILE=1 pytest --profile -v som_opendata/api_test.py::Api_Test::test__profiling
pyprof2calltree -i prof/combined.prof -o profiling-$(date -Iseconds).out

Releasing

  • Generate language strings (see above)
  • Update version in:
    • som_opendata/__init__.py
    • package.json
    • openapi.yaml
    • som_opendata/api.py
  • Generate the documentation (see above)
  • Update the CHANGES.md (replace unreleased by the actual release date)
  • Commit the version bump
  • tag it opendata-M.m.r

External data sources

Could be used in the future

somenergia-opendata's People

Contributors

vokimon avatar marjene avatar joanafigueira avatar juan-pe avatar polmonso avatar benjirs94 avatar somenergia-it avatar somtraductors avatar blackleg avatar

Stargazers

 avatar  avatar Daniel Hernández avatar Ferran avatar Hector Espert avatar

Watchers

 avatar  avatar James Cloos avatar javikalsan avatar David Riera avatar Roger avatar Dani Estanyol avatar Daniel Quilez avatar Daniel Hernández avatar  avatar Eduard Quintana avatar  avatar  avatar Diego Quintana avatar  avatar  avatar

somenergia-opendata's Issues

Zero valued metrics fail on maps

Zero valued data fails in maps when generating the scale.

Accesing /v0.2/map/contracts/by/city/on/2011-01-01?localgroup=BaixMontseny&lang=ca

  File "/home/vokimon/somenergia/somenergia-opendata/som_opendata/common.py", line 237, in svg_response
    return make_svg(f(*args, **kwds))
  File "/home/vokimon/somenergia/somenergia-opendata/som_opendata/api.py", line 758, in map
    result = renderMap(
  File "/home/vokimon/somenergia/somenergia-opendata/som_opendata/map.py", line 209, in renderMap
    return fillMap(
  File "/home/vokimon/somenergia/somenergia-opendata/som_opendata/map.py", line 154, in fillMap
    dataDict = dataToTemplateDict(
  File "/home/vokimon/somenergia/somenergia-opendata/som_opendata/map.py", line 110, in dataToTemplateDict
    updateDict(code, region["values"][frame])
  File "/home/vokimon/somenergia/somenergia-opendata/som_opendata/map.py", line 106, in updateDict
    'color_' + code: colors(scale(value)),
  File "/home/vokimon/somenergia/somenergia-opendata/som_opendata/scale.py", line 86, in __call__
    return log10(val / self.low) / log10(self.high / self.low)
ZeroDivisionError: float division by zero

Unclosed intervals should end at the last avaiable date, not today

In order to make the API workable for the early users, for date intervals, it would be convenient to interpret the lack of 'to/<date> as the last available date, not today.

  • Pros: For incomplete data sets we reduce the chance of failure because the lack of a date.
  • Cons: Semantics may change in the future

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.