Skip to content

Commit

Permalink
DPLT-1044 Create scheduled Lambda to write lag behind near social (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley committed Jul 19, 2023
1 parent 3691cdf commit d700a85
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 35 deletions.
33 changes: 0 additions & 33 deletions indexer-js-queue-handler/latest-post-metrics-writer.js

This file was deleted.

4 changes: 2 additions & 2 deletions indexer-js-queue-handler/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
63 changes: 63 additions & 0 deletions indexer-js-queue-handler/social-lag-metrics-writer.js
Original file line number Diff line number Diff line change
@@ -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);
};

0 comments on commit d700a85

Please sign in to comment.