Skip to content

Commit

Permalink
fix: completion log timezone
Browse files Browse the repository at this point in the history
fix #322
  • Loading branch information
moeakwak committed Jan 1, 2024
1 parent 425972f commit efeb4ae
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 35 deletions.
8 changes: 7 additions & 1 deletion backend/api/models/doc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional, Any, Literal, Union, Annotated, Dict

from beanie import Document, TimeSeriesConfig, Granularity
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, field_serializer

from api.enums import OpenaiWebChatModels, OpenaiApiChatModels
from api.models.doc.openai_web_code_interpreter import OpenaiWebChatMessageMetadataAggregateResult, \
Expand Down Expand Up @@ -285,6 +285,12 @@ class AskLogDocument(Document):
queueing_time: Optional[float]
ask_time: Optional[float]

@field_serializer("time")
def serialize_dt(self, time: Optional[datetime.datetime], _info):
if time:
return time.replace(tzinfo=datetime.timezone.utc)
return None

class Settings:
name = "ask_logs"
timeseries = TimeSeriesConfig(
Expand Down
39 changes: 39 additions & 0 deletions frontend/src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { i18n } from '@/i18n';
import { useAppStore } from '@/store';
const appStore = useAppStore();
const t = i18n.global.t as any;

export const shouldFixUTC = (create_time: string) => !create_time.endsWith('Z') && !/[+-]\d\d:?\d\d/.test(create_time);

export const parseTimeString = (time: string) => {
if (shouldFixUTC(time)) {
time += 'Z';
}
const lang = appStore.language;
return new Date(time).toLocaleString(lang == 'zh-CN' ? 'zh-CN' : 'en-US', {
hour12: false,
timeZone: lang == 'zh-CN' ? 'Asia/Shanghai' : 'America/New_York',
});
};

export const parseTimeToRelative = (time: string) => {
if (shouldFixUTC(time)) {
time += 'Z';
}

const diff = (new Date().getTime() - new Date(time).getTime()) / 1000;

if (diff < 60) {
return t('commons.justNow');
} else if (diff < 24 * 60 * 60) {
const minutes = Math.floor(diff / 60);
const hours = Math.floor(minutes / 60);
if (hours > 0) {
return t('commons.hoursMinutesAgo', [hours, minutes % 60]);
} else {
return t('commons.minutesAgo', [minutes]);
}
} else {
return parseTimeString(time);
}
};
3 changes: 2 additions & 1 deletion frontend/src/views/admin/components/CompletionLogContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import { getAllUserApi } from '@/api/user';
import { AskLogDocument, UserReadAdmin } from '@/types/schema';
import { getChatModelNameTrans } from '@/utils/chat';
import { getDateStringSorter } from '@/utils/table';
import { parseTimeString } from '@/utils/time';
import { Message } from '@/utils/tips';
import UserUsageChart from './charts/UserUsageChart.vue';
Expand Down Expand Up @@ -124,7 +125,7 @@ const columns = computed<DataTableColumns<AskLogDocument>>(() => [
NTooltip,
{ trigger: 'hover' },
{
trigger: () => new Date(row.time!).toLocaleString(),
trigger: () => parseTimeString(row.time!),
default: () => row.time,
}
);
Expand Down
36 changes: 3 additions & 33 deletions frontend/src/views/conversation/components/MessageRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ import ChatGPTAvatar from '@/components/ChatGPTAvatar.vue';
import { useAppStore } from '@/store';
import { BaseChatMessage, OpenaiWebChatMessageMetadata } from '@/types/schema';
import { getTextMessageContent, splitMessagesInGroup } from '@/utils/chat';
import { parseTimeString, parseTimeToRelative } from '@/utils/time';
import { Message } from '@/utils/tips';
import { determineMessageType, DisplayItem } from '../utils/message';
Expand Down Expand Up @@ -173,47 +174,16 @@ const lastMessage = computed<BaseChatMessage | null>(() => {
else return props.messages[props.messages.length - 1];
});
const shouldFixUTC = (create_time: string) => !create_time.endsWith('Z') && !/[+-]\d\d:?\d\d/.test(create_time);
const timeString = computed<string>(() => {
if (!lastMessage.value || !lastMessage.value.create_time) return '';
let create_time = lastMessage.value.create_time;
// 如果不以Z结尾,按照UTC时区处理;按Z结尾,或者是+时区的,则不处理
if (shouldFixUTC(create_time)) {
create_time += 'Z';
}
// 根据当前语言是 zhCN 还是 enUS 设置时区
const lang = appStore.language;
// return new Date(create_time).toLocaleString();
return new Date(create_time).toLocaleString(lang == 'zh-CN' ? 'zh-CN' : 'en-US', {
hour12: false,
timeZone: lang == 'zh-CN' ? 'Asia/Shanghai' : 'America/New_York',
});
return parseTimeString(create_time);
});
const relativeTimeString = computed<string>(() => {
if (!lastMessage.value || !lastMessage.value.create_time) return '';
let create_time = lastMessage.value.create_time;
// 如果不以Z结尾,按照UTC时区处理;按Z结尾,或者是+时区的,则不处理
if (shouldFixUTC(create_time)) {
create_time += 'Z';
}
const diff = (new Date().getTime() - new Date(create_time).getTime()) / 1000;
if (diff < 60) {
return t('commons.justNow');
} else if (diff < 24 * 60 * 60) {
const minutes = Math.floor(diff / 60);
const hours = Math.floor(minutes / 60);
if (hours > 0) {
return t('commons.hoursMinutesAgo', [hours, minutes % 60]);
} else {
return t('commons.minutesAgo', [minutes]);
}
} else {
return timeString.value;
}
return parseTimeToRelative(create_time);
});
const messageGroups = computed<BaseChatMessage[][]>(() => {
Expand Down

0 comments on commit efeb4ae

Please sign in to comment.