From d700a85c28c6520c5e382d637a5af4610c494674 Mon Sep 17 00:00:00 2001 From: Morgan McCauley Date: Thu, 20 Jul 2023 07:12:51 +1200 Subject: [PATCH] DPLT-1044 Create scheduled Lambda to write lag behind near social (#137) --- .../latest-post-metrics-writer.js | 33 ---------- indexer-js-queue-handler/serverless.yml | 4 +- .../social-lag-metrics-writer.js | 63 +++++++++++++++++++ 3 files changed, 65 insertions(+), 35 deletions(-) delete mode 100644 indexer-js-queue-handler/latest-post-metrics-writer.js create mode 100644 indexer-js-queue-handler/social-lag-metrics-writer.js 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); +};