Skip to content

Commit

Permalink
rolling append logic - disabled for now
Browse files Browse the repository at this point in the history
  • Loading branch information
insmac committed Dec 10, 2024
1 parent 4871103 commit eae0eb1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/web-console/src/scenes/Editor/Metrics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
defaultSampleByForDuration,
getRollingAppendRowLimit,
MetricViewMode,
FetchMode,
} from "./utils"
import {
GridAlt,
Expand Down Expand Up @@ -118,6 +119,7 @@ export const Metrics = () => {
const [metrics, setMetrics] = useState<Metric[]>([])
const telemetryConfig = useSelector(selectors.telemetry.getConfig)
const [lastRefresh, setLastRefresh] = useState<number>(new Date().getTime())
const [fetchMode, setFetchMode] = useState<FetchMode>(FetchMode.OVERWRITE)

const tabInFocusRef = React.useRef<boolean>(true)
const refreshRateRef = React.useRef<RefreshRate>()
Expand Down Expand Up @@ -182,6 +184,7 @@ export const Metrics = () => {
const focusListener = useCallback(() => {
tabInFocusRef.current = true
if (refreshRateRef.current !== RefreshRate.OFF) {
setFetchMode(FetchMode.OVERWRITE)
setLastRefresh(new Date().getTime())
}
}, [refreshRateRef.current])
Expand All @@ -195,6 +198,7 @@ export const Metrics = () => {
intervalRef.current = setInterval(
() => {
if (!tabInFocusRef.current) return
setFetchMode(FetchMode.ROLLING_APPEND)
setLastRefresh(new Date().getTime())
},
refreshRateInSec > 0 ? refreshRateInSec * 1000 : 0,
Expand Down Expand Up @@ -241,7 +245,7 @@ export const Metrics = () => {
},
})
updateBuffer(buffer.id, merged)

setFetchMode(FetchMode.OVERWRITE)
setLastRefresh(new Date().getTime())
}
}, [metricDuration, refreshRate, metricViewMode])
Expand Down Expand Up @@ -414,6 +418,8 @@ export const Metrics = () => {
onColorChange={handleColorChange}
onMetricDurationChange={setMetricDuration}
lastRefresh={lastRefresh}
fetchMode={fetchMode}
rollingAppendLimit={rollingAppendLimit}
/>
))}
</Charts>
Expand Down
20 changes: 19 additions & 1 deletion packages/web-console/src/scenes/Editor/Metrics/metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
LastNotNull,
ResultType,
hasData,
FetchMode,
mergeRollingData,
} from "./utils"
import { widgets } from "./widgets"
import { QuestContext } from "../../../providers"
Expand Down Expand Up @@ -44,6 +46,8 @@ export const Metric = ({
onColorChange,
onMetricDurationChange,
lastRefresh,
fetchMode,
rollingAppendLimit,
}: {
metric: MetricItem
metricDuration: MetricDuration
Expand All @@ -52,6 +56,8 @@ export const Metric = ({
onColorChange: (metric: MetricItem, color: string) => void
onMetricDurationChange: (duration: MetricDuration) => void
lastRefresh?: number
fetchMode: FetchMode
rollingAppendLimit: number
}) => {
const { quest } = useContext(QuestContext)
const [loading, setLoading] = useState(false)
Expand All @@ -78,6 +84,9 @@ export const Metric = ({
widgetConfig.getQuery({
tableId: metric.tableId,
metricDuration: metricDuration,
// ...(fetchMode === FetchMode.ROLLING_APPEND && {
// limit: -rollingAppendLimit,
// }),
}),
),
quest.query<LastNotNull>(
Expand All @@ -89,7 +98,16 @@ export const Metric = ({
const alignedData = widgetConfig.alignData(
responses[0].data as unknown as ResultType[MetricType],
)
setData(alignedData)
if (fetchMode === FetchMode.ROLLING_APPEND) {
setData(alignedData)
// console.log(
// metric.metricType,
// mergeRollingData(data, alignedData, rollingAppendLimit),
// )
// setData(mergeRollingData(data, alignedData, rollingAppendLimit))
} else {
setData(alignedData)
}
}

if (responses[1] && responses[1].type === QuestDB.Type.DQL) {
Expand Down
20 changes: 20 additions & 0 deletions packages/web-console/src/scenes/Editor/Metrics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export enum RefreshRate {
OFF = "Off",
}

export enum FetchMode {
ROLLING_APPEND = "Rolling append",
OVERWRITE = "Overwrite",
}

export const refreshRatesInSeconds: Record<RefreshRate, number> = {
[RefreshRate.AUTO]: 0,
[RefreshRate.OFF]: 0,
Expand Down Expand Up @@ -223,3 +228,18 @@ export const hasData = (data?: uPlot.AlignedData) => {
data[1].length > 0 && data[1].some((value) => value !== null && value !== 0)
)
}

export const mergeRollingData = (
oldData: uPlot.AlignedData | undefined,
alignedData: uPlot.AlignedData,
rollingAppendLimit: number,
) => {
const slicedOldData: uPlot.AlignedData = oldData
? oldData.map((d) => d.slice(rollingAppendLimit))
: Array(alignedData.length).fill([])

return alignedData.map((d, i) => [
...slicedOldData[i],
...d,
]) as uPlot.AlignedData
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ where ${tableId ? `tableId = ${tableId} and ` : ""}
event = 105
and ${getTimeFilter(minutes)}
sample by ${sampleBy ?? defaultSampleByForDuration[metricDuration]}
fill(null)
${limit ? `limit ${limit}` : ""}`
},
getQueryLastNotNull: (tableId) => `
Expand Down

0 comments on commit eae0eb1

Please sign in to comment.