Skip to content

Commit

Permalink
feat(global-stats): get median TTFB from header (#165)
Browse files Browse the repository at this point in the history
Co-authored-by: Amean Asad <[email protected]>
  • Loading branch information
Diego Rodríguez Baquero and AmeanAsad authored Oct 3, 2023
1 parent 9b18d73 commit 9ccdcb3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/contexts/NodesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ import {
import { Node } from "../hooks/useNodes";
import { EntityType } from "./AppContext";

type GlobalStats = {
medianTTFB: number;
}

type NodesContextType = {
nodes: Node[] | [];
setNodes: (nodes: Node[] | []) => void;
globalStats: GlobalStats;
};

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

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

useEffect(() => {
let animationFrameId: number;
Expand All @@ -39,6 +46,11 @@ 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"));

if (medianTTFB) {
setGlobalStats({ medianTTFB });
}

const onChunk = ({
done,
Expand Down Expand Up @@ -99,6 +111,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 9ccdcb3

Please sign in to comment.