Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- CreateTable
CREATE TABLE "YDocSnapshot" (
"id" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"message" TEXT,
"yDocBlob" BYTEA,
"repoId" TEXT,

CONSTRAINT "YDocSnapshot_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "YDocSnapshot" ADD CONSTRAINT "YDocSnapshot_repoId_fkey" FOREIGN KEY ("repoId") REFERENCES "Repo"("id") ON DELETE SET NULL ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "YDocSnapshot" ADD COLUMN "deleted" BOOLEAN NOT NULL DEFAULT false;
12 changes: 12 additions & 0 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ model UserRepoData {
@@id([userId, repoId])
}

model YDocSnapshot {
id String @id
createdAt DateTime @default(now())
message String?
yDocBlob Bytes?
deleted Boolean @default(false)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't want to add deleted here since it adds complexity. We could reply on DB backup if we really want to ensure data safety.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am not so familiar with DB backup, any reference for its pros and cons? I am considering a scenario where users might decide to delete a snapshot, but then want to undo the deletion later. Furthermore, if there are 1,000 users performing these operations at different times, can DB backups gracefully address this scenario?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd skip this scenario for now. If the user deletes it, it is deleted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd skip this scenario for now. If the user deletes it, it is deleted.

OK, let me revert the change.

repo Repo? @relation(fields: [repoId], references: [id])
Copy link
Collaborator

@lihebi lihebi Aug 18, 2023

Choose a reason for hiding this comment

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

yDocBlob and repo/repoId sound to be required, not optional. I.e.,

   yDocBlob Bytes
   repo: Repo
   repoId: String

repoId String?
}

model Repo {
id String @id
name String?
Expand All @@ -83,6 +93,8 @@ model Repo {
UserRepoData UserRepoData[]
stargazers User[] @relation("STAR")
yDocBlob Bytes?
yDocSnapshots YDocSnapshot[]

}

enum PodType {
Expand Down
41 changes: 41 additions & 0 deletions api/src/resolver_repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,50 @@ async function copyRepo(_, { repoId }, { userId }) {
return id;
}

/**
* Create yDoc snapshot upon request.
*/
async function addRepoSnapshot(_, { repoId, message }) {
const repo = await prisma.repo.findFirst({
where: { id: repoId },
include: {
owner: true,
collaborators: true,
},
});
if (!repo) throw Error("Repo not exists.");
if (!repo.yDocBlob) throw Error(`yDocBlob on ${repoId} not found`);
const snapshot = await prisma.yDocSnapshot.create({
data: {
id: await nanoid(),
yDocBlob: repo.yDocBlob,
message: message,
repo: { connect: { id: repoId } },
},
});
return snapshot.id;
}

/**
* Query yDoc snapshot for a repo.
*/
async function getRepoSnapshots(_, { repoId }) {
const snapshots = await prisma.yDocSnapshot.findMany({
where: { repo: { id: repoId }, deleted: false },
});
if (!snapshots) throw Error(`No snapshot exists for repo ${repoId}.`);

return snapshots.map((snapshot) => ({
...snapshot,
yDocBlob: JSON.stringify(snapshot.yDocBlob),
}));
}

export default {
Query: {
repo,
getDashboardRepos,
getRepoSnapshots,
},
Mutation: {
createRepo,
Expand All @@ -531,6 +571,7 @@ export default {
addCollaborator,
updateVisibility,
deleteCollaborator,
addRepoSnapshot,
star,
unstar,
},
Expand Down
8 changes: 8 additions & 0 deletions api/src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export const typeDefs = gql`
ttl: Int
}

type YDocSnapshot {
id: String
createdAt: String
message: String
}

type Query {
hello: String
users: [User]
Expand All @@ -102,6 +108,7 @@ export const typeDefs = gql`
repo(id: String!): Repo
pod(id: ID!): Pod
getDashboardRepos: [Repo]
getRepoSnapshots(repoId: String!): [YDocSnapshot]
activeSessions: [String]
listAllRuntimes: [RuntimeInfo]
infoRuntime(sessionId: String!): RuntimeInfo
Expand Down Expand Up @@ -137,6 +144,7 @@ export const typeDefs = gql`

exportJSON(repoId: String!): String!
exportFile(repoId: String!): String!
addRepoSnapshot(repoId: String!, message: String!): String!
updateCodeiumAPIKey(apiKey: String!): Boolean
}
`;
103 changes: 103 additions & 0 deletions ui/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
import ListItemText from "@mui/material/ListItemText";
import ListItemButton from "@mui/material/ListItemButton";
import AddIcon from "@mui/icons-material/Add";
import ChevronLeftIcon from "@mui/icons-material/ChevronLeft";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
import RestartAltIcon from "@mui/icons-material/RestartAlt";
Expand Down Expand Up @@ -1267,6 +1268,105 @@ function TableofPods() {
);
}

function SnapshotItem({ id, message }) {
const [anchorEl, setAnchorEl] = useState(null);

const open = Boolean(anchorEl);

const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};

const handleClose = () => {
setAnchorEl(null);
};
return (
<ListItem disablePadding key={id}>
<ListItemText primary={message} />
<IconButton aria-label="More options" onClick={handleClick}>
<MoreVertIcon />
</IconButton>
<Popover open={open} anchorEl={anchorEl} onClose={handleClose}>
<MenuList>
<MenuItem>Restore</MenuItem>
<MenuItem>Delete</MenuItem>
</MenuList>
</Popover>
</ListItem>
);
}

function RepoSnapshots() {
const { id: repoId } = useParams();
const { error: queryError, data: queryResult } = useQuery(gql`
query GetRepoSnapshots {
getRepoSnapshots(repoId: "${repoId}") {
id
createdAt
message
}
}
`);
const [addRepoSnapshot, { error: mutationError, data: mutationResult }] =
useMutation(
gql`
mutation addRepoSnapshot($repoId: String!, $message: String!) {
addRepoSnapshot(repoId: $repoId, message: $message)
}
`,
{
refetchQueries: ["GetRepoSnapshots"],
}
);

if (queryError) {
return <Box>ERROR: {queryError.message}</Box>;
}

const snapshots = queryResult?.getRepoSnapshots.slice();
return (
<Box>
<Box
style={{
display: "flex",
flexDirection: "row",
justifyContent: "space-between",
alignItems: "stretch",
}}
>
<Box
style={{
display: "flex",
alignItems: "center",
}}
>
<Typography variant="h6">Snapshots</Typography>
</Box>
<IconButton
onClick={() => {
addRepoSnapshot({
variables: { repoId: repoId, message: "placeholder" },
});
}}
>
<AddIcon />
</IconButton>
</Box>
<List>
{snapshots &&
snapshots.length > 0 &&
snapshots.map((snapshot) => (
<SnapshotItem
key={snapshot.id}
id={snapshot.id}
message={snapshot.createdAt}
/>
))}
</List>
</Box>
);
}

export const Sidebar: React.FC<SidebarProps> = ({
width,
open,
Expand Down Expand Up @@ -1353,6 +1453,9 @@ export const Sidebar: React.FC<SidebarProps> = ({
<Divider />
<Typography variant="h6">Table of Pods</Typography>
<TableofPods />

<Divider />
<RepoSnapshots />
</Stack>
</Box>
</Drawer>
Expand Down