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

Improve validation error display #1579

Merged
merged 6 commits into from
Jun 15, 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
29 changes: 18 additions & 11 deletions dandiapi/api/models/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def valid(self) -> bool:
return not self.assets.exclude(status=Asset.Status.VALID).exists()

@property
def asset_validation_errors(self) -> list[str]:
def asset_validation_errors(self) -> list[dict[str, str]]:
# Import here to avoid dependency cycle
from .asset import Asset

Expand All @@ -90,18 +90,25 @@ def asset_validation_errors(self) -> list[str]:
status=Asset.Status.VALID
).values('status', 'path', 'validation_errors')

# Aggregate errors
asset_validating_error = {
'field': '',
'message': 'asset is currently being validated, please wait.',
}

def inject_path(asset: dict, err: dict):
return {**err, 'path': asset['path']}

# Aggregate errors, ensuring the path of the asset is included
errors = []
for asset in invalid_assets:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be really slow and/or memory intensive if a dandiset has hundreds of thousands of invalid assets. In general, I don't like how we go about storing asset validation errors in the database, but this PR is a strict improvement over the current situation imo. Perhaps related to #1214.

if asset['status'] == Asset.Status.INVALID:
errors.extend(asset['validation_errors'])
else:
errors.append(
{
'field': asset['path'],
'message': 'asset is currently being validated, please wait.',
}
)
# Must be pending or validating
if asset['status'] != Asset.Status.INVALID:
errors.append(inject_path(asset, asset_validating_error))
continue

# Must be invalid, only add entry in map if it has any errors
if asset['validation_errors']:
errors.extend([inject_path(asset, err) for err in asset['validation_errors']])

return errors

Expand Down
38 changes: 16 additions & 22 deletions dandiapi/api/tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,35 +422,29 @@ def test_version_rest_info(api_client, version):

@pytest.mark.django_db
@pytest.mark.parametrize(
'asset_status,expected_validation_error',
[
(
Asset.Status.PENDING,
[{'field': '', 'message': 'asset is currently being validated, please wait.'}],
),
(
Asset.Status.VALIDATING,
[{'field': '', 'message': 'asset is currently being validated, please wait.'}],
),
(Asset.Status.VALID, []),
(Asset.Status.INVALID, []),
],
'asset_status',
[Asset.Status.PENDING, Asset.Status.VALIDATING, Asset.Status.VALID, Asset.Status.INVALID],
)
def test_version_rest_info_with_asset(
api_client,
draft_version_factory,
draft_asset_factory,
asset_status: Asset.Status,
expected_validation_error: str,
api_client, draft_version_factory, draft_asset_factory, asset_status: Asset.Status
):
version = draft_version_factory(status=Version.Status.VALID)
asset = draft_asset_factory(status=asset_status)
version.assets.add(asset)
add_version_asset_paths(version=version)

# These validation error types should have the asset path prepended to them:
if asset_status == Asset.Status.PENDING or asset_status == Asset.Status.VALIDATING:
expected_validation_error[0]['field'] = asset.path
# Create expected validation errors for pending/validating assets
expected_validation_errors = (
[
{
'field': '',
'message': 'asset is currently being validated, please wait.',
'path': asset.path,
}
]
if asset_status in [Asset.Status.PENDING, Asset.Status.VALIDATING]
else []
)

