Skip to content

Commit

Permalink
Merge pull request #18 from jaypyles/16-create-a-favorites-table
Browse files Browse the repository at this point in the history
feat: add favorites tab in jobs
  • Loading branch information
jaypyles authored Jul 22, 2024
2 parents b4e059a + af898cf commit e2e6018
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 140 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ From the table, users can download an excel sheet of the job's results, along wi
- Download csv containing results
- Rerun jobs
- View status of queued jobs
- Favorite and view favorited jobs

![job_page](https://github.com/jaypyles/www-scrape/blob/master/docs/job_page.png)

Expand Down
8 changes: 8 additions & 0 deletions api/backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
query,
insert,
delete_jobs,
update_job,
)
from api.backend.models import (
DownloadJob,
GetStatistics,
SubmitScrapeJob,
DeleteScrapeJobs,
RetrieveScrapeJobs,
UpdateJobs,
)
from api.backend.auth.auth_router import auth_router
import traceback
Expand Down Expand Up @@ -67,6 +69,12 @@ def read_favicon():
return FileResponse("dist/favicon.ico")


@app.post("/api/update")
async def update(update_jobs: UpdateJobs):
"""Used to update jobs"""
await update_job(update_jobs.ids, update_jobs.field, update_jobs.value)


@app.post("/api/submit-scrape-job")
async def submit_scrape_job(job: SubmitScrapeJob, background_tasks: BackgroundTasks):
LOG.info(f"Recieved job: {job}")
Expand Down
12 changes: 6 additions & 6 deletions api/backend/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ async def query(filter: dict[str, Any]) -> list[dict[str, Any]]:
return results


async def update_job(id: str, field: str, value: Any):
async def update_job(ids: list[str], field: str, value: Any):
collection = get_job_collection()
result = await collection.update_one(
{"id": id},
{"$set": {field: value}},
)
return result.modified_count
for id in ids:
_ = await collection.update_one(
{"id": id},
{"$set": {field: value}},
)


async def delete_jobs(jobs: list[str]):
Expand Down
6 changes: 6 additions & 0 deletions api/backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ class DeleteScrapeJobs(pydantic.BaseModel):

class GetStatistics(pydantic.BaseModel):
user: str


