Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
api: add transitive search
Browse files Browse the repository at this point in the history
* Adds an extra Model that keeps track of all the equivalent IDs.
  (closes #49)

* Enables transitive queries to fetch all the related claims (through
  `/claims?type=xxx&value=yyy&recurse=1`.

* Adds a new API resource `/eqids` to list all the equivalent IDs
  (one can also filter by a specific id using the argument `eqid=`).

* Adds command `claimstore index` in order to do operations with
  indeces. For instance: `claimstore index clear_eqids` and
  `claimstore index rebuild_eqids`.

Signed-off-by: Jose Benito Gonzalez Lopez <jose.benito.gonzalez@cern.ch>
  • Loading branch information
jbenito3 committed Sep 18, 2015
1 parent 304f5a9 commit ad1195d
Show file tree
Hide file tree
Showing 12 changed files with 771 additions and 54 deletions.
14 changes: 5 additions & 9 deletions claimstore/ext/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@
def setup_app(app):
"""Setup sqlalchemy."""
# Add extension CLI to application.
app.cli.add_command(dropalldb)
app.cli.add_command(database)
db.init_app(app)


@click.command()
@click.group()
@with_appcontext
def dropalldb():
"""Drop database."""
if click.confirm('Are you sure you want to drop the whole database?'):
db.drop_all()
click.echo('Database dropped')
else:
click.echo('Command aborted')
def database():
"""Database related commands."""
pass
53 changes: 46 additions & 7 deletions claimstore/modules/claims/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@
import click
from flask_cli import with_appcontext

from claimstore.ext.sqlalchemy import db
from claimstore.ext.sqlalchemy import database, db
from claimstore.modules.claims.fixtures.claim import load_all_claims
from claimstore.modules.claims.fixtures.claimant import load_all_claimants
from claimstore.modules.claims.fixtures.pid import load_all_pids
from claimstore.modules.claims.fixtures.predicate import load_all_predicates
from claimstore.modules.claims.models import EquivalentIdentifier


@click.command()
@database.command()
@click.option('--config', help='Path to a folder with the db configuration')
@with_appcontext
def initdb(config):
def create(config):
"""Create database and populate it with basic data.
The database will be populated with the predicates, persistent identifiers
Expand Down Expand Up @@ -66,10 +67,10 @@ def initdb(config):
click.echo('Database initialisation completed.')


@click.command()
@database.command()
@click.option('--data', help='Path to a folder with data (claims)')
@with_appcontext
def populatedb(data):
def populate(data):
"""Populate database with claims.
The database will be populated with the claims that are defined in
Expand Down Expand Up @@ -98,8 +99,46 @@ def populatedb(data):
click.echo('Database populate completed.')
except Exception:
click.echo(
'Claims could not be loaded. Try `claimstore initdb` first.'
'Claims could not be loaded. Try `claimstore database create` '
'first.'
)


commands = [initdb, populatedb]
@database.command()
@with_appcontext
def drop():
"""Drop the whole database."""
if click.confirm('Are you sure you want to drop the whole database?'):
db.drop_all()
click.echo('Database dropped')
else:
click.echo('Command aborted')


@click.group()
@with_appcontext
def eqid():
"""Command providing actions to alter the Equivalent Identifier index."""
pass


@eqid.command('drop')
@with_appcontext
def drop_eqid():
"""Delete all the entries in the eqid index."""
if click.confirm('Are you sure to drop the whole index?'):
EquivalentIdentifier.clear()
click.echo('Index cleared.')
else:
click.echo('Command aborted')


@eqid.command()
@with_appcontext
def reindex():
"""Process all claims to rebuild the eqid index."""
EquivalentIdentifier.rebuild()
click.echo('Index rebuilt.')


commands = [eqid]
23 changes: 23 additions & 0 deletions claimstore/modules/claims/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
#
# This file is part of ClaimStore.
# Copyright (C) 2015 CERN.
#
# ClaimStore is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# ClaimStore is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ClaimStore; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
# USA.

"""Module configuration."""

CFG_EQUIVALENT_PREDICATES = ['is_same_as', 'is_variant_of']
30 changes: 21 additions & 9 deletions claimstore/modules/claims/fixtures/claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,32 @@ def load_all_claims(test_app=None, config_path=None):


@pytest.fixture
def dummy_claim():
def dummy_subject():
"""Fixture of a dummy subject."""
return {
"type": "CDS_RECORD_ID",
"value": "test-2001192"
}


@pytest.fixture
def dummy_object():
"""Fixture of a dummy object."""
return {
"type": "CDS_REPORT_NUMBER",
"value": "CMS-PAS-HIG-14-008"
}


@pytest.fixture
def dummy_claim(dummy_subject, dummy_object):
"""Fixture that creates a dummy claim."""
return {
"claimant": "dummy_claimant",
"subject": {
"type": "CDS_RECORD_ID",
"value": "2001192"
},
"subject": dummy_subject,
"predicate": "is_same_as",
"certainty": 1.0,
"object": {
"type": "CDS_REPORT_NUMBER",
"value": "CMS-PAS-HIG-14-008"
},
"object": dummy_object,
"arguments": {
"human": 0,
"actor": "CDS_submission"
Expand Down
9 changes: 9 additions & 0 deletions claimstore/modules/claims/fixtures/claimant.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ def dummy_claimant():
}


@pytest.fixture
def create_dummy_claimant(webtest_app, dummy_claimant):
"""Add dummy claimant to the database."""
webtest_app.post_json(
'/subscribe',
dummy_claimant
)


@pytest.fixture
def all_claimants(db):
"""Fixture that loads all claimants."""
Expand Down
Loading

0 comments on commit ad1195d

Please sign in to comment.