Skip to content

Commit

Permalink
feat(global-stats): get median TTFB from header
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Rodriguez Baquero committed Oct 2, 2023
1 parent 9b18d73 commit acf39a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/contexts/NodesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { EntityType } from "./AppContext";
type NodesContextType = {
nodes: Node[] | [];
setNodes: (nodes: Node[] | []) => void;
globalStats: { medianTTFB: number };
};

const initialValues = {
nodes: [],
setNodes: () => {},
globalStats: { medianTTFB: 0 },
};

const NodesContext = createContext<NodesContextType>(initialValues);
Expand All @@ -28,6 +30,7 @@ export const NodesContextProvider = ({
const [nodes, setNodes] = useState<Node[] | []>([]);
const [tempNodes, setTempNodes] = useState<Node[] | []>([]);
const [isLoading, setIsLoading] = useState<boolean>();
const [globalStats, setGlobalStats] = useState<{ medianTTFB: number }>({ medianTTFB: 0 });

useEffect(() => {
let animationFrameId: number;
Expand All @@ -39,6 +42,9 @@ export const NodesContextProvider = ({
const decoder = new TextDecoder();
const reader = response.body?.getReader();
const nodesMap = new Map<string, Node>();
const medianTTFB = Number(response.headers.get("x-saturn-median-ttfb")) || 0;

setGlobalStats({ medianTTFB });

const onChunk = ({
done,
Expand Down Expand Up @@ -99,6 +105,7 @@ export const NodesContextProvider = ({
}, [isLoading]);

const value = {
globalStats,
nodes,
setNodes,
};
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useNodes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type Node = {
export type Nodes = Node[];

export const useNodes = () => {
const { nodes } = useContext(NodesContext);
const { nodes, globalStats } = useContext(NodesContext);

const getNodeByID = (queryNodeId: string) => {
return nodes.find((node) => node.id === queryNodeId);
Expand All @@ -78,6 +78,7 @@ export const useNodes = () => {

return {
nodes,
globalStats,
getNodeByID,
getNodesByLocationId,
getNodesByCountryId,
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const computeStats = (
export const useStats = () => {
const {
nodes: nodesMap,
globalStats,
getNodesByContinentId,
getNodesByCountryId,
getNodesByLocationId,
Expand All @@ -111,8 +112,8 @@ export const useStats = () => {

const getGlobalStats = () => {
if (!nodes) return;
const globalStats = computeStats(nodes, undefined);
return globalStats;
const computedGlobalStats = computeStats(nodes, undefined);
return { ...computedGlobalStats, ...globalStats };
};

const getStatsByContinentId = (continentId: string) => {
Expand Down

0 comments on commit acf39a9

Please sign in to comment.