Skip to content

Commit

Permalink
feat: add a new view CLI command for managing views
Browse files Browse the repository at this point in the history
  • Loading branch information
jrdh committed Aug 15, 2024
1 parent 4f3a41e commit d288200
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 44 deletions.
44 changes: 0 additions & 44 deletions dataimporter/cli/emu.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,50 +103,6 @@ def queue(amount: str, config: Config):
amount -= 1


@emu_group.command("ingest")
@click.argument("view", type=click.Choice(EMU_VIEWS))
@click.option(
"--everything",
is_flag=True,
show_default=True,
default=False,
help="Add all records to MongoDB regardless of whether they have changed. This is "
"primarily useful when a change has been made to a view's filters or "
"representation",
)
@with_config()
def ingest(view: str, config: Config, everything: bool = False):
"""
On the given view, updates MongoDB with any queued EMu changes and flushes the
queues.
"""
with DataImporter(config) as importer:
console.log(f"Adding changes from {view} view to mongo")
importer.add_to_mongo(view, everything=everything)
console.log(f"Added changes from {view} view to mongo")


@emu_group.command("sync")
@click.argument("view", type=click.Choice(EMU_VIEWS))
@click.option(
"--resync",
is_flag=True,
show_default=True,
default=False,
help="Resynchronise all data in Elasticsearch with MongoDB for this view, not just "
"the records that have changed.",
)
@with_config()
def sync(view: str, config: Config, resync: bool = False):
"""
Updates Elasticsearch with the changes in MongoDB for the given view.
"""
with DataImporter(config) as importer:
console.log(f"Syncing changes from {view} view to elasticsearch")
importer.sync_to_elasticsearch(view, resync=resync)
console.log(f"Finished with {view}")


@emu_group.command("get-emu-date")
@with_config()
def get_emu_date(config: Config):
Expand Down
1 change: 1 addition & 0 deletions dataimporter/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def get_status(config: Config):

cli.add_command(emu_group)
cli.add_command(ext_group)
cli.add_command(view_group)
cli.add_command(portal_group)
cli.add_command(maintenance_group)

Expand Down
84 changes: 84 additions & 0 deletions dataimporter/cli/view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import click

from dataimporter.cli.utils import with_config, console
from dataimporter.importer import DataImporter
from dataimporter.lib.config import Config


@click.group("view")
def view_group():
pass


@view_group.command("list")
@with_config()
def list_views(config: Config):
"""
List the name of all the views in current use.
"""
with DataImporter(config) as importer:
console.log(", ".join(view.name for view in importer.views))


@view_group.command("flush")
@click.argument("view", type=str)
@with_config()
def flush(view: str, config: Config):
"""
Flushes the given view's queue.
For non-published views this should always be safe to do (but maybe think about it
first, just to make sure) but for published views you should make sure you know what
you're doing.
"""
with DataImporter(config) as importer:
view = importer.get_view(view)
console.log(
f"Flushing {view.name} view queue, currently has", view.count(), "IDs in it"
)
view.flush()
console.log(f"{view.name} flushed")


@view_group.command("ingest")
@click.argument("view", type=str)
@click.option(
"--everything",
is_flag=True,
show_default=True,
default=False,
help="Add all records to MongoDB regardless of whether they have changed. This is "
"primarily useful when a change has been made to a view's filters or "
"representation",
)
@with_config()
def ingest(view: str, config: Config, everything: bool = False):
"""
On the given view, updates MongoDB with any queued EMu changes and flushes its
queue.
"""
with DataImporter(config) as importer:
console.log(f"Adding changes from {view} view to mongo")
importer.add_to_mongo(view, everything=everything)
console.log(f"Added changes from {view} view to mongo")


@view_group.command("sync")
@click.argument("view", type=str)
@click.option(
"--resync",
is_flag=True,
show_default=True,
default=False,
help="Resynchronise all data in Elasticsearch with MongoDB for this view, not just "
"the records that have changed.",
)
@with_config()
def sync(view: str, config: Config, resync: bool = False):
"""
Updates Elasticsearch with the changes in MongoDB for the given view.
"""
with DataImporter(config) as importer:
console.log(f"Syncing changes from {view} view to elasticsearch")
importer.sync_to_elasticsearch(view, resync=resync)
console.log(f"Finished with {view}")

0 comments on commit d288200

Please sign in to comment.