Skip to content

Commit

Permalink
Set fetch mode in event payload
Browse files Browse the repository at this point in the history
  • Loading branch information
insmac committed Jan 9, 2025
1 parent ed5fba8 commit 358ebe1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
15 changes: 8 additions & 7 deletions packages/web-console/src/scenes/Editor/Metrics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export const Metrics = () => {
const [dialogOpen, setDialogOpen] = useState(false)
const [metrics, setMetrics] = useState<Metric[]>([])
const telemetryConfig = useSelector(selectors.telemetry.getConfig)
const [fetchMode, setFetchMode] = useState<FetchMode>(FetchMode.OVERWRITE)

const tabInFocusRef = React.useRef<boolean>(true)
const refreshRateRef = React.useRef<RefreshRate>()
const intervalRef = React.useRef<NodeJS.Timeout>()
const fetchModeRef = React.useRef<FetchMode>(FetchMode.OVERWRITE)

const { autoRefreshTables } = useLocalStorage()

Expand Down Expand Up @@ -136,6 +136,7 @@ export const Metrics = () => {
eventBus.publish<MetricsRefreshPayload>(EventType.METRICS_REFRESH_DATA, {
dateFrom,
dateTo,
overwrite: fetchModeRef.current === FetchMode.OVERWRITE,
})
}

Expand Down Expand Up @@ -170,20 +171,20 @@ export const Metrics = () => {
}

const handleDateFromToChange = (dateFrom: string, dateTo: string) => {
setFetchMode(FetchMode.OVERWRITE)
fetchModeRef.current = FetchMode.OVERWRITE
setDateFrom(dateFrom)
setDateTo(dateTo)
}

const handleFullRefresh = () => {
setFetchMode(FetchMode.OVERWRITE)
fetchModeRef.current = FetchMode.OVERWRITE
refreshMetricsData()
}

const focusListener = useCallback(() => {
tabInFocusRef.current = true
if (refreshRateRef.current !== RefreshRate.OFF) {
setFetchMode(FetchMode.OVERWRITE)
fetchModeRef.current = FetchMode.OVERWRITE
refreshMetricsData()
}
}, [refreshRateRef.current])
Expand All @@ -200,7 +201,7 @@ export const Metrics = () => {
intervalRef.current = setInterval(
() => {
if (!tabInFocusRef.current) return
setFetchMode(FetchMode.ROLLING_APPEND)
fetchModeRef.current = FetchMode.ROLLING_APPEND
refreshMetricsData()
},
refreshRateInSec > 0 ? refreshRateInSec * 1000 : 0,
Expand Down Expand Up @@ -261,7 +262,7 @@ export const Metrics = () => {
}
if (dateFrom && dateTo && refreshRate && metricViewMode) {
updateBuffer(buffer.id, merged)
setFetchMode(FetchMode.OVERWRITE)
fetchModeRef.current = FetchMode.OVERWRITE
refreshMetricsData()
}
}
Expand Down Expand Up @@ -417,7 +418,7 @@ export const Metrics = () => {
onRemove={handleRemoveMetric}
onTableChange={handleTableChange}
onColorChange={handleColorChange}
fetchMode={fetchMode}
fetchMode={fetchModeRef.current}
rollingAppendLimit={rollingAppendLimit}
/>
))}
Expand Down
10 changes: 7 additions & 3 deletions packages/web-console/src/scenes/Editor/Metrics/metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getTimeFilter,
getSamplingRateForPeriod,
durationTokenToDate,
MetricsRefreshPayload,
} from "./utils"
import { widgets } from "./widgets"
import { QuestContext } from "../../../providers"
Expand Down Expand Up @@ -138,8 +139,8 @@ export const Metric = ({
}
}

const refreshMetricsData = () => {
fetchMetric()
const refreshMetricsData = (payload?: MetricsRefreshPayload) => {
fetchMetric(payload?.overwrite)
}

useEffect(() => {
Expand All @@ -152,7 +153,10 @@ export const Metric = ({
}, [metric.tableId])

useEffect(() => {
eventBus.subscribe(EventType.METRICS_REFRESH_DATA, refreshMetricsData)
eventBus.subscribe<MetricsRefreshPayload>(
EventType.METRICS_REFRESH_DATA,
refreshMetricsData,
)

return () => {
eventBus.unsubscribe(EventType.METRICS_REFRESH_DATA, refreshMetricsData)
Expand Down
1 change: 1 addition & 0 deletions packages/web-console/src/scenes/Editor/Metrics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type Widget = {
export type MetricsRefreshPayload = {
dateFrom: string
dateTo: string
overwrite?: boolean
}

export type Duration = {
Expand Down

0 comments on commit 358ebe1

Please sign in to comment.