# A report of counts

A report that counts several things is a query for each count and an
action that puts them together. Each query has `aggregate:` and no
`group:`, so it gives one row, whose count is 0 when nothing matches
([Counts, sums and groups](/docs/counts-sums-and-groups.md)).

Here a help desk reports, for the past 7 days, how many enquiries were
opened and solved, and how many are open now. The request didn't say what
"the past 7 days" means, so the action reads it one reasonable way and
says so in its `///` line, where the app's owner sees it:

```jadpo
/// The desk's last seven days, and how it stands now.
type WeekOfEnquiries = Output {
    opened: Int
    solved: Int
    open_now: Int
}
```

```jadpo
use desk/entities { Enquiry }
use desk/types { EnquiryCount, EnquiryTime }

/// How many enquiries were opened at or after `since`.
query opened_since(since: EnquiryTime) -> EnquiryCount {
    from: Enquiry
    where: Enquiry.raised_at >= since
    aggregate: {
        enquiries: count()
    }
}
```

```jadpo
use desk/entities { Enquiry }
use desk/types { EnquiryCount, EnquiryStatus, EnquiryTime }

/// How many enquiries are solved and were solved at or after `since`.
/// Reopening a enquiry clears `solved_at`, so a enquiry solved and reopened
/// since isn't counted.
query solved_since(since: EnquiryTime) -> EnquiryCount {
    from: Enquiry
    where: Enquiry.status == EnquiryStatus.solved
    where: Enquiry.solved_at >= since
    aggregate: {
        enquiries: count()
    }
}
```

```jadpo
use desk/entities { Enquiry }
use desk/types { EnquiryCount, EnquiryStatus }

/// How many enquiries aren't solved: open or pending.
query open_now() -> EnquiryCount {
    from: Enquiry
    where: Enquiry.status != EnquiryStatus.solved
    aggregate: {
        enquiries: count()
    }
}
```

```jadpo
use desk/types { EnquiryCount, EnquiryTime, WeekOfEnquiries }
use desk/queries { open_now, opened_since, solved_since }

/// The report for the past 7 days. The request said only "the past 7
/// days", so this reads it as the 7 × 24 hours before the report is made
/// (a rolling week, in UTC as every `Instant` is), not 7 calendar days.
action weekly_report() -> WeekOfEnquiries {
    var since: EnquiryTime = EnquiryTime(clock.now - 7d)
    var opened: EnquiryCount = opened_since(since)
    var solved: EnquiryCount = solved_since(since)
    var open: EnquiryCount = open_now()
    return WeekOfEnquiries {
        opened: opened.enquiries
        solved: solved.enquiries
        open_now: open.enquiries
    }
}
```

```jadpo
use desk/entities { Enquiry }
use desk/types { WeekOfEnquiries }
use desk/actions { open_enquiry, solve_enquiry }

test "the report counts the last 7 × 24 hours, and what is open now" {
    clock: "2026-11-01T09:00:00Z"

    var old: Enquiry = open_enquiry("Printer jammed")
    clock.advance(3d)
    var login: Enquiry = open_enquiry("Can't sign in")
    var refund: Enquiry = open_enquiry("Refund, please")
    var solved: Enquiry = solve_enquiry(refund)
    assert solved.solved_at == clock.now
    clock.advance(5d)
    assert weekly_report() == WeekOfEnquiries {
        opened: 2
        solved: 1
        open_now: 2
    }
    assert login.status == old.status
}
```

A report kept for each day is the same action run by a
[schedule](/docs/schedules.md), its counts stored in a record
of their own.
