Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

status logic tweaks #68

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/services/prometheus/getAtlasStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ type QueryRangeResponse = {
}
}

const baseUrl = `http://localhost:${process.env.PROMETHEUS_HTTP_PORT ?? 9090}/api/v1/query_range?query=histogram_quantile(0.50, sum(rate(pgcr_crawl_summary_lag_bucket[2m])) by (le))&?query=histogram_quantile(0.25, sum(rate(pgcr_crawl_summary_lag_bucket[2m])) by (le))`
const baseUrl = `http://localhost:${process.env.PROMETHEUS_HTTP_PORT ?? 9090}/api/v1/query_range?query=histogram_quantile(0.50, sum(rate(pgcr_crawl_summary_lag_bucket[2m])) by (le))`
const intervalMins = 5
const step = "15s"

export const getAtlasStatus = async (): Promise<
| {
Expand All @@ -28,32 +29,37 @@ export const getAtlasStatus = async (): Promise<
const url = new URL(baseUrl)
url.searchParams.set("start", new Date(start - intervalMins * 60000).toISOString())
url.searchParams.set("end", new Date(start).toISOString())
url.searchParams.set("step", "15s")
url.searchParams.set("step", step)

const response = await fetch(url)
const queryRangeResponse = (await response.json()) as QueryRangeResponse

if (!queryRangeResponse.data?.result?.[0]?.values.length) {
const values = queryRangeResponse.data?.result?.[0]?.values
.map(([timestamp, lag]) => ({
timestamp: timestamp * 1000,
lag: parseFloat(lag)
}))
.filter(({ lag }) => lag > 0)

if (!values?.length) {
return { isCrawling: false, lag: null }
}

const values = queryRangeResponse.data.result[0].values

const mostRecentTimestamp = values[values.length - 1][0]
const skew = start - 1000 * mostRecentTimestamp
const mostRecentTimestamp = values[values.length - 1].timestamp
const skew = start - mostRecentTimestamp

if (skew >= 30000) {
if (skew >= 30_000) {
return { isCrawling: false, lag: null }
}

// Calculate the weighted average of the lag values
const { totalWeight, sum } = values.slice(-6).reduce<{
const { totalWeight, sum } = values.slice(-Math.min(values.length - 1, 6)).reduce<{
totalWeight: number
sum: number
}>(
(acc, [, lag], idx) => ({
(acc, { lag }, idx) => ({
totalWeight: acc.totalWeight + (idx + 1),
sum: acc.sum + Number(lag) * (idx + 1)
sum: acc.sum + lag * (idx + 1)
}),
{
totalWeight: 0,
Expand All @@ -62,7 +68,7 @@ export const getAtlasStatus = async (): Promise<
)
const weightedLag = sum / totalWeight

if (weightedLag < 60) {
if (weightedLag <= 60) {
return {
isCrawling: true,
lag: weightedLag,
Expand All @@ -76,8 +82,8 @@ export const getAtlasStatus = async (): Promise<
const newest = values[values.length - 1]

// Measured in seconds per second
const rateA = (Number(middle[1]) - Number(newest[1])) / (middle[0] - newest[0])
const rateB = (Number(oldest[1]) - Number(middle[1])) / (oldest[0] - middle[0])
const rateA = ((middle.lag - newest.lag) / (middle.timestamp - newest.timestamp)) * 1000
const rateB = ((oldest.lag - middle.lag) / (oldest.timestamp - middle.timestamp)) * 1000

// Calculate the weighted average of the rates, seconds per second
const weightedRate = (2 * rateA + rateB) / 3
Expand Down