Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions front/src/app/embalse/[embalse]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default async function EmbalseDetallePage({ params }: Props) {
const { embalse } = await params;
const embalseDoc = await getEmbalseBySlugCached(embalse);
const embalseInfo = await getReservoirInfoBySlugCached(embalse);
const actualYear = new Date().getFullYear();
const actualMonth = new Date().getMonth(); // return month 0-11

if (!embalseDoc) {
Expand All @@ -52,7 +51,6 @@ export default async function EmbalseDetallePage({ params }: Props) {
const averageHistoricalData = await getAverageHistoricalByMonthCached(
embalseDoc.nombre,
actualMonth + 1,
actualYear - 10, // 10 years ago
).then(mapHistoricalReservoirToViewModel);

return (
Expand Down
1 change: 0 additions & 1 deletion front/src/pods/embalse/api/embalse.api-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ export interface ReservoirLastYearModel {
export interface HistoricalAverageReservoir {
embalse: string;
mes: number;
año: number;
promedio_agua_actual: number;
}
2 changes: 0 additions & 2 deletions front/src/pods/embalse/api/embalse.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,11 @@ export const getAverageHistoricalByMonthCached = unstable_cache(
async (
reservoirName: string,
month: number,
year: number,
): Promise<HistoricalAverageReservoir> => {
try {
const historicalStatistics = await getAverageHistoricalByMonth(
reservoirName,
month,
year,
);

if (!historicalStatistics) {
Expand Down
3 changes: 1 addition & 2 deletions front/src/pods/embalse/components/chart/chart-legend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ export const ChartLegend: React.FC<Props> = ({
<div className="mx-auto h-0.5 w-10 border-t-4 border-dotted border-(--line-average-last-ten-years)" />
<div>
<span>
{monthsNames[dataTenYearsAgo.month - 1]} de {dataTenYearsAgo.year}
:
Media {monthsNames[dataTenYearsAgo.month - 1]} (10 años):
</span>
<span className="pl-1">{dataTenYearsAgo.average} Hm³</span>
</div>
Expand Down
4 changes: 1 addition & 3 deletions front/src/pods/embalse/components/chart/history-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ export const HistoryChart: React.FC<ChartModel> = ({
y={labelY}
textAnchor="middle"
fontSize="16px"
fill={
isOutside ? "var(--color-base-content)" : "var(--color-brand-100)"
}
fill="var(--color-brand-100)"
fontWeight="900"
>
{reservoirData.currentVolume} Hm³
Expand Down
2 changes: 0 additions & 2 deletions front/src/pods/embalse/embalse.mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ describe("mapReservoirLastYearToViewModel", () => {
describe("mapHistoricalReservoirToViewModel", () => {
it("should accurately map the fields for the last ten years", () => {
const mockHistoricalData: historicalApi.HistoricalAverageReservoir = {
año: 2026,
embalse: "Viñuela, La",
mes: 3,
promedio_agua_actual: 149.21,
Expand All @@ -241,7 +240,6 @@ describe("mapHistoricalReservoirToViewModel", () => {
mapHistoricalReservoirToViewModel(mockHistoricalData);

const expectResult = {
year: 2026,
nameReservoir: "Viñuela, La",
month: 3,
average: 149.21,
Expand Down
1 change: 0 additions & 1 deletion front/src/pods/embalse/embalse.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const mapHistoricalReservoirToViewModel = (
return Boolean(apiData)
? {
nameReservoir: apiData.embalse,
year: apiData.año,
month: apiData.mes,
average: apiData.promedio_agua_actual,
}
Expand Down
24 changes: 17 additions & 7 deletions front/src/pods/embalse/embalse.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,15 @@ export const getAverageLastYearByMonth = async (
};

/**
* Obtain the average monthly and annual values ​​received per parameter
* Obtain the average for a given month across the last 10 years.
*
* @param name:string Reservoir name
* @param month:number Month number (1-12)
* @param year:number Year number (yyyy)
* @returns HistoricalAverageReservoir:
*/
export const getAverageHistoricalByMonth = async (
reservoirName: string,
month: number,
year: number,
): Promise<ApiModel.HistoricalAverageReservoir> => {
try {
const db = await getDb();
Expand All @@ -79,14 +77,26 @@ export const getAverageHistoricalByMonth = async (
.aggregate<ApiModel.HistoricalAverageReservoir>([
{ $match: { embalse: reservoirName } },
{ $unwind: "$meses" },
{ $match: { "meses.año": year, "meses.mes": month } },
{
$match: {
"meses.mes": month,
"meses.año": { $lt: new Date().getFullYear() },
},
},
{
$group: {
_id: null,
embalse: { $first: "$embalse" },
mes: { $first: "$meses.mes" },
promedio_agua_actual: { $avg: "$meses.promedio_agua_actual" },
},
},
{
$project: {
_id: 0,
embalse: 1,
año: "$meses.año",
mes: "$meses.mes",
promedio_agua_actual: "$meses.promedio_agua_actual",
mes: 1,
promedio_agua_actual: { $round: ["$promedio_agua_actual", 2] },
},
},
])
Expand Down
2 changes: 1 addition & 1 deletion front/src/pods/embalse/embalse.vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const createEmptyDataLastYearModel = (): DataLastYearModel => ({
export interface HistoricalAverageReservoir {
nameReservoir: string;
month: number;
year: number;
year?: number;
average: number;
}
export const createEmptyHistoricalAverageReservoir =
Expand Down
Loading