Skip to content

Commit

Permalink
test aggregation on national charts
Browse files Browse the repository at this point in the history
  • Loading branch information
braddf committed Nov 26, 2024
1 parent 44b5a58 commit 300dac1
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,9 @@ const GspPvRemixChart: FC<{
gspForecastDataOneGSP,
gspNHourData
} = useGetGspData(gspId);
// TODO – if aggregation is zone, we need to get the data for all GSPs in the zone
if (nationalAggregationLevel === NationalAggregation.zone) {
errors = [];
pvRealDataAfter = [];
pvRealDataIn = [];
// TODO – temp reset; if aggregation is zones, make sure data is all set
if ([NationalAggregation.DNO, NationalAggregation.zone].includes(nationalAggregationLevel)) {
gspLocationInfo = [];
gspForecastDataOneGSP = [];
gspNHourData = [];
}
// const gspData = fcAll?.forecasts.find((fc) => fc.location.gspId === gspId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,108 @@
import { API_PREFIX, getAllForecastUrl } from "../../../constant";
import { FcAllResData, ForecastData, GspEntities, PvRealData } from "../../types";
import dnoGspGroupings from "../../../data/dno_gsp_groupings.json";
import ngGspZoneGroupings from "../../../data/ng_gsp_zone_groupings.json";
import useGlobalState from "../../helpers/globalState";
import { useLoadDataFromApi } from "../../hooks/useLoadDataFromApi";
import { NationalAggregation } from "../../map/types";
import { components } from "../../../types/quartz-api";

const useGetGspData = (gspId: number) => {
const aggregateTruthData = (
pvDataRaw: components["schemas"]["GSPYieldGroupByDatetime"][] | undefined,
gspIds: number[],
key: string
) => {
if (!pvDataRaw?.length) return [];
return pvDataRaw?.map((d) => {
return {
datetimeUtc: d.datetimeUtc,
[key]: Number(
gspIds.reduce((acc, gspId) => {
if (!d.generationKwByGspId?.[gspId]) return acc;
const value = Number(d.generationKwByGspId[gspId]);
if (isNaN(value)) return acc;
return acc + value;
}, 0)
)
};
}) as PvRealData;
};
const aggregateForecastData = (
pvDataRaw: components["schemas"]["OneDatetimeManyForecastValues"][] | undefined,
gspIds: number[]
) => {
if (!pvDataRaw?.length) return [];
return pvDataRaw?.map((d) => {
return {
targetTime: d.datetimeUtc,
expectedPowerGenerationMegawatts: Number(
gspIds.reduce((acc, gspId) => {
if (!d.forecastValues?.[gspId]) return acc;
const value = Number(d.forecastValues[gspId]);
if (isNaN(value)) return acc;
return acc + value;
}, 0)
)
};
}) as ForecastData;
};

const useGetGspData = (gspId: number | string) => {
const [show4hView] = useGlobalState("showNHourView");
const [nHourForecast] = useGlobalState("nHourForecast");
const [nationalAggregationLevel] = useGlobalState("nationalAggregationLevel");
let errors: Error[] = [];

const { data: pvRealDataIn, error: pvRealInDat } = useLoadDataFromApi<PvRealData>(
`${API_PREFIX}/solar/GB/gsp/pvlive/${gspId}?regime=in-day`
let gspIds: number[] = typeof gspId === "number" ? [gspId] : [];
if (nationalAggregationLevel === NationalAggregation.DNO) {
// Get the GSP ids for the DNO
gspIds = dnoGspGroupings[gspId as keyof typeof dnoGspGroupings] || [];
}
if (nationalAggregationLevel === NationalAggregation.zone) {
gspIds = ngGspZoneGroupings[gspId as keyof typeof ngGspZoneGroupings] || [];
}

// TODO add check for gspIds before making the api call
const { data: pvRealDataInRaw, error: pvRealInDayError } = useLoadDataFromApi<
components["schemas"]["GSPYieldGroupByDatetime"][]
>(
// `${API_PREFIX}/solar/GB/gsp/pvlive/${gspId}?regime=in-day`
`${API_PREFIX}/solar/GB/gsp/pvlive/all?regime=in-day&gsp_ids=${encodeURIComponent(
gspIds.join(",")
)}&compact=true`
);
const pvRealDataIn = aggregateTruthData(pvRealDataInRaw, gspIds, "solarGenerationKw");

const { data: pvRealDataAfter, error: pvRealDayAfter } = useLoadDataFromApi<PvRealData>(
`${API_PREFIX}/solar/GB/gsp/pvlive/${gspId}?regime=day-after`
const { data: pvRealDataAfterRaw, error: pvRealDayAfterError } = useLoadDataFromApi<
components["schemas"]["GSPYieldGroupByDatetime"][]
>(
// `${API_PREFIX}/solar/GB/gsp/pvlive/${gspId}?regime=day-after`
`${API_PREFIX}/solar/GB/gsp/pvlive/all?regime=day-after&gsp_ids=${encodeURIComponent(
gspIds.join(",")
)}&compact=true`
);
const pvRealDataAfter = aggregateTruthData(pvRealDataAfterRaw, gspIds, "solarGenerationKw");

//add new useSWR for gspChartData
const { data: gspForecastDataOneGSP, error: gspForecastDataOneGSPError } =
useLoadDataFromApi<ForecastData>(`${API_PREFIX}/solar/GB/gsp/${gspId}/forecast`, {
const { data: gspForecastDataOneGSPRaw, error: gspForecastDataOneGSPError } = useLoadDataFromApi<
components["schemas"]["OneDatetimeManyForecastValues"][]
>(
// `${API_PREFIX}/solar/GB/gsp/${gspId}/forecast`,
`${API_PREFIX}/solar/GB/gsp/forecast/all/?gsp_ids=${encodeURIComponent(
gspIds.join(",")
)}&compact=true&historic=true`,
{
dedupingInterval: 1000 * 30
});
}
);
const gspForecastDataOneGSP = aggregateForecastData(gspForecastDataOneGSPRaw, gspIds);

//add new useSWR for gspLocationInfo since this is not
const { data: gspLocationInfo, error: gspLocationError } = useLoadDataFromApi<GspEntities>(
`${API_PREFIX}/system/GB/gsp/?gsp_id=${gspId}`
);

// TODO: nHour with aggregation
const nMinuteForecast = nHourForecast * 60;
const { data: gspNHourData, error: pvNHourError } = useLoadDataFromApi<ForecastData>(
show4hView
Expand All @@ -35,8 +112,8 @@ const useGetGspData = (gspId: number) => {

return {
errors: [
pvRealInDat,
pvRealDayAfter,
pvRealInDayError,
pvRealDayAfterError,
gspForecastDataOneGSPError,
gspLocationError,
pvNHourError
Expand Down

0 comments on commit 300dac1

Please sign in to comment.