Skip to content

Commit

Permalink
🔬🌒 ↝ Upgrading evolution contract & adding flask functions for queryi…
Browse files Browse the repository at this point in the history
…ng nft trait types

Signal-K/sytizen#1 && Signal-K/sytizen#6
  • Loading branch information
Gizmotronn committed Jan 7, 2023
1 parent 9bd1990 commit 521ed1a
Show file tree
Hide file tree
Showing 7 changed files with 13,733 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vercel
115 changes: 115 additions & 0 deletions backend/dbInteractions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from flask import Flask, request
from datetime import datetime, timezone
import psycopg2
import os

CREATE_ROOMS_TABLE = (
"CREATE TABLE IF NOT EXISTS rooms (id SERIAL PRIMARY KEY, name TEXT);"
)
CREATE_TEMPS_TABLE = """CREATE TABLE IF NOT EXISTS temperatures (room_id INTEGER, temperature REAL,
date TIMESTAMP, FOREIGN KEY(room_id) REFERENCES rooms(id) ON DELETE CASCADE);"""

INSERT_ROOM_RETURN_ID = "INSERT INTO rooms (name) VALUES (%s) RETURNING id;"
INSERT_TEMP = (
"INSERT INTO temperatures (room_id, temperature, date) VALUES (%s, %s, %s);"
)

ROOM_NAME = """SELECT name FROM rooms WHERE id = (%s)"""

ROOM_NUMBER_OF_DAYS = """SELECT COUNT(DISTINCT DATE(date)) AS days FROM temperatures WHERE room_id = (%s);"""
ROOM_ALL_TIME_AVG = (
"SELECT AVG(temperature) as average FROM temperatures WHERE room_id = (%s);"
)


ROOM_TERM = """SELECT DATE(temperatures.date) as reading_date,
AVG(temperatures.temperature)
FROM temperatures
WHERE temperatures.room_id = (%s)
GROUP BY reading_date
HAVING DATE(temperatures.date) > (SELECT MAX(DATE(temperatures.date))-(%s) FROM temperatures);"""

GLOBAL_NUMBER_OF_DAYS = (
"""SELECT COUNT(DISTINCT DATE(date)) AS days FROM temperatures;"""
)
GLOBAL_AVG = """SELECT AVG(temperature) as average FROM temperatures;"""


url = os.environ.get("DATABASE_URL")
connection = psycopg2.connect(url)
app = Flask(__name__)


# {"name": "Room name"}
@app.post("/api/room")
def create_room():
data = request.get_json()
name = data["name"]
with connection:
with connection.cursor() as cursor:
cursor.execute(CREATE_ROOMS_TABLE)
cursor.execute(INSERT_ROOM_RETURN_ID, (name,))
room_id = cursor.fetchone()[0]
return {"id": room_id, "message": f"Room {name} created."}, 201


# {"temperature": 15.9, "room": 2 "date": "%m-%d-%Y %H:%M:%S"} date is optional
@app.post("/api/temperature")
def add_temp():
data = request.get_json()
temperature = data["temperature"]
room_id = data["room"]
try:
date = datetime.strptime(data["date"], "%m-%d-%Y %H:%M:%S")
except KeyError:
date = datetime.now(timezone.utc)
with connection:
with connection.cursor() as cursor:
cursor.execute(CREATE_TEMPS_TABLE)
cursor.execute(INSERT_TEMP, (room_id, temperature, date))
return {"message": "Temperature added."}, 201


@app.get("/api/room/<int:room_id>")
def get_room_all(room_id):
args = request.args
term = args.get("term")
if term is not None:
return get_room_term(room_id, term)
else:
with connection:
with connection.cursor() as cursor:
cursor.execute(ROOM_NAME, (room_id,))
name = cursor.fetchone()[0]
cursor.execute(ROOM_ALL_TIME_AVG, (room_id,))
average = cursor.fetchone()[0]
cursor.execute(ROOM_NUMBER_OF_DAYS, (room_id,))
days = cursor.fetchone()[0]
return {"name": name, "average": round(average, 2), "days": days}


def get_room_term(room_id, term):
terms = {"week": 7, "month": 30}
with connection:
with connection.cursor() as cursor:
cursor.execute(ROOM_NAME, (room_id,))
name = cursor.fetchone()[0]
cursor.execute(ROOM_TERM, (room_id, terms[term]))
dates_temperatures = cursor.fetchall()
average = sum(day[1] for day in dates_temperatures) / len(dates_temperatures)
return {
"name": name,
"temperatures": dates_temperatures,
"average": round(average, 2),
}


@app.get("/api/average")
def get_global_avg():
with connection:
with connection.cursor() as cursor:
cursor.execute(GLOBAL_AVG)
average = cursor.fetchone()[0]
cursor.execute(GLOBAL_NUMBER_OF_DAYS)
days = cursor.fetchone()[0]
return {"average": round(average, 2), "days": days}
4 changes: 4 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
flask = "*"
requests = "*"
python-dotenv = "*"
psycopg2-binary = "*"
15 changes: 15 additions & 0 deletions backend/vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": 2,
"builds": [
{
"src": "./app.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}
1 change: 1 addition & 0 deletions evolve
Submodule evolve added at 81690e
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"@3rdweb/hooks": "^1.6.0",
"@3rdweb/sdk": "^1.27.1",
"@thirdweb-dev/contracts": "^3.3.0",
"dotenv": "^10.0.0",
"ethers": "^5.5.2",
"react": "^17.0.2",
Expand Down
Loading

0 comments on commit 521ed1a

Please sign in to comment.