Skip to content

Commit

Permalink
fix from/to logic
Browse files Browse the repository at this point in the history
  • Loading branch information
insmac committed Dec 13, 2024
1 parent ae93d16 commit 215c853
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
1 change: 0 additions & 1 deletion packages/web-console/src/scenes/Editor/Metrics/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ type Props = {
export const Graph = ({
dateFrom,
dateTo,
lastRefresh,
tableId,
tableName,
beforeLabel,
Expand Down
14 changes: 12 additions & 2 deletions packages/web-console/src/scenes/Editor/Metrics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const Metrics = () => {
const tabInFocusRef = React.useRef<boolean>(true)
const refreshRateRef = React.useRef<RefreshRate>()
const intervalRef = React.useRef<NodeJS.Timeout>()
const metricDurationRef = React.useRef<MetricDuration>()

const { autoRefreshTables } = useLocalStorage()

Expand Down Expand Up @@ -164,8 +165,12 @@ export const Metrics = () => {
}

const refreshMetricsData = () => {
if (!metricDurationRef?.current) return
const now = new Date()
const dateFrom = subMinutes(now, durationInMinutes[duration])
const dateFrom = subMinutes(
now,
durationInMinutes[metricDurationRef.current],
)
const dateTo = now
eventBus.publish<MetricsRefreshPayload>(EventType.METRICS_REFRESH_DATA, {
dateFrom,
Expand Down Expand Up @@ -246,7 +251,9 @@ export const Metrics = () => {
}
if (metricDuration) {
setMetricDuration(metricDuration)
setSampleBy(defaultSampleByForDuration[metricDuration])
if (!sampleBy) {
setSampleBy(defaultSampleByForDuration[metricDuration])
}
}
if (sampleBy) {
setSampleBy(sampleBy)
Expand Down Expand Up @@ -278,6 +285,9 @@ export const Metrics = () => {
}),
},
})
if (metricDuration) {
metricDurationRef.current = metricDuration
}
if (metricDuration && refreshRate && metricViewMode && sampleBy) {
updateBuffer(buffer.id, merged)
setFetchMode(FetchMode.OVERWRITE)
Expand Down
28 changes: 17 additions & 11 deletions packages/web-console/src/scenes/Editor/Metrics/metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SampleBy,
getTimeFilter,
MetricsRefreshPayload,
metricDurationToDate,
} from "./utils"
import { widgets } from "./widgets"
import { QuestContext } from "../../../providers"
Expand Down Expand Up @@ -72,6 +73,9 @@ export const Metric = ({
const [lastNotNull, setLastNotNull] = useState<number>()
const [colorPickerOpen, setColorPickerOpen] = useState(false)
const [fromTo, setFromTo] = useState<MetricsRefreshPayload>()
const [lastTableId, setLastTableId] = useState<number | undefined>(
metric.tableId,
)

const tables = useSelector(selectors.query.getTables)

Expand All @@ -86,10 +90,14 @@ export const Metric = ({
widgetConfig.querySupportsRollingAppend &&
fetchMode === FetchMode.ROLLING_APPEND

const dateNow = new Date()
const dateFrom = fromTo?.dateFrom
? fromTo.dateFrom
: subMinutes(dateNow, durationInMinutes[metricDuration])
const dateTo = fromTo?.dateTo ?? dateNow

const fetchMetric = async () => {
if (!fromTo) return
const { dateFrom, dateTo } = fromTo
const timeout = setTimeout(() => setLoading(true), 250)
setLoading(true)
try {
const subtracted = subMinutes(dateTo, durationInMinutes[metricDuration])
const timeFilter = getTimeFilter(subtracted, dateTo)
Expand Down Expand Up @@ -133,7 +141,6 @@ export const Metric = ({
} catch (err) {
console.error(err)
} finally {
clearTimeout(timeout)
setLoading(false)
}
}
Expand All @@ -156,7 +163,8 @@ export const Metric = ({
}

useEffect(() => {
if (metric.tableId && fromTo) {
if ((metric.tableId && metric.tableId !== lastTableId) || fromTo) {
setLastTableId(metric.tableId)
fetchMetric()
}
}, [metric.tableId, fromTo])
Expand All @@ -183,20 +191,18 @@ export const Metric = ({
const tableName = tables.find((t) => t.id === metric.tableId)?.table_name

const canZoomToData =
fromTo?.dateTo && tableName && lastNotNull
dateTo && tableName && lastNotNull
? lastNotNull >=
subMinutes(
fromTo.dateTo,
dateTo,
minuteDurations[minuteDurations.length - 1][1],
).getTime()
: false

if (!fromTo) return null

return (
<Graph
dateFrom={fromTo.dateFrom}
dateTo={fromTo.dateTo}
dateFrom={dateFrom}
dateTo={dateTo}
lastRefresh={lastRefresh}
data={metric.tableId && hasData(data) ? data : [[], []]}
canZoomToData={canZoomToData}
Expand Down
7 changes: 6 additions & 1 deletion packages/web-console/src/scenes/Editor/Metrics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import uPlot from "uplot"

export enum MetricType {
COMMIT_RATE = "Commit rate",
WRITE_THROUGHPUT = "Rows applied",
WRITE_THROUGHPUT = "Write throughput",
LATENCY = "Latency",
WRITE_AMPLIFICATION = "Write amplification",
}
Expand Down Expand Up @@ -184,6 +184,11 @@ export const minutesToHours = (durationInMinutes: number) =>
export const minutesToSeconds = (durationInMinutes: number) =>
durationInMinutes * 60

export const metricDurationToDate = (
metricDuration: MetricDuration,
dateNow: Date,
) => subMinutes(dateNow, durationInMinutes[metricDuration])

export const xAxisFormat = {
[MetricDuration.FIVE_MINUTES]: (rawValue: number) =>
utcToLocal(rawValue, "HH:mm:ss"),
Expand Down
22 changes: 12 additions & 10 deletions packages/web-console/src/scenes/Schema/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ const Schema = ({
}
}

const handleAddMetricsBuffer = async () => {
await addBuffer({
metricsViewState: {
metrics: [],
metricDuration: MetricDuration.ONE_HOUR,
refreshRate: RefreshRate.AUTO,
viewMode: MetricViewMode.GRID,
},
})
}

useEffect(() => {
void fetchTables()
void fetchColumns()
Expand Down Expand Up @@ -469,16 +480,7 @@ const Schema = ({
<Button
data-hook="schema-add-metrics-button"
skin="transparent"
onClick={() => {
addBuffer({
metricsViewState: {
metrics: [],
metricDuration: MetricDuration.ONE_HOUR,
refreshRate: RefreshRate.AUTO,
viewMode: MetricViewMode.GRID,
},
})
}}
onClick={handleAddMetricsBuffer}
>
<AddChart size="20px" />
</Button>
Expand Down

0 comments on commit 215c853

Please sign in to comment.