ConsultancyProject_2_ETL/dashboard/routes/web.php

229 lines
7.0 KiB
PHP

<?php
use Illuminate\Support\Facades\Route;
use App\Api;
use App\Chart;
Route::get('/', function () {
$regionBase = Api::regionBase(-1);
$regionPropertiesCapacities = Api::regionPropertiesCapacities(-1);
$propertiesGrowth = Api::propertiesGrowth();
$regions = Api::regions()['regions'];
$propertiesPerRegion = $regions;
$regions[] = ['name' => 'Alle Regionen', 'id' => -1];
$propertiesGeo = Api::propertiesGeo()['properties'];
$heatmapValues = [];
foreach ($regionPropertiesCapacities['values'] as $el) {
$heatmapValues[] = array_values($el);
}
$diagramsOptions = [
"shared" => [
"extractionDates" => json_encode($regionPropertiesCapacities['dates']),
"colors" => Chart::colors()
],
"heatmap" => [
"yAxis" => [
"data" => json_encode($regionPropertiesCapacities['property_ids'])
],
"series" => [
"data" => json_encode($heatmapValues)
]
],
"propertiesPerRegion" => [
"yAxis" => [
"data" => json_encode(array_column($propertiesPerRegion, 'count_properties'))
],
"xAxis" => [
"data" => json_encode(array_column($propertiesPerRegion, 'name'))
]
],
"extractions" => [
"series" => $propertiesGrowth,
]
];
return view('overview', [
"regions" => $regions,
"region" => $regionBase,
"diagramsOptions" => $diagramsOptions,
"geo" => $propertiesGeo,
]);
});
Route::get('/region/{id}', function (int $id) {
$regions = Api::regions()['regions'];
$regions[] = ['name' => 'Alle Regionen', 'id' => -1];
$region = $id >= 0 ? Api::regionBase($id) : ['name' => 'Alle Regionen'];
$regionPropertiesCapacities = Api::regionPropertiesCapacities($id);
$regionCapacitiesRegion = Api::regionCapacities($id);
$regionCapacitiesAll = Api::regionCapacities(-1);
$regionCapacitiesMonthly = [];
$regionCapacitiesDaily = [];
$regionPredictions = [];
$heatmapValues = [];
foreach ($regionPropertiesCapacities['values'] as $el) {
$heatmapValues[] = array_values($el);
}
foreach ($regionCapacitiesRegion['dates'] as $date) {
$regionCapacitiesMonthly[] = Api::regionCapacitiesMonthly($id, $date);
$regionCapacitiesDaily[] = Api::regionCapacitiesDaily($id, $date);
$regionPredictions[] = Api::regionMovingAverage($id, $date);
}
$diagramsOptions = [
"shared" => [
"extractionDates" => json_encode($regionPropertiesCapacities['dates']),
"colors" => Chart::colors()
],
"heatmap" => [
"xAxis" => [
"data" => json_encode($regionPropertiesCapacities['dates'])
],
"yAxis" => [
"data" => json_encode($regionPropertiesCapacities['property_ids'])
],
"series" => [
"data" => json_encode($heatmapValues)
]
],
"predictions" => [
"options" => $regionPredictions,
],
"capacityMonthly" => [
"options" => $regionCapacitiesMonthly,
],
"capacityDaily" => [
"options" => $regionCapacitiesDaily,
],
"capacity" => [
"xAxis" => [
"data" => json_encode($regionCapacitiesRegion['dates'])
],
"series" => [
"all" => [
"data" => json_encode($regionCapacitiesAll['capacities'])
],
"region" => [
"data" => json_encode($regionCapacitiesRegion['capacities'])
]
]
]
];
return view('region', [
'diagramsOptions' => $diagramsOptions,
'startDate' => $regionCapacitiesRegion['dates'][0],
'regions' => $regions,
'region' => $region,
'regionId' => $id,
'regionPropertiesCapacities' => $regionPropertiesCapacities,
'predictions' => $regionPredictions]);
});
Route::get('/property/{id}', function (int $id) {
$regions = Api::regions()['regions'];
$regions[] = ['name' => 'Alle Regionen', 'id' => -1];
$base = Api::propertyBase($id);
$calendars = Api::propertyExtractions($id)['extractions'];
$propertyCapacities = Api::propertyCapacities($id);
$propertyNeighbours = Api::propertyNeighbours($id)['neighbours'];
$regionCapacitiesRegion = Api::regionCapacities($base['region_id']);
$regionCapacitiesAll = Api::regionCapacities(-1);
$regionCapacities = [[],[]];
$propertyCapacitiesMonthly = [];
$propertyCapacitiesDaily = [];
if($propertyCapacities){
foreach ($propertyCapacities['dates'] as $date) {
$propertyCapacitiesMonthly[] = Api::propertyCapacitiesMonthly($id, $date);
$propertyCapacitiesDaily[] = Api::propertyCapacitiesDaily($id, $date);
}
// filter out all date, which were not scraped for the property
foreach ($regionCapacitiesAll['dates'] as $index => $date) {
if(in_array($date, $propertyCapacities['dates'])){
$regionCapacities[0][] = $regionCapacitiesAll['capacities'][$index];
}
}
foreach ($regionCapacitiesRegion['dates'] as $index => $date) {
if(in_array($date, $propertyCapacities['dates'])){
$regionCapacities[1][] = $regionCapacitiesRegion['capacities'][$index];
}
}
}else{
return view('property-nodata', [
'base' => $base,
'regions' => $regions,
]);
}
// prepare data for calendar chart
$calendarData = [];
foreach ($calendars as $el) {
$series = [];
$calendar = json_decode($el['calendar'], 1);
foreach ($calendar as $date => $status) {
$series[] = [$date, $status];
}
$calendarData[] = $series;
}
$diagramsOptions = [
"shared" => [
"colors" => Chart::colors(),
"extractiondates" => json_encode($propertyCapacities['dates'])
],
"calendar" => [
"series" => $calendarData
],
"capacities" => [
"xAxis" => [
"data" => json_encode($propertyCapacities['dates'])
],
"series" => [
["data" => json_encode($propertyCapacities['capacities'])],
["data" => json_encode($regionCapacities[0])],
["data" => json_encode($regionCapacities[1])],
]
],
"capacityMonthly" => [
"options" => $propertyCapacitiesMonthly,
],
"capacityDaily" => [
"options" => $propertyCapacitiesDaily,
],
];
return view('property', [
'diagramsOptions' => $diagramsOptions,
'startDate' => $propertyCapacities['dates'][0],
'base' => $base,
'regions' => $regions,
'neighbours' => $propertyNeighbours
]);
});