Count the failed dispatch jobs OPS-2215

OpenShell basics · Easy · Do · about 15 min ·Linux

Lab machine

A private Linux machine with the problem already set up. Sessions last up to 60 minutes.
Sal Brennan opened OPS-2215 at 09:47task

The carrier billed Northstar for every failed dispatch attempt. Finance wants to dispute it with the real number: distinct jobs that failed, not attempts.

Sal from finance forwards an invoice from FastFreight, the carrier that collects from depot-west. They charge a penalty for each failed dispatch, and their count includes every retry.

"If a job fails three times and then goes out, that is one failed job, not three. I need the list of job IDs and the total by noon. Please be exact, they will check."

ops/logs/dispatch.log has one line per attempt. Each line has a status= field and a msg= field. Some successful lines mention the word "fail" in their message, so a plain search for fail overcounts.

Your task

Build the list with a pipeline: keep the attempts whose status is fail, extract the job IDs, and remove duplicates. Write the IDs, one per line, to reports/failed-ids.txt, and the count, as a bare number, to reports/failed-jobs.txt. Leave the log unchanged.

On the machine

  • ops/logs/dispatch.log, a few hundred lines from Saturday morning
  • TICKET.md

Timeline

09:05FastFreight sends an invoice line: "failed dispatch attempts, Saturday".
09:40Sal notices the count includes every retry of the same job.
09:47Sal asks platform for the real number of failed jobs.
12:00Sal's reply to the carrier is due.

Done when

  1. reports/failed-ids.txt lists every job ID with a status=fail line, each exactly once.
  2. reports/failed-jobs.txt holds the number of those jobs.

Hints

Hint 1

Look at a few lines with head. Which field says whether an attempt failed?

Hint 2

grep 'status=fail' matches the field; grep fail also matches messages.

Hint 3

Cut out the job IDs (grep -o 'job=J[0-9]*' or sed), then sort -u to keep each once.

Hint 4

wc -l < reports/failed-ids.txt > reports/failed-jobs.txt

Show the solution

grep 'status=fail' ops/logs/dispatch.log | grep -o 'J[0-9]*' | sort -u > reports/failed-ids.txt, then wc -l < reports/failed-ids.txt > reports/failed-jobs.txt.