completefrontend

Write the codeEasy

Write an alert rule that fires when the daily p75 stays over a line for several days in a row.

Leaf and Kettle's RUM pipeline works out, each day, how many phone visits the tea page had and their p75 LCP. Write alertEvents(days, { threshold, inARow, minVisits }), the alert rule that runs over them. days is in order, each { day, visits, p75 }, with p75 in ms; threshold is the budget in ms, inARow is at least 1, and minVisits is the fewest visits a day needs before its p75 counts. Return the messages the rule sends, in order, each { day, event, p75 }, where event is "fire" or "resolve". The rules:

a day with fewer than minVisits visits is skipped: it doesn't count as over or under, so it
  neither adds to a run of days over the line nor breaks one, and it can't fire or resolve the alert
a day that counts is over when its p75 is above threshold; exactly on threshold is under
the alert fires on the day that makes inARow counted days over in a row
while it's firing, more days over send nothing
it resolves on the first counted day under, and a new run starts from zero after that
alertEvents(
  [
    { day: 1, visits: 410, p75: 2300 },
    { day: 2, visits: 395, p75: 2620 },
    { day: 3, visits: 60, p75: 3900 }, // too few visits to count
    { day: 4, visits: 402, p75: 2700 },
    { day: 5, visits: 388, p75: 2650 },
    { day: 6, visits: 415, p75: 2480 },
  ],
  { threshold: 2500, inARow: 3, minVisits: 100 },
);
// [{ day: 5, event: "fire", p75: 2650 }, { day: 6, event: "resolve", p75: 2480 }]
Write this: solution.js
function alertEvents(days, { threshold, inARow, minVisits }) {
  // Today it sends a message for every day over the line, however few visits that day had.
  return days.filter((day) => day.p75 > threshold).map(({ day, p75 }) => ({ day, event: "fire", p75 }));
}
The tests your code must pass
  • fires on the day that makes the run long enough
  • a day exactly on the line is under, and breaks the run
  • a day with too few visits neither counts nor breaks the run
  • sends one message while it stays over, and resolves on the first day back under
  • a quiet day can't resolve it either
  • after it resolves, it needs a whole new run to fire again
  • one day over is enough when inARow is 1, and no days sends nothing

Try it in the editor

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…