-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from cptKNJO/ui
UI
- Loading branch information
Showing
10 changed files
with
1,312 additions
and
775 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
67 changes: 67 additions & 0 deletions
67
urbackupserver/www2/src/features/statistics/SelectStorageUsageClient.tsx
This file contains 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,67 @@ | ||
import { | ||
useComboboxFilter, | ||
ComboboxProps, | ||
Combobox, | ||
} from "@fluentui/react-components"; | ||
import { useId, useState } from "react"; | ||
import { ClientInfo } from "../../api/urbackupserver"; | ||
|
||
export function SelectStorageUsageClient({ | ||
clients, | ||
onSelect, | ||
}: { | ||
clients: ClientInfo[]; | ||
onSelect: (value?: string) => void; | ||
}) { | ||
const options = [ | ||
{ | ||
children: "All clients", | ||
value: "all", | ||
}, | ||
...clients.map((client) => ({ | ||
children: client.name, | ||
value: String(client.id), | ||
})), | ||
]; | ||
|
||
const comboId = useId(); | ||
|
||
const [query, setQuery] = useState<string>(options[0].children); | ||
const comboBoxChildren = useComboboxFilter(query, options, { | ||
optionToText: (d) => d.children, | ||
noOptionsMessage: `No results matched "${query}"`, | ||
}); | ||
|
||
const onOptionSelect: ComboboxProps["onOptionSelect"] = (_, data) => { | ||
const text = data.optionValue; | ||
const selectedClient = clients.find((client) => String(client.id) === text); | ||
|
||
if (!selectedClient) { | ||
onSelect(); | ||
setQuery(options[0].children); | ||
return; | ||
} | ||
|
||
onSelect(String(selectedClient.id)); | ||
setQuery(selectedClient.name); | ||
}; | ||
|
||
return ( | ||
<div className="cluster"> | ||
<label id={comboId}>Select client</label> | ||
<Combobox | ||
aria-labelledby={comboId} | ||
onOptionSelect={onOptionSelect} | ||
onChange={(ev) => setQuery(ev.target.value)} | ||
onOpenChange={(_, data) => { | ||
if (data.open) { | ||
setQuery(""); | ||
} | ||
}} | ||
value={query} | ||
> | ||
{comboBoxChildren} | ||
</Combobox> | ||
</div> | ||
); | ||
} |
182 changes: 182 additions & 0 deletions
182
urbackupserver/www2/src/features/statistics/StorageUsage.tsx
This file contains 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,182 @@ | ||
import { useState } from "react"; | ||
import { useSuspenseQuery } from "@tanstack/react-query"; | ||
import { | ||
IChartProps, | ||
ILineChartStyles, | ||
LineChart, | ||
} from "@fluentui/react-charting"; | ||
import { Tab, TabList, tokens } from "@fluentui/react-components"; | ||
|
||
import { urbackupServer } from "../../App"; | ||
import { SelectStorageUsageClient } from "./SelectStorageUsageClient"; | ||
|
||
const DURATIONS = { | ||
d: "Day", | ||
m: "Month", | ||
y: "Year", | ||
} as const; | ||
|
||
const lineChartStyles: ILineChartStyles = { | ||
root: { | ||
overflow: "hidden", | ||
line: { | ||
strokeWidth: "1px", | ||
}, | ||
}, | ||
chartWrapper: { | ||
overflow: "hidden", | ||
}, | ||
axisTitle: { | ||
fill: "currentColor", | ||
color: "currentColor", | ||
}, | ||
yAxis: { | ||
text: { | ||
fill: "currentColor", | ||
}, | ||
line: { | ||
stroke: "currentColor", | ||
}, | ||
}, | ||
xAxis: { | ||
text: { | ||
fill: "currentColor", | ||
}, | ||
}, | ||
calloutContentRoot: { | ||
background: "inherit", | ||
}, | ||
calloutBlockContainer: { | ||
borderLeft: "0", | ||
margin: 0, | ||
padding: 0, | ||
color: "currentColor", | ||
}, | ||
calloutContentX: { | ||
color: "currentColor", | ||
}, | ||
calloutlegendText: { | ||
display: "none", | ||
}, | ||
}; | ||
|
||
export function StorageUsage({ | ||
width, | ||
height, | ||
}: { | ||
width: number; | ||
height: number; | ||
}) { | ||
const [duration, setDuration] = | ||
useState<Parameters<typeof urbackupServer.getUsageGraphData>[0]>("d"); | ||
|
||
const [selectedClientId, setSelectedClientId] = useState< | ||
string | undefined | ||
>(); | ||
|
||
const storageUsageResult = useSuspenseQuery({ | ||
queryKey: ["usage-graph", duration, selectedClientId], | ||
queryFn: () => urbackupServer.getUsageGraphData(duration, selectedClientId), | ||
}); | ||
|
||
const clientsResult = useSuspenseQuery({ | ||
queryKey: ["clients"], | ||
queryFn: () => urbackupServer.getClients(), | ||
}); | ||
|
||
const clients = clientsResult.data; | ||
|
||
const storageUsage = storageUsageResult.data!; | ||
|
||
const limits = { | ||
min: Math.min(...storageUsage.map((d) => d.data)), | ||
max: Math.max(...storageUsage.map((d) => d.data)), | ||
}; | ||
|
||
// const width = 550; | ||
// const height = 300; | ||
|
||
const data: IChartProps = { | ||
chartTitle: "Storage Usage", | ||
lineChartData: [ | ||
{ | ||
legend: "Storage Usage", | ||
data: storageUsage.map((d) => ({ | ||
x: new Date(d.xlabel), | ||
y: d.data, | ||
xAxisCalloutData: new Intl.DateTimeFormat("en-US", { | ||
dateStyle: "long", | ||
}).format(Date.parse(d.xlabel)), | ||
yAxisCalloutData: `${d.data.toFixed(0)} MB`, | ||
})), | ||
color: tokens.colorBrandBackground, | ||
}, | ||
], | ||
}; | ||
|
||
return ( | ||
<div className="flow"> | ||
<div | ||
className="cluster" | ||
style={ | ||
{ | ||
"--cluster-horizontal-alignment": "space-between", | ||
} as React.CSSProperties | ||
} | ||
> | ||
<div> | ||
<SelectStorageUsageClient | ||
clients={clients} | ||
onSelect={(id) => setSelectedClientId(id)} | ||
/> | ||
</div> | ||
<TabList | ||
size="small" | ||
defaultSelectedValue={duration} | ||
selectTabOnFocus={true} | ||
onTabSelect={(_, data) => { | ||
setDuration(data.value as typeof duration); | ||
}} | ||
> | ||
<Tab value="d">{DURATIONS.d}</Tab> | ||
<Tab value="m">{DURATIONS.m}</Tab> | ||
<Tab value="y">{DURATIONS.y}</Tab> | ||
</TabList> | ||
</div> | ||
<div | ||
style={{ | ||
height: `${height}px`, | ||
overflow: "hidden", | ||
}} | ||
> | ||
<LineChart | ||
key={selectedClientId ? `${selectedClientId}-${duration}` : duration} | ||
culture={window.navigator.language} | ||
enablePerfOptimization={true} | ||
data={data} | ||
hideLegend | ||
yMinValue={limits.min} | ||
yMaxValue={limits.max} | ||
width={width} | ||
height={height} | ||
yAxisTitle="Storage Usage (MB)" | ||
xAxisTitle={DURATIONS[duration]} | ||
{...(duration === "y" && { | ||
customDateTimeFormatter: (d) => d.getFullYear().toString(), | ||
// Restrict tick values to the number of xLabels returned | ||
tickValues: data.lineChartData?.[0].data.map((d) => d.x as Date), | ||
})} | ||
calloutProps={{ | ||
styles: { | ||
calloutMain: { | ||
background: "var(--colorTooltipBackground)", | ||
color: "var(--colorTooltipForeground)", | ||
}, | ||
}, | ||
}} | ||
styles={lineChartStyles} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains 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
This file contains 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,44 @@ | ||
import { Suspense, useEffect, useRef, useState } from "react"; | ||
import { Spinner } from "@fluentui/react-components"; | ||
|
||
import { StorageUsage } from "../features/statistics/StorageUsage"; | ||
|
||
export const StatisticsPage = () => { | ||
const ref = useRef<HTMLDivElement | null>(null); | ||
|
||
const [width, setWidth] = useState(0); | ||
|
||
useEffect(() => { | ||
function handleResize() { | ||
if (ref.current) { | ||
const rect = ref.current.getBoundingClientRect(); | ||
setWidth(rect.width); | ||
} | ||
} | ||
|
||
window.addEventListener("resize", handleResize); | ||
|
||
return () => { | ||
window.removeEventListener("resize", handleResize); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<Suspense fallback={<Spinner />}> | ||
<div className="flow"> | ||
<h3>Statistics</h3> | ||
<div | ||
className="flow" | ||
ref={ref} | ||
style={{ | ||
overflow: "hidden", | ||
width: "clamp(500px, 84vw, 1200px)", | ||
}} | ||
> | ||
<h4>Storage Usage</h4> | ||
<StorageUsage width={width / 2} height={300} /> | ||
</div> | ||
</div> | ||
</Suspense> | ||
); | ||
}; |