refactoring; more consistent naming for API endpoint and variables.
This commit is contained in:
1 parent
e0c8b3eb1b
commit
e4e05b4788
16 files changed
+325
-238
No files matched your search
+60
-49
@@ -1,13 +1,13 @@
|
||||
import data
|
||||
import polars as pl
|
||||
from data import etl_property_capacities as etl_pc
|
||||
from data import etl_property_capacities_daily as etl_pcd
|
||||
from data import etl_property_capacities_monthly as etl_pcm
|
||||
from data import etl_property_capacities_weekdays as etl_pcw
|
||||
from data import etl_property_neighbours as etl_pn
|
||||
from data import etl_region_capacities as etl_rc
|
||||
from data import etl_region_capacities_comparison as etl_rcc
|
||||
from data import etl_region_capacities_daily as etl_rcd
|
||||
from data import etl_region_capacities_monthly as etl_rcm
|
||||
from data import etl_region_capacities_weekdays as etl_rcw
|
||||
from data import etl_region_movAverage as etl_rmA
|
||||
from data import etl_region_properties_capacities as etl_rpc
|
||||
from fastapi import FastAPI, Response
|
||||
@@ -20,85 +20,96 @@ app = FastAPI()
|
||||
def read_root():
|
||||
return {"Hi there!"}
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
def read_item(item_id: int):
|
||||
ext = d.extractions_for(item_id).pl()
|
||||
out = ext.with_columns(pl.col("calendar").str.extract_all(r"([0-9]{4}-[0-9]{2}-[0-9]{2})|[0-2]").alias("calendar_data"))
|
||||
out = out.drop(['calendar', 'property_id'])
|
||||
return Response(content=out.write_json(), media_type="application/json")
|
||||
|
||||
@app.get("/region/properties")
|
||||
# regions overview: contains names, ids, and count of properties of regions
|
||||
@app.get("/regions")
|
||||
def properties_region():
|
||||
return d.properties_per_region().pl().to_dicts()
|
||||
|
||||
@app.get("/properties/growth")
|
||||
def properties_growth():
|
||||
options = {"dates" : d.properties_growth().pl()['date'].to_list(), "total_all" : d.properties_growth().pl()['total_all'].to_list(), "total_heidiland" : d.properties_growth().pl()['total_heidiland'].to_list(), "total_engadin" : d.properties_growth().pl()['total_engadin'].to_list(), "total_davos" : d.properties_growth().pl()['total_davos'].to_list(), "total_stmoritz" : d.properties_growth().pl()['total_stmoritz'].to_list()}
|
||||
return options
|
||||
|
||||
@app.get("/properties/geo")
|
||||
def properties_geo():
|
||||
return d.properties_geo().pl().to_dicts()
|
||||
|
||||
@app.get("/property/{id}/neighbours")
|
||||
def property_neighbours(id: int):
|
||||
capacities = etl_pn.property_neighbours(id)
|
||||
return capacities
|
||||
|
||||
@app.get("/property/{id}/extractions")
|
||||
def property_extractions(id: int):
|
||||
return d.extractions_for(property_id = id).pl().to_dicts()
|
||||
|
||||
@app.get("/property/{id}/capacities")
|
||||
def property_capacities_data(id: int):
|
||||
capacities = etl_pc.property_capacities(id)
|
||||
return capacities
|
||||
|
||||
@app.get("/property/{id}/capacities/monthly/{scrapeDate}")
|
||||
def property_capacities_data(id: int, scrapeDate: str):
|
||||
capacities = etl_pcm.property_capacities_monthly(id, scrapeDate)
|
||||
return capacities
|
||||
|
||||
@app.get("/property/{id}/capacities/weekdays/{scrapeDate}")
|
||||
def property_capacities_data(id: int, scrapeDate: str):
|
||||
capacities = etl_pcw.property_capacities_weekdays(id, scrapeDate)
|
||||
return capacities
|
||||
|
||||
@app.get("/property/{id}/base")
|
||||
def property_base_data(id: int):
|
||||
return d.property_base_data(id).pl().to_dicts()
|
||||
|
||||
# get capacities of properties, for every scraping, filterd by region; -1 = all regions
|
||||
@app.get("/region/{id}/properties/capacities")
|
||||
def region_property_capacities_data(id: int):
|
||||
capacities = etl_rpc.region_properties_capacities(id)
|
||||
return capacities
|
||||
|
||||
# get the capacity of a region for every scraping; -1 = all regions
|
||||
@app.get("/region/{id}/capacities")
|
||||
def region_capacities_data(id: int):
|
||||
capacities = etl_rc.region_capacities(id)
|
||||
return capacities
|
||||
|
||||
# get the capacity of a region for desired scraping date by months; -1 = all regions
|
||||
@app.get("/region/{id}/capacities/monthly/{scrapeDate}")
|
||||
def region_capacities_data(id: int, scrapeDate: str):
|
||||
capacities = etl_rcm.region_capacities_monthly(id, scrapeDate)
|
||||
return capacities
|
||||
|
||||
@app.get("/region/{id}/capacities/weekdays/{scrapeDate}")
|
||||
# get the capacity of a region for desired scraping date by days; -1 = all regions
|
||||
@app.get("/region/{id}/capacities/daily/{scrapeDate}")
|
||||
def region_capacities_data(id: int, scrapeDate: str):
|
||||
capacities = etl_rcw.region_capacities_weekdays(id, scrapeDate)
|
||||
capacities = etl_rcd.region_capacities_daily(id, scrapeDate)
|
||||
return capacities
|
||||
|
||||
# compare the capacities of two regions
|
||||
@app.get("/region/capacities/comparison/{id_1}/{id_2}")
|
||||
def region_capacities_data(id_1: int, id_2: int):
|
||||
capacities = etl_rcc.region_capacities_comparison(id_1, id_2)
|
||||
return capacities
|
||||
|
||||
# get the moving average for a region beginning from desired date
|
||||
@app.get("/region/{id}/movingAverage/{startDate}")
|
||||
def region_capacities_data(id: int, startDate: str):
|
||||
result = etl_rmA.region_movingAverage(id, startDate)
|
||||
return result
|
||||
|
||||
# get id and name of a region
|
||||
@app.get("/region/{id}/base")
|
||||
def region_base_data(id: int):
|
||||
return d.region_base_data(id).pl().to_dicts()
|
||||
|
||||
# get growth of properties categorized by regions
|
||||
@app.get("/properties/growth")
|
||||
def properties_growth():
|
||||
options = {"dates" : d.properties_growth().pl()['date'].to_list(), "total_all" : d.properties_growth().pl()['total_all'].to_list(), "total_heidiland" : d.properties_growth().pl()['total_heidiland'].to_list(), "total_engadin" : d.properties_growth().pl()['total_engadin'].to_list(), "total_davos" : d.properties_growth().pl()['total_davos'].to_list(), "total_stmoritz" : d.properties_growth().pl()['total_stmoritz'].to_list()}
|
||||
return options
|
||||
|
||||
# get the geo coordinates for all properties
|
||||
@app.get("/properties/geo")
|
||||
def properties_geo():
|
||||
return d.properties_geo().pl().to_dicts()
|
||||
|
||||
# get the 10 nearest properties from desired property
|
||||
@app.get("/property/{id}/neighbours")
|
||||
def property_neighbours(id: int):
|
||||
capacities = etl_pn.property_neighbours(id)
|
||||
return capacities
|
||||
|
||||
# get scraped data for all scrapings from desired property
|
||||
@app.get("/property/{id}/extractions")
|
||||
def property_extractions(id: int):
|
||||
return d.extractions_for(property_id = id).pl().to_dicts()
|
||||
|
||||
# get scraped data for all scrapings from desired property
|
||||
@app.get("/property/{id}/capacities")
|
||||
def property_capacities_data(id: int):
|
||||
capacities = etl_pc.property_capacities(id)
|
||||
return capacities
|
||||
|
||||
# get the capacity of a property for desired scraping date by months
|
||||
@app.get("/property/{id}/capacities/monthly/{scrapeDate}")
|
||||
def property_capacities_data(id: int, scrapeDate: str):
|
||||
capacities = etl_pcm.property_capacities_monthly(id, scrapeDate)
|
||||
return capacities
|
||||
|
||||
# get the capacity of a property for desired scraping date by days
|
||||
@app.get("/property/{id}/capacities/daily/{scrapeDate}")
|
||||
def property_capacities_data(id: int, scrapeDate: str):
|
||||
capacities = etl_pcd.property_capacities_daily(id, scrapeDate)
|
||||
return capacities
|
||||
|
||||
# get first / last time a property was fond, region id and name in which the property is located and the coordinates
|
||||
@app.get("/property/{id}/base")
|
||||
def property_base_data(id: int):
|
||||
return d.property_base_data(id).pl().to_dicts()
|
||||
|
||||
|
||||
|
||||
@@ -328,7 +328,7 @@ class Database:
|
||||
return self.connection.sql(f"""
|
||||
SELECT
|
||||
JSON_EXTRACT(body, '$.content.days') as calendar,
|
||||
created_at
|
||||
created_at as date
|
||||
FROM
|
||||
consultancy_d.extractions
|
||||
WHERE
|
||||
@@ -405,7 +405,7 @@ class Database:
|
||||
p.property_platform_id,
|
||||
p.created_at as first_found,
|
||||
p.last_found,
|
||||
p.check_data,
|
||||
p.check_data as latlng,
|
||||
r.id as region_id,
|
||||
r.name as region_name
|
||||
FROM
|
||||
@@ -423,8 +423,8 @@ class Database:
|
||||
where = f"WHERE r.id = {id}"
|
||||
return self.connection.sql(f"""
|
||||
SELECT
|
||||
r.id as region_id,
|
||||
r.name as region_name
|
||||
r.id as id,
|
||||
r.name as name
|
||||
FROM
|
||||
consultancy_d.regions r
|
||||
{where}
|
||||
|
||||
@@ -34,7 +34,7 @@ def property_capacities(id: int):
|
||||
count_days.append(len(liste))
|
||||
|
||||
counts = pl.DataFrame({"count_days" : count_days, "sum" : sum_hor})
|
||||
result = {"capacities": [], "dates": extractions['created_at'].cast(pl.Date).to_list() }
|
||||
result = {"capacities": [], "dates": extractions['date'].cast(pl.Date).to_list() }
|
||||
|
||||
for row in counts.rows(named=True):
|
||||
max_capacity = row['count_days'] * 2
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@ from data import etl_cache
|
||||
|
||||
d = data.load()
|
||||
|
||||
def property_capacities_weekdays(id: int, scrapeDate: str):
|
||||
def property_capacities_daily(id: int, scrapeDate: str):
|
||||
|
||||
file = f"etl_property_capacities_weekdays_{id}_{scrapeDate}.obj"
|
||||
obj = etl_cache.openObj(file)
|
||||
@@ -36,6 +36,6 @@ def property_capacities_weekdays(id: int, scrapeDate: str):
|
||||
df_calendar = df_calendar.sort('weekday_num')
|
||||
df_calendar = df_calendar.drop('weekday_num')
|
||||
|
||||
result = {"scraping-date": scrapeDate, "weekdays": df_calendar['weekday'].to_list(), 'capacities': df_calendar['column_0'].to_list()}
|
||||
result = {"date": scrapeDate, "weekdays": df_calendar['weekday'].to_list(), 'capacities': df_calendar['column_0'].to_list()}
|
||||
etl_cache.saveObj(file, result)
|
||||
return result
|
||||
@@ -33,6 +33,6 @@ def property_capacities_monthly(id: int, scrapeDate: str):
|
||||
df_calendar = df_calendar.group_by(['dates', 'date_short', 'numDays']).agg(pl.col("column_0").sum())
|
||||
df_calendar = df_calendar.with_columns((pl.col("column_0") / pl.col("numDays") / 2 * 100).alias("column_0"))
|
||||
df_calendar = df_calendar.sort('dates')
|
||||
result = {"scraping-date": scrapeDate, "months": df_calendar['date_short'].to_list(), 'capacities': df_calendar['column_0'].to_list()}
|
||||
result = {"date": scrapeDate, "months": df_calendar['date_short'].to_list(), 'capacities': df_calendar['column_0'].to_list()}
|
||||
etl_cache.saveObj(file, result)
|
||||
return result
|
||||
@@ -1,8 +1,9 @@
|
||||
import data
|
||||
import polars as pl
|
||||
from io import StringIO
|
||||
import numpy as np
|
||||
|
||||
import numpy as np
|
||||
import polars as pl
|
||||
|
||||
import data
|
||||
|
||||
d = data.load()
|
||||
|
||||
@@ -58,7 +59,7 @@ def region_capacities_comparison(id_1: int, id_2: int):
|
||||
listOfPropertyIDs = listOfPropertyIDs.to_list()
|
||||
|
||||
# Create JSON
|
||||
tempDict = {'scrapeDates': listOfDates, 'property_ids': listOfPropertyIDs, 'values': gridData}
|
||||
tempDict = {'dates': listOfDates, 'property_ids': listOfPropertyIDs, 'values': gridData}
|
||||
outDictList.append(tempDict)
|
||||
|
||||
outDict = {'region1': outDictList[0], 'region2': outDictList[1],}
|
||||
|
||||
+2
-2
@@ -8,7 +8,7 @@ from data import etl_cache
|
||||
|
||||
d = data.load()
|
||||
|
||||
def region_capacities_weekdays(id: int, scrapeDate_start: str):
|
||||
def region_capacities_daily(id: int, scrapeDate_start: str):
|
||||
|
||||
file = f"etl_region_capacities_weekdays_{id}_{scrapeDate_start}.obj"
|
||||
obj = etl_cache.openObj(file)
|
||||
@@ -62,6 +62,6 @@ def region_capacities_weekdays(id: int, scrapeDate_start: str):
|
||||
outDf = outDf.insert_column(1, means)
|
||||
outDf = outDf[['weekday', 'mean']]
|
||||
|
||||
result = {"scraping-date": scrapeDate, "weekdays": outDf['weekday'].to_list(),'capacities': outDf['mean'].to_list()}
|
||||
result = {"date": scrapeDate, "weekdays": outDf['weekday'].to_list(),'capacities': outDf['mean'].to_list()}
|
||||
etl_cache.saveObj(file, result)
|
||||
return result
|
||||
@@ -63,6 +63,6 @@ def region_capacities_monthly(id: int, scrapeDate_start: str):
|
||||
outDf = outDf.insert_column(1, means)
|
||||
outDf = outDf[['date_short', 'mean']]
|
||||
|
||||
result = {"scraping-date": scrapeDate, "months": outDf['date_short'].to_list(),'capacities': outDf['mean'].to_list()}
|
||||
result = {"date": scrapeDate, "months": outDf['date_short'].to_list(),'capacities': outDf['mean'].to_list()}
|
||||
etl_cache.saveObj(file, result)
|
||||
return result
|
||||
@@ -59,6 +59,6 @@ def region_properties_capacities(id: int):
|
||||
listOfPropertyIDs = listOfPropertyIDs.cast(pl.String).to_list()
|
||||
|
||||
# Create JSON
|
||||
outDict = {'scrapeDates': listOfDates, 'property_ids': listOfPropertyIDs, 'values': values}
|
||||
outDict = {'dates': listOfDates, 'property_ids': listOfPropertyIDs, 'values': values}
|
||||
etl_cache.saveObj(file, outDict)
|
||||
return outDict
|
||||
Reference in new issue
Block a user