Skip to content

Commit

Permalink
feat: Improve figuring out dashboard last refresh (#20163)
Browse files Browse the repository at this point in the history
  • Loading branch information
webjunkie authored Feb 7, 2024
1 parent a2ff3f7 commit 43ea549
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
9 changes: 3 additions & 6 deletions frontend/src/scenes/dashboard/DashboardReloadAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import { DASHBOARD_MIN_REFRESH_INTERVAL_MINUTES, dashboardLogic } from 'scenes/d

export const LastRefreshText = (): JSX.Element => {
const { lastRefreshed } = useValues(dashboardLogic)
return (
<span>
Last updated{' '}
<span className="font-medium">{lastRefreshed ? dayjs(lastRefreshed).fromNow() : 'a while ago'}</span>
</span>
)
return <span>Last updated {lastRefreshed ? dayjs(lastRefreshed).fromNow() : 'a while ago'}</span>
}

// in seconds
Expand Down Expand Up @@ -49,6 +44,8 @@ export function DashboardReloadAction(): JSX.Element {
disabledReason={
blockRefresh
? `Dashboards can only be refreshed every ${DASHBOARD_MIN_REFRESH_INTERVAL_MINUTES} minutes.`
: itemsLoading
? 'Refreshing...'
: ''
}
sideAction={{
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/scenes/dashboard/dashboardLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
TileLayout,
} from '~/types'

import { getResponseBytes, sortDates } from '../insights/utils'
import { getResponseBytes, sortDates, sortDayJsDates } from '../insights/utils'
import { teamLogic } from '../teamLogic'
import type { dashboardLogicType } from './dashboardLogicType'

Expand Down Expand Up @@ -670,9 +670,9 @@ export const dashboardLogic = kea<dashboardLogicType>([
return null
}

const oldest = sortDates(insightTiles.map((i) => i.last_refresh))
const candidateShortest = oldest.length > 0 ? dayjs(oldest[0]) : null
return candidateShortest?.isValid() ? candidateShortest : null
const validDates = insightTiles.map((i) => dayjs(i.last_refresh)).filter((date) => date.isValid())
const oldest = sortDayJsDates(validDates)
return oldest.length > 0 ? oldest[0] : null
},
],
blockRefresh: [
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/scenes/insights/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ export function sortDates(dates: Array<string | null>): Array<string | null> {
return dates.sort((a, b) => (dayjs(a).isAfter(dayjs(b)) ? 1 : -1))
}

export function sortDayJsDates(dates: Array<dayjs.Dayjs>): Array<dayjs.Dayjs> {
return dates.sort((a, b) => (a.isAfter(b) ? 1 : -1))
}

// Gets content-length header from a fetch Response
export function getResponseBytes(apiResponse: Response): number {
return parseInt(apiResponse.headers.get('Content-Length') ?? '0')
Expand Down

0 comments on commit 43ea549

Please sign in to comment.