Skip to main content

Quick and easy metrics notifier

With Interval you can:

  • Send email or Slack notifications from actions
  • Schedule actions to be run on a recurring basis

Since Interval already works alongside your database and backend, these features in combination make it trivial to quickly spin up reminders to keep your team aware of important metrics. You can even send notifications on a particular threshold or condition, which is useful to maintain data integrity and alert when expectations are not met.

Get started with this example

Try it out

To create a fresh project based off this example, run:

npx create-interval-app --template=metrics-notifier
The full source code for this tool can also be found in our examples repo.

How it works

This example uses Prisma to query our PostgreSQL database, but you can easily swap these out for your own metrics with whatever you typically use to query your datastore.

First, we'll add our queries to a basic Interval action and simply log the output. When building tools with Interval, it's often useful to leverage ctx.log for debugging and getting a better idea of the data available to you.

import "dotenv/config";
import Interval, { io } from "@interval/sdk";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

export async function activeUsers() {
return await prisma.$queryRaw`SELECT count(*) FROM "Users" WHERE active = true`
}

const interval = new Interval({
apiKey: process.env.INTERVAL_KEY,
actions: {
post_metrics: async () => {
const count = await activeUsers();
ctx.log("Active user count:", count);
},
},
});

interval.listen();

Next we'll make a call to ctx.notify to post the query results to our team's slack channel.

info

To send Slack notifications, you'll need to:

import "dotenv/config";
import Interval, { io } from "@interval/sdk";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

export async function activeUsers() {
return await prisma.$queryRaw`SELECT count(*) FROM "Users" WHERE active = true`
}

const interval = new Interval({
apiKey: process.env.INTERVAL_KEY,
actions: {
post_metrics: async () => {
const count = await activeUsers();
ctx.log("Active user count:", count);
await ctx.notify({
message: `As of today, we have *${count}* monthly active users`,
title: "Latest active user count 📈",
delivery: [
{
to: "#metrics",
method: "SLACK",
},
],
});
},
},
});

interval.listen();

Now, each time the action runs, Interval will send off the results of your query to post to the specified Slack channel.

Notifications are not delivered for actions running in the Console with your personal development key. To test notifications for real, you'll have to deploy your action to production. :::

Next, we'll schedule the action to run automatically at a set time. To add a schedule, we first need to configure our action to run in the background. You can update this setting in the dashboard or you can configure the backgroundable property in your action definition code.

import 'dotenv/config'
import Interval, { io } from '@interval/sdk'
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

export async function activeUsers() {
return await prisma.$queryRaw`SELECT count(*) FROM "Users" WHERE active = true`
}

const interval = new Interval({
apiKey: process.env.INTERVAL_KEY,
actions: {
post_metrics: async () => {
post_metrics: {
handler: async () => {
const count = await activeUsers()
ctx.log('Active user count:', count)
await ctx.notify({
message: `As of today, we have *${count}* monthly active users`,
title: "Latest active user count 📈",
delivery: [
{
to: "#metrics",
method: "SLACK",
},
],
});
},
backgroundable: true,
},
},
})

interval.listen()

Once the action is backgroundable, a recurring schedule can be set on the action's settings page in the Interval dashboard.

The full source code is available for this example.

API methods used

Was this section useful?

548 Market St PMB 31323
San Francisco, CA 94104

© 2022

Join our mailing list

Every Friday we send an email with the latest from Interval, including events, product updates, SDK releases, and more.