Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,3 @@ files/public/*
#dvc files
dvc
dvc/*

.DS_Store
7 changes: 1 addition & 6 deletions api/models/Resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,7 @@ def version_resource_with_dvc(sender, instance: ResourceFileDetails, created, **
# Create a temporary directory for the previous version
with tempfile.TemporaryDirectory() as temp_dir:
# Get the previous version file path
file_name = instance.file.name
if not file_name:
raise ValueError("File name is missing")
prev_file_name = (
f"prev_version_{instance.resource.id}.{file_name.split('.')[-1]}"
)
prev_file_name = f"prev_version_{instance.resource.id}.{instance.file.name.split('.')[-1]}"
prev_file_path = os.path.join(temp_dir, prev_file_name)

# Use DVC to get the previous version
Expand Down
28 changes: 19 additions & 9 deletions api/schema/access_model_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# mypy: disable-error-code="valid-type"

import uuid
from enum import Enum
from typing import Any, Dict, List, Optional, Union
Expand Down Expand Up @@ -51,7 +49,9 @@ class EditAccessModelInput:
@strawberry.type(name="Query")
class Query:
@strawberry_django.field
def access_model_resources(self, info: Info, dataset_id: uuid.UUID) -> List[TypeAccessModel]:
def access_model_resources(
self, info: Info, dataset_id: uuid.UUID
) -> List[TypeAccessModel]:
models = AccessModel.objects.filter(dataset_id=dataset_id)
return [TypeAccessModel.from_django(model) for model in models]

Expand Down Expand Up @@ -88,12 +88,16 @@ def _add_update_access_model_resources(
try:
dataset_resource = Resource.objects.get(id=resource_input.resource)
except Resource.DoesNotExist as e:
raise ValueError(f"Resource with ID {resource_input.resource} does not exist.")
raise ValueError(
f"Resource with ID {resource_input.resource} does not exist."
)

access_model_resource = AccessModelResource.objects.create(
access_model=access_model, resource=dataset_resource
)
_add_resource_fields(access_model_resource, dataset_resource, resource_input.fields)
_add_resource_fields(
access_model_resource, dataset_resource, resource_input.fields
)


def _update_access_model_fields(
Expand All @@ -118,13 +122,15 @@ def create_access_model(
try:
dataset = Dataset.objects.get(id=access_model_input.dataset)
except Dataset.DoesNotExist:
raise ValueError(f"Dataset with ID {access_model_input.dataset} does not exist.")
raise ValueError(
f"Dataset with ID {access_model_input.dataset} does not exist."
)

access_model = AccessModel.objects.create(
dataset=dataset,
name=access_model_input.name,
description=access_model_input.description,
type=access_model_input.type.value, # type: ignore[attr-defined]
type=access_model_input.type.value,
)

_update_access_model_fields(access_model, access_model_input)
Expand All @@ -139,11 +145,15 @@ def edit_access_model(
try:
dataset = Dataset.objects.get(id=access_model_input.dataset)
except Dataset.DoesNotExist as e:
raise ValueError(f"Dataset with ID {access_model_input.dataset} does not exist.")
raise ValueError(
f"Dataset with ID {access_model_input.dataset} does not exist."
)
access_model = AccessModel.objects.create(dataset=dataset)
else:
try:
access_model = AccessModel.objects.get(id=access_model_input.access_model_id)
access_model = AccessModel.objects.get(
id=access_model_input.access_model_id
)
except AccessModel.DoesNotExist as e:
raise ValueError(
f"Access Model with ID {access_model_input.access_model_id} does not exist."
Expand Down
14 changes: 7 additions & 7 deletions api/schema/aimodel_schema.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""GraphQL schema for AI Model."""

# mypy: disable-error-code="union-attr,misc,valid-type"
# mypy: disable-error-code="union-attr,misc"

import datetime
from typing import Any, Dict, List, Optional
from typing import List, Optional

import strawberry
import strawberry_django
Expand Down Expand Up @@ -419,14 +419,14 @@ def create_ai_model(
description = input.description or ""

# Prepare supported_languages
supported_languages: List[str] = input.supported_languages or []
supported_languages = input.supported_languages or []

# Prepare schemas
input_schema: Any = input.input_schema or {}
output_schema: Any = input.output_schema or {}
input_schema = input.input_schema or {}
output_schema = input.output_schema or {}

# Prepare metadata
metadata: Any = input.metadata or {}
metadata = input.metadata or {}

try:
model = AIModel.objects.create(
Expand Down Expand Up @@ -802,7 +802,7 @@ def create_ai_model_version(
ai_model=model,
version=input.version,
version_notes=input.version_notes or "",
lifecycle_stage=input.lifecycle_stage.value if input.lifecycle_stage else "DEVELOPMENT", # type: ignore[attr-defined]
lifecycle_stage=input.lifecycle_stage.value if input.lifecycle_stage else "DEVELOPMENT", # type: ignore[misc]
supports_streaming=input.supports_streaming,
max_tokens=input.max_tokens,
supported_languages=input.supported_languages or [],
Expand Down
1 change: 0 additions & 1 deletion api/schema/dataset_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# mypy: disable-error-code=union-attr
# mypy: disable-error-code=valid-type
import datetime
import uuid
from typing import Any, List, Optional, Union
Expand Down
22 changes: 15 additions & 7 deletions api/schema/resource_chart_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# mypy: disable-error-code=valid-type

import datetime
import uuid
from typing import Any, Dict, List, Optional
Expand All @@ -21,12 +19,16 @@
@strawberry.type(name="Query")
class Query:
@strawberry_django.field
def charts_details(self, info: Info, dataset_id: uuid.UUID) -> List[TypeResourceChart]:
def charts_details(
self, info: Info, dataset_id: uuid.UUID
) -> List[TypeResourceChart]:
charts = ResourceChartDetails.objects.filter(resource__dataset_id=dataset_id)
return [TypeResourceChart.from_django(chart) for chart in charts]

@strawberry_django.field
def resource_chart(self, info: Info, chart_details_id: uuid.UUID) -> TypeResourceChart:
def resource_chart(
self, info: Info, chart_details_id: uuid.UUID
) -> TypeResourceChart:
chart = ResourceChartDetails.objects.get(id=chart_details_id)
return TypeResourceChart.from_django(chart)

Expand Down Expand Up @@ -186,10 +188,14 @@ def _update_chart_fields(
if value: # Only process if list is not empty
options[field_name] = [
{
"field": ResourceSchema.objects.get(id=column.field_name),
"field": ResourceSchema.objects.get(
id=column.field_name
),
"label": column.label,
"color": column.color,
"value_mapping": _update_value_mapping(column.value_mapping),
"value_mapping": _update_value_mapping(
column.value_mapping
),
}
for column in value
]
Expand Down Expand Up @@ -289,7 +295,9 @@ def create_resource_chart(
)
],
)
def edit_resource_chart(self, info: Info, chart_input: ResourceChartInput) -> TypeResourceChart:
def edit_resource_chart(
self, info: Info, chart_input: ResourceChartInput
) -> TypeResourceChart:
if not chart_input.chart_id:
chart = ResourceChartDetails()
else:
Expand Down
74 changes: 53 additions & 21 deletions api/schema/resource_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from enum import Enum

# mypy: disable-error-code=operator
# mypy: disable-error-code=valid-type
from typing import List, Optional

import strawberry
Expand Down Expand Up @@ -112,7 +111,9 @@ class Query:

@strawberry_django.field
@trace_resolver(name="get_dataset_resources", attributes={"component": "resource"})
def dataset_resources(self, info: Info, dataset_id: uuid.UUID) -> List[TypeResource]:
def dataset_resources(
self, info: Info, dataset_id: uuid.UUID
) -> List[TypeResource]:
"""Get resources for a dataset."""
resources = Resource.objects.filter(dataset_id=dataset_id)
return [TypeResource.from_django(resource) for resource in resources]
Expand Down Expand Up @@ -161,17 +162,23 @@ def _reset_file_resource_schema(resource: Resource) -> None:
data_table = index_resource_data(resource)


def _update_file_resource_schema(resource: Resource, updated_schema: List[SchemaUpdate]) -> None:
def _update_file_resource_schema(
resource: Resource, updated_schema: List[SchemaUpdate]
) -> None:
"""Update file resource schema and re-index if necessary."""
# Check if we need to re-index after schema update
format_changes = False

# Update schema fields
existing_schema: QuerySet[ResourceSchema] = ResourceSchema.objects.filter(resource=resource)
existing_schema: QuerySet[ResourceSchema] = ResourceSchema.objects.filter(
resource=resource
)

for schema in existing_schema: # type: ResourceSchema
try:
schema_change = next(item for item in updated_schema if item.id == str(schema.id))
schema_change = next(
item for item in updated_schema if item.id == str(schema.id)
)
# Check if format is changing, which might require re-indexing
if schema.format != schema_change.format.value:
format_changes = True
Expand All @@ -181,7 +188,9 @@ def _update_file_resource_schema(resource: Resource, updated_schema: List[Schema
schema.format = schema_change.format.value
schema.save()

logger.info(f"Updated schema field {schema.field_name} for resource {resource.id}")
logger.info(
f"Updated schema field {schema.field_name} for resource {resource.id}"
)
except StopIteration:
continue

Expand All @@ -201,8 +210,12 @@ def _update_resource_preview_details(
if file_resource_input.preview_details:
# If preview_details already exists, update it
if preview_details:
preview_details.is_all_entries = file_resource_input.preview_details.is_all_entries
preview_details.start_entry = file_resource_input.preview_details.start_entry
preview_details.is_all_entries = (
file_resource_input.preview_details.is_all_entries
)
preview_details.start_entry = (
file_resource_input.preview_details.start_entry
)
preview_details.end_entry = file_resource_input.preview_details.end_entry
preview_details.save()
# Otherwise, create a new one
Expand Down Expand Up @@ -247,9 +260,9 @@ def create_file_resources(
raise ValueError(f"Dataset with ID {dataset_id} does not exist.")

for file in file_resource_input.files:
resource = Resource.objects.create(name=file.name, dataset=dataset) # type: ignore[attr-defined]
resource = Resource.objects.create(name=file.name, dataset=dataset)
ResourceFileDetails.objects.create(
file=file, size=file.size, resource=resource # type: ignore[attr-defined]
file=file, size=file.size, resource=resource
)
_validate_file_details_and_update_format(resource)
_create_file_resource_schema(resource)
Expand Down Expand Up @@ -292,15 +305,20 @@ def create_file_resource(
"resource_id": str(result.id),
"resource_name": result.name,
"updated_fields": {
"name": (file_resource_input.name if file_resource_input.name else None),
"name": (
file_resource_input.name
if file_resource_input.name
else None
),
"description": (
file_resource_input.description
if file_resource_input.description is not None
else None
),
"preview_enabled": file_resource_input.preview_enabled,
"file_updated": file_resource_input.file is not None,
"preview_details_updated": file_resource_input.preview_details is not None,
"preview_details_updated": file_resource_input.preview_details
is not None,
},
},
)
Expand All @@ -314,7 +332,9 @@ def update_file_resource(
try:
resource = Resource.objects.get(id=file_resource_input.id)
except Resource.DoesNotExist as e:
raise ValueError(f"Resource with ID {file_resource_input.id} does not exist.")
raise ValueError(
f"Resource with ID {file_resource_input.id} does not exist."
)

if file_resource_input.name:
resource.name = file_resource_input.name
Expand All @@ -327,12 +347,12 @@ def update_file_resource(
file_details = getattr(resource, "resourcefiledetails", None)
if file_details:
file_details.file = file_resource_input.file
file_details.size = file_resource_input.file.size # type: ignore[attr-defined]
file_details.size = file_resource_input.file.size
file_details.save()
else:
ResourceFileDetails.objects.create(
file=file_resource_input.file,
size=file_resource_input.file.size, # type: ignore[attr-defined]
size=file_resource_input.file.size,
resource=resource,
)
_validate_file_details_and_update_format(resource)
Expand All @@ -344,22 +364,30 @@ def update_file_resource(
return TypeResource.from_django(resource)

@strawberry_django.mutation(handle_django_errors=True)
@trace_resolver(name="update_file_resource_schema", attributes={"component": "resource"})
@trace_resolver(
name="update_file_resource_schema", attributes={"component": "resource"}
)
def update_file_resource_schema(
self, info: Info, schema_update_input: SchemaUpdateInput
) -> TypeResource:
"""Update file resource schema."""
try:
resource = Resource.objects.get(id=schema_update_input.resource)
except Resource.DoesNotExist as e:
raise ValueError(f"Resource with ID {schema_update_input.resource} does not exist.")
raise ValueError(
f"Resource with ID {schema_update_input.resource} does not exist."
)

_update_file_resource_schema(resource, schema_update_input.updates)
return TypeResource.from_django(resource)

@strawberry_django.mutation(handle_django_errors=True)
@trace_resolver(name="reset_file_resource_schema", attributes={"component": "resource"})
def reset_file_resource_schema(self, info: Info, resource_id: uuid.UUID) -> TypeResource:
@trace_resolver(
name="reset_file_resource_schema", attributes={"component": "resource"}
)
def reset_file_resource_schema(
self, info: Info, resource_id: uuid.UUID
) -> TypeResource:
"""Reset file resource schema."""
try:
resource = Resource.objects.get(id=resource_id)
Expand Down Expand Up @@ -406,7 +434,9 @@ def delete_file_resource(self, info: Info, resource_id: uuid.UUID) -> bool:
],
)
@trace_resolver(name="create_major_version", attributes={"component": "resource"})
def create_major_version(self, info: Info, input: CreateMajorVersionInput) -> TypeResource:
def create_major_version(
self, info: Info, input: CreateMajorVersionInput
) -> TypeResource:
"""Create a major version for a resource.

This should be used when significant changes are made to the resource data structure,
Expand All @@ -432,7 +462,9 @@ def create_major_version(self, info: Info, input: CreateMajorVersionInput) -> Ty
new_version = "v1.0.0"
else:
# Increment major version
new_version = _increment_version(last_version.version_number, increment_type="major")
new_version = _increment_version(
last_version.version_number, increment_type="major"
)

# Initialize DVC manager
dvc = DVCManager(settings.DVC_REPO_PATH)
Expand Down
Loading
Loading