Skip to content

Commit

Permalink
📝 rename table (#487)
Browse files Browse the repository at this point in the history
* 📝 rename table

* 📝 rename table

* 📝 rename table

* 📝 rename table

* 📝 rename table

* 📝 rename table

* 📝 rename table

* 📝 rename table

* 📝 rename table

* 📝reaname table

* 🔥

* wip: fix zod and replace incident with status report

* fix: wrong status-report url

* fix: tests

* wip: rename

* fix: status

---------

Co-authored-by: mxkaske <[email protected]>
  • Loading branch information
thibaultleouay and mxkaske authored Dec 2, 2023
1 parent 920ab4f commit 9c64fe2
Show file tree
Hide file tree
Showing 52 changed files with 2,012 additions and 850 deletions.
1 change: 0 additions & 1 deletion apps/server/fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ primary_region = "ams"
force_https = true
auto_stop_machines = false
auto_start_machines = false
min_machines_running = 12
processes = ["app"]
[http_service.concurrency]
type = "requests"
Expand Down
46 changes: 25 additions & 21 deletions apps/server/src/public/status.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Hono } from "hono";
import { endTime, setMetric, startTime } from "hono/timing";

import { db, eq } from "@openstatus/db";
import { and, db, eq } from "@openstatus/db";
import {
incident,
monitor,
monitorsToIncidents,
monitorsToPages,
monitorsToStatusReport,
page,
pagesToIncidents,
pagesToStatusReports,
statusReport,
} from "@openstatus/db/src/schema";
import { getMonitorList, Tinybird } from "@openstatus/tinybird";
import { Redis } from "@openstatus/upstash";
Expand Down Expand Up @@ -36,40 +36,44 @@ status.get("/:slug", async (c) => {
const { slug } = c.req.param();

const cache = await redis.get(slug);

if (cache) {
setMetric(c, "OpenStatus-Cache", "HIT");

return c.json({ status: cache });
}

startTime(c, "database");
// { monitors, pages, monitors_to_pages }

const monitorData = await db
.select()
.from(monitorsToPages)
.leftJoin(monitor, eq(monitorsToPages.monitorId, monitor.id))
.leftJoin(monitor, and(eq(monitorsToPages.monitorId, monitor.id)))
.leftJoin(
monitorsToStatusReport,
eq(monitor.id, monitorsToStatusReport.monitorId),
)
.leftJoin(
monitorsToIncidents,
eq(monitor.id, monitorsToIncidents.monitorId),
statusReport,
eq(monitorsToStatusReport.statusReportId, statusReport.id),
)
.leftJoin(incident, eq(monitorsToIncidents.incidentId, incident.id))
.leftJoin(page, eq(monitorsToPages.pageId, page.id))
.where(eq(page.slug, slug))
.where(eq(page.slug, slug)) // TODO: query only active monitors
.all();

const pageIncidentData = await db
const pageStatusReportData = await db
.select()
.from(pagesToIncidents)
.leftJoin(incident, eq(pagesToIncidents.incidentId, incident.id))
.leftJoin(page, eq(pagesToIncidents.pageId, page.id))
.from(pagesToStatusReports)
.leftJoin(
statusReport,
eq(pagesToStatusReports.statusReportId, statusReport.id),
)
.leftJoin(page, eq(pagesToStatusReports.pageId, page.id))
.where(eq(page.slug, slug))
.all();

endTime(c, "database");

const isIncident = [...pageIncidentData, ...monitorData].some((data) => {
if (!data.incident) return false;
return !["monitoring", "resolved"].includes(data.incident.status);
const isIncident = [...pageStatusReportData, ...monitorData].some((data) => {
if (!data.status_report) return false;
return !["monitoring", "resolved"].includes(data.status_report.status);
});

startTime(c, "clickhouse");
Expand All @@ -83,7 +87,7 @@ status.get("/:slug", async (c) => {
}),
);
endTime(c, "clickhouse");
// { ok, count }

const data = lastMonitorPings.reduce(
(prev, curr) => {
if (curr.status === "fulfilled") {
Expand Down
8 changes: 4 additions & 4 deletions apps/server/src/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { logger } from "hono/logger";

import type { Plan } from "@openstatus/plans";

import { incidentApi } from "./incident";
import { incidenUpdateApi } from "./incidentUpdate";
import { middleware } from "./middleware";
import { monitorApi } from "./monitor";
import { statusReportApi } from "./statusReport";
import { statusReportUpdateApi } from "./statusReportUpdate";

export type Variables = {
workspaceId: string;
Expand All @@ -29,6 +29,6 @@ api.doc("/openapi", {
api.use("/*", middleware);
api.use("/*", logger());
api.route("/monitor", monitorApi);
api.route("/incident_update", incidenUpdateApi);
api.route("/status_report_update", statusReportUpdateApi);

api.route("/incident", incidentApi);
api.route("/status_report", statusReportApi);
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { expect, test } from "bun:test";

import { api } from ".";

test("GET one incident update ", async () => {
const res = await api.request("/incident/1", {
test("GET one status report", async () => {
const res = await api.request("/status_report/1", {
headers: {
"x-openstatus-key": "1",
},
Expand All @@ -12,13 +12,14 @@ test("GET one incident update ", async () => {
expect(await res.json()).toMatchObject({
id: 1,
status: "investigating",
title: "Test Incident",
incident_updates: expect.any(Array),
title: "Test Status Report",
// TODO: discuss if we should return `updates` instead of `status_report_updates`
status_report_updates: expect.any(Array),
});
});

test("create one incident ", async () => {
const res = await api.request("/incident", {
test("create one status report", async () => {
const res = await api.request("/status_report", {
method: "POST",
headers: {
"x-openstatus-key": "1",
Expand All @@ -27,13 +28,13 @@ test("create one incident ", async () => {
body: JSON.stringify({
status: "investigating",
date: "2023-11-08T21:03:13.000Z",
title: "Test Incident",
title: "Test Status Report",
}),
});
expect(res.status).toBe(200);
expect(await res.json()).toMatchObject({
id: expect.any(Number),
status: "investigating",
title: "Test Incident",
title: "Test Status Report",
});
});
Loading

0 comments on commit 9c64fe2

Please sign in to comment.