Module 3 · Troubleshoot and Review · Lesson 8 of 9

Diagnose before you change

Fix a crash on a malformed row, a wrong time window and a time-zone mix-up, and tell duplicate events from duplicate alerts.

In this lesson · about 25 minutes

  • Reproduce a bug with the smallest possible input
  • Test boundaries just inside, at and just outside the limit
  • Normalise time zones before comparing times

Exercise

Bug: the 12:00 burst never alerts

A teammate's detector finds the 09:00 root burst but misses the one at 12:00 to 12:05. The spec says events exactly five minutes old are included. Find and fix the one-character bug.

Code that runs before yours
import csv
from collections import defaultdict, deque
from datetime import datetime, timezone, timedelta

def parse_rows(path):
    events, rejected = [], 0
    with open(path, newline="", encoding="utf-8") as f:
        for row in csv.DictReader(f):
            try:
                user = (row.get("user") or "").strip()
                ip = (row.get("source_ip") or "").strip()
                outcome = (row.get("outcome") or "").strip().lower()
                ts = datetime.fromisoformat((row.get("timestamp") or "").strip())
                if not user or not ip or outcome not in ("success", "failure"):
                    raise ValueError("missing or bad field")
                if ts.tzinfo is None:
                    raise ValueError("timestamp has no time zone")
            except ValueError:
                rejected += 1
                continue
            events.append({"ts": ts.astimezone(timezone.utc), "user": user,
                           "source_ip": ip, "outcome": outcome})
    return events, rejected

events, rejected = parse_rows("cyber-login.csv")

Exercise

Bug: two servers, two time zones

One server logs in UTC, another in UTC+3. Write to_utc(text) that parses an ISO timestamp with an offset and returns it in UTC as an ISO string. It should raise ValueError for a timestamp with no offset.

Code that runs before yours
def _raises(fn, *args):
    try:
        fn(*args)
    except Exception as error:
        return type(error).__name__
    return "no error"

Guided dialogue

Two more bugs with your coach

0 of 2 answered

C

Let's reason through two more problems. Pick the best next step.

C

Malformed row crashes the script. Your detector dies with KeyError: 'user' on a new export. What do you do first?

Check your understanding0 of 3 correct

Question 1

1.Why sort events before using a sliding window?

Question 2

2.What is wrong with silently catching every exception?

Question 3

3.Which boundary tests should a 5-minute window have?

Quiz score: 0 of 3

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Noviq Academy is free and for learning. All data in this course is synthetic and uses documentation IP ranges. Never paste real authentication logs or credentials into online tools. Python runs in your own browser with Pyodide; nothing you type is sent to Noviq.