Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

polish: added hourly truncation of log timestamps in event chart

TzeYiingd6f1f2a46657
diff --git a/studio/components/interfaces/Settings/Logs/LogEventChart.tsx b/studio/components/interfaces/Settings/Logs/LogEventChart.tsx
index 47259e9e730..88b82ea8fe1 100644
--- a/studio/components/interfaces/Settings/Logs/LogEventChart.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogEventChart.tsx
@@ -35,7 +35,30 @@ const LogEventChart: React.FC<Props> = ({ data, onBarClick }) => {
const useAggregated = (data: LogData[]) => {
const truncateToMinute = (micro: number) => Math.floor(micro / 1000 / 1000 / 60) * 60
+ const truncateToHour = (micro: number) => Math.floor(micro / 1000 / 1000 / 60 / 60) * 60 * 60
+ const getDiffMinute = (currentTimestamp: number, olderTimestamp: number) =>
+ Math.round((currentTimestamp - olderTimestamp) / 1000 / 60)
+ const getDiffHour = (currentTimestamp: number, olderTimestamp: number) =>
+ Math.round((currentTimestamp - olderTimestamp) / 1000 / 60 / 60)
+ const diffMultiplierMinute = (v: number) => v * 60
+ const diffMultiplierHour = (v: number) => v * 60 * 60
return useMemo(() => {
+ const oldest = data[data.length - 1]
+ if (!oldest) return
+ const latest = data[0]
+ const oldestDayjs = dayjs(oldest.timestamp / 1000)
+ const latestDayjs = dayjs(latest.timestamp / 1000)
+ let truncFunc = truncateToMinute
+ let getDiff = getDiffMinute
+ let diffMultiplier = diffMultiplierMinute
+ console.log([oldestDayjs.format(),latestDayjs.format()])
+ console.log(oldestDayjs.diff(latestDayjs, 'day', true))
+ if (Math.abs(oldestDayjs.diff(latestDayjs, 'day', true)) > 0.25) {
+ truncFunc = truncateToHour
+ getDiff = getDiffHour
+ diffMultiplier = diffMultiplierHour
+ }
+
const countMap = data
.map((d) => {
// truncate to per-minute
@@ -55,12 +78,10 @@ const useAggregated = (data: LogData[]) => {
if (!oldestEvent) return []
const currentTimestamp = new Date().getTime()
const oldestTimestampMicro = oldestEvent.timestamp
- const latestTimestamp = truncateToMinute(data[0]['timestamp'])
- const minutesDifference = Math.round(
- (currentTimestamp - oldestTimestampMicro / 1000) / 1000 / 60
- )
- for (const minToAdd of Array.from(Array(minutesDifference).keys())) {
- const tsToCheck = truncateToMinute(oldestTimestampMicro) + minToAdd * 60
+ const latestTimestamp = truncFunc(data[0]['timestamp'])
+ const diff = getDiff(currentTimestamp, oldestTimestampMicro / 1000)
+ for (const toAdd of Array.from(Array(diff).keys())) {
+ const tsToCheck = truncFunc(oldestTimestampMicro) + diffMultiplier(toAdd)
if (!(tsToCheck in countMap) && tsToCheck <= latestTimestamp) {
countMap[tsToCheck] = 0
}