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

Prototype of ML backend #312

Merged
merged 19 commits into from
Nov 24, 2023
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
Empty file added ami/base/admin.py
Empty file.
25 changes: 25 additions & 0 deletions ami/base/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.db import models

import ami.tasks


class BaseModel(models.Model):
""" """

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self) -> str:
"""All django models should have this method."""
if hasattr(self, "name"):
name = getattr(self, "name") or "Untitled"
return name
else:
return f"{self.__class__.__name__} #{self.pk}"

def save_async(self, *args, **kwargs):
"""Save the model in a background task."""
ami.tasks.model_task.delay(self.__class__.__name__, self.pk, "save", *args, **kwargs)

class Meta:
abstract = True
2 changes: 1 addition & 1 deletion ami/main/api/pagination.py → ami/base/pagination.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework.pagination import LimitOffsetPagination

from ami.main.api.permissions import add_collection_level_permissions
from .permissions import add_collection_level_permissions


class LimitOffsetPaginationWithPermissions(LimitOffsetPagination):
Expand Down
File renamed without changes.
39 changes: 39 additions & 0 deletions ami/base/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import typing

import pydantic


class ConfigurableStageParam(pydantic.BaseModel):
"""A configurable parameter of a stage of a pipeline or job."""

name: str
key: str
category: str = "default"
value: typing.Any


class ConfigurableStage(pydantic.BaseModel):
"""A configurable stage of a pipeline or job."""

key: str
name: str
params: list[ConfigurableStageParam] = []


def default_stage() -> ConfigurableStage:
return ConfigurableStage(
key="default",
name="Default Stage",
params=[
ConfigurableStageParam(
name="Placeholder",
key="default",
category="placeholder",
value=0,
)
],
)


def default_stages() -> list[ConfigurableStage]:
return [default_stage()]
49 changes: 49 additions & 0 deletions ami/base/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import typing
import urllib.parse

from rest_framework import serializers
from rest_framework.request import Request
from rest_framework.reverse import reverse

from .permissions import add_object_level_permissions


def reverse_with_params(viewname: str, args=None, kwargs=None, request=None, params: dict = {}, **extra) -> str:
query_string = urllib.parse.urlencode(params)
base_url = reverse(viewname, request=request, args=args, kwargs=kwargs, **extra)
url = urllib.parse.urlunsplit(("", "", base_url, query_string, ""))
return url


def add_format_to_url(url: str, format: typing.Literal["json", "html", "csv"]) -> str:
"""
Add a format suffix to a URL.

This is a workaround for the DRF `format_suffix_patterns` decorator not working
with the `reverse` function.
"""
url_parts = urllib.parse.urlsplit(url)
url_parts = url_parts._replace(path=f"{url_parts.path.rstrip('/')}.{format}")
return urllib.parse.urlunsplit(url_parts)


def get_current_user(request: Request | None):
if request:
return request.user
else:
return None


class DefaultSerializer(serializers.HyperlinkedModelSerializer):
url_field_name = "details"
id = serializers.IntegerField(read_only=True)

def get_permissions(self, instance_data):
request = self.context.get("request")
user = request.user if request else None
return add_object_level_permissions(user, instance_data)

def to_representation(self, instance):
instance_data = super().to_representation(instance)
instance_data = self.get_permissions(instance_data)
return instance_data
25 changes: 25 additions & 0 deletions ami/jobs/migrations/0008_alter_job_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.2 on 2023-11-10 01:33

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
("ml", "0001_initial"),
("jobs", "0007_alter_job_options_alter_job_progress_and_more"),
]

operations = [
migrations.AlterField(
model_name="job",
name="pipeline",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="jobs",
to="ml.pipeline",
),
),
]
16 changes: 16 additions & 0 deletions ami/jobs/migrations/0009_remove_job_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 4.2.2 on 2023-11-10 01:52

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("jobs", "0008_alter_job_pipeline"),
]

operations = [
migrations.RemoveField(
model_name="job",
name="config",
),
]
Loading