Rework dashboard

This commit is contained in:
Luca Haid
2025-01-10 11:12:33 +00:00
parent 1147cb259f
commit 50fb2ca7d8
5 changed files with 204 additions and 55 deletions
@@ -158,6 +158,26 @@ class DashboardNewController extends mfBaseController {
}
};
$baufortschritt_140 = $countFunction([">status_code" => "139", "<status_code" => "899"]);
$timeline_baufortschritt_140 = $this->getTimeline('weekly-leerrohr', $campaign_ids, $gemeinde_ids);
$ont_installiert_300 = $countFunction([">status_code" => "299", "<status_code" => "899"]);
$timeline_ont_installiert_300 = $this->getTimeline('weekly-ont-installed', $campaign_ids, $gemeinde_ids);
$baufortschritt_140_base_value_before_history = $baufortschritt_140 - $timeline_baufortschritt_140[0][count($timeline_baufortschritt_140[0]) - 1]['value'];
foreach ($timeline_baufortschritt_140[0] as $key => $value) {
$timeline_baufortschritt_140[0][$key]['value'] += $baufortschritt_140_base_value_before_history;
}
$ont_installiert_300_base_value_before_history = $ont_installiert_300 - $timeline_ont_installiert_300[0][count($timeline_ont_installiert_300[0]) - 1]['value'];
foreach ($timeline_ont_installiert_300[0] as $key => $value) {
$timeline_ont_installiert_300[0][$key]['value'] += $ont_installiert_300_base_value_before_history;
}
self::returnJson([
'order_max_home_addrdb' => $order_max_homes,
'order_actual_order' => $countFunction([]),
@@ -167,21 +187,23 @@ class DashboardNewController extends mfBaseController {
'order_mph' => $countFunction(["connection_type" => $mph_connection_types]),
'order_mph_vorsorge' => $countFunction(["connection_type" => $mph_connection_types, "type" => "provision"]),
'order_mph_vollanschluss' => $countFunction(["connection_type" => $mph_connection_types, "type" => "order"]),
'baufortschritt_140' => $countFunction([">status_code" => "139", "<status_code" => "899"]),
'baufortschritt_140' => $baufortschritt_140,
'installationspaket_erhalten' => $countFunction(["connection_type" => $efh_connection_types], 145),
'lehrrohr_im_haus' => $countFunction(["connection_type" => $efh_connection_types], 200),
'inhouse_kabel_verlegt_efh' => $countFunction(["connection_type" => $efh_connection_types], 242),
'inhouse_kabel_verlegt_mph' => $countFunction(["connection_type" => $mph_connection_types], 242),
'installationsfortschritt_245' => $countFunction([">status_code" => "244", "<status_code" => "899"]),
'ont_installiert_300' => $countFunction([">status_code" => "299", "<status_code" => "899"]),
'ont_installiert_300' => $ont_installiert_300,
'vollanschluss_dokumentiert_350' => $countFunction(["status_code" => ["350","500"], "type" => "order"]),
'vorsorge_dokumentiert_351' => $countFunction(["status_code" => ["351","500"], "type" => "provision"]),
'provider_bestellt_500' => $countFunction(["status_code" => "500"]),
'timeline' => $this->getTimeline('weekly-orders', $campaign_ids, $gemeinde_ids)
'timeline' => $this->getTimeline('weekly-orders', $campaign_ids, $gemeinde_ids),
'timeline_leerrohr' => $timeline_baufortschritt_140,
'timeline_ont_installed' => $timeline_ont_installiert_300
]);
}
private function getTimeline($type, $campaign_ids, $gemeinde_ids) {
private function getTimeline($type, $campaign_ids, $gemeinde_ids) { //TODO: fix gemeinde
$timeline = [];
$baseParams = ["preordercampaign_id" => $campaign_ids];
if (!empty($gemeinde_ids)) {
@@ -196,6 +218,11 @@ class DashboardNewController extends mfBaseController {
return $timeline;
}
// add Leerrohr to timeline
// add ont installed to timeline
private function getTimelineData($type, $params) {
$timeline = [];
$start = strtotime('-1 year');
@@ -203,18 +230,74 @@ class DashboardNewController extends mfBaseController {
$interval = new DateInterval('P1W');
$daterange = new DatePeriod(date_create(date('Y-m-d', $start)), $interval, date_create(date('Y-m-d', $end)));
foreach ($daterange as $date) {
$date = $date->format('Y-m-d');
$params['add-where'] = " AND tt_preorder.`create` >= UNIX_TIMESTAMP('" . $date . " 00:00:00') AND tt_preorder.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$timeline[] = [
'date' => date(DATE_ATOM, strtotime($date)),
'value' => PreorderModel::countActive($params)
];
// Generate a unique cache key based on the function parameters
$cacheKey = md5($type . serialize($params));
$cacheFile = TEMP_DIR . "/Dashboard/" . $cacheKey . ".json";
// Ensure the cache directory exists
if (!is_dir(TEMP_DIR . "/Dashboard/")) {
mkdir(TEMP_DIR . "/Dashboard/", 0755, true);
}
return $timeline;
// Load cached data if it exists
if (file_exists($cacheFile)) {
$cachedData = json_decode(file_get_contents($cacheFile), true);
$lastCachedDate = end($cachedData)['date'];
$lastCachedTimestamp = strtotime($lastCachedDate);
}
// Only process the last 5 weeks
$daterange = new DatePeriod(
date_create(date('Y-m-d', max($lastCachedTimestamp, strtotime('-5 weeks')))),
$interval,
date_create(date('Y-m-d', $end))
);
$timeline = $cachedData;
}
foreach ($daterange as $date) {
$date = $date->format('Y-m-d');
$value = 0;
switch ($type) {
case 'weekly-orders':
$params['add-where'] = " AND tt_preorder.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countActive($params);
break;
case 'weekly-leerrohr':
$params['add-where'] = " AND ph.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countHistoryStatus($params, 140);
break;
case 'weekly-ont-installed':
$params['add-where'] = " AND ph.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countHistoryStatus($params, 300);
break;
}
$newData = [
'date' => date(DATE_ATOM, strtotime($date)),
'value' => $value
];
// Update existing data or add new data
$existingIndex = array_search($newData['date'], array_column($timeline, 'date'));
if ($existingIndex !== false) {
$timeline[$existingIndex] = $newData;
} else {
$timeline[] = $newData;
}
}
// Sort the timeline by date
usort($timeline, function($a, $b) {
return strtotime($a['date']) - strtotime($b['date']);
});
// Cache the updated data
file_put_contents($cacheFile, json_encode($timeline));
return $timeline;
}
private function getTotalHomes(array $preordercampaign_id = [], array $gemeinde_id = []) {
$baseSQL = "SELECT COUNT(adb_wohneinheit.id) as cnt FROM `" . ADDRESSDB_DBNAME . "`.Wohneinheit adb_wohneinheit