179 lines
7.8 KiB
PHP
179 lines
7.8 KiB
PHP
<?php
|
|
|
|
function build_date_dropdown(mysqli $db, string $table): array
|
|
{
|
|
$first_date_query = 'SELECT MIN(date) FROM ' . $table . ';';
|
|
$first_date_result = $db->query($first_date_query);
|
|
$first_date = new DateTime($first_date_result->fetch_column(0));
|
|
|
|
$last_date_query = 'SELECT MAX(date) FROM ' . $table . ';';
|
|
$last_date_result = $db->query($last_date_query);
|
|
$last_date = new DateTime($last_date_result->fetch_column(0));
|
|
|
|
$first_date->modify('first day of this month');
|
|
$last_date->modify('first day of this month');
|
|
|
|
$dates = [];
|
|
|
|
for ($date = $last_date; $date >= $first_date; $date = $date->modify('-1 month')) {
|
|
$dates[] = $date->format('Y-m');
|
|
}
|
|
|
|
return $dates;
|
|
}
|
|
|
|
function get_month(mysqli $db, string $table): array
|
|
{
|
|
$date_query = 'SELECT MIN(date) AS first, MAX(date) AS last FROM ' . $table . ';';
|
|
$date = $db->query($date_query)->fetch_assoc();
|
|
|
|
$first_date = new DateTime($date['first']);
|
|
$last_date = new DateTime($date['last']);
|
|
|
|
return ['first' => $first_date, 'last' => $last_date];
|
|
}
|
|
|
|
function get_years(mysqli $db, string $table): array
|
|
{
|
|
$years_query = 'SELECT MIN(YEAR(date)) AS first, MAX(YEAR(date)) AS last FROM ' . $table . ';';
|
|
|
|
return $db->query($years_query)->fetch_assoc();
|
|
}
|
|
|
|
function get_aggregation(mysqli $db, string $table, $date): array
|
|
{
|
|
$value_query = 'SELECT date, meter_consumption, power_sensor, grid_feed, production_eg, production_og FROM ' . $table . ' WHERE date LIKE "' . $date . '%" ORDER BY date ASC;';
|
|
$value_result = $db->query($value_query);
|
|
|
|
$values = [];
|
|
$values['avg_meter'] = 0;
|
|
$values['avg_power'] = 0;
|
|
$values['avg_feed'] = 0;
|
|
$values['avg_production'] = 0;
|
|
while ($row = $value_result->fetch_assoc()) {
|
|
$production = ($row['production_eg'] + $row['production_og']);
|
|
$values['date'][] = $row['date'];
|
|
$values['meter_consumption'][] = $row['meter_consumption'];
|
|
$values['power_sensor'][] = $row['power_sensor'];
|
|
$values['grid_feed'][] = $row['grid_feed'];
|
|
$values['production'][] = $production;
|
|
$values['min_meter'] = (isset($values['min_meter']) && $values['min_meter'] < $row['meter_consumption']) ? $values['min_meter'] : $row['meter_consumption'];
|
|
$values['max_meter'] = (isset($values['max_meter']) && $values['max_meter'] > $row['meter_consumption']) ? $values['max_meter'] : $row['meter_consumption'];
|
|
$values['avg_meter'] += $row['meter_consumption'];
|
|
$values['min_power'] = (isset($values['min_power']) && $values['min_power'] < $row['power_sensor']) ? $values['min_power'] : $row['power_sensor'];
|
|
$values['max_power'] = (isset($values['max_power']) && $values['max_power'] > $row['power_sensor']) ? $values['max_power'] : $row['power_sensor'];
|
|
$values['avg_power'] += $row['power_sensor'];
|
|
$values['min_feed'] = (isset($values['min_feed']) && $values['min_feed'] < $row['grid_feed']) ? $values['min_feed'] : $row['grid_feed'];
|
|
$values['max_feed'] = (isset($values['max_feed']) && $values['max_feed'] > $row['grid_feed']) ? $values['max_feed'] : $row['grid_feed'];
|
|
$values['avg_feed'] += $row['grid_feed'];
|
|
$values['min_production'] = (isset($values['min_production']) && $values['min_production'] < $production) ? $values['min_production'] : $production;
|
|
$values['max_production'] = (isset($values['max_production']) && $values['max_production'] > $production) ? $values['max_production'] : $production;
|
|
$values['avg_production'] += $production;
|
|
}
|
|
|
|
$values['avg_meter'] /= count($values['meter_consumption']);
|
|
$values['avg_power'] /= count($values['power_sensor']);
|
|
$values['avg_feed'] /= count($values['grid_feed']);
|
|
$values['avg_production'] /= count($values['production']);
|
|
|
|
return $values;
|
|
}
|
|
|
|
function get_month_aggregation(mysqli $db, string $table, $date): array
|
|
{
|
|
$sum_query = 'SELECT SUM(meter_consumption) / 1000 as meter_consumption, SUM(power_sensor) / 1000 as power_sensor, SUM(grid_feed) / 1000 as grid_feed FROM ' . $table . ' WHERE date LIKE "' . $date . '%";';
|
|
$sum_result = $db->query($sum_query);
|
|
|
|
return $sum_result->fetch_assoc();
|
|
}
|
|
|
|
function get_year_aggregation(mysqli $db, string $table, $date): array
|
|
{
|
|
$year = substr($date, 0, 4);
|
|
$sum_query = 'SELECT SUM(meter_consumption) / 1000 as meter_consumption, SUM(power_sensor) / 1000 as power_sensor, SUM(grid_feed) / 1000 as grid_feed FROM ' . $table . ' WHERE date LIKE "' . $year . '%";';
|
|
$sum_result = $db->query($sum_query);
|
|
|
|
return $sum_result->fetch_assoc();
|
|
}
|
|
|
|
function get_grid_feed_by_month(mysqli $db, string $table_aggregation, string $table_production, $date): array
|
|
{
|
|
$year = substr($date, 0, 4);
|
|
$feed_query = 'SELECT SUM(grid_feed), YEAR(date) as year, MONTH(date) as month FROM ' . $table_aggregation . ' WHERE date LIKE "' . $year . '%" GROUP BY YEAR(date), MONTH(date);';
|
|
$feed_result = $db->query($feed_query);
|
|
|
|
$price_query = 'SELECT YEAR(date) as year, MONTH(date) as month, price FROM ' . $table_production . ' WHERE date LIKE "' . $year . '%";';
|
|
$price_result = $db->query($price_query);
|
|
|
|
$price = [];
|
|
while ($row = $price_result->fetch_assoc()) {
|
|
$price[$row['year'] . '-' . str_pad($row['month'], 2, '0', STR_PAD_LEFT)] = $row['price'];
|
|
}
|
|
|
|
$grid_feed_by_month = [];
|
|
|
|
while ($row = $feed_result->fetch_assoc()) {
|
|
$grid_feed_by_month[$row['year'] . '-' . str_pad($row['month'], 2, '0', STR_PAD_LEFT)] = $row['SUM(grid_feed)'] * $price[$row['year'] . '-' . str_pad($row['month'], 2, '0', STR_PAD_LEFT)] / 100000;
|
|
}
|
|
|
|
return $grid_feed_by_month;
|
|
}
|
|
|
|
function get_month_production(mysqli $db, string $table, $date): array
|
|
{
|
|
$sum_query = 'SELECT eg, og, price FROM ' . $table . ' WHERE date LIKE "' . $date . '%";';
|
|
$sum_result = $db->query($sum_query);
|
|
if ($sum_result->num_rows === 0) {
|
|
return ['eg' => 0, 'og' => 0, 'price' => 0];
|
|
}
|
|
|
|
return $sum_result->fetch_assoc();
|
|
}
|
|
|
|
function get_year_production(mysqli $db, string $table, $date): array
|
|
{
|
|
$year = substr($date, 0, 4);
|
|
$sum_query = 'SELECT SUM(eg) as eg, SUM(eg * price) / 100 as eg_price, SUM(og) as og, SUM(og * price) / 100 as og_price FROM ' . $table . ' WHERE date LIKE "' . $year . '%";';
|
|
$sum_result = $db->query($sum_query);
|
|
|
|
return $sum_result->fetch_assoc();
|
|
}
|
|
|
|
function get_last_value(mysqli $db, string $table, $date): float
|
|
{
|
|
$value_query = 'SELECT `last_value` FROM ' . $table . ' WHERE date LIKE "' . $date . '%" ORDER BY date DESC LIMIT 1;';
|
|
$value_result = $db->query($value_query);
|
|
$data = $value_result->fetch_assoc();
|
|
|
|
return floatval($data['last_value']) / 1000;
|
|
}
|
|
|
|
function get_production_values_for_month(mysqli $db, string $table, $date): array
|
|
{
|
|
$value_query = 'SELECT date, production_og, production_eg FROM ' . $table . ' WHERE date LIKE "' . $date . '%" ORDER BY date ASC;';
|
|
$value_result = $db->query($value_query);
|
|
$production_values = [];
|
|
|
|
while ($row = $value_result->fetch_assoc()) {
|
|
$production_values[] = $row;
|
|
}
|
|
|
|
return $production_values;
|
|
}
|
|
|
|
function get_year_values(mysqli $db, string $table, $year): array
|
|
{
|
|
$query = 'SELECT MONTH(date) AS month, ROUND(SUM(meter_consumption / 1000)) AS meter_consumption, ROUND(SUM(power_sensor / 1000)) as power_sensor, ROUND(SUM(grid_feed / 1000)) as grid_feed, ROUND(SUM(production_og + production_eg) / 1000) AS production FROM ' . $table . ' WHERE YEAR(date) = ' . $year . ' GROUP BY MONTH(date) ORDER BY MONTH(date);';
|
|
$result = $db->query($query);
|
|
$values = [];
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
$values['date'][] = $row['month'];
|
|
$values['meter_consumption'][] = $row['meter_consumption'];
|
|
$values['power_sensor'][] = $row['power_sensor'];
|
|
$values['grid_feed'][] = $row['grid_feed'];
|
|
$values['production'][] = $row['production'];
|
|
}
|
|
|
|
return $values;
|
|
} |