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-1034 feat: allow to edit indexer when forking #127

Merged
merged 2 commits into from
Jul 14, 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
52 changes: 17 additions & 35 deletions frontend/src/components/Editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const Editor = ({
debugMode,
isCreateNewIndexer,
indexerNameField,
setAccountId,
} = useContext(IndexerDetailsContext);

const DEBUG_LIST_STORAGE_KEY = `QueryAPI:debugList:${indexerDetails.accountId}#${indexerDetails.indexerName}`
Expand Down Expand Up @@ -90,28 +91,6 @@ const Editor = ({
localStorage.setItem(DEBUG_LIST_STORAGE_KEY, heights);
}, [heights]);

// useEffect(() => {
Copy link
Collaborator

@darunrs darunrs Jul 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commented code seems to be doing something useful, checking for too large block heights. Is this code implemented elsewhere or do we not need to do this check anymore? Also the code on line 24 and 43 may not be needed since they reference things that are only present in the code being removed. Unless they're used by another class somewhere else. Also, given that this code is in FrontEnd, would it be worth it to add unit tests which pass in valid and invalid block heights?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to have a limitation in the number of blocks we would allow to be historically indexed. As a result we showed error messages if the user's set the block height prior to what our limit at that time was. I believe it was an hour or 3,600 blocks.

We have removed this limit now, and this code is not going to be needed anymore.

@gabehamilton thought that the relative block heights that we provided were quite helpful, so we still provide some helpful information regarding setting block height in this modal.
image

// if (selectedOption == "latestBlockHeight") {
// setBlockHeightError(null);
// return;
// }
//
// if (height - blockHeight > BLOCKHEIGHT_LIMIT) {
// setBlockHeightError(
// `Warning: Please enter a valid start block height. At the moment we only support historical indexing of the last ${BLOCKHEIGHT_LIMIT} blocks or ${BLOCKHEIGHT_LIMIT / 3600
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code that is not needed anymore.

// } hrs. Choose a start block height between ${height - BLOCKHEIGHT_LIMIT
// } - ${height}.`
// );
// } else if (blockHeight > height) {
// setBlockHeightError(
// `Warning: Start Block Hieght can not be in the future. Please choose a value between ${height - BLOCKHEIGHT_LIMIT
// } - ${height}.`
// );
// } else {
// setBlockHeightError(null);
// }
// }, [blockHeight, height, selectedOption]);

const checkSQLSchemaFormatting = () => {
try {
let formatted_sql = formatSQL(schema);
Expand All @@ -127,10 +106,21 @@ const Editor = ({
}
};


const forkIndexer = async(indexerName) => {
let code = indexingCode;
setAccountId(currentUserAccountId)
let prevAccountId = indexerDetails.accountId.replaceAll(".", "_");
let newAccountId = currentUserAccountId.replaceAll(".", "_");
let prevIndexerName = indexerDetails.indexerName.replaceAll("-", "_").trim().toLowerCase();
let newIndexerName = indexerName.replaceAll("-", "_").trim().toLowerCase();
code = code.replaceAll(prevAccountId, newAccountId);
code = code.replaceAll(prevIndexerName, newIndexerName);
setIndexingCode(formatIndexingCode(code))
}

const registerFunction = async (indexerName, indexerConfig) => {
let formatted_schema = checkSQLSchemaFormatting();
let isForking = indexerDetails.accountId !== currentUserAccountId;

let innerCode = indexingCode.match(/getBlock\s*\([^)]*\)\s*{([\s\S]*)}/)[1];
indexerName = indexerName.replaceAll(" ", "_");
if (formatted_schema == undefined) {
Expand All @@ -140,14 +130,6 @@ const Editor = ({
);
return;
}

if (isForking) {
let prevAccountName = indexerDetails.accountId.replace(".", "_");
let newAccountName = currentUserAccountId.replace(".", "_");

innerCode = innerCode.replaceAll(prevAccountName, newAccountName);
}

setError(() => undefined);

request("register-function", {
Expand All @@ -170,8 +152,8 @@ const Editor = ({
const handleReload = async () => {
if (isCreateNewIndexer) {
setShowResetCodeModel(false);
setIndexingCode(defaultCode);
setSchema(defaultSchema);
setIndexingCode((formatIndexingCode(indexerDetails.code)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When reloading, if you have forked an indexer, then it will reload the forked indexer rather than the default code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty nice change!

setSchema(formatSQL(indexerDetails.schema))
return;
}

Expand Down Expand Up @@ -331,7 +313,7 @@ const Editor = ({
blockHeightError={blockHeightError}
/>
<ForkIndexerModal
registerFunction={registerFunction}
forkIndexer={forkIndexer}
/>

<div
Expand Down
23 changes: 17 additions & 6 deletions frontend/src/components/Editor/EditorButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const EditorButtons = ({
<Breadcrumb.Item className="flex align-center " href="#">
{accountId}
</Breadcrumb.Item>
{!isCreateNewIndexer && (
{indexerName && (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when you have a forked Indexer, it has a indexerName. We want to show the indexerName as part of the breadcrumb paths.

But when we are creating a new indexer, it is fine not to show any breadcrumbs.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is a Breadcrumb in this case?

Copy link
Contributor Author

@roshaans roshaans Jul 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Those are breadcrumbs. Weird name haha

<Breadcrumb.Item href="#" active style={{ display: "flex" }}>
{indexerName}
</Breadcrumb.Item>
Expand Down Expand Up @@ -169,21 +169,33 @@ const EditorButtons = ({
<Justify style={{ paddingRight: "2px" }} size={24} />
</Button>
</OverlayTrigger>
{currentUserAccountId && (
{(!isUserIndexer && !isCreateNewIndexer) ? (
<OverlayTrigger
placement="bottom"
overlay={<Tooltip>{getActionButtonText()}</Tooltip>}
overlay={<Tooltip>Fork Indexer</Tooltip>}
>
<Button
variant="primary"
className="px-3"
onClick={() => setShowForkIndexerModal(true)}
>
Fork Indexer
</Button>
</OverlayTrigger>
) : (
<OverlayTrigger
placement="bottom"
overlay={<Tooltip>Publish</Tooltip>}
>
<Button
variant="primary"
className="px-3"
onClick={() => setShowPublishModal(true)}
>
{getActionButtonText()}
Publish
</Button>
</OverlayTrigger>
)}

</ButtonGroup>
</Col>
</Row>
Expand All @@ -208,6 +220,5 @@ const EditorButtons = ({
</>
);
};


export default EditorButtons;
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const IndexerConfigOptions = ({ updateConfig }) => {
<InputGroup.Text> Indexer Name </InputGroup.Text>
<Form.Control
type="text"
placeholder="Indexer Name"
placeholder="indexer_name"
aria-label="IndexerName"
value={indexerNameField}
disabled={!isCreateNewIndexer && showPublishModal}
Expand Down
69 changes: 38 additions & 31 deletions frontend/src/components/Modals/ForkIndexerModal.jsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
import React, { useContext, useState } from "react";
import { Button, Modal, Alert } from "react-bootstrap";
import { Button, Modal, Alert, InputGroup, Form } from "react-bootstrap";
import IndexerConfigOptions from "../Form/IndexerConfigOptionsInputGroup";
import { IndexerDetailsContext } from '../../contexts/IndexerDetailsContext';
import { IndexerDetailsContext } from "../../contexts/IndexerDetailsContext";
import { validateContractId } from "../../utils/validators";

export const ForkIndexerModal = ({
registerFunction,
}) => {
export const ForkIndexerModal = ({ registerFunction, forkIndexer }) => {
const {
indexerDetails,
showForkIndexerModal,
setShowForkIndexerModal,
setIsCreateNewIndexer,
setIndexerName,
setIndexerConfig,
isCreateNewIndexer,
} = useContext(IndexerDetailsContext);
const [indexerConfig, setIndexerConfig] = useState({ filter: "social.near", startBlockHeight: null })
const [indexerName, setIndexerName] = useState("")
const [error, setError] = useState(null)
const [indexerName, setIndexerNameField] = useState("");
const [error, setError] = useState(null);

const updateConfig = (indexerName, filter, startBlockHeight, option) => {
let finalStartBlockHeight = option === "latestBlockHeight" ? null : startBlockHeight;
setIndexerConfig({ filter, startBlockHeight: finalStartBlockHeight })
setIndexerName(indexerName)
}
const register = async () => {
const fork = async () => {
if (!indexerName) {
setError("Please provide an Indexer Name")
return
setError("Please provide an Indexer Name");
return;
}

if (indexerName === indexerDetails.indexerName) {
setError("Please provide a different Indexer Name than the orginal Indexer")
return
setError(
"Please provide a different Indexer Name than the orginal Indexer"
);
return;
}


if (!validateContractId(indexerConfig.filter)) {
setError("Please provide a valid contract name")
return
}
setError(null)
registerFunction(indexerName, indexerConfig)
setShowForkIndexerModal(false)
}
setError(null);
setIndexerName(indexerName);
setIsCreateNewIndexer(true);
forkIndexer(indexerName);
setShowForkIndexerModal(false);
};

return (
<Modal
Expand All @@ -52,19 +47,31 @@ export const ForkIndexerModal = ({
<Modal.Title> Enter Indexer Details</Modal.Title>
</Modal.Header>
<Modal.Body>
<IndexerConfigOptions updateConfig={updateConfig} />
<InputGroup size="sm">
<InputGroup.Text> Indexer Name </InputGroup.Text>
<Form.Control
type="text"
placeholder="indexer_name"
aria-label="IndexerName"
value={indexerName}
onChange={(e) => setIndexerNameField(e.target.value.trim().toLowerCase())}
/>
</InputGroup>
{error && (
<Alert className="px-3 mt-3" variant="danger">
{error}
</Alert>
)}
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={() => setShowPublishModal(false)}>
<Button
variant="secondary"
onClick={() => setShowForkIndexerModal(false)}
>
Cancel
</Button>
<Button variant="primary" onClick={() => register()}>
Fork Your Own Indexer
<Button variant="primary" onClick={() => fork()}>
Fork Indexer
</Button>
</Modal.Footer>
</Modal>
Expand Down