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-1010 testing jira automation #85

Closed
wants to merge 9 commits into from
Closed
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: 20 additions & 13 deletions block-server/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ const S3= new AWS.S3();
const NETWORK = process.env.NETWORK || 'mainnet';

module.exports.block = async (event) => {
try {
// parse request params
const { block_height } = event.pathParameters;
const block = await fetchStreamerMessage(block_height);
return {
statusCode: 200,
body: JSON.stringify(block)
}
} catch (err) {
return {
statusCode: err.statusCode || 400,
body: err.message || JSON.stringify(err.message)
}
let headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
};

try {
// parse request params
const { block_height } = event.pathParameters;
const block = await fetchStreamerMessage(block_height);
return {
statusCode: 200,
headers,
body: JSON.stringify(block)
}
} catch (err) {
return {
statusCode: err.statusCode || 400,
headers,
body: err.message || JSON.stringify(err.message)
}
}
};

const normalizeBlockHeight = function(block_height) {
Expand Down
1 change: 1 addition & 0 deletions block-server/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ functions:
- httpApi:
path: /block/{block_height}
method: get
cors: true

# Define function environment variables here
# environment:
Expand Down
3 changes: 2 additions & 1 deletion frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ WORKDIR ./
COPY package.json yarn.lock ./
RUN npm install


FROM node:16-alpine as builder
# Set build arguments and environment variables
ARG NEXT_PUBLIC_REGISTRY_CONTRACT_ID
ARG NEXT_PUBLIC_HASURA_ENDPOINT
ENV NEXT_PUBLIC_HASURA_ENDPOINT=$NEXT_PUBLIC_HASURA_ENDPOINT
ENV NEXT_PUBLIC_REGISTRY_CONTRACT_ID=$NEXT_PUBLIC_REGISTRY_CONTRACT_ID

FROM node:16-alpine as builder
WORKDIR ./
COPY . .
COPY --from=dependencies ./node_modules ./node_modules
Expand Down
48 changes: 14 additions & 34 deletions frontend/src/components/Editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import { queryIndexerFunctionDetails } from "../../utils/queryIndexerFunction";
import { Alert } from "react-bootstrap";
import primitives from "!!raw-loader!../../../primitives.d.ts";
import { request, useInitialPayload } from "near-social-bridge";
import Indexer from "../../utils/indexerRunner";
import IndexerRunner from "../../utils/indexerRunner";
import { block_details } from "./block_details";
import ResizableLayoutEditor from "./ResizableLayoutEditor";
import { ResetChangesModal } from "../Modals/resetChanges";
import { FileSwitcher } from "./FileSwitcher";
import EditorButtons from "./EditorButtons";
import { PublishModal } from "../Modals/PublishModal";
const BLOCKHEIGHT_LIMIT = 3600;
const BLOCK_FETCHER_API =
"https://70jshyr5cb.execute-api.eu-central-1.amazonaws.com/block/";

const contractRegex = RegExp(
"^(([a-zd]+[-_])*[a-zd]+.)*([a-zd]+[-_])*[a-zd]+$"
Expand All @@ -38,16 +36,17 @@ const Editor = ({
const [originalSQLCode, setOriginalSQLCode] = useState(defaultSchema);
const [originalIndexingCode, setOriginalIndexingCode] = useState(defaultCode);
const [debugMode, setDebugMode] = useState(false);
const [logs, setLogs] = useState([]);
const [heights, setHeights] = useState([]);
const [showPublishModal, setShowPublishModal] = useState(false);
const [debugModeInfoDisabled, setDebugModeInfoDisabled] = useState(false);
const handleLog = (blockHeight, log) => {
console.log(`Block #${blockHeight}: ${log}`);
setLogs((prevLogs) => [...prevLogs, log]);
const handleLog = (blockHeight, log, callback) => {
if(log) console.log(log);
if (callback) {
callback();
}
};

const indexerRunner = new Indexer(handleLog);
const indexerRunner = new IndexerRunner(handleLog);

const [indexingCode, setIndexingCode] = useState(defaultCode);
const [schema, setSchema] = useState(defaultSchema);
Expand Down Expand Up @@ -184,6 +183,9 @@ const Editor = ({
setSelectedOption("specificBlockHeight");
setBlockHeight(data.start_block_height);
}
if(data.filter) {
setContractFilter(data.filter.matching_rule.affected_account_id)
}
} catch (error) {
console.log(error);
setError(() => "An Error occured while trying to format the code.");
Expand Down Expand Up @@ -269,7 +271,6 @@ const Editor = ({
const modifiedEditor = editor.getModifiedEditor();
modifiedEditor.onDidChangeModelContent((_) => {
if (fileName == "indexingLogic.js") {
console.log("mountin");
setIndexingCode(modifiedEditor.getValue());
}
if (fileName == "schema.sql") {
Expand Down Expand Up @@ -300,28 +301,8 @@ const Editor = ({
}
}

async function fetchBlockDetails(blockHeight) {
try {
const response = await fetch(
`${BLOCK_FETCHER_API}${String(blockHeight)}`
);
const block_details = await response.json();
return block_details;
} catch {
console.log(`Error Fetching Block Height details at ${blockHeight}`);
}
}

async function executeIndexerFunction() {
setLogs(() => []);
let innerCode = indexingCode.match(/getBlock\s*\([^)]*\)\s*{([\s\S]*)}/)[1];
// for loop with await
for await (const height of heights) {
const block_details = await fetchBlockDetails(height);
if (block_details) {
await indexerRunner.runFunction(block_details, height, innerCode);
}
}
await indexerRunner.executeIndexerFunction(heights,indexingCode)
}

return (
Expand Down Expand Up @@ -371,12 +352,12 @@ const Editor = ({
handleOptionChange={handleOptionChange}
blockHeight={blockHeight}
setBlockHeight={setBlockHeight}
contractFilter={contractFilter}
handleSetContractFilter={handleSetContractFilter}
isContractFilterValid={isContractFilterValid}
actionButtonText={getActionButtonText()}
submit={submit}
blockHeightError={blockHeightError}
contractFilter={contractFilter}
handleSetContractFilter={handleSetContractFilter}
isContractFilterValid={isContractFilterValid}
/>
<div
className="px-3 pt-3"
Expand Down Expand Up @@ -428,7 +409,6 @@ const Editor = ({
options={options}
handleEditorWillMount={handleEditorWillMount}
handleEditorMount={handleEditorMount}
logs={logs}
/>
</div>
</div>
Expand Down
22 changes: 20 additions & 2 deletions frontend/src/components/Editor/EditorButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const EditorButtons = ({
latestHeight,
isUserIndexer,
handleDeleteIndexer,
contractFilter,
handleSetContractFilter,
isContractFilterValid,
}) => {
const removeHeight = (index) => {
setHeights(heights.filter((_, i) => i !== index));
Expand All @@ -59,7 +62,8 @@ const EditorButtons = ({
}}
>
<Row className="w-100">
<Col style={{ display: "flex", justifyContent: "start" }}>
<Col style={{ display: "flex", justifyContent: "start", flexDirection: "column" }}>

<Breadcrumb className="flex">
<Breadcrumb.Item className="flex align-center " href="#">
{accountId}
Expand All @@ -78,7 +82,21 @@ const EditorButtons = ({
)}
</Breadcrumb.Item>
</Breadcrumb>
</Col>
<InputGroup size="sm" hasValidation={true} style = {{width: "fit-content"}}>
<InputGroup.Text> Contract Filter</InputGroup.Text>
<Form.Control
disabled={!options.create_new_indexer}
value={contractFilter}
onChange={handleSetContractFilter}
type="text"
placeholder="social.near"
required={true}
/>
<Form.Control.Feedback type="invalid">
Please provide a valid contract name.
</Form.Control.Feedback>
</InputGroup>
</Col>
<Col style={{ display: "flex", justifyContent: "center" }}>
{debugMode && (
<BlockPicker
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/Editor/ResizableLayoutEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export default function ResizableLayoutEditor({
options,
handleEditorWillMount,
handleEditorMount,
logs,
}) {
const {
dragBarRef: dragBarRefConsole,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const BlockHeightOptions = ({
type="text"
placeholder="social.near"
required={true}
isValid={isContractFilterValid}
/>
<Form.Control.Feedback type="invalid">
Please provide a valid contract name.
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/Playground/graphiql.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const HASURA_ENDPOINT =
"https://queryapi-hasura-graphql-24ktefolwq-ew.a.run.app/v1/graphql";

const graphQLFetcher = async (graphQLParams, accountId) => {
console.log(HASURA_ENDPOINT, "Hashura Endpoint");
const response = await fetch(HASURA_ENDPOINT, {
method: "post",
credentials: "omit",
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/utils/fetchBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const BLOCK_FETCHER_API =
"https://70jshyr5cb.execute-api.eu-central-1.amazonaws.com/block/";

export async function fetchBlockDetails(blockHeight) {
try {
const response = await fetch(
`${BLOCK_FETCHER_API}${String(blockHeight)}`
);
const block_details = await response.json();
return block_details;
} catch {
console.log(`Error Fetching Block Height details at ${blockHeight}`);
}
}
60 changes: 52 additions & 8 deletions frontend/src/utils/indexerRunner.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import { Block } from "@near-lake/primitives";
import { Buffer } from "buffer";
import {fetchBlockDetails} from "./fetchBlock";

global.Buffer = Buffer;
export default class Indexer {
export default class IndexerRunner {
constructor(handleLog) {
this.handleLog = handleLog;
}

async executeIndexerFunction(heights, indexingCode) {
console.clear()
console.group('%c Welcome! Lets test your indexing logic on some Near Blocks!', 'color: white; background-color: navy; padding: 5px;');
if(heights.length === 0) {
console.warn("No Block Heights Selected")
}
console.log("Note: GraphQL Mutations & Queries will not be executed on your database. They will simply return an empty object. Please keep this in mind as this may cause unintended behavior of your indexer function.")
let innerCode = indexingCode.match(/getBlock\s*\([^)]*\)\s*{([\s\S]*)}/)[1];
// for loop with await
for await (const height of heights) {
console.group(`Block Height #${height}`)
const block_details = await fetchBlockDetails(height);
console.time('Indexing Execution Complete')
if (block_details) {
await this.runFunction(block_details, height, innerCode);
}
console.timeEnd('Indexing Execution Complete')
console.groupEnd()
}
console.groupEnd()
}

async runFunction(streamerMessage, blockHeight, indexerCode) {
const innerCodeWithBlockHelper =
`
Expand All @@ -25,21 +50,40 @@ export default class Indexer {

// Define the custom context object
const context = {
set: async () => {
set: async (key, value) => {
this.handleLog(
blockHeight,
"Context.set() is not supported in debug mode."
"",
() => {
console.group(`Setting Key/Value`);
console.log({key: value});
console.groupEnd();
}
);
return {};
},
graphql: async (query, mutationData) => {
this.handleLog(
blockHeight,
"Context.graphql() is not supported in debug mode."
);
this.handleLog(
blockHeight,
`mutationData: ${JSON.stringify(mutationData)}`
"",
() => {
let operationType, operationName
const match = query.match(/(query|mutation)\s+(\w+)\s*(\(.*?\))?\s*\{([\s\S]*)\}/);
if (match) {
operationType = match[1];
operationName = match[2];
}

console.group(`Executing GraphQL ${operationType}: (${operationName})`);
if (operationType === 'mutation') console.log('%c Mutations in debug mode do not alter the database', 'color: black; background-color: yellow; padding: 5px;');
console.group(`Data passed to ${operationType}`);
console.dir(mutationData);
console.groupEnd();
console.group(`Data returned by ${operationType}`);
console.log({})
console.groupEnd();
console.groupEnd();
}
);
return {};
},
Expand Down
3 changes: 0 additions & 3 deletions indexer-js-queue-handler/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ AWSXRay.captureAWS(AWS);
export const consumer = async (event) => {
const indexer = new Indexer('mainnet', 'eu-central-1');

const results = [];
for (const record of event.Records) {
const jsonBody = JSON.parse(record.body);
const block_height = jsonBody.block_height;
Expand All @@ -19,7 +18,5 @@ export const consumer = async (event) => {
functions[function_name] = function_config;

const mutations = await indexer.runFunctions(block_height, functions, {imperative: true, provision: true});
results.push(...mutations);
}
return results;
};
2 changes: 1 addition & 1 deletion indexer-js-queue-handler/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class Indexer {
simultaneousPromises.push(this.writeLog(function_name, block_height, 'Running function', function_name));

const hasuraRoleName = function_name.split('/')[0].replace(/[.-]/g, '_');
const functionNameWithoutAccount = function_name.split('/')[1];
const functionNameWithoutAccount = function_name.split('/')[1].replace(/[.-]/g, '_');

if (options.provision && !indexerFunction["provisioned"]) {
const schemaName = `${function_name.replace(/[.\/-]/g, '_')}`
Expand Down
6 changes: 3 additions & 3 deletions indexer-js-queue-handler/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ provider:
name: aws
runtime: nodejs16.x
region: eu-central-1
timeout: 60
timeout: 15
environment:
HASURA_ENDPOINT: ${env:HASURA_ENDPOINT}
HASURA_ADMIN_SECRET: ${env:HASURA_ADMIN_SECRET}
Expand All @@ -21,13 +21,13 @@ constructs:
fifo: true
worker:
handler: handler.consumer
timeout: 60 # 6 minutes as lift multiplies this value by 6 (https://github.com/getlift/lift/blob/master/docs/queue.md#retry-delay)
timeout: 15 # 1.5 minutes as lift multiplies this value by 6 (https://github.com/getlift/lift/blob/master/docs/queue.md#retry-delay)
startFromBlock-runner:
type: queue
fifo: true
worker:
handler: handler.consumer
timeout: 60 # 6 minutes as lift multiplies this value by 6 (https://github.com/getlift/lift/blob/master/docs/queue.md#retry-delay)
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:

Expand Down
Loading