class UpdateJobs(pydantic.BaseModel):
ids: list[str]
field: str
value: Any
Binary file modified docs/job_page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/log_page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/login.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/main_page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
179 changes: 47 additions & 132 deletions src/components/JobTable.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import React, { useState } from "react";
import {
Table,
TableBody,
TableCell,
TableHead,
TableRow,
IconButton,
Box,
Typography,
Accordion,
AccordionSummary,
AccordionDetails,
Checkbox,
Tooltip,
Button,
TextField,
FormControl,
InputLabel,
Expand All @@ -23,19 +13,11 @@ import {
} from "@mui/material";
import DeleteIcon from "@mui/icons-material/Delete";
import SelectAllIcon from "@mui/icons-material/SelectAll";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import DownloadIcon from "@mui/icons-material/Download";
import StarIcon from "@mui/icons-material/Star";
import { useRouter } from "next/router";

interface Job {
id: string;
url: string;
elements: Object[];
result: Object;
time_created: Date;
status: string;
job_options: Object;
}
import { Favorites, JobQueue } from "./jobs";
import { Job } from "../types";

interface JobTableProps {
jobs: Job[];
Expand All @@ -57,6 +39,7 @@ const JobTable: React.FC<JobTableProps> = ({ jobs, fetchJobs }) => {
const [allSelected, setAllSelected] = useState(false);
const [searchQuery, setSearchQuery] = useState<string>("");
const [searchMode, setSearchMode] = useState<string>("url");
const [favoriteView, setFavoriteView] = useState<boolean>(false);

const router = useRouter();

Expand Down Expand Up @@ -139,6 +122,22 @@ const JobTable: React.FC<JobTableProps> = ({ jobs, fetchJobs }) => {
return true;
});

const favoriteJob = async (ids: string[], field: string, value: any) => {
const postBody = {
ids: ids,
field: field,
value: value,
};

await fetch("/api/update", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(postBody),
});

await fetchJobs();
};

return (
<Box
width="100%"
Expand Down Expand Up @@ -191,6 +190,16 @@ const JobTable: React.FC<JobTableProps> = ({ jobs, fetchJobs }) => {
</IconButton>
</span>
</Tooltip>
<Tooltip title="Favorites">
<span>
<IconButton
color={favoriteView ? "warning" : "default"}
onClick={() => setFavoriteView(!favoriteView)}
>
<StarIcon />
</IconButton>
</span>
</Tooltip>
</div>
<div className="flex flex-row space-x-2 w-1/2">
<TextField
Expand Down Expand Up @@ -219,117 +228,23 @@ const JobTable: React.FC<JobTableProps> = ({ jobs, fetchJobs }) => {
</div>
</Box>
<Box sx={{ overflow: "auto" }}>
<Table sx={{ tableLayout: "fixed", width: "100%" }}>
<TableHead>
<TableRow>
<TableCell>Select</TableCell>
<TableCell>Id</TableCell>
<TableCell>Url</TableCell>
<TableCell>Elements</TableCell>
<TableCell>Result</TableCell>
<TableCell>Time Created</TableCell>
<TableCell>Status</TableCell>
<TableCell>Actions</TableCell>
</TableRow>
</TableHead>
<TableBody>
{filteredJobs.map((row, index) => (
<TableRow key={index}>
<TableCell padding="checkbox">
<Checkbox
checked={selectedJobs.has(row.id)}
onChange={() => handleSelectJob(row.id)}
/>
</TableCell>
<TableCell sx={{ maxWidth: 100, overflow: "auto" }}>
<Box sx={{ maxHeight: 100, overflow: "auto" }}>
{row.id}
</Box>
</TableCell>
<TableCell sx={{ maxWidth: 200, overflow: "auto" }}>
<Box sx={{ maxHeight: 100, overflow: "auto" }}>
{row.url}
</Box>
</TableCell>
<TableCell sx={{ maxWidth: 150, overflow: "auto" }}>
<Box sx={{ maxHeight: 100, overflow: "auto" }}>
{JSON.stringify(row.elements)}
</Box>
</TableCell>
<TableCell
sx={{ maxWidth: 150, overflow: "auto", padding: 0 }}
>
<Accordion sx={{ margin: 0, padding: 0.5 }}>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-controls="panel1a-content"
id="panel1a-header"
sx={{
minHeight: 0,
"&.Mui-expanded": { minHeight: 0 },
}}
>
<Box
sx={{
maxHeight: 150,
overflow: "auto",
width: "100%",
}}
>
<Typography sx={{ fontSize: "0.875rem" }}>
Show Result
</Typography>
</Box>
</AccordionSummary>
<AccordionDetails sx={{ padding: 1 }}>
<Box sx={{ maxHeight: 200, overflow: "auto" }}>
<Typography
sx={{
fontSize: "0.875rem",
whiteSpace: "pre-wrap",
}}
>
{JSON.stringify(row.result, null, 2)}
</Typography>
</Box>
</AccordionDetails>
</Accordion>
</TableCell>
<TableCell sx={{ maxWidth: 150, overflow: "auto" }}>
<Box sx={{ maxHeight: 100, overflow: "auto" }}>
{new Date(row.time_created).toLocaleString()}
</Box>
</TableCell>
<TableCell sx={{ maxWidth: 150, overflow: "auto" }}>
<Box sx={{ maxHeight: 100, overflow: "auto" }}>
<Box
className="rounded-md p-2 text-center"
sx={{ bgcolor: COLOR_MAP[row.status], opactity: "50%" }}
>
{row.status}
</Box>
</Box>
</TableCell>
<TableCell sx={{ maxWidth: 100, overflow: "auto" }}>
<Button
onClick={() => {
handleDownload([row.id]);
}}
>
Download
</Button>
<Button
onClick={() =>
handleNavigate(row.elements, row.url, row.job_options)
}
>
Rerun
</Button>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
{!favoriteView ? (
<JobQueue
stateProps={{ selectedJobs, filteredJobs }}
colors={COLOR_MAP}
onDownload={handleDownload}
onNavigate={handleNavigate}
onSelectJob={handleSelectJob}
onFavorite={favoriteJob}
></JobQueue>
) : (
<Favorites
stateProps={{ selectedJobs, filteredJobs }}
onNavigate={handleNavigate}
onSelectJob={handleSelectJob}
onFavorite={favoriteJob}
></Favorites>
)}
</Box>
</Box>
</Box>
Expand Down
102 changes: 102 additions & 0 deletions src/components/jobs/Favorites.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import React from "react";
import {
Tooltip,
IconButton,
Table,
TableBody,
TableCell,
TableHead,
TableRow,
Box,
Checkbox,
Button,
} from "@mui/material";
import { Job } from "../../types";
import StarIcon from "@mui/icons-material/Star";

interface stateProps {
selectedJobs: Set<string>;
filteredJobs: Job[];
}

interface Props {
onSelectJob: (job: string) => void;
onNavigate: (elements: Object[], url: string, options: any) => void;
onFavorite: (ids: string[], field: string, value: any) => void;
stateProps: stateProps;
}

export const Favorites = ({
stateProps,
onSelectJob,
onNavigate,
onFavorite,
}: Props) => {
const { selectedJobs, filteredJobs } = stateProps;
const favoritedJobs = filteredJobs.filter((job) => job.favorite);

return (
<Table sx={{ tableLayout: "fixed", width: "100%" }}>
<TableHead>
<TableRow>
<TableCell>Select</TableCell>
<TableCell>Id</TableCell>
<TableCell>Url</TableCell>
<TableCell>Elements</TableCell>
<TableCell>Time Created</TableCell>
<TableCell>Actions</TableCell>
</TableRow>
</TableHead>
<TableBody>
{favoritedJobs.map((row, index) => (
<TableRow key={index}>
<TableCell padding="checkbox">
<Checkbox
checked={selectedJobs.has(row.id)}
onChange={() => onSelectJob(row.id)}
/>
<Tooltip title="Favorite Job">
<span>
<IconButton
color={row.favorite ? "warning" : "default"}
onClick={() => {
onFavorite([row.id], "favorite", !row.favorite);
row.favorite = !row.favorite;
}}
>
<StarIcon />
</IconButton>
</span>
</Tooltip>
</TableCell>
<TableCell sx={{ maxWidth: 100, overflow: "auto" }}>
<Box sx={{ maxHeight: 100, overflow: "auto" }}>{row.id}</Box>
</TableCell>
<TableCell sx={{ maxWidth: 200, overflow: "auto" }}>
<Box sx={{ maxHeight: 100, overflow: "auto" }}>{row.url}</Box>
</TableCell>
<TableCell sx={{ maxWidth: 150, overflow: "auto" }}>
<Box sx={{ maxHeight: 100, overflow: "auto" }}>
{JSON.stringify(row.elements)}
</Box>
</TableCell>
<TableCell sx={{ maxWidth: 150, overflow: "auto" }}>
<Box sx={{ maxHeight: 100, overflow: "auto" }}>
{new Date(row.time_created).toLocaleString()}
</Box>
</TableCell>
<TableCell sx={{ maxWidth: 100, overflow: "auto" }}>
<Button
onClick={() =>
onNavigate(row.elements, row.url, row.job_options)
}
>
Run
</Button>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
};
Loading

0 comments on commit e2e6018

Please sign in to comment.