-
-
Notifications
You must be signed in to change notification settings - Fork 37
CSV downlaod on nowcasting app #711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
suvanbanerjee
wants to merge
2
commits into
development
Choose a base branch
from
feat/csv-download
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { CombinedData } from "../types"; | ||
| import { DateTime } from "luxon"; | ||
| import { CSVColumn } from "../layout/header/csv-download-modal"; | ||
| import { getSettlementPeriodForDate } from "./chartUtils"; | ||
|
|
||
| interface CSVRow { | ||
| startDateTime: string; | ||
| endDateTime: string; | ||
| settlementPeriod: number | null; | ||
| solarGenerationPvliveInitial: number | null; | ||
| solarGenerationPvliveUpdated: number | null; | ||
| solarForecast: number | null; | ||
| solarForecastP10: number | null; | ||
| solarForecastP90: number | null; | ||
| nForecast: number | null; | ||
| } | ||
|
|
||
| const COLUMN_CONFIG: Record<CSVColumn, { key: keyof CSVRow; header: string }> = { | ||
| startDateTime: { key: "startDateTime", header: "Start DateTime" }, | ||
| endDateTime: { key: "endDateTime", header: "End DateTime" }, | ||
| settlementPeriod: { key: "settlementPeriod", header: "Settlement Period" }, | ||
| solarGenerationPvliveInitial: { | ||
| key: "solarGenerationPvliveInitial", | ||
| header: "Solar Generation PVLive Initial (MW)" | ||
| }, | ||
| solarGenerationPvliveUpdated: { | ||
| key: "solarGenerationPvliveUpdated", | ||
| header: "Solar Generation PVLive Updated (MW)" | ||
| }, | ||
| solarForecast: { key: "solarForecast", header: "Solar Forecast (MW)" }, | ||
| solarForecastP10: { key: "solarForecastP10", header: "Solar Forecast P10 (MW)" }, | ||
| solarForecastP90: { key: "solarForecastP90", header: "Solar Forecast P90 (MW)" }, | ||
| nForecast: { key: "nForecast", header: "N Forecast (MW)" } | ||
| }; | ||
|
|
||
| const createEmptyRow = (timestamp: string): CSVRow => { | ||
| const start = DateTime.fromISO(timestamp); | ||
| const end = start.plus({ minutes: 30 }); | ||
| const settlementPeriod = getSettlementPeriodForDate(start); | ||
|
|
||
| return { | ||
| startDateTime: start.toISO() || "", | ||
| endDateTime: end.toISO() || "", | ||
| settlementPeriod, | ||
| solarGenerationPvliveInitial: null, | ||
| solarGenerationPvliveUpdated: null, | ||
| solarForecast: null, | ||
| solarForecastP10: null, | ||
| solarForecastP90: null, | ||
| nForecast: null | ||
| }; | ||
| }; | ||
|
|
||
| const getOrCreateRow = (map: Map<string, CSVRow>, ts: string): CSVRow => { | ||
| if (!map.has(ts)) { | ||
| map.set(ts, createEmptyRow(ts)); | ||
| } | ||
| return map.get(ts)!; | ||
| }; | ||
|
|
||
| export const downloadNationalCsv = ( | ||
| combinedData: CombinedData | null, | ||
| selectedColumns: CSVColumn[] | ||
| ) => { | ||
| if (!combinedData) return; | ||
|
|
||
| const dataByTimestamp = new Map<string, CSVRow>(); | ||
|
|
||
| // PV initial | ||
| combinedData.pvRealDayInData?.forEach((entry) => { | ||
| const row = getOrCreateRow(dataByTimestamp, entry.datetimeUtc); | ||
| row.solarGenerationPvliveInitial = entry.solarGenerationKw | ||
| ? entry.solarGenerationKw / 1000 | ||
| : null; | ||
| }); | ||
|
|
||
| // PV updated | ||
| combinedData.pvRealDayAfterData?.forEach((entry) => { | ||
| const row = getOrCreateRow(dataByTimestamp, entry.datetimeUtc); | ||
| row.solarGenerationPvliveUpdated = entry.solarGenerationKw | ||
| ? entry.solarGenerationKw / 1000 | ||
| : null; | ||
| }); | ||
|
|
||
| // Forecast | ||
| combinedData.nationalForecastData?.forEach((entry) => { | ||
| const row = getOrCreateRow(dataByTimestamp, entry.targetTime); | ||
| row.solarForecast = entry.expectedPowerGenerationMegawatts; | ||
| row.solarForecastP10 = entry.plevels?.plevel_10 ?? null; | ||
| row.solarForecastP90 = entry.plevels?.plevel_90 ?? null; | ||
| }); | ||
|
|
||
| // N forecast | ||
| combinedData.nationalNHourData?.forEach((entry) => { | ||
| const row = getOrCreateRow(dataByTimestamp, entry.targetTime); | ||
| row.nForecast = entry.expectedPowerGenerationMegawatts; | ||
| }); | ||
|
|
||
| // sort + build rows | ||
| const csvRows = Array.from(dataByTimestamp.entries()) | ||
| .sort(([a], [b]) => a.localeCompare(b)) | ||
| .map(([, row]) => row); | ||
|
|
||
| const csv = generateCsv(csvRows, selectedColumns); | ||
|
|
||
| // download | ||
| const blob = new Blob([csv], { type: "text/csv" }); | ||
| const url = URL.createObjectURL(blob); | ||
|
|
||
| const a = document.createElement("a"); | ||
| a.href = url; | ||
|
|
||
| const now = DateTime.now().toUTC().toFormat("yyyy-MM-dd_HH-mm"); | ||
| a.download = `Quartz-National-${now}.csv`; | ||
|
|
||
| document.body.appendChild(a); | ||
| a.click(); | ||
| document.body.removeChild(a); | ||
| URL.revokeObjectURL(url); | ||
| }; | ||
|
|
||
| function generateCsv(rows: CSVRow[], selectedColumns: CSVColumn[]): string { | ||
| const headers = selectedColumns.map((col) => COLUMN_CONFIG[col].header); | ||
|
|
||
| const lines = rows.map((row) => | ||
| selectedColumns | ||
| .map((col) => { | ||
| const value = row[COLUMN_CONFIG[col].key]; | ||
| return value ?? ""; | ||
| }) | ||
| .join(",") | ||
| ); | ||
|
|
||
| return [headers.join(","), ...lines].join("\n"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
apps/nowcasting-app/components/layout/header/csv-download-modal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import React, { useState } from "react"; | ||
|
|
||
| export type CSVColumn = | ||
| | "startDateTime" | ||
| | "endDateTime" | ||
| | "settlementPeriod" | ||
| | "solarGenerationPvliveInitial" | ||
| | "solarGenerationPvliveUpdated" | ||
| | "solarForecast" | ||
| | "solarForecastP10" | ||
| | "solarForecastP90" | ||
| | "nForecast"; | ||
|
|
||
| const FIXED_COLUMNS: CSVColumn[] = ["startDateTime", "endDateTime"]; | ||
|
|
||
| const SELECTABLE_COLUMNS: { id: CSVColumn; label: string }[] = [ | ||
| { id: "settlementPeriod", label: "Settlement Period" }, | ||
| { id: "solarGenerationPvliveInitial", label: "PVLive Initial (MW)" }, | ||
| { id: "solarGenerationPvliveUpdated", label: "PVLive Updated (MW)" }, | ||
| { id: "solarForecast", label: "Solar Forecast (MW)" }, | ||
| { id: "solarForecastP10", label: "Forecast P10 (MW)" }, | ||
| { id: "solarForecastP90", label: "Forecast P90 (MW)" }, | ||
| { id: "nForecast", label: "N Forecast (MW)" } | ||
| ]; | ||
|
|
||
| interface Props { | ||
| isOpen: boolean; | ||
| onClose: () => void; | ||
| onDownload: (cols: CSVColumn[]) => void; | ||
| } | ||
|
|
||
| export const CSVDownloadModal: React.FC<Props> = ({ isOpen, onClose, onDownload }) => { | ||
| const allSelectableIds = SELECTABLE_COLUMNS.map((c) => c.id); | ||
|
|
||
| const [selected, setSelected] = useState<CSVColumn[]>(allSelectableIds); | ||
|
|
||
| const toggle = (id: CSVColumn) => | ||
| setSelected((prev) => (prev.includes(id) ? prev.filter((c) => c !== id) : [...prev, id])); | ||
|
|
||
| const toggleAll = () => | ||
| setSelected((prev) => (prev.length === allSelectableIds.length ? [] : allSelectableIds)); | ||
|
|
||
| const download = () => { | ||
| onDownload([...FIXED_COLUMNS, ...selected]); | ||
| onClose(); | ||
| }; | ||
|
|
||
| if (!isOpen) return null; | ||
|
|
||
| const allSelected = selected.length === allSelectableIds.length; | ||
|
|
||
| return ( | ||
| <> | ||
| <div className="fixed inset-0 bg-black/50 z-40" onClick={onClose} /> | ||
|
|
||
| <div className="fixed inset-0 z-50 flex items-center justify-center p-4"> | ||
| <div className="bg-white rounded-lg shadow-lg max-w-md w-full max-h-[80vh] overflow-y-auto"> | ||
| <div className="sticky top-0 bg-white border-b p-4"> | ||
| <h2 className="text-lg font-semibold">Select Columns to Download</h2> | ||
| </div> | ||
|
|
||
| <div className="p-4 space-y-3"> | ||
| <label className="flex items-center gap-3 font-semibold border-b pb-2 cursor-pointer"> | ||
| <input type="checkbox" checked={allSelected} onChange={toggleAll} /> | ||
| Select All | ||
| </label> | ||
|
|
||
| {SELECTABLE_COLUMNS.map((col) => ( | ||
| <label key={col.id} className="flex items-center gap-3 cursor-pointer"> | ||
| <input | ||
| type="checkbox" | ||
| checked={selected.includes(col.id)} | ||
| onChange={() => toggle(col.id)} | ||
| /> | ||
| {col.label} | ||
| </label> | ||
| ))} | ||
| </div> | ||
|
|
||
| <div className="sticky bottom-0 border-t p-4 flex gap-2"> | ||
| <button onClick={onClose} className="flex-1 px-4 py-2 bg-gray-100"> | ||
| Cancel | ||
| </button> | ||
|
|
||
| <button | ||
| onClick={download} | ||
| disabled={!selected.length} | ||
| className={`flex-1 px-4 py-2 ${ | ||
| selected.length ? "bg-ocf-yellow" : "bg-gray-100 cursor-not-allowed" | ||
| }`} | ||
| > | ||
| Download | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </> | ||
| ); | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personal preference is for camelCase file names, especially on React Component files 👍