-
-
Notifications
You must be signed in to change notification settings - Fork 724
Active incident status panel in the side menu #2033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
import { ExclamationTriangleIcon } from "@heroicons/react/20/solid"; | ||
import { json } from "@remix-run/node"; | ||
import { useFetcher } from "@remix-run/react"; | ||
import { useCallback, useEffect } from "react"; | ||
import { motion } from "framer-motion"; | ||
import { LinkButton } from "~/components/primitives/Buttons"; | ||
import { Paragraph } from "~/components/primitives/Paragraph"; | ||
import { useFeatures } from "~/hooks/useFeatures"; | ||
import { BetterStackClient } from "~/services/betterstack/betterstack.server"; | ||
|
||
export async function loader() { | ||
const client = new BetterStackClient(); | ||
const result = await client.getIncidents(); | ||
|
||
if (!result.success) { | ||
return json({ operational: true }); | ||
} | ||
|
||
return json({ | ||
operational: result.data.attributes.aggregate_state === "operational", | ||
}); | ||
} | ||
|
||
export function IncidentStatusPanel() { | ||
const { isManagedCloud } = useFeatures(); | ||
if (!isManagedCloud) { | ||
return null; | ||
} | ||
|
||
const fetcher = useFetcher<typeof loader>(); | ||
|
||
const fetchIncidents = useCallback(() => { | ||
if (fetcher.state === "idle") { | ||
fetcher.load("/resources/incidents"); | ||
} | ||
}, [fetcher]); | ||
|
||
useEffect(() => { | ||
fetchIncidents(); | ||
|
||
const interval = setInterval(fetchIncidents, 60 * 1000); // 1 minute | ||
|
||
return () => clearInterval(interval); | ||
}, []); | ||
|
||
const operational = fetcher.data?.operational ?? true; | ||
|
||
return ( | ||
<> | ||
{!operational && ( | ||
<motion.div | ||
initial={{ opacity: 0 }} | ||
animate={{ opacity: 1 }} | ||
exit={{ opacity: 0 }} | ||
transition={{ duration: 0.3 }} | ||
className="p-1" | ||
> | ||
<div className="flex flex-col gap-2 rounded border border-warning/20 bg-warning/5 p-2 pt-1.5"> | ||
<div className="flex items-center gap-1 border-b border-warning/20 pb-1 text-warning"> | ||
<ExclamationTriangleIcon className="size-4" /> | ||
<Paragraph variant="small/bright" className="text-warning"> | ||
Active incident | ||
</Paragraph> | ||
</div> | ||
<Paragraph variant="extra-small/bright" className="text-warning/80"> | ||
Our team is working on resolving the issue. Check our status page for more | ||
information. | ||
</Paragraph> | ||
<LinkButton | ||
variant="secondary/small" | ||
to="https://status.trigger.dev" | ||
target="_blank" | ||
fullWidth | ||
className="border-warning/20 bg-warning/10 hover:!border-warning/30 hover:!bg-warning/20" | ||
> | ||
<span className="text-warning">View status page</span> | ||
</LinkButton> | ||
</div> | ||
</motion.div> | ||
)} | ||
</> | ||
); | ||
} |
88 changes: 88 additions & 0 deletions
88
apps/webapp/app/services/betterstack/betterstack.server.ts
This file contains hidden or 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,88 @@ | ||
import { type ApiResult, wrapZodFetch } from "@trigger.dev/core/v3/zodfetch"; | ||
import { createCache, DefaultStatefulContext, Namespace } from "@unkey/cache"; | ||
import { MemoryStore } from "@unkey/cache/stores"; | ||
import { z } from "zod"; | ||
import { env } from "~/env.server"; | ||
|
||
const IncidentSchema = z.object({ | ||
data: z.object({ | ||
id: z.string(), | ||
type: z.string(), | ||
attributes: z.object({ | ||
aggregate_state: z.string(), | ||
}), | ||
}), | ||
}); | ||
|
||
export type Incident = z.infer<typeof IncidentSchema>; | ||
|
||
const ctx = new DefaultStatefulContext(); | ||
const memory = new MemoryStore({ persistentMap: new Map() }); | ||
|
||
const cache = createCache({ | ||
query: new Namespace<ApiResult<Incident>>(ctx, { | ||
stores: [memory], | ||
fresh: 15_000, | ||
stale: 30_000, | ||
}), | ||
}); | ||
|
||
export class BetterStackClient { | ||
private readonly baseUrl = "https://uptime.betterstack.com/api/v2"; | ||
|
||
async getIncidents() { | ||
const apiKey = env.BETTERSTACK_API_KEY; | ||
if (!apiKey) { | ||
return { success: false as const, error: "BETTERSTACK_API_KEY is not set" }; | ||
} | ||
|
||
const statusPageId = env.BETTERSTACK_STATUS_PAGE_ID; | ||
if (!statusPageId) { | ||
return { success: false as const, error: "BETTERSTACK_STATUS_PAGE_ID is not set" }; | ||
} | ||
|
||
const cachedResult = await cache.query.swr("betterstack", async () => { | ||
try { | ||
const result = await wrapZodFetch( | ||
IncidentSchema, | ||
`${this.baseUrl}/status-pages/${statusPageId}`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${apiKey}`, | ||
"Content-Type": "application/json", | ||
}, | ||
}, | ||
{ | ||
retry: { | ||
maxAttempts: 3, | ||
minTimeoutInMs: 1000, | ||
maxTimeoutInMs: 5000, | ||
}, | ||
} | ||
); | ||
|
||
return result; | ||
} catch (error) { | ||
console.error("Failed to fetch incidents from BetterStack:", error); | ||
return { | ||
success: false as const, | ||
error: error instanceof Error ? error.message : "Unknown error", | ||
}; | ||
} | ||
}); | ||
|
||
if (cachedResult.err) { | ||
return { success: false as const, error: cachedResult.err }; | ||
} | ||
|
||
if (!cachedResult.val) { | ||
return { success: false as const, error: "No result from BetterStack" }; | ||
} | ||
|
||
if (!cachedResult.val.success) { | ||
return { success: false as const, error: cachedResult.val.error }; | ||
} | ||
|
||
return { success: true as const, data: cachedResult.val.data.data }; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.