diff --git a/functions/functions.php b/functions/functions.php index 43e7da7..5e303bc 100644 --- a/functions/functions.php +++ b/functions/functions.php @@ -22,6 +22,13 @@ function build_date_dropdown(mysqli $db, string $table): array return $dates; } +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;'; @@ -141,4 +148,21 @@ function get_production_values_for_month(mysqli $db, string $table, $date): arra } 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; } \ No newline at end of file diff --git a/public/index.php b/public/index.php index 509e31e..76d1fb2 100644 --- a/public/index.php +++ b/public/index.php @@ -37,7 +37,7 @@ $last_value = get_last_value($local_db, $table_aggregation, $chosen_date);
-