-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8f1a28
commit eafc802
Showing
35 changed files
with
182 additions
and
207 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
87 changes: 36 additions & 51 deletions
87
lib/workload/stateless/stacks/workflow-manager/workflow_manager/fields.py
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,70 +1,55 @@ | ||
import hashlib | ||
import ulid | ||
|
||
from django.db import models | ||
from django.core.validators import RegexValidator | ||
from django.db import models | ||
|
||
orcabus_id_validator = RegexValidator( | ||
regex=r'[\w]{26}$', | ||
message='ULID is expected to be 26 characters long', | ||
code='invalid_orcabus_id' | ||
) | ||
|
||
ULID_REGEX_STR = r"[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}" | ||
ulid_validator = RegexValidator(regex=ULID_REGEX_STR, | ||
message='ULID is expected to be 26 characters long', | ||
code='invalid_orcabus_id') | ||
|
||
class OrcabusIdField(models.CharField): | ||
description = "An OrcaBus internal ID (ULID)" | ||
|
||
def __init__(self, prefix, *args, **kwargs): | ||
kwargs["max_length"] = 26 # ULID length | ||
kwargs['unique'] = True | ||
kwargs['editable'] = False | ||
kwargs['blank'] = False | ||
kwargs['null'] = False | ||
kwargs['default'] = ulid.new | ||
kwargs['validators'] = [orcabus_id_validator] | ||
super().__init__(*args, **kwargs) | ||
def get_ulid() -> str: | ||
return ulid.new().str | ||
|
||
|
||
class HashField(models.CharField): | ||
description = ( | ||
"HashField is related to some base fields (other columns) in a model and" | ||
"stores its hashed value for better indexing performance." | ||
) | ||
class UlidField(models.CharField): | ||
description = "An OrcaBus internal ID (ULID)" | ||
|
||
def __init__(self, base_fields, *args, **kwargs): | ||
""" | ||
:param base_fields: name of fields storing the value to be hashed | ||
""" | ||
self.base_fields = base_fields | ||
kwargs["max_length"] = 64 | ||
super(HashField, self).__init__(*args, **kwargs) | ||
def __init__(self, *args, **kwargs): | ||
kwargs['max_length'] = 26 # ULID length | ||
kwargs['validators'] = [ulid_validator] | ||
kwargs['default'] = get_ulid | ||
super().__init__(*args, **kwargs) | ||
|
||
def deconstruct(self): | ||
name, path, args, kwargs = super().deconstruct() | ||
del kwargs["max_length"] | ||
if self.base_fields is not None: | ||
kwargs["base_fields"] = self.base_fields | ||
del kwargs['validators'] | ||
del kwargs['default'] | ||
return name, path, args, kwargs | ||
|
||
def pre_save(self, instance, add): | ||
self.calculate_hash(instance) | ||
return super(HashField, self).pre_save(instance, add) | ||
|
||
def calculate_hash(self, instance): | ||
sha256 = hashlib.sha256() | ||
for field in self.base_fields: | ||
value = getattr(instance, field) | ||
sha256.update(value.encode("utf-8")) | ||
setattr(instance, self.attname, sha256.hexdigest()) | ||
class OrcaBusIdField(UlidField): | ||
description = "An OrcaBus internal ID (based on ULID)" | ||
|
||
def __init__(self, prefix='', *args, **kwargs): | ||
self.prefix = prefix | ||
super().__init__(*args, **kwargs) | ||
|
||
@property | ||
def non_db_attrs(self): | ||
return super().non_db_attrs + ("prefix",) | ||
|
||
class HashFieldHelper(object): | ||
def __init__(self): | ||
self.__sha256 = hashlib.sha256() | ||
def from_db_value(self, value, expression, connection): | ||
if value and self.prefix != '': | ||
return f"{self.prefix}.{value}" | ||
else: | ||
return value | ||
|
||
def add(self, value): | ||
self.__sha256.update(value.encode("utf-8")) | ||
return self | ||
def to_python(self, value): | ||
# This will be called when the function | ||
return self.get_prep_value(value) | ||
|
||
def calculate_hash(self): | ||
return self.__sha256.hexdigest() | ||
def get_prep_value(self, value): | ||
# We just want the last 26 characters which is the ULID (ignoring any prefix) when dealing with the database | ||
return value[-26:] |
64 changes: 64 additions & 0 deletions
64
...s/workflow-manager/workflow_manager/migrations/0003_alter_analysis_orcabus_id_and_more.py
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,64 @@ | ||
# Generated by Django 5.1.2 on 2024-12-17 08:04 | ||
|
||
import workflow_manager.fields | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('workflow_manager', '0002_workflowruncomment'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='analysis', | ||
name='orcabus_id', | ||
field=workflow_manager.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='analysiscontext', | ||
name='orcabus_id', | ||
field=workflow_manager.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='analysisrun', | ||
name='orcabus_id', | ||
field=workflow_manager.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='library', | ||
name='orcabus_id', | ||
field=workflow_manager.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='libraryassociation', | ||
name='orcabus_id', | ||
field=workflow_manager.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='payload', | ||
name='orcabus_id', | ||
field=workflow_manager.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='state', | ||
name='orcabus_id', | ||
field=workflow_manager.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='workflow', | ||
name='orcabus_id', | ||
field=workflow_manager.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='workflowrun', | ||
name='orcabus_id', | ||
field=workflow_manager.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='workflowruncomment', | ||
name='orcabus_id', | ||
field=workflow_manager.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
] |
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
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
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
Oops, something went wrong.