/
page.tsx
167 lines (156 loc) · 5.4 KB
/
page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use client'
import { LinkIcon } from '@heroicons/react/20/solid'
import { GlobeAltIcon } from '@heroicons/react/24/outline'
import {
AreaChart,
Button,
Card,
Grid,
Metric,
Subtitle,
Text,
TextInput,
} from '@tremor/react'
import { useState } from 'react'
import { regions } from '~/helpers/regions'
import Image from 'next/image'
import pongLogo from './favicon.png'
type JobDTO = { region: string; duration: number; statusCode: number }
type Job = Omit<JobDTO, 'duration'> & { duration: number[]; avg: number }
const TOTAL_REGIONS = Object.keys(regions).length
export default function Home() {
const [task, setTask] = useState<{ url: string; jobs: Job[] }>({
url: '',
jobs: [],
})
const [url, setUrl] = useState('https://zolplay.com/')
const [isRunning, setIsRunning] = useState(false)
const [finishedRegions, setFinishedRegions] = useState(0)
const handlePong = async () => {
if (isRunning) return
setIsRunning(true)
if (url !== task.url) {
setTask({ url, jobs: [] })
}
await runJobs()
setIsRunning(false)
}
const runJobs = async () => {
setFinishedRegions(0)
await Promise.allSettled(
Object.keys(regions).map(async (region) => {
const response = await fetch(`/api/${region}`, {
method: 'POST',
body: JSON.stringify({ url }),
signal: AbortSignal.timeout(30 * 1000),
})
const data = (await response.json()) as JobDTO
setFinishedRegions((count) => count + 1)
setTask((task) => {
const job: Job = task.jobs.find((job) => job.region === region) || {
...data,
duration: [],
avg: 0,
}
const nextDuration =
data.duration === -1
? job.duration
: [...job.duration, data.duration]
const avg =
nextDuration.reduce((result, cur) => result + cur, 0) /
nextDuration.length
return {
...task,
jobs: [
...task.jobs.filter((job) => job.region !== region),
{ ...job, duration: nextDuration, avg },
].sort((a, b) => a.avg - b.avg),
}
})
})
)
}
return (
<main className="max-w-5xl mx-auto pb-32">
{/* <div className="relative">
<div
className="absolute inset-x-0 -top-40 -z-10 transform-gpu overflow-hidden blur-3xl sm:-top-80"
aria-hidden="true"
>
<div
className="relative left-[calc(50%-11rem)] aspect-[1155/678] w-[36.125rem] -translate-x-1/2 rotate-[30deg] bg-gradient-to-tr from-[#ff80b5] to-[#9089fc] opacity-30 sm:left-[calc(50%-30rem)] sm:w-[72.1875rem]"
style={{
clipPath:
"polygon(74.1% 44.1%, 100% 61.6%, 97.5% 26.9%, 85.5% 0.1%, 80.7% 2%, 72.5% 32.5%, 60.2% 62.4%, 52.4% 68.1%, 47.5% 58.3%, 45.2% 34.5%, 27.5% 76.7%, 0.1% 64.9%, 17.9% 100%, 27.6% 76.8%, 76.1% 97.7%, 74.1% 44.1%)",
}}
/>
</div>
</div> */}
{/* <div className="h-96">map</div> */}
<div className="my-6 w-full flex justify-center items-center">
<Image src={pongLogo} alt="" className="w-16 h-16" />
</div>
<div className="flex max-w-md justify-center mx-auto">
<TextInput
type="text"
required={false}
placeholder="Enter your URL"
value={url}
icon={LinkIcon}
onChange={(e) => setUrl(e.target.value)}
/>
<Button
className="flex-none rounded-md ml-4 bg-black px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-black/80 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-500"
onClick={handlePong}
icon={GlobeAltIcon}
>
Pong!
</Button>
</div>
<div className="my-6 max-w-md mx-auto">
{!!task.url && (
<Card>
<Text>Regions tested</Text>
<Metric>
{finishedRegions}/{TOTAL_REGIONS}
</Metric>
</Card>
)}
</div>
<Grid numCols={1} numColsMd={2} numColsLg={3} className="gap-3">
{task.jobs.map((job) => (
<Card key={job.region} className="mt-5 relative">
<Text className="text-xs">
{regions[job.region].emoji} {regions[job.region].location}
</Text>
<AreaChart
data={job.duration.map((duration, idx) => ({
idx: `#${idx + 1}`,
Latency: duration,
}))}
categories={['Latency']}
index="idx"
/>
<div className="text-sm font-bold text-gray-900 ml-auto flex flex-col items-end">
<Metric className="flex items-center">
{isNaN(job.avg) ? '/' : job.avg.toFixed(0)}
<Subtitle className="text-xs text-black/40 ml-2">ms</Subtitle>
</Metric>
{job.duration.length > 1 && (
<div className="font-light text-xs mt-[2px]">
<span className="text-green-600 mr-1">
{Math.min(...job.duration)}
</span>
/
<span className="text-red-600 ml-1">
{Math.max(...job.duration)}
</span>
</div>
)}
</div>
</Card>
))}
</Grid>
</main>
)
}