Skip to content

Commit

Permalink
fix: integer too large error (#569)
Browse files Browse the repository at this point in the history
Co-authored-by: hughcrt <[email protected]>
  • Loading branch information
7HR4IZ3 and hughcrt committed Sep 20, 2024
1 parent c035de5 commit 7d314e3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/backend/src/api/v1/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,9 +818,9 @@ analytics.get(
const topModels = await sql`
select
name,
coalesce(sum(prompt_tokens), 0)::int as prompt_tokens,
coalesce(sum(completion_tokens), 0)::int as completion_tokens,
coalesce(sum(prompt_tokens + completion_tokens), 0)::int as total_tokens,
coalesce(sum(prompt_tokens), 0)::bigint as prompt_tokens,
coalesce(sum(completion_tokens), 0)::bigint as completion_tokens,
coalesce(sum(prompt_tokens + completion_tokens), 0)::bigint as total_tokens,
coalesce(sum(cost), 0)::float as cost
from
run r
Expand Down
17 changes: 17 additions & 0 deletions packages/backend/src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ const sql = postgres(process.env.DATABASE_URL!, {
...postgres.camel,
undefined: null,
},
types: {
bigint: {
...postgres.BigInt,

// Convert Postgres BIGINT to JS Number
// Postgres BIGINT range: -9_223_372_036_854_775_808 to 9_223_372_036_854_775_807
// JS Number.MAX_SAFE_INTEGER: 9_007_199_254_740_991
// Values outside JS safe range will be capped at +/- Infinity, because above Number.MAX_SAFE_INTEGER there are rounding approximations
parse: (x: string) => {
const number = Number(x);
if (number > Number.MAX_SAFE_INTEGER) {
return Infinity;
}
return Number(x);
},
},
},
max: isProduction ? 50 : 5,
connection: {
application_name: `backend-${isProduction ? "production" : "development"}-${new Date().getTime()}`,
Expand Down
5 changes: 3 additions & 2 deletions packages/frontend/components/analytics/BarList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ type BarListProps = {
// A table of progress bars, with the progress value the proportion relative to the total
// and the second column the value of the bar
function BarList({ data, columns, filterZero = true }: BarListProps) {
// Moved to avoid unnecessary computations
if (!data) return <>No data.</>;

const dataColumns = columns.filter((col) => !col.bar && col.key);
const main = dataColumns.find((col) => col.main) || dataColumns[0];
const mainTotal = data?.reduce((acc, item) => acc + (item[main.key] || 0), 0);
const scheme = useComputedColorScheme();

if (!data) return <>No data.</>;

return (
<>
<Table
Expand Down

0 comments on commit 7d314e3

Please sign in to comment.