This commit is contained in:
mmaurostoffel committed 2024-12-19 18:11:17 +01:00
commit 4b7067fb63
20 files changed
+562 -744

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
# How to run
```bash
fastapi dev api/main.py
```
Generated
+1 -3
View File
@@ -2200,9 +2200,7 @@ packages:
name: consultancy-2
version: 0.1.0
path: .
sha256: c5e1a7be44a8bc92a9119ccc4b7e7e7b3db765694b975b5cd30b288638254471
requires_dist:
- polars
sha256: 878bb6af1502cc9ac71feab6f184f593077f134626d6f8c552e9bcafb178f6b4
requires_python: '>=3.11'
editable: true
- kind: conda
+2 -3
View File
@@ -1,7 +1,6 @@
[project]
authors = [{name = "Giò Diani", email = "mail@gionathandiani.name"}]
dependencies = ["polars"]
description = "Add a short description here"
authors = [{name = "Giò Diani", email = "mail@gionathandiani.name"}, {name = "Mauro Stoffel", email = "mauro.stoffel@stud.fhgr.ch"}, {name = "Colin Bolli", email = "colin.bolli@stud.fhgr.ch"}, {name = "Charles Winkler", email = "charles.winkler@stud.fhgr.ch"}]
description = "Datenauferbeitung"
name = "consultancy_2"
requires-python = ">= 3.11"
version = "0.1.0"
+41
View File
@@ -0,0 +1,41 @@
import data
import polars as pl
from fastapi import FastAPI, Response
d = data.load()
app = FastAPI()
@app.get("/")
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")
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}/extractions")
def property_extractions(id: int):
return d.extractions_for(property_id = id).pl().to_dicts()
@app.get("/property/{id}/base")
def property_base_data(id: int):
return d.property_base_data(id).pl().to_dicts()
-22
View File
@@ -1,22 +0,0 @@
from typing import Union
import polars as pl
from fastapi import FastAPI, Response
import data
d = data.load()
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@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")
+117 -9
View File
@@ -46,13 +46,93 @@ class Database:
def properties_growth(self):
return self.connection.sql("""
WITH PropertiesALL AS (
SELECT
strftime(created_at, '%Y-%m-%d') AS date,
COUNT(*) as properties_count,
SUM(properties_count) OVER (ORDER BY date) AS total
FROM
consultancy_d.properties p
GROUP BY
date
ORDER BY
date
),
PropertiesR1 AS (
SELECT
strftime(created_at, '%Y-%m-%d') AS date,
COUNT(*) as properties_count,
SUM(properties_count) OVER (ORDER BY date) AS total
FROM
consultancy_d.properties p
WHERE
p.seed_id = 1
GROUP BY
date
ORDER BY
date
),
PropertiesR2 AS (
SELECT
strftime(created_at, '%Y-%m-%d') AS date,
COUNT(*) as properties_count,
SUM(properties_count) OVER (ORDER BY date) AS total
FROM
consultancy_d.properties p
WHERE
p.seed_id = 2
GROUP BY
date
ORDER BY
date
),
PropertiesR3 AS (
SELECT
strftime(created_at, '%Y-%m-%d') AS date,
COUNT(*) as properties_count,
SUM(properties_count) OVER (ORDER BY date) AS total
FROM
consultancy_d.properties p
WHERE
p.seed_id = 3
GROUP BY
date
ORDER BY
date
),
PropertiesR4 AS (
SELECT
strftime(created_at, '%Y-%m-%d') AS date,
COUNT(*) as properties_count,
SUM(properties_count) OVER (ORDER BY date) AS total
FROM
consultancy_d.properties p
WHERE
p.seed_id = 4
GROUP BY
date
ORDER BY
date
)
SELECT
strftime(created_at, '%Y-%m-%d') AS date,
COUNT(*) as properties_count
p.date,
p.total AS total_all,
pR1.total as total_heidiland,
pR2.total AS total_davos,
pR3.total AS total_engadin,
pR4.total AS total_stmoritz
FROM
consultancy_d.properties
GROUP BY
date;
PropertiesAll p
LEFT JOIN
PropertiesR1 pR1 ON p.date = pR1.date
LEFT JOIN
PropertiesR2 pR2 ON p.date = pR2.date
LEFT JOIN
PropertiesR3 pR3 ON p.date = pR3.date
LEFT JOIN
PropertiesR4 pR4 ON p.date = pR4.date
ORDER BY
p.date
""")
def properties_per_region(self):
@@ -69,6 +149,8 @@ class Database:
GROUP BY
properties.seed_id,
regions.name
ORDER BY
count_properties ASC
""")
def propIds_with_region(self):
@@ -208,7 +290,7 @@ class Database:
""")
def extractions(self):
return self.connection.sql(f"""
return self.connection.sql("""
SELECT
JSON_EXTRACT(body, '$.content.days') as calendar,
property_id,
@@ -243,15 +325,15 @@ class Database:
return self.connection.sql(f"""
SELECT
JSON_EXTRACT(body, '$.content.days') as calendar,
property_id,
created_at
FROM
consultancy_d.extractions
WHERE
type == 'calendar' AND
property_id = {property_id}
property_id = {property_id} AND
calendar NOT NULL
ORDER BY
property_id
created_at
""")
# Anzahl der extrahierten properties pro Exktraktionsvorgang
@@ -297,3 +379,29 @@ class Database:
ORDER BY property_id
""")
def property_base_data(self, id):
return self.connection.sql(f"""
SELECT
p.property_platform_id,
p.created_at as first_found,
p.last_found,
r.name as region_name
FROM
consultancy_d.properties p
INNER JOIN consultancy_d.seeds s ON s.id = p.seed_id
INNER JOIN consultancy_d.regions r ON s.region_id = r.id
WHERE
p.id = {id}
""")
def properties_geo(self):
return self.connection.sql("""
SELECT
p.id,
p.check_data as coordinates
FROM
consultancy_d.properties p
""")
-149
View File
@@ -1,149 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src=" https://cdn.jsdelivr.net/npm/echarts@5.5.1/dist/echarts.min.js "></script>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
border: 0;
}
#dia-capacity{width: 100%; height: 400px;}
main{
display: grid;
width: 100%;
height: 100vh;
grid-template-columns: 1fr 1fr ;
grid-template-rows: 1fr;
}
#map{
width: 100%;
height: 100%;
background: red;
}
#leaflet{
widows: 100%;
height: 100%;
}
</style>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>
</head>
<body>
<main>
<div>
<select>
<option>Alle Regionen</option>
<option>Davos</option>
<option>Engadin</option>
<option>Heidiland</option>
<option>St. Moritz</option>
</select>
<input type="date" id="start" name="trip-start" value="2018-07-22" min="2018-01-01" max="2018-12-31" />
<div id="dia-capacity"></div>
</div>
<section id="map">
<div id="leaflet"></div>
</section>
</main>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script>
<script type="text/javascript">
let properties = [[46.49593,9.83718],[46.48089,9.81599],[46.48824,9.83301],[46.497932,9.836767],[46.48615,9.83104],[46.4972399,9.8513866],[46.4867345,9.8349723],[46.4926593,9.8352605],[46.488409,9.840455],[46.5002929,9.8428447],[46.48024,9.81368],[46.48627,9.83095],[46.4904722,9.8346281],[46.48602,9.83109],[46.47736,9.81286],[46.4865553,9.8404283],[46.486563,9.8311711],[46.493988,9.836039],[46.4975,9.83937],[46.4852478,9.8316748],[46.488567,9.8424808],[46.4883782,9.8355532],[46.4868242,9.8332824],[46.49642,9.836712],[46.4883782,9.8355532],[46.47748,9.80948],[46.5002642,9.8429209],[46.4980738,9.8498749],[46.5034907,9.8465318],[46.4868242,9.8332824],[46.47927,9.81362],[46.4875089,9.8328176],[46.4884205,9.837406],[46.4861438,9.8311151],[46.49757,9.83946],[46.4933517,9.8352912],[46.4864667,9.8404068],[46.4866534,9.830708],[46.480585,9.815248],[46.4870555,9.8334099],[46.4786218,9.8127465],[46.48837,9.84037],[46.48604,9.83109],[46.4809238,9.8160696],[46.4898,9.83629],[46.48683,9.8333162],[46.4883782,9.8355532],[46.4771336,9.8134695],[46.4928286,9.8350188],[46.488891,9.841088],[46.499849,9.837025],[46.495869,9.837594],[46.47701,9.81146],[46.486404,9.8416679],[46.4783711,9.8132931],[46.49159,9.83546],[46.488303,9.840426],[46.4975017,9.8395553],[46.48672,9.83717],[46.4883753,9.8403742],[46.4883753,9.8403742],[46.4899857,9.8363568],[46.4863272,9.8405407],[46.497487,9.839477],[46.5020769,9.8418942],[46.50238,9.84558],[46.4884,9.84045],[46.48676,9.835],[46.4782248,9.8130534],[46.47749,9.80944],[46.48824,9.83301],[46.4869233,9.8376718],[46.4883782,9.8355532],[46.4882516,9.8402106],[46.49735,9.83945],[46.4883782,9.8355532],[46.48665,9.83723],[46.4883782,9.8355532],[46.49731,9.838838],[46.4883753,9.8403742],[46.4955609,9.8375856],[46.484052,9.8379806],[46.4883782,9.8355532],[46.496111,9.837175],[46.4843287,9.8376702],[46.4864902,9.8329028],[46.4895127,9.8359552],[46.503276,9.84604],[46.4852328,9.8323521],[46.4873273,9.8371615],[46.503511,9.846615],[46.48668,9.83719],[46.488172,9.833806],[46.4873397,9.831455],[46.4866534,9.830708],[46.495563,9.837539],[46.4984,9.84463],[46.4980738,9.8498749],[46.489777,9.8345806],[46.477581,9.810936],[46.4928286,9.8350188],[46.4974869,9.839558],[46.47591,9.8105111],[46.4887022,9.8352726],[46.4883233,9.8402706],[46.48668,9.83717],[46.47862,9.81366],[46.4891482,9.8343803],[46.4768608,9.813171],[46.477549,9.810314],[46.4895127,9.8359552],[46.498344,9.844374],[46.5032565,9.8460512],[46.4791522,9.8122151],[46.4985892,9.8401749],[46.48664,9.83696],[46.4859316,9.8307934],[46.48665,9.83723],[46.49876,9.837459],[46.4866,9.83716],[46.4885071,9.8411413],[46.4985324,9.8401342],[46.4849695,9.8320309],[46.4978506,9.8383183],[46.503635,9.846695],[46.5023548,9.8455593],[46.4865151,9.8403382],[46.48691,9.83212],[46.498764,9.8374597],[46.5003728,9.8424702],[46.498543,9.840123],[46.4987598,9.8374593],[46.487275,9.8323903],[46.5004613,9.8432744],[46.493155,9.835243],[46.4954953,9.8360637],[46.4890826,9.8354328],[46.4879702,9.8425062],[46.4974869,9.839558],[46.497987,9.8351241],[46.4768315,9.8132372],[46.503511,9.846615],[46.4891952,9.8354489],[46.489485,9.836001],[46.48838,9.83555],[46.4986225,9.8441318],[46.4886203,9.838023],[46.4883753,9.8403742],[46.496488,9.836766],[46.486678,9.837191],[46.476946,9.811528],[46.4786,9.81389],[46.4795257,9.8137264],[46.48089,9.81599],[46.498671,9.837675],[46.4883805,9.8355637],[46.4974869,9.839558],[46.4931507,9.8350282],[46.496111,9.837175],[46.4759,9.80941],[46.4809147,9.8159934],[46.477291,9.811467],[46.4763346,9.8103601],[46.48686,9.84097],[46.4909066,9.8347269],[46.502291,9.845428],[46.480674,9.814221],[46.484197,9.83739],[46.4918196,9.8345876],[46.4915284,9.8338491],[46.4974338,9.8350768],[46.48672,9.83717],[46.4867237,9.8371725],[46.48669,9.83724],[46.4939895,9.8359017],[46.5032761,9.84604],[46.498566,9.840089],[46.498344,9.844374],[46.4786235,9.8136651],[46.50028,9.84108],[46.4893186,9.8335381],[46.4873711,9.8325248],[46.4945882,9.835511],[46.475007,9.807983],[46.47591,9.8105111],[46.4971764,9.8369717],[46.4952387,9.8375284],[46.4940576,9.8369153],[46.4840021,9.8379366],[46.493951,9.835891],[46.5012405,9.8427173],[46.5388,9.87594],[46.60211,9.96407],[46.4946,9.90307],[46.55315,9.88566],[46.57833,9.92136],[46.55325,9.88494],[46.57227,9.93144],[46.6964,10.09554],[46.57807,9.92007],[46.4604,9.80592],[46.4329,9.76601],[46.50247,9.89315],[46.5144288,9.8599243],[46.60363,9.96082],[46.51445,9.85767],[46.57808,9.92007],[46.50533,9.89026],[46.57808,9.92007],[46.5785,9.91998],[46.6968,10.09621],[46.51028,9.85812],[46.57609,9.93219],[46.65407,10.0328],[46.55137,9.8876],[46.5759,9.93054],[46.41343,9.74001],[46.57322,9.93254],[46.51526,9.85821],[46.43307,9.76493],[46.53437,9.86964],[46.50093,9.8949],[46.60214,9.96407],[46.55337,9.89231],[46.49926,9.8962],[46.50068,9.8926],[46.50294,9.89265],[46.43313,9.7644],[46.55482,9.88683],[46.7947,10.24635],[46.69741,10.0937],[46.57324,9.93254],[46.57473,9.93214],[46.50247,9.89317],[46.42964,9.76165],[46.4989708,9.8966111],[46.53261,9.86Line truncated
var map = L.map('leaflet').setView([46.862962, 9.535296], 9);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
properties.forEach( prop => {
L.marker(prop).addTo(map);
})
const chartDom = document.getElementById('dia-capacity');
const myChart = echarts.init(chartDom);
const filters = {
regions: ["Davos", "Engadin", "Heidiland", "St. Moritz"]
}
const option = {
title: {
text: 'Anzahl Properties in Extraction'
},
tooltip: {
trigger: 'axis'
},
legend: {
data: filters.regions
},
grid: {
left: '0',
right: '0',
bottom: '0',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['2024-04-14','2024-04-15','2024-04-16','2024-04-19','2024-04-22','2024-04-25','2024-04-28','2024-05-01','2024-05-04','2024-05-07','2024-05-10','2024-05-13','2024-05-16','2024-05-19','2024-05-22','2024-05-25','2024-05-28','2024-05-31','2024-06-01','2024-06-04','2024-06-07','2024-06-10','2024-06-13','2024-06-16','2024-06-19','2024-06-22','2024-06-25','2024-06-28','2024-07-01','2024-07-04','2024-07-07','2024-07-10','2024-07-13','2024-07-16','2024-07-19','2024-07-22','2024-07-25','2024-07-28','2024-07-31','2024-08-01','2024-08-04','2024-08-07','2024-08-10','2024-08-13','2024-08-16','2024-08-19','2024-08-22']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'Alle',
type: 'line',
stack: 'Total',
data: [596, 239, 835, 673, 863, 1803, 904, 915, 958, 966, 1001, 1031, 1044, 1055, 1158, 1162, 1181, 1203, 1207, 1214, 1254, 1258, 1264, 1288, 1296, 1305, 1318, 1323, 1330, 1333, 1342, 1350, 1436, 1454, 1461, 1469, 1492, 1504, 1506, 1510, 1512, 1518, 1534, 1535, 1541, 1544, 1500]
},
{
name: 'Heidiland',
type: 'line',
stack: 'Heidiland',
data: [133,64,197,151,197,417,210,213,215,220,226,239,247,251,251,252,262,275,276,277,281,283,284,286,287,287,287,287,287,287,287,289,290,292,293,294,294,294,295,295,295,296,312,313,313,313,301]
},
{
name: 'Davos',
type: 'line',
stack: 'Davos',
data: [133,56,189,152,196,409,206,209,209,209,221,223,223,224,226,227,230,234,236,238,250,252,252,259,261,263,267,270,272,272,274,274,277,278,279,281,286,289,289,289,289,290,290,290,293,296,285]
},
{
name: 'Engadin',
type: 'line',
stack: 'Engadin',
data: [185,73,258,212,278,569,284,289,326,326,340,346,350,355,413,413,413,413,414,415,438,438,442,455,460,463,470,472,477,479,484,486,544,554,558,561,578,585,586,590,592,595,595,595,597,597,583]
},
{
name: 'St. Moritz',
type: 'line',
stack: 'St. Moritz',
data: [145,46,191,158,192,408,204,204,208,211,214,223,224,225,268,270,276,281,281,284,285,285,286,288,288,292,294,294,294,295,297,301,325,330,331,333,334,336,336,336,336,337,337,337,338,338,331]
},
]
};
option && myChart.setOption(option);
</script>
</body>
</html>
-16
View File
@@ -1,16 +0,0 @@
import polars as pl
import data
inst = data.load()
test = inst.extractions_for(1).pl()
out = test.with_columns(
pl.col("calendar").str.extract_all(r"([0-9]{4}-[0-9]{2}-[0-9]{2})|[0-2]").alias("extracted_cal"),
)
out = out.drop(['calendar', 'property_id'])
print(out.to_dict(as_series=True))