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

DPLT-1044 Create scheduled Lambda to write lag behind near social #137

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
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
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);
};