Backend/Charts and metrics

Charts and metrics

Revenue, growth, ARR and pipeline numbers are worked out in the browser from the sample records. This page shows where each number comes from and how to work it out in your database instead.

Where the numbers come from

NumberWorked out inFrom
Collected and invoiced per monthcollectedByMonth, invoicedByMonthSuccessful payments; invoices by issue date
ARR and live customers per montharrByMonth, liveCustomersByMonthContracts live at the end of each month
Collected by plancollectedByPlanPayments, by contract plan
A customer's revenue and growthrevenueByMonth, growthOfThat customer's payments
Pipeline, weighted value, win rate, funnelpipelineStats, stageReachLeads and their stage history
Outstanding, overdue, ageinguseInvoices()Invoices and payments (see Invoices)

The monthly functions are in src/lib/data/trends.ts and src/lib/data/growth.ts; the pipeline ones in src/lib/data/leads.ts. All months end at DEMO_TODAY, the fixed date the sample data is built around.

Revenue and growth

A customer's revenue for a month counts each payment toward the months it pays for: a yearly payment is spread over 12 months and a quarterly one over 3. So a customer billed once a year doesn't look like it grew and then shrank to nothing.

Growth compares the last 6 months with the 6 before. A customer with no revenue in the earlier 6 months shows "New". The rating follows from growth (ratingForGrowth): Excellent from +50%, Good from +10%, Normal within ±10%, Bad down to −40% and Very Bad below that, unless someone sets a rating by hand.

Work it out in your database

Working these out in the browser means loading every payment and contract. With real data, let the database do it and send the app only the totals. A view for collected per month:

Collected per month (SQL)
create view collected_by_month as
select workspace_id,
       date_trunc('month', paid_at)::date as month,
       sum(amount_usd) as collected
from payments
where status = 'succeeded'
group by 1, 2;

And a customer's revenue, spread over the months each payment covers:

Revenue per customer per month (SQL)
create view customer_revenue_by_month as
select p.workspace_id, p.customer_id,
       date_trunc('month', p.paid_at + make_interval(months => m))::date as month,
       sum(p.amount_usd / c.months_paid) as revenue
from payments p
join contracts c on c.id = p.contract_id   -- months_paid: 1, 3 or 12
cross join lateral generate_series(0, c.months_paid - 1) as m
where p.status = 'succeeded'
group by 1, 2, 3;

Growth is then two sums over that view. For dashboards many people open, keep the results in a table refreshed by a scheduled job, or a materialized view.

Keep the screens the same

The cards and charts only need arrays of numbers in month order, oldest first. Fetch the totals on the server and pass them where the card calls the function today, for example collectedByMonth(range) in the Collected vs invoiced card. Use the same month keys (lastMonths, monthLabel) with today's date in place of DEMO_TODAY, and fill months with no payments with 0 so the line doesn't skip.