diff --git a/indexer-js-queue-handler/latest-post-metrics-writer.js b/indexer-js-queue-handler/latest-post-metrics-writer.js deleted file mode 100644 index 95f03c8f9..000000000 --- a/indexer-js-queue-handler/latest-post-metrics-writer.js +++ /dev/null @@ -1,33 +0,0 @@ -import fetch from "node-fetch"; -import AWS from "aws-sdk"; - -import Metrics from "./metrics.js"; - -export const handler = async () => { - const metrics = new Metrics("QueryAPI"); - - const response = await fetch("https://api.near.social/index", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - action: "post", - key: "main", - options: { - limit: 1, - order: "desc", - }, - }), - }); - - const body = await response.text(); - - if (response.status !== 200) { - throw new Error(body); - } - - const [{ blockHeight }] = JSON.parse(body); - - await metrics.putBlockHeight("social.near", "posts", blockHeight); -}; diff --git a/indexer-js-queue-handler/serverless.yml b/indexer-js-queue-handler/serverless.yml index a44657e3e..368400cc7 100644 --- a/indexer-js-queue-handler/serverless.yml +++ b/indexer-js-queue-handler/serverless.yml @@ -41,8 +41,8 @@ constructs: timeout: 15 # 1.5 minutes as lift multiplies this value by 6 (https://github.com/getlift/lift/blob/master/docs/queue.md#retry-delay) functions: - latestPostMetricsWriter: - handler: latest-post-metrics-writer.handler + socialLagMetricsWriter: + handler: social-lag-metrics-writer.handler events: - schedule: rate(1 minute) diff --git a/indexer-js-queue-handler/social-lag-metrics-writer.js b/indexer-js-queue-handler/social-lag-metrics-writer.js new file mode 100644 index 000000000..ec9d28f92 --- /dev/null +++ b/indexer-js-queue-handler/social-lag-metrics-writer.js @@ -0,0 +1,63 @@ +import fetch from "node-fetch"; +import AWS from "aws-sdk"; + +import Metrics from "./metrics.js"; + +const fetchJson = async (url, requestBody, requestHeaders) => { + const response = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + ...requestHeaders, + }, + body: JSON.stringify(requestBody), + }); + + const responseBody = await response.json(); + + if (response.status !== 200 || responseBody.errors) { + throw new Error(JSON.stringify(responseBody)); + } + + return responseBody; +}; + +export const handler = async () => { + const metrics = new Metrics("QueryAPI"); + + const [nearSocialResponse, feedIndexerResponse] = await Promise.all([ + fetchJson(`https://api.near.social/index`, { + action: "post", + key: "main", + options: { + limit: 1, + order: "desc", + }, + }), + fetchJson( + `${process.env.HASURA_ENDPOINT}/v1/graphql`, + { + query: `{ + dataplatform_near_social_feed_posts( + limit: 1, + order_by: { block_height: desc } + ) { + block_height + } + }`, + }, + { + ["X-Hasura-Role"]: "dataplatform_near", + } + ), + ]); + + const nearSocialBlockHeight = nearSocialResponse[0].blockHeight; + const feedIndexerBlockHeight = + feedIndexerResponse.data.dataplatform_near_social_feed_posts[0] + .block_height; + + const lag = nearSocialBlockHeight - feedIndexerBlockHeight; + + await metrics.putCustomMetric("dataplatform.near", "social_feed", false, 'SOCIAL_LAG', lag); +};