-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #870 from MolSSI/task_queue_idx
Small task queue fix & migration code location
- Loading branch information
Showing
4 changed files
with
83 additions
and
10 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
30 changes: 30 additions & 0 deletions
30
qcfractal/qcfractal/alembic/versions/2024-12-18-8263992eb6c8_fix_task_queue_sort.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,30 @@ | ||
"""Fix task queue sort | ||
Revision ID: 8263992eb6c8 | ||
Revises: f8a7c273f18a | ||
Create Date: 2024-12-18 11:34:57.186942 | ||
""" | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "8263992eb6c8" | ||
down_revision = "f8a7c273f18a" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index("ix_task_queue_sort", table_name="task_queue") | ||
op.execute( | ||
"CREATE INDEX ix_task_queue_sort ON task_queue (priority DESC, sort_date, id, tag) WHERE available = True;" | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
pass | ||
# ### end Alembic commands ### |
48 changes: 48 additions & 0 deletions
48
qcfractal/qcfractal/alembic/versions/migration_helpers/hashing.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,48 @@ | ||
import base64 | ||
import json | ||
from hashlib import sha256 | ||
from typing import Any | ||
|
||
import numpy as np | ||
|
||
try: | ||
from pydantic.v1 import BaseModel | ||
from pydantic.v1.json import pydantic_encoder | ||
except ImportError: | ||
from pydantic import BaseModel | ||
from pydantic.json import pydantic_encoder | ||
|
||
|
||
class _JSONEncoder_1(json.JSONEncoder): | ||
def default(self, obj: Any) -> Any: | ||
# JSON does not handle byte arrays | ||
# So convert to base64 | ||
if isinstance(obj, bytes): | ||
return {"_bytes_base64_": base64.b64encode(obj).decode("ascii")} | ||
|
||
# Now do anything with pydantic, excluding unset fields | ||
# Also always use aliases when serializing | ||
if isinstance(obj, BaseModel): | ||
return obj.dict(exclude_unset=True, by_alias=True) | ||
|
||
# Let pydantic handle other things | ||
try: | ||
return pydantic_encoder(obj) | ||
except TypeError: | ||
pass | ||
|
||
# Flatten numpy arrays | ||
# This is mostly for Molecule class | ||
# TODO - remove once all data in the database in converted | ||
if isinstance(obj, np.ndarray): | ||
if obj.shape: | ||
return obj.ravel().tolist() | ||
else: | ||
return obj.tolist() | ||
|
||
return json.JSONEncoder.default(self, obj) | ||
|
||
|
||
def hash_dict_1(d): | ||
j = json.dumps(d, ensure_ascii=True, sort_keys=True, cls=_JSONEncoder_1).encode("utf-8") | ||
return sha256(j).hexdigest() |
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