Repository: supabase/supabase
File: apps/studio/components/interfaces/HomeNew/ProjectUsageSection.utils.ts (TypeScript)

Code before:
  const grouped: Record<
    ServiceKey,
    { current: StatsLike['eventChartData']; previous: StatsLike['eventChartData'] }
  > = {
    db: { current: [], previous: [] },
    functions: { current: [], previous: [] },
    auth: { current: [], previous: [] },
    storage: { current: [], previous: [] },
    realtime: { current: [], previous: [] },
  }

  const toIso = (microseconds: number) => new Date(microseconds / 1000).toISOString()

  for (const r of data ?? []) {
    const bucket = grouped[r.service as ServiceKey]
    const target = r.time_window === 'current' ? bucket.current : bucket.previous
    target.push({
      timestamp: toIso(r.timestamp),
      ok_count: r.ok_count,
      warning_count: r.warning_count,
      error_count: r.error_count,
    })
  }

  const byTime = (a: { timestamp: string }, b: { timestamp: string }) =>
    a.timestamp < b.timestamp ? -1 : a.timestamp > b.timestamp ? 1 : 0
  for (const key of Object.keys(grouped) as ServiceKey[]) {
    grouped[key].current.sort(byTime)
    grouped[key].previous.sort(byTime)
  }

  const toStats = (rows: StatsLike['eventChartData'] | undefined): StatsLike =>
    rows ? { ...base, eventChartData: rows } : empty

  return {
    db: { current: toStats(grouped.db.current), previous: toStats(grouped.db.previous) },
    functions: {
      current: toStats(grouped.functions.current),
      previous: toStats(grouped.functions.previous),
    },
    auth: { current: toStats(grouped.auth.current), previous: toStats(grouped.auth.previous) },
    storage: {
      current: toStats(grouped.storage.current),
      previous: toStats(grouped.storage.previous),
    },
    realtime: {
      current: toStats(grouped.realtime.current),
      previous: toStats(grouped.realtime.previous),
    },
  }
}

Review comment (suggestion):
```suggestion
  const byTime = (a: { timestamp: string }, b: { timestamp: string }) =>
    a.timestamp - b.timestamp
```

maybe personal preference, I find it easier to read

Code after:
  const grouped: Record<
    ServiceKey,
    { current: StatsLike['eventChartData']; previous: StatsLike['eventChartData'] }
  > = {
    db: { current: [], previous: [] },
    functions: { current: [], previous: [] },
    auth: { current: [], previous: [] },
    storage: { current: [], previous: [] },
    realtime: { current: [], previous: [] },
  }

  const toIso = (microseconds: number) => new Date(microseconds / 1000).toISOString()

  for (const r of data ?? []) {
    const bucket = grouped[r.service as ServiceKey]
    const target = r.time_window === 'current' ? bucket.current : bucket.previous
    target.push({
      timestamp: toIso(r.timestamp),
      ok_count: r.ok_count,
      warning_count: r.warning_count,
      error_count: r.error_count,
    })
  }

  const byTime = (a: { timestamp: string }, b: { timestamp: string }) =>
    Date.parse(a.timestamp) - Date.parse(b.timestamp)
  for (const key of Object.keys(grouped) as ServiceKey[]) {
    grouped[key].current.sort(byTime)
    grouped[key].previous.sort(byTime)
  }

  const toStats = (rows: StatsLike['eventChartData'] | undefined): StatsLike =>
    rows ? { ...base, eventChartData: rows } : empty

  return {
    db: { current: toStats(grouped.db.current), previous: toStats(grouped.db.previous) },
    functions: {
      current: toStats(grouped.functions.current),
      previous: toStats(grouped.functions.previous),
    },
    auth: { current: toStats(grouped.auth.current), previous: toStats(grouped.auth.previous) },
    storage: {
      current: toStats(grouped.storage.current),
      previous: toStats(grouped.storage.previous),
    },
    realtime: {
      current: toStats(grouped.realtime.current),
      previous: toStats(grouped.realtime.previous),
    },
  }
}