Create index.json

This commit is contained in:
Vladyslav Fedoriuk 2023-08-28 21:28:49 +02:00
parent fc4f709189
commit f608ff0500
4 changed files with 73109 additions and 7 deletions

53
app/create_index.py Normal file
View File

@ -0,0 +1,53 @@
"""Create index.json file from database."""
import json
from pathlib import Path
import aiofiles
import sqlalchemy.orm
import typer
from app.database import Repo
from app.models import RepoDetail
from app.uow import async_session_uow
INDEX_PATH = Path(__file__).parent.parent / "index.json"
async def create_index() -> None:
"""
Create index.json file from database.
Creates an index which is going to be used by the frontend.
:return: None
"""
async with async_session_uow() as session, aiofiles.open(
INDEX_PATH, "w"
) as index_file:
await index_file.write(
json.dumps(
{
"repos": [
RepoDetail.model_validate(repo).model_dump()
async for repo in (
await session.stream_scalars(
sqlalchemy.select(Repo)
.order_by(Repo.id)
.options(sqlalchemy.orm.selectinload(Repo.dependencies))
)
)
],
},
indent=4,
)
)
def main() -> None:
"""Create index.json file from database."""
import asyncio
asyncio.run(create_index())
if __name__ == "__main__":
typer.run(main)

View File

@ -2,13 +2,12 @@
import asyncio
import subprocess
from collections.abc import Sequence
from typing import NewType
import aiofiles.tempfile
import stamina
from app.database import Repo
from app.models import DependencyCreateData
from app.models import DependencyCreateData, RevisionHash
async def run_command(*cmd: str, cwd: str | None = None) -> str:
@ -38,10 +37,6 @@ async def run_command(*cmd: str, cwd: str | None = None) -> str:
return stdout.decode()
# TODO: Organize the types
RevisionHash = NewType("RevisionHash", str)
async def acquire_dependencies_data_for_repository(
repo: Repo,
) -> tuple[RevisionHash, list[DependencyCreateData]]:

View File

@ -1,13 +1,43 @@
"""Module contains the models for the application."""
from typing import NewType
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict, NonNegativeInt
# TODO: Organize the types
RepoId = NewType("RepoId", int)
DependencyId = NewType("DependencyId", int)
RevisionHash = NewType("RevisionHash", str)
class DependencyCreateData(BaseModel):
"""A dependency of a repository."""
name: str
class DependencyDetail(BaseModel):
"""A dependency of a repository."""
model_config = ConfigDict(
from_attributes=True,
)
id: DependencyId
name: str
class RepoDetail(BaseModel):
"""A repository that is being tracked."""
model_config = ConfigDict(
from_attributes=True,
)
id: RepoId
url: str
description: str
stars: NonNegativeInt
source_graph_repo_id: int
dependencies: list[DependencyDetail]
last_checked_revision: RevisionHash | None

73024
index.json Normal file

File diff suppressed because it is too large Load Diff