-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
count_submitted_responses
as property to Records
databa…
…se model (#5118) # Description This PR include the following changes: * Added `count_submitted_responses` as a property of `Record` database model. * This property requires record responses to be pre-loaded. * This value is get from the database using a subquery. * Added `count_submitted_responses` to search engine mapping. * Record `status` is exposed by API schemas and is calculated based in `count_submitted_responses` column property from `Record` database model. * This `status` is defined as a property inside `Record` database model and it's using the `dataset` distribution strategy to calculate the value. ## Missing changes in this PR - [ ] Make test suite to pass after changes. - [ ] Add support to `status` value in search endpoints so we can filter by `status=pending&response_status=pending`. - [ ] Check that we are refreshing the record `count_submitted_responses` values before indexing the record and add a partial update into the search engine when some associated entity (like responses) are create/updated/deleted for a record. (We probably should add a partial update of the index for this record attribute). - [ ] Change dataset progress metrics. - [ ] Change user metrics. Refs #5069 **Type of change** (Please delete options that are not relevant. Remember to title the PR according to the type of change) - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Refactor (change restructuring the codebase without changing functionality) - [ ] Improvement (change adding some improvement to an existing functionality) - [ ] Documentation update **How Has This Been Tested** (Please describe the tests that you ran to verify your changes. And ideally, reference `tests`) - [ ] Test A - [ ] Test B **Checklist** - [ ] I added relevant documentation - [ ] follows the style guidelines of this project - [ ] I did a self-review of my code - [ ] I made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK) (see text above) - [ ] I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/) --------- Co-authored-by: Paco Aranda <[email protected]>
- Loading branch information
1 parent
c8aa1a9
commit 58c8257
Showing
26 changed files
with
769 additions
and
46 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
60 changes: 60 additions & 0 deletions
60
...er/src/argilla_server/alembic/versions/237f7c674d74_add_status_column_to_records_table.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,60 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""add status column to records table | ||
Revision ID: 237f7c674d74 | ||
Revises: 45a12f74448b | ||
Create Date: 2024-06-18 17:59:36.992165 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "237f7c674d74" | ||
down_revision = "45a12f74448b" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
record_status_enum = sa.Enum("pending", "completed", name="record_status_enum") | ||
|
||
|
||
def upgrade() -> None: | ||
record_status_enum.create(op.get_bind()) | ||
|
||
op.add_column("records", sa.Column("status", record_status_enum, server_default="pending", nullable=False)) | ||
op.create_index(op.f("ix_records_status"), "records", ["status"], unique=False) | ||
|
||
# NOTE: Updating existent records to have "completed" status when they have | ||
# at least one response with "submitted" status. | ||
op.execute(""" | ||
UPDATE records | ||
SET status = 'completed' | ||
WHERE id IN ( | ||
SELECT DISTINCT record_id | ||
FROM responses | ||
WHERE status = 'submitted' | ||
); | ||
""") | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_index(op.f("ix_records_status"), table_name="records") | ||
op.drop_column("records", "status") | ||
|
||
record_status_enum.drop(op.get_bind()) |
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
42 changes: 42 additions & 0 deletions
42
argilla-server/src/argilla_server/contexts/distribution.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,42 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import List | ||
|
||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from argilla_server.enums import DatasetDistributionStrategy, RecordStatus | ||
from argilla_server.models import Record | ||
|
||
|
||
# TODO: Do this with one single update statement for all records if possible to avoid too many queries. | ||
async def update_records_status(db: AsyncSession, records: List[Record]): | ||
for record in records: | ||
await update_record_status(db, record) | ||
|
||
|
||
async def update_record_status(db: AsyncSession, record: Record) -> Record: | ||
if record.dataset.distribution_strategy == DatasetDistributionStrategy.overlap: | ||
return await _update_record_status_with_overlap_strategy(db, record) | ||
|
||
raise NotImplementedError(f"unsupported distribution strategy `{record.dataset.distribution_strategy}`") | ||
|
||
|
||
async def _update_record_status_with_overlap_strategy(db: AsyncSession, record: Record) -> Record: | ||
if len(record.responses_submitted) >= record.dataset.distribution["min_submitted"]: | ||
record.status = RecordStatus.completed | ||
else: | ||
record.status = RecordStatus.pending | ||
|
||
return await record.save(db, autocommit=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
Oops, something went wrong.