quintel / etengine

Calculation engine for the Energy Transition Model
https://energytransitionmodel.com/
MIT License
14 stars 7 forks source link

Gquery API #1415

Closed noracato closed 2 months ago

noracato commented 3 months ago

Adds an API endpoint to collect all gqueries.

GET api/v3/gqueries

Accepts labels parameter containing a list of labels to filter on. The filter work cumulative, so passing heat and final_demand retrieves all gqueries with either a label final_demand or heat.

The API returns a list of gqueries in JSON format:

{
  "key":    "hourly_demand_for_space_heating_apartments_1945_1964",
  "unit":   "curve",
  "description":    null,
  "labels": 
    [  
      "general",
      "curves",
      "households",
      "space_heating",
      "hourly_demand_per_consumer"
    ]
}

The labels are based on the folder structure in which the queries can be found.

Also opens up the inspect mode for the index and show pages of Gqueries for all users.

mabijkerk commented 3 months ago

@noracato for accessibility I added the GQuery navigator to the subnav bar like this:

Screenshot 2024-04-05 at 13 23 48

mabijkerk commented 3 months ago

When I run the script below in my terminal, I expect only a very limited number of gqueries to be returned. Instead, I get the full list. Is my script faulty or is the filter not working properly? FYI - I do include the local url and my personal token.

labels = {
  "labels": 
    [  
      "bezier_178_source_of_heat_in_industry_chemical_other"
    ]
}

response = requests.get(ete_url, headers=headers, params=labels)

if response.status_code == 200:
    print(response.text)
else:
    print(f"Request failed with status code {response.status_code}")
noracato commented 2 months ago

Thanks for the review!

When I try to open a scenario on etmodel,

That was because in the mean time multi user was merged. A simple rebase fixed this.

Is my script faulty or is the filter not working properly?

It's a JSON API, so instead of params you send json parameters:

response = requests.get(ete_url, json=labels)

I tested you script with this change and I do get the correct results!

So actually I think we can merge, if you are OK?

mabijkerk commented 2 months ago

@noracato do we need to update the API docs as well? Should I ask someone to write it?

noracato commented 2 months ago

Yes! That would be great!