assert api_client.get(
f'/api/dandisets/{version.dandiset.identifier}/versions/{version.version}/info/'
Expand All @@ -470,7 +464,7 @@ def test_version_rest_info_with_asset(
'metadata': version.metadata,
'size': version.size,
'status': Asset.Status.VALID,
'asset_validation_errors': expected_validation_error,
'asset_validation_errors': expected_validation_errors,
'version_validation_errors': [],
'contact_person': version.metadata['contributor'][0]['name'],
}
Expand Down
199 changes: 199 additions & 0 deletions web/src/components/DLP/ValidationErrorDialog.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<template>
<v-card
min-height="90vh"
class="pa-2"
>
<v-tabs v-model="tab">
<v-tab
v-if="showMetadataTab"
key="metadata"
href="#metadata"
>
Metadata
</v-tab>
<v-tab
v-if="showAssetsTab"
key="assets"
href="#assets"
>
Assets
</v-tab>
</v-tabs>
<v-tabs-items v-model="tab">
<!-- Metadata -->
<v-tab-item
v-if="showMetadataTab"
key="metadata"
value="metadata"
:transition="false"
>
<v-btn
v-if="owner"
class="mt-1"
color="primary"
@click="$emit('openMeditor')"
>
Fix issues
</v-btn>
<v-list
class="overflow-y-auto"
>
<div
v-for="(error, index) in versionValidationErrors"
:key="index"
>
<v-list-item>
<v-list-item-icon>
<v-icon>
{{ getValidationErrorIcon(error.field) }}
</v-icon>
</v-list-item-icon>

<v-list-item-content>
<template v-if="error.field">
{{ error.field }}:
</template>
{{ error.message }}
</v-list-item-content>
</v-list-item>
<v-divider />
</div>
</v-list>
</v-tab-item>

<!-- Assets -->
<v-tab-item
v-if="showAssetsTab"
key="assets"
value="assets"
:transition="false"
>
<v-list
class="overflow-y-auto"
>
<v-expansion-panels multiple>
<template v-for="(errors, path) in groupedAssetValidationErrors">
<v-list-item
:key="path"
>
<v-list-item-icon>
<v-icon>
<template v-if="errors.length > 1">
mdi-alert-plus
</template>
<template v-else>
{{ getValidationErrorIcon(errors[0].field) }}
</template>
</v-icon>
</v-list-item-icon>

<!-- Inline single errors -->
<template v-if="errors.length === 1">
<v-list-item-content>
<strong>{{ path }}</strong>
<template v-if="errors[0].field">
{{ errors[0].field }} -
</template>
{{ errors[0].message }}
</v-list-item-content>
</template>

<!-- Group multiple asset errors -->
<template v-else>
<v-list-group class="multi-error-list-group">
<template #activator>
<v-list-item-content>
<strong>{{ path }}</strong>
<v-list-item-subtitle>Click to expand</v-list-item-subtitle>
</v-list-item-content>
</template>

<v-list-item
v-for="error in errors"
:key="`${error.field}-${error.message}`"
>
<v-list-item-icon>
<v-icon>
{{ getValidationErrorIcon(error.field) }}
</v-icon>
</v-list-item-icon>
<v-list-item-content>
<template v-if="error.field">
{{ error.field }}:
</template>
{{ error.message }}
</v-list-item-content>
</v-list-item>
</v-list-group>
</template>
</v-list-item>
</template>
</v-expansion-panels>
</v-list>
</v-tab-item>
</v-tabs-items>
</v-card>
</template>

<script setup lang="ts">
import type { ValidationError } from '@/types';
import type { PropType } from 'vue';
import { watch, computed, ref } from 'vue';

import { VALIDATION_ICONS } from '@/utils/constants';

const props = defineProps({
assetValidationErrors: {
type: Array as PropType<ValidationError[]>,
required: true,
},
versionValidationErrors: {
type: Array as PropType<ValidationError[]>,
required: true,
},
selectedTab: {
type: String as PropType<'metadata' | 'assets'>,
required: false,
default: 'metadata',
},
owner: {
type: Boolean,
required: false,
default: false,
},
});

const tab = ref(props.selectedTab);
watch(() => props.selectedTab, (val) => {
tab.value = val;
});

const showMetadataTab = computed(() => !!props.versionValidationErrors.length);
const showAssetsTab = computed(() => !!Object.keys(props.assetValidationErrors).length);
const groupedAssetValidationErrors = computed(() => {
const path_asset_map: Record<string, ValidationError[]> = {};
props.assetValidationErrors.forEach((err) => {
if (!(err.path in path_asset_map)) {
path_asset_map[err.path] = [];
}
path_asset_map[err.path].push(err);
});

return path_asset_map;
});

function getValidationErrorIcon(errorField: string): string {
const icons = Object.keys(VALIDATION_ICONS).filter((field) => errorField.includes(field));
if (icons.length > 0) {
return (VALIDATION_ICONS as any)[icons[0]];
}
return VALIDATION_ICONS.DEFAULT;
}

</script>
<style>
.multi-error-list-group .v-list-group__header {
padding-left: 0;
padding-right: 0;
}
</style>
1 change: 1 addition & 0 deletions web/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface DandisetSearchResult extends Dandiset {
export interface ValidationError {
field: string,
message: string,
path: string,
}

export interface Version {
Expand Down
Loading