Openstatus
www.openstatus.dev
1import { OSTinybird } from "@openstatus/tinybird";
2
3import { env } from "@/env";
4
5const tb = new OSTinybird(env.TINY_BIRD_API_KEY);
6
7// REMINDER: we could extend the limits (WorkspacePlan) by
8// knowing which plan the user is on and disable some periods
9const periods = ["1d", "7d", "14d"] as const;
10const types = ["http", "tcp"] as const;
11
12// FIXME: check we we can also use Period from elswhere
13type Period = (typeof periods)[number];
14// FIMXE: use JobType instead!
15type Type = (typeof types)[number];
16
17// REMINDER: extend if needed
18export function prepareListByPeriod(period: Period, type: Type = "http") {
19 switch (period) {
20 case "1d": {
21 const getData = {
22 http: tb.legacy_httpListDaily,
23 tcp: tb.legacy_tcpListDaily,
24 } as const;
25 return { getData: getData[type] };
26 }
27 case "7d": {
28 const getData = {
29 http: tb.legacy_httpListWeekly,
30 tcp: tb.legacy_tcpListWeekly,
31 } as const;
32 return { getData: getData[type] };
33 }
34 case "14d": {
35 const getData = {
36 http: tb.legacy_httpListBiweekly,
37 tcp: tb.legacy_tcpListBiweekly,
38 } as const;
39 return { getData: getData[type] };
40 }
41 default: {
42 const getData = {
43 http: tb.legacy_httpListDaily,
44 tcp: tb.legacy_tcpListDaily,
45 } as const;
46 return { getData: getData[type] };
47 }
48 }
49}
50
51export function prepareMetricsByPeriod(period: Period, type: Type = "http") {
52 switch (period) {
53 case "1d": {
54 const getData = {
55 http: tb.legacy_httpMetricsDaily,
56 tcp: tb.legacy_tcpMetricsDaily,
57 } as const;
58 return { getData: getData[type] };
59 }
60 case "7d": {
61 const getData = {
62 http: tb.legacy_httpMetricsWeekly,
63 tcp: tb.legacy_tcpMetricsWeekly,
64 } as const;
65 return { getData: getData[type] };
66 }
67 case "14d": {
68 const getData = {
69 http: tb.legacy_httpMetricsBiweekly,
70 tcp: tb.legacy_tcpMetricsBiweekly,
71 } as const;
72 return { getData: getData[type] };
73 }
74 default: {
75 const getData = {
76 http: tb.legacy_httpMetricsDaily,
77 tcp: tb.legacy_tcpMetricsDaily,
78 } as const;
79 return { getData: getData[type] };
80 }
81 }
82}
83
84export function prepareMetricByRegionByPeriod(
85 period: Period,
86 type: Type = "http",
87) {
88 switch (period) {
89 case "1d": {
90 const getData = {
91 http: tb.httpMetricsByRegionDaily,
92 tcp: tb.tcpMetricsByRegionDaily,
93 } as const;
94 return { getData: getData[type] };
95 }
96 case "7d": {
97 const getData = {
98 http: tb.httpMetricsByRegionWeekly,
99 tcp: tb.tcpMetricsByRegionWeekly,
100 } as const;
101 return { getData: getData[type] };
102 }
103 case "14d": {
104 const getData = {
105 http: tb.httpMetricsByRegionBiweekly,
106 tcp: tb.tcpMetricsByRegionBiweekly,
107 } as const;
108 return { getData: getData[type] };
109 }
110 default: {
111 const getData = {
112 http: tb.httpMetricsByRegionDaily,
113 tcp: tb.tcpMetricsByRegionDaily,
114 } as const;
115 return { getData: getData[type] };
116 }
117 }
118}
119
120export function prepareMetricByIntervalByPeriod(
121 period: Period,
122 type: Type = "http",
123) {
124 switch (period) {
125 case "1d": {
126 const getData = {
127 http: tb.httpMetricsByIntervalDaily,
128 tcp: tb.tcpMetricsByIntervalDaily,
129 } as const;
130 return { getData: getData[type] };
131 }
132 case "7d": {
133 const getData = {
134 http: tb.httpMetricsByIntervalWeekly,
135 tcp: tb.tcpMetricsByIntervalWeekly,
136 } as const;
137 return { getData: getData[type] };
138 }
139 case "14d": {
140 const getData = {
141 http: tb.httpMetricsByIntervalBiweekly,
142 tcp: tb.tcpMetricsByIntervalBiweekly,
143 } as const;
144 return { getData: getData[type] };
145 }
146 default: {
147 const getData = {
148 http: tb.httpMetricsByIntervalDaily,
149 tcp: tb.tcpMetricsByIntervalDaily,
150 } as const;
151 return { getData: getData[type] };
152 }
153 }
154}
155
156export function prepareStatusByPeriod(
157 period: "7d" | "45d",
158 type: Type = "http",
159) {
160 switch (period) {
161 case "7d": {
162 const getData = {
163 http: tb.httpStatusWeekly,
164 tcp: tb.tcpStatusWeekly,
165 } as const;
166 return { getData: getData[type] };
167 }
168 case "45d": {
169 const getData = {
170 http: tb.legacy_httpStatus45d,
171 tcp: tb.legacy_tcpStatus45d,
172 } as const;
173 return { getData: getData[type] };
174 }
175 default: {
176 const getData = {
177 http: tb.httpStatusWeekly,
178 tcp: tb.tcpStatusWeekly,
179 } as const;
180 return { getData: getData[type] };
181 }
182 }
183}
184
185export function prepareGetByPeriod(period: "30d", type: Type = "http") {
186 switch (period) {
187 case "30d": {
188 const getData = {
189 http: tb.httpGetMonthly,
190 tcp: tb.tcpGetMonthly,
191 } as const;
192 return { getData: getData[type] };
193 }
194 default: {
195 const getData = {
196 http: tb.httpGetMonthly,
197 tcp: tb.tcpGetMonthly,
198 } as const;
199 return { getData: getData[type] };
200 }
201 }
202}
203
204// FOR MIGRATION
205export type ResponseTimeMetrics = Awaited<
206 ReturnType<OSTinybird["legacy_httpMetricsDaily"]>
207>["data"][number];
208
209export type ResponseTimeMetricsByRegion = Awaited<
210 ReturnType<OSTinybird["httpMetricsByRegionDaily"]>
211>["data"][number];
212
213export type ResponseGraph = Awaited<
214 ReturnType<OSTinybird["httpMetricsByIntervalDaily"]>
215>["data"][number];
216
217export type ResponseStatusTracker = Awaited<
218 ReturnType<OSTinybird["httpStatusWeekly"]>
219>["data"][number];