-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(explorer): transaction timings (#3274)
Co-authored-by: Kevin Ingersoll <[email protected]>
- Loading branch information
Showing
11 changed files
with
150 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
"@latticexyz/explorer": patch | ||
--- | ||
|
||
Transactions in Observe tab are now populated with timing metrics when using the `observer` Viem decorator in local projects. | ||
|
||
You can wire up your local project to use transaction timings with: | ||
|
||
``` | ||
import { observer } from "@latticexyz/explorer/observer"; | ||
// Extend the Viem client that is performing writes | ||
walletClient.extend(observer()); | ||
``` |
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
37 changes: 37 additions & 0 deletions
37
...plorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/observe/TimingRowExpanded.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,37 @@ | ||
import { Separator } from "../../../../../../components/ui/Separator"; | ||
import { type Write } from "../../../../../../observer/store"; | ||
import { cn } from "../../../../../../utils"; | ||
import { useTimings } from "./useTimings"; | ||
|
||
export function TimingRowExpanded(write: Write) { | ||
const timings = useTimings(write); | ||
return ( | ||
<> | ||
<Separator className="my-5" /> | ||
<div className="flex items-start gap-x-4 pb-2"> | ||
<h3 className="inline-block w-[45px] pb-2 text-2xs font-bold uppercase">Timing</h3> | ||
<div className="w-full border border-white/20 p-2 pb-3"> | ||
<div className="grid grid-cols-[auto_1fr_auto] items-center gap-x-4 gap-y-1"> | ||
{timings.map((timing) => ( | ||
<> | ||
<span className="text-xs">{timing.label}:</span> | ||
<span | ||
className={cn(`h-1`, { | ||
"bg-[#5c9af6]": timing.type === "write", | ||
"bg-[#4d7cc0]": timing.type === "waitForTransaction", | ||
"bg-[#3d5c8a]": timing.type === "waitForTransactionReceipt", | ||
})} | ||
style={{ | ||
width: `${timing.widthPercentage}%`, | ||
marginLeft: `${timing.startPercentage}%`, | ||
}} | ||
/> | ||
<span className="text-right text-xs">{timing.duration}ms</span> | ||
</> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} |
26 changes: 26 additions & 0 deletions
26
...explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/observe/TimingRowHeader.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,26 @@ | ||
import { type Write } from "../../../../../../observer/store"; | ||
import { cn } from "../../../../../../utils"; | ||
import { useTimings } from "./useTimings"; | ||
|
||
export function TimingRowHeader(write: Write) { | ||
const timings = useTimings(write); | ||
return ( | ||
<div className="ml-4 inline-block h-full w-14 grayscale lg:-mr-8 lg:ml-8 xl:-mr-16 xl:ml-16"> | ||
{timings.map((timing) => ( | ||
<div | ||
key={timing.label} | ||
title={timing.label} | ||
className={cn(`h-1`, { | ||
"bg-[#5c9af6]": timing.type === "write", | ||
"mt-0.5 bg-[#4d7cc0]": timing.type === "waitForTransaction", | ||
"mt-0.5 bg-[#3d5c8a]": timing.type === "waitForTransactionReceipt", | ||
})} | ||
style={{ | ||
width: `${timing.widthPercentage}%`, | ||
marginLeft: `${timing.startPercentage}%`, | ||
}} | ||
/> | ||
))} | ||
</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
31 changes: 0 additions & 31 deletions
31
packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/observe/Write.tsx
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/observe/Writes.tsx
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/observe/useTimings.ts
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,46 @@ | ||
import { isDefined } from "@latticexyz/common/utils"; | ||
import { type Write } from "../../../../../../observer/store"; | ||
|
||
const eventConfig = { | ||
write: { priority: 1, label: "write" }, | ||
"write:result": { priority: 2 }, | ||
waitForTransaction: { priority: 3, label: "state update" }, | ||
"waitForTransaction:result": { priority: 4 }, | ||
waitForTransactionReceipt: { priority: 5, label: "transaction receipt" }, | ||
"waitForTransactionReceipt:result": { priority: 6 }, | ||
} as const; | ||
|
||
type EventType = keyof typeof eventConfig; | ||
|
||
export function useTimings({ time: start, events }: Write) { | ||
const maxLen = Math.max(...events.map((event) => event.time - start)); | ||
const sortedEvents = Object.values(events).sort((a, b) => { | ||
const priorityA = eventConfig[a.type as EventType]?.priority; | ||
const priorityB = eventConfig[b.type as EventType]?.priority; | ||
return priorityA - priorityB; | ||
}); | ||
|
||
return sortedEvents | ||
.map((event) => { | ||
const type = event.type as EventType; | ||
if (type.endsWith(":result")) return; | ||
|
||
const writeResult = events.find((e) => e.type === `${type}:result`); | ||
const endTime = writeResult?.time ?? event.time; | ||
const duration = endTime - event.time; | ||
const startOffset = event.time - start; | ||
|
||
const startPercentage = (startOffset / maxLen) * 100; | ||
const widthPercentage = (duration / maxLen) * 100; | ||
|
||
const config = eventConfig[type]; | ||
return { | ||
type, | ||
label: "label" in config ? config.label : type, | ||
duration, | ||
startPercentage, | ||
widthPercentage, | ||
}; | ||
}) | ||
.filter(isDefined); | ||
} |
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 |
---|---|---|
|
@@ -22,5 +22,5 @@ export function timeAgo(timestamp: bigint) { | |
} | ||
} | ||
|
||
return "just now"; | ||
return "0s ago"; | ||
} |