Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 70 additions & 14 deletions apps/alerting/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BunRuntime } from "@effect/platform-bun"
import { AlertRuntime, AlertsService, Database, Env, makeTelemetryLayer, OrgTinybirdSettingsService, QueryEngineService, TinybirdService } from "@maple/api/alerting"
import { AlertRuntime, AlertsService, Database, DigestService, EmailService, Env, makeTelemetryLayer, OrgTinybirdSettingsService, QueryEngineService, TinybirdService } from "@maple/api/alerting"
import { Cause, Duration, Effect, Layer, Schedule } from "effect"

const DatabaseLive = Database.Default.pipe(
Expand Down Expand Up @@ -38,24 +38,36 @@ const AlertsServiceLive = AlertsService.Live.pipe(
Layer.provide(AlertsDependenciesLive),
)

const EmailServiceLive = EmailService.Default.pipe(
Layer.provide(Env.Default),
)

const DigestDependenciesLive = Layer.mergeAll(
BaseLive,
TinybirdServiceLive,
EmailServiceLive,
)

const DigestServiceLive = DigestService.Default.pipe(
Layer.provide(DigestDependenciesLive),
)

const TelemetryLive = makeTelemetryLayer("alerting")

const program = Effect.gen(function* () {
const alertLoop = Effect.gen(function* () {
const alerts = yield* AlertsService

yield* Effect.logInfo("Alerting worker started")

yield* alerts.runSchedulerTick().pipe(
Effect.tap((result) =>
Effect.logInfo("Alerting worker tick complete").pipe(
Effect.annotateLogs({
evaluatedCount: result.evaluatedCount,
processedCount: result.processedCount,
evaluationFailureCount: result.evaluationFailureCount,
deliveryFailureCount: result.deliveryFailureCount,
}),
),
Effect.logInfo("Alerting worker tick complete").pipe(
Effect.annotateLogs({
evaluatedCount: result.evaluatedCount,
processedCount: result.processedCount,
evaluationFailureCount: result.evaluationFailureCount,
deliveryFailureCount: result.deliveryFailureCount,
}),
),
),
Effect.catchCause((cause) =>
Effect.logError("Alerting worker tick failed").pipe(
Effect.annotateLogs({ error: Cause.pretty(cause) }),
Expand All @@ -67,9 +79,53 @@ const program = Effect.gen(function* () {
),
),
)
})

const digestLoop = Effect.gen(function* () {
const digest = yield* DigestService

yield* digest.runDigestTick().pipe(
Effect.tap((result) =>
Effect.logInfo("Digest tick complete").pipe(
Effect.annotateLogs({
sentCount: result.sentCount,
errorCount: result.errorCount,
skipped: result.skipped,
}),
),
),
Effect.catchCause((cause) =>
Effect.logError("Digest tick failed").pipe(
Effect.annotateLogs({ error: Cause.pretty(cause) }),
),
),
Effect.repeat(
Schedule.spaced(Duration.minutes(15)).pipe(
Schedule.jittered,
),
),
)
})

const program = Effect.gen(function* () {
yield* Effect.logInfo("Alerting worker started")

// Run digest loop detached, alert loop in foreground
yield* digestLoop.pipe(
Effect.catchCause((cause) =>
Effect.logError("Digest loop terminated unexpectedly").pipe(
Effect.annotateLogs({ error: Cause.pretty(cause) }),
),
),
Effect.forkDetach,
)
yield* alertLoop
}).pipe(
Effect.provide(AlertsServiceLive),
Effect.provide(TelemetryLive),
Effect.provide(
Layer.mergeAll(AlertsServiceLive, DigestServiceLive).pipe(
Layer.provide(TelemetryLive),
),
),
)

BunRuntime.runMain(program)
1 change: 1 addition & 0 deletions apps/alerting/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"jsx": "react-jsx",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"types": ["bun"],
"moduleResolution": "bundler",
Expand Down
2 changes: 2 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"@libsql/client": "0.15.15",
"@maple/db": "workspace:*",
"@maple/domain": "workspace:*",
"@maple/email": "workspace:*",
"@react-email/components": "^1.0.11",
"@maple/query-engine": "workspace:*",
"@tinybirdco/sdk": "catalog:tinybird",
"autumn-js": "^1.2.5",
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/alerting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { AlertRuntime, AlertsService } from "./services/AlertsService";
export { Database } from "./services/DatabaseLive";
export { DigestService } from "./services/DigestService";
export { EmailService } from "./services/EmailService";
export { Env } from "./services/Env";
export { OrgTinybirdSettingsService } from "./services/OrgTinybirdSettingsService";
export { QueryEngineService } from "./services/QueryEngineService";
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { HttpAlertsLive } from "./routes/alerts.http";
import { HttpAuthLive, HttpAuthPublicLive } from "./routes/auth.http";
import { HttpCloudflareLogpushLive } from "./routes/cloudflare-logpush.http";
import { HttpDashboardsLive } from "./routes/dashboards.http";
import { HttpDigestLive } from "./routes/digest.http";
import { HttpIngestKeysLive } from "./routes/ingest-keys.http";
import { HttpObservabilityLive } from "./routes/observability.http";
import { HttpOrgTinybirdSettingsLive } from "./routes/org-tinybird-settings.http";
Expand All @@ -19,6 +20,7 @@ export const HttpApiRoutes = HttpApiBuilder.layer(MapleApi).pipe(
Layer.provide(HttpAlertsLive),
Layer.provide(HttpCloudflareLogpushLive),
Layer.provide(HttpDashboardsLive),
Layer.provide(HttpDigestLive),
Layer.provide(HttpIngestKeysLive),
Layer.provide(HttpObservabilityLive),
Layer.provide(HttpOrgTinybirdSettingsLive),
Expand Down
15 changes: 13 additions & 2 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { AuthorizationLive } from "./services/AuthorizationLive";
import { CloudflareLogpushService } from "./services/CloudflareLogpushService";
import { DashboardPersistenceService } from "./services/DashboardPersistenceService";
import { Database } from "./services/DatabaseLive";
import { DigestService } from "./services/DigestService";
import { EmailService } from "./services/EmailService";
import { Env } from "./services/Env";
import { OrgIngestKeysService } from "./services/OrgIngestKeysService";
import { OrgTinybirdSettingsService } from "./services/OrgTinybirdSettingsService";
Expand All @@ -35,7 +37,7 @@ const DocsRoute = HttpApiScalar.layer(MapleApi, {
});

const InfraLive = Database.layer.pipe(
Layer.provideMerge(Env.layer),
Layer.provideMerge(Env.Default),
)

const CoreServicesLive = Layer.mergeAll(
Expand All @@ -62,11 +64,20 @@ const AlertsServiceLive = AlertsService.layer.pipe(
Layer.provideMerge(Layer.mergeAll(CoreServicesLive, QueryEngineServiceLive, AlertRuntime.Default)),
)

const EmailServiceLive = EmailService.Default.pipe(
Layer.provide(Env.Default),
)

const DigestServiceLive = DigestService.Default.pipe(
Layer.provideMerge(Layer.mergeAll(InfraLive, TinybirdServiceLive, EmailServiceLive)),
)

const MainLive = Layer.mergeAll(
CoreServicesLive,
TinybirdServiceLive,
QueryEngineServiceLive,
AlertsServiceLive,
DigestServiceLive,
)

const AllRoutes = Layer.mergeAll(
Expand Down Expand Up @@ -105,7 +116,7 @@ const RuntimeLive = Layer.mergeAll(
const app = HttpRouter.serve(AllRoutes).pipe(
Layer.provide(RuntimeLive),
Layer.provide(MainLive),
Layer.provide(AuthorizationLive.pipe(Layer.provideMerge(Env.layer))),
Layer.provide(AuthorizationLive.pipe(Layer.provideMerge(Env.Default))),
);

BunRuntime.runMain(app.pipe(Layer.launch as never));
48 changes: 48 additions & 0 deletions apps/api/src/routes/digest.http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { HttpApiBuilder } from "effect/unstable/httpapi"
import { CurrentTenant, MapleApi } from "@maple/domain/http"
import { Effect } from "effect"
import { DigestService } from "../services/DigestService"

export const HttpDigestLive = HttpApiBuilder.group(
MapleApi,
"digest",
(handlers) =>
Effect.gen(function* () {
const digest = yield* DigestService

return handlers
.handle("getSubscription", () =>
Effect.gen(function* () {
const tenant = yield* CurrentTenant.Context
return yield* digest.getSubscription(tenant.orgId, tenant.userId)
}),
)
.handle("upsertSubscription", ({ payload }) =>
Effect.gen(function* () {
const tenant = yield* CurrentTenant.Context
return yield* digest.upsertSubscription(
tenant.orgId,
tenant.userId,
{
email: payload.email,
enabled: payload.enabled,
dayOfWeek: payload.dayOfWeek,
timezone: payload.timezone,
},
)
}),
)
.handle("deleteSubscription", () =>
Effect.gen(function* () {
const tenant = yield* CurrentTenant.Context
yield* digest.deleteSubscription(tenant.orgId, tenant.userId)
}),
)
.handle("preview", () =>
Effect.gen(function* () {
const tenant = yield* CurrentTenant.Context
return yield* digest.preview(tenant.orgId)
}),
)
}),
)
Loading
Loading