-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: See the job status with persisted stats
- Loading branch information
Showing
6 changed files
with
361 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,95 @@ | ||
from os import environ as env | ||
|
||
from app.helpers.logging import logger | ||
from azure.core.credentials import AzureKeyCredential | ||
from azure.core.exceptions import ResourceExistsError | ||
from azure.search.documents.aio import SearchClient | ||
from azure.storage.blob.aio import BlobServiceClient, ContainerClient | ||
from azure.storage.queue.aio import QueueClient, QueueServiceClient | ||
from contextlib import asynccontextmanager | ||
from openai import AsyncAzureOpenAI | ||
|
||
from app.helpers.logging import logger | ||
from typing import AsyncGenerator | ||
|
||
|
||
@asynccontextmanager | ||
async def openai_client( | ||
api_key: str, | ||
api_version: str, | ||
endpoint: str, | ||
) -> AsyncAzureOpenAI: | ||
) -> AsyncGenerator[AsyncAzureOpenAI, None]: | ||
""" | ||
Get the Azure OpenAI client. | ||
""" | ||
return AsyncAzureOpenAI( | ||
async with AsyncAzureOpenAI( | ||
# Deployment | ||
api_version=api_version, | ||
azure_endpoint=endpoint, | ||
# Authentication | ||
api_key=api_key, | ||
) | ||
) as client: | ||
yield client | ||
|
||
|
||
@asynccontextmanager | ||
async def search_client( | ||
api_key: str, | ||
endpoint: str, | ||
index: str, | ||
) -> SearchClient: | ||
) -> AsyncGenerator[SearchClient, None]: | ||
""" | ||
Get the Azure AI Search client. | ||
""" | ||
return SearchClient( | ||
async with SearchClient( | ||
# Deployment | ||
endpoint=endpoint, | ||
index_name=index, | ||
# Authentication | ||
credential=AzureKeyCredential(api_key), | ||
) | ||
) as client: | ||
yield client | ||
|
||
|
||
@asynccontextmanager | ||
async def blob_client( | ||
connection_string: str, | ||
container: str, | ||
) -> ContainerClient: | ||
) -> AsyncGenerator[ContainerClient, None]: | ||
""" | ||
Get the Azure Blob Storage client. | ||
""" | ||
client = BlobServiceClient.from_connection_string( | ||
async with BlobServiceClient.from_connection_string( | ||
connection_string | ||
).get_container_client(container) | ||
) as x: | ||
client = x.get_container_client(container) | ||
|
||
# Create if it does not exist | ||
try: | ||
await client.create_container() | ||
logger.info('Created Blob Storage "%s"', container) | ||
except ResourceExistsError: | ||
pass | ||
# Create if it does not exist | ||
try: | ||
await client.create_container() | ||
logger.info('Created Blob Storage "%s"', container) | ||
except ResourceExistsError: | ||
pass | ||
|
||
# Return client | ||
return client | ||
# Return client | ||
yield client | ||
|
||
|
||
@asynccontextmanager | ||
async def queue_client( | ||
connection_string: str, | ||
queue: str, | ||
) -> QueueClient: | ||
) -> AsyncGenerator[QueueClient, None]: | ||
""" | ||
Get the Azure Queue Storage client. | ||
""" | ||
client = QueueServiceClient.from_connection_string( | ||
async with QueueServiceClient.from_connection_string( | ||
connection_string | ||
).get_queue_client(queue) | ||
) as x: | ||
client = x.get_queue_client(queue) | ||
|
||
# Create if it does not exist | ||
try: | ||
await client.create_queue() | ||
logger.info('Created Queue Storage "%s"', queue) | ||
except ResourceExistsError: | ||
pass | ||
# Create if it does not exist | ||
try: | ||
await client.create_queue() | ||
logger.info('Created Queue Storage "%s"', queue) | ||
except ResourceExistsError: | ||
pass | ||
|
||
# Return client | ||
return client | ||
# Return client | ||
yield client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from datetime import datetime, UTC | ||
from pydantic import BaseModel, Field | ||
|
||
|
||
class StateJobModel(BaseModel): | ||
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC)) | ||
last_updated: datetime = Field(default_factory=lambda: datetime.now(UTC)) | ||
processed: int = 0 | ||
queued: int = 0 | ||
|
||
|
||
class StateScrapedModel(BaseModel): | ||
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC)) |
Oops, something went wrong.