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

437 implement api to manage indexing queues #444

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
21 changes: 20 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
BackendStatusDatasetsSchema,
AgentSchema,
ServerSchema,
FilePathInputSchema,
)


Expand Down Expand Up @@ -174,6 +175,8 @@ def __call__(self, user: User = Depends(current_user)):
can_manage_queries = RoleChecker([UserRole.can_manage_queries])
can_list_queries = RoleChecker([UserRole.can_list_queries])
can_download_files = RoleChecker([UserRole.can_download_files])
can_manage_queues = RoleChecker([UserRole.can_manage_queues])
can_view_queues = RoleChecker([UserRole.can_view_queues])


def get_user_roles(user: User) -> List[UserRole]:
Expand All @@ -196,12 +199,14 @@ def expand_role(role: UserRole) -> List[UserRole]:
UserRole.user,
UserRole.can_list_all_queries,
UserRole.can_manage_all_queries,
UserRole.can_manage_queues,
],
UserRole.user: [
UserRole.can_view_queries,
UserRole.can_manage_queries,
UserRole.can_list_queries,
UserRole.can_download_files,
UserRole.can_view_queues,
],
UserRole.can_manage_all_queries: [UserRole.can_manage_queries],
UserRole.can_list_all_queries: [UserRole.can_list_queries],
Expand Down Expand Up @@ -551,7 +556,7 @@ def job_statuses(user: User = Depends(current_user)) -> JobsSchema:
@app.delete(
"/api/query/{job_id}",
response_model=StatusSchema,
dependencies=[Depends(can_manage_queries)],
Copy link
Member

Choose a reason for hiding this comment

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

accidental change?

dependencies=[Depends(can_manage_queues)],
)
def query_remove(
job_id: str, user: User = Depends(current_user)
Expand Down Expand Up @@ -589,6 +594,20 @@ def serve_index(path: str) -> FileResponse:
return FileResponse(Path(__file__).parent / "mqueryfront/dist/index.html")


@app.post(
"/api/queue/{ursadb_id}",
response_model=StatusSchema,
tags=["queue"],
dependencies=[Depends(can_manage_queues)],
)
def list_of_paths_to_index(
ursadb_id: str, file_paths: List[FilePathInputSchema] = Body(...)
):
db.set_queued_file(ursadb_id, file_paths)

return StatusSchema(status="ok")


@app.get("/recent", include_in_schema=False)
@app.get("/status", include_in_schema=False)
@app.get("/query", include_in_schema=False)
Expand Down
17 changes: 17 additions & 0 deletions src/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .models.job import Job, JobStatus
from .models.jobagent import JobAgent
from .models.match import Match
from .models.queuedfile import QueuedFile
from .schema import MatchesSchema, ConfigSchema
from .config import app_config

Expand Down Expand Up @@ -56,6 +57,8 @@ class UserRole(Enum):
can_list_queries = auto()
can_view_queries = auto()
can_download_files = auto()
can_view_queues = auto()
can_manage_queues = auto()


# Type alias for Job ids
Expand Down Expand Up @@ -474,3 +477,17 @@ def alembic_upgrade(self) -> None:
config_file = Path(__file__).parent / "alembic.ini"
alembic_cfg = Config(str(config_file))
command.upgrade(alembic_cfg, "head")

def set_queued_file(self, ursadb_id, file_paths):
Copy link
Member

Choose a reason for hiding this comment

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

add_queued_file maybe, or enqueue_file

Copy link
Member

Choose a reason for hiding this comment

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

(also ursadb: str, file_paths: list[str] guessing by variable names, but I didn't check the types)

Copy link
Member

Choose a reason for hiding this comment

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

or List[FilePathInputSchema] actually I guess

with self.session() as session:
session.bulk_insert_mappings(
QueuedFile, [
{
"ursadb_id": ursadb_id,
"path": file.path,
"index_types": file.index_types,
"tags": file.tags
} for file in file_paths
]
)
session.commit()
6 changes: 6 additions & 0 deletions src/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,9 @@ class ServerSchema(BaseModel):
openid_url: Optional[str]
openid_client_id: Optional[str]
about: str


class FilePathInputSchema(BaseModel):
path: str
index_types: List[str]
Copy link
Member

Choose a reason for hiding this comment

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

We should validate the index_types somehow. I thought there's an enum for that, but apparently not. I think the easiest solution is to add a validator checking that every element is in ["gram3", "text4", "hash4", "wide8"]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can i use List[Literal["gram3", "text4", "hash4", "wide8"]] from typing?

tags: List[str]
Loading