C.W.K.
Stream
Lesson 02 of 05 · published

The Status Header — Three Numbers That Tell You Everything

~15 min · status-header, metrics, color

Level 0Greenhorn
0 XP0/53 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

The header is the part you see first and most often. It must answer "is everything normal?" in under a second.

The three numbers

MetricNormalYellowRed
Active sessions1–2 (you)3–4 (laptop + phone + tablet)5+ or sessions from unknown IPs
Failed attempts (last hour)01–35+ (probable brute force)
Blacklisted IPs01–25+ (something is targeting you)

The color encoding

Use color, but don't rely on it alone. Each badge should also have an icon (✅ ⚠️ 🚨) and the count, so colorblind / dark-mode / phone-glance scenarios all work.

Code

The query behind each metric·python
def dashboard_stats():
    active = db.execute(
        "SELECT COUNT(*) FROM security_sessions WHERE expires_at > ?",
        (now(),)
    ).fetchone()[0]

    fails_1h = db.execute("""
      SELECT COUNT(*) FROM security_attempts
      WHERE success = 0 AND attempted_at > ?
    """, (now() - 3600,)).fetchone()[0]

    blacklisted = db.execute(
        "SELECT COUNT(*) FROM security_blacklist"
    ).fetchone()[0]

    status = "green"
    if fails_1h >= 5 or blacklisted >= 5:
        status = "red"
    elif fails_1h >= 1 or blacklisted >= 1:
        status = "yellow"

    return {"active": active, "fails_1h": fails_1h,
            "blacklisted": blacklisted, "status": status}

External links

Exercise

Implement dashboard_stats() and render the three numbers as colored badges in your /admin/security page. Test by manually inserting rows into security_attempts to bump fails_1h and watch the status flip green → yellow → red. The thresholds are tuneable to your traffic.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.