Changed api to fetch manually only most recent files

This commit is contained in:
Guntis Smaukstelis
2025-11-04 22:58:53 +02:00
parent 56b85cbae2
commit 9ab4cfe3fe
3 changed files with 56 additions and 79 deletions
+13 -50
View File
@@ -3,70 +3,33 @@ import { createSignal } from "solid-js";
import { apiHost } from "../../consts";
export function FetchFiles() {
const [getStart, setStart] = createSignal(new Date());
const [getEnd, setEnd] = createSignal(new Date());
const [getFetchResult, setFetchResult] = createSignal("");
const [isSpinner, setIsSpinner] = createSignal(false);
const startStr = () => moment(getStart()).format("YYYY-MM-DD");
const endStr = () => moment(getEnd()).format("YYYY-MM-DD");
function handleSubmit(e: MouseEvent) {
async function handleSubmit(e: MouseEvent) {
e.preventDefault();
const dates = getDatesBetween(getStart(), getEnd());
timerFetch(dates);
}
async function timerFetch(dates: Date[]) {
setIsSpinner(true);
for (const date of dates) {
const response = await fetch(`${apiHost}/api/fetch/date/${moment(date).format("YYYYMMDD")}`);
const text = await response.text();
setFetchResult(text);
await new Promise(resolve => setTimeout(resolve, 200));
}
setIsSpinner(false);
const response = await fetch(`${apiHost}/api/fetch/lvgmc/stations`);
const text = await response.text();
setFetchResult(text);
await new Promise(resolve => setTimeout(resolve, 200));
setIsSpinner(false)
}
return (
<form>
<h3>Date range fetch</h3>
<h3>Manual fetch</h3>
<p>
<input
type="date"
value={ startStr() }
onChange={e => setStart(new Date(e.target.value))}
/> start
</p>
<p>
<input
type="date"
value={ endStr() }
onChange={e => setEnd(new Date(e.target.value))}
/> end
</p>
<p>
<input type="submit" onClick={handleSubmit} value="Fetch .csv files" />
<input type="submit" onClick={handleSubmit} value="Fetch most recent .csv file" />
</p>
<p>result:</p>
{ isSpinner() && <div>
<span class="spinner"></span>
<span style={{ "padding-left": "16px" }}>Fetching csv files</span>
</div>
{isSpinner() && <div>
<span class="spinner"></span>
<span style={{ "padding-left": "16px" }}>Fetching csv files</span>
</div>
}
<p>{ getFetchResult() }</p>
<p>{getFetchResult()}</p>
</form>
)
}
function getDatesBetween(startDate: Date, endDate: Date): Date[] {
const dates: Date[] = [];
let currentDate = new Date(startDate); // start from the start date
while (currentDate <= endDate) {
dates.push(new Date(currentDate)); // add current date to the list
currentDate.setDate(currentDate.getDate() + 1); // increment the date
}
return dates;
}