Fix issue with timezones

This commit is contained in:
David Corbitt
2023-08-08 18:15:42 -07:00
parent 3424aa36ba
commit 760bfbbe32
4 changed files with 38 additions and 14 deletions

View File

@@ -331,13 +331,19 @@ const loggedCallsToCreate: Prisma.LoggedCallCreateManyInput[] = [];
const loggedCallModelResponsesToCreate: Prisma.LoggedCallModelResponseCreateManyInput[] = []; const loggedCallModelResponsesToCreate: Prisma.LoggedCallModelResponseCreateManyInput[] = [];
const loggedCallsToUpdate: Prisma.LoggedCallUpdateArgs[] = []; const loggedCallsToUpdate: Prisma.LoggedCallUpdateArgs[] = [];
const loggedCallTagsToCreate: Prisma.LoggedCallTagCreateManyInput[] = []; const loggedCallTagsToCreate: Prisma.LoggedCallTagCreateManyInput[] = [];
for (let i = 0; i < 100; i++) { for (let i = 0; i < 437; i++) {
const loggedCallId = uuidv4(); const loggedCallId = uuidv4();
const loggedCallModelResponseId = uuidv4(); const loggedCallModelResponseId = uuidv4();
const template =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
MODEL_RESPONSE_TEMPLATES[Math.floor(Math.random() * MODEL_RESPONSE_TEMPLATES.length)]!;
const model = template.reqPayload.model;
// choose random time in the last two weeks, with a bias towards the last few days // choose random time in the last two weeks, with a bias towards the last few days
const startTime = new Date(Date.now() - Math.random() * 1000 * 60 * 60 * 24 * 14); const startTime = new Date(Date.now() - Math.pow(Math.random(), 2) * 1000 * 60 * 60 * 24 * 14);
// choose random time anywhere from 1 to 25 seconds later // choose random delay anywhere from 2 to 10 seconds later for gpt-4, or 1 to 5 seconds for gpt-3.5
const endTime = new Date(startTime.getTime() + Math.random() * 1000 * 24); const delay =
model === "gpt-4" ? 1000 * 2 + Math.random() * 1000 * 8 : 1000 + Math.random() * 1000 * 4;
const endTime = new Date(startTime.getTime() + delay);
loggedCallsToCreate.push({ loggedCallsToCreate.push({
id: loggedCallId, id: loggedCallId,
cacheHit: false, cacheHit: false,
@@ -345,9 +351,18 @@ for (let i = 0; i < 100; i++) {
organizationId: org.id, organizationId: org.id,
createdAt: startTime, createdAt: startTime,
}); });
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const template = const { promptTokenPrice, completionTokenPrice } =
MODEL_RESPONSE_TEMPLATES[Math.floor(Math.random() * MODEL_RESPONSE_TEMPLATES.length)]!; model === "gpt-4"
? {
promptTokenPrice: 0.00003,
completionTokenPrice: 0.00006,
}
: {
promptTokenPrice: 0.0000015,
completionTokenPrice: 0.000002,
};
loggedCallModelResponsesToCreate.push({ loggedCallModelResponsesToCreate.push({
id: loggedCallModelResponseId, id: loggedCallModelResponseId,
startTime, startTime,
@@ -363,7 +378,8 @@ for (let i = 0; i < 100; i++) {
inputTokens: template.inputTokens, inputTokens: template.inputTokens,
outputTokens: template.outputTokens, outputTokens: template.outputTokens,
finishReason: template.finishReason, finishReason: template.finishReason,
totalCost: template.inputTokens * 0.06 + template.outputTokens * 0.06, totalCost:
template.inputTokens * promptTokenPrice + template.outputTokens * completionTokenPrice,
}); });
loggedCallsToUpdate.push({ loggedCallsToUpdate.push({
where: { where: {

View File

@@ -32,11 +32,15 @@ export const dashboardRouter = createTRPCRouter({
.execute(); .execute();
let originalDataIndex = periods.length - 1; let originalDataIndex = periods.length - 1;
let dayToMatch = dayjs(input.startDate).startOf("day"); // *SLAMS DOWN GLASS OF WHISKEY* timezones, amirite?
let dayToMatch = dayjs(input.startDate || new Date()).add(1, "day");
const backfilledPeriods: typeof periods = []; const backfilledPeriods: typeof periods = [];
// Backfill from now to 14 days ago or the date of the first logged call, whichever is earlier // Backfill from now to 14 days ago or the date of the first logged call, whichever is earlier
while (backfilledPeriods.length < 14 || originalDataIndex >= 0) { while (
backfilledPeriods.length < 14 ||
(periods[0]?.period && !dayToMatch.isBefore(periods[0]?.period, "day"))
) {
const nextOriginalPeriod = periods[originalDataIndex]; const nextOriginalPeriod = periods[originalDataIndex];
if (nextOriginalPeriod && dayjs(nextOriginalPeriod?.period).isSame(dayToMatch, "day")) { if (nextOriginalPeriod && dayjs(nextOriginalPeriod?.period).isSame(dayToMatch, "day")) {
backfilledPeriods.unshift(nextOriginalPeriod); backfilledPeriods.unshift(nextOriginalPeriod);

View File

@@ -1,11 +1,13 @@
import { env } from "~/env.mjs"; import { env } from "~/env.mjs";
import { default as OriginalOpenAI } from "openai"; import { default as OriginalOpenAI } from "openai";
import { OpenAI } from "openpipe"; // import { OpenAI } from "openpipe";
const openAIConfig = { apiKey: env.OPENAI_API_KEY ?? "dummy-key" }; const openAIConfig = { apiKey: env.OPENAI_API_KEY ?? "dummy-key" };
// Set a dummy key so it doesn't fail at build time // Set a dummy key so it doesn't fail at build time
export const openai = env.OPENPIPE_API_KEY // export const openai = env.OPENPIPE_API_KEY
? new OpenAI.OpenAI(openAIConfig) // ? new OpenAI.OpenAI(openAIConfig)
: new OriginalOpenAI(openAIConfig); // : new OriginalOpenAI(openAIConfig);
export const openai = new OriginalOpenAI(openAIConfig);

View File

@@ -1,9 +1,11 @@
import dayjs from "dayjs"; import dayjs from "dayjs";
import duration from "dayjs/plugin/duration"; import duration from "dayjs/plugin/duration";
import relativeTime from "dayjs/plugin/relativeTime"; import relativeTime from "dayjs/plugin/relativeTime";
import timezone from "dayjs/plugin/timezone";
dayjs.extend(duration); dayjs.extend(duration);
dayjs.extend(relativeTime); dayjs.extend(relativeTime);
dayjs.extend(timezone);
export const formatTimePast = (date: Date) => export const formatTimePast = (date: Date) =>
dayjs.duration(dayjs(date).diff(dayjs())).humanize(true); dayjs.duration(dayjs(date).diff(dayjs())).humanize(true);