Last active
February 16, 2024 21:48
-
-
Save bbartling/f1b78ada8f87ac6bb56c9cd433e4931e to your computer and use it in GitHub Desktop.
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
import strawberry | |
from fastapi import FastAPI | |
from strawberry.asgi import GraphQL | |
from typing import Tuple | |
import pandas as pd | |
# Sample DataFrame for Zones | |
zone_data = { | |
'Zone1_Temp': [20, 21, 19], | |
'Zone2_Temp': [22, 23, 21], | |
'Zone3_Temp': [24, 25, 23] | |
} | |
df_zones = pd.DataFrame(zone_data) | |
# Sample DataFrame for AHUs | |
ahu_data = { | |
'AHU1_Flow': [5000, 5100, 5050], | |
'AHU2_Flow': [6000, 6100, 6050] | |
} | |
df_ahus = pd.DataFrame(ahu_data) | |
# Sample DataFrame for Chillers | |
chiller_data = { | |
'Chiller1_Load': [70, 75, 65], | |
'Chiller2_Load': [80, 85, 75] | |
} | |
df_chillers = pd.DataFrame(chiller_data) | |
@strawberry.type | |
class Query: | |
# fake Zone Temperature Queries | |
@strawberry.field | |
def averageZoneAirTemperatureSensor(self) -> float: | |
return df_zones.mean().mean() | |
@strawberry.field | |
def lowestZoneAirTemperatureSensorName(self) -> str: | |
coldest_temp = df_zones.min().min() | |
coldest_zone = df_zones.idxmin()[df_zones.min() == coldest_temp].index[0] | |
return coldest_zone | |
@strawberry.field | |
def lowestZoneAirTemperatureSensorReading(self) -> float: | |
return df_zones.min().min() | |
@strawberry.field | |
def highestZoneAirTemperatureSensorName(self) -> str: | |
warmest_temp = df_zones.max().max() | |
warmest_zone = df_zones.idxmax()[df_zones.max() == warmest_temp].index[0] | |
return warmest_zone | |
@strawberry.field | |
def highestZoneAirTemperatureSensorReading(self) -> float: | |
return df_zones.max().max() | |
# Combined name and temperature as a Tuple | |
@strawberry.field | |
def lowestZoneAirTemperatureSensorInfo(self) -> Tuple[str, float]: | |
coldest_temp = df_zones.min().min() | |
coldest_zone = df_zones.idxmin()[df_zones.min() == coldest_temp].index[0] | |
return (coldest_zone, coldest_temp) | |
@strawberry.field | |
def highestZoneAirTemperatureSensorInfo(self) -> Tuple[str, float]: | |
warmest_temp = df_zones.max().max() | |
warmest_zone = df_zones.idxmax()[df_zones.max() == warmest_temp].index[0] | |
return (warmest_zone, warmest_temp) | |
# Updated AHU Queries | |
@strawberry.field | |
def averageAHUAirflowSensor(self) -> float: | |
return df_ahus.mean().mean() | |
# Updated Chiller Queries | |
@strawberry.field | |
def averageChillerLoadSensor(self) -> float: | |
return df_chillers.mean().mean() | |
schema = strawberry.Schema(query=Query) | |
graphql_app = GraphQL(schema) | |
app = FastAPI() | |
app.add_route("/graphql", graphql_app) | |
# Uncomment to run the app with Uvicorn | |
# uvicorn.run(app, host="0.0.0.0", port=8000) |
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
import requests | |
import json | |
# Endpoint URL | |
url = 'http://192.168.0.102:8000/graphql' | |
def make_graphql_query(query: str): | |
# Headers | |
headers = { | |
'Content-Type': 'application/json' | |
} | |
# Make the POST request | |
response = requests.post(url, headers=headers, json={'query': query}) | |
# Parse and return the response | |
if response.status_code == 200: | |
return json.dumps(response.json(), indent=4) | |
else: | |
return f"Query failed with status code {response.status_code}. Query: {query}" | |
# Prepare and execute queries for each field | |
fields = [ | |
'averageZoneAirTemperatureSensor', | |
'lowestZoneAirTemperatureSensorName', | |
'lowestZoneAirTemperatureSensorReading', | |
'highestZoneAirTemperatureSensorName', | |
'highestZoneAirTemperatureSensorReading', | |
'lowestZoneAirTemperatureSensorInfo', | |
'highestZoneAirTemperatureSensorInfo', | |
'averageAHUAirflowSensor', | |
'averageChillerLoadSensor' | |
] | |
for field in fields: | |
query = f'{{ {field} }}' | |
print(f"\n{field.title()}:") | |
print(make_graphql_query(query)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment