Skip to content

Commit

Permalink
progress bar on update and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Baker committed Oct 11, 2023
1 parent c6f7a09 commit 489b495
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
10 changes: 7 additions & 3 deletions lang/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import atexit
import readline
import json

import firebase_admin

import typer
Expand All @@ -18,6 +17,8 @@
app = typer.Typer(rich_markup_mode="rich")

QUERY_COMMAND_HELP = "The query to execute against the Firestore database."
PROGRESS_HOLDER = {"progress": None}


@app.command(epilog="See https://github.com/crbaker/fikl for more details.")
def query(query_text: Annotated[(str), typer.Argument(help=QUERY_COMMAND_HELP)] = None):
Expand All @@ -26,7 +27,8 @@ def query(query_text: Annotated[(str), typer.Argument(help=QUERY_COMMAND_HELP)]
"""
try:
if (env_var := 'GOOGLE_APPLICATION_CREDENTIALS') not in os.environ:
rprint(f"""[italic yellow]Warning: {env_var} is not set[/italic yellow]""")
rprint(
f"""[italic yellow]Warning: {env_var} is not set[/italic yellow]""")

configure_firebase()

Expand All @@ -37,10 +39,12 @@ def query(query_text: Annotated[(str), typer.Argument(help=QUERY_COMMAND_HELP)]
except ql.QueryError as exception:
typer.echo(exception)


def configure_firebase():
""" Configures the Firebase SDK. """
firebase_admin.initialize_app()


def run_query_and_output(query_text):
"""
Runs the supplied query and outputs the results.
Expand Down Expand Up @@ -84,7 +88,7 @@ def save_history(history_path=history_path):
if current_query == "exit":
go_again = False
rprint("Bye!:waving_hand:")
if current_query == "cls":
elif current_query == "cls":
typer.clear()
current_query = None
elif current_query.endswith(';'):
Expand Down
34 changes: 22 additions & 12 deletions lang/ql.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import pyperclip
import pydash

import typer

from firebase_admin import firestore as fs

from google.cloud.firestore_v1.base_query import FieldFilter
Expand Down Expand Up @@ -75,6 +77,7 @@ def run_query(query: str) -> list[dict] | dict:
except Exception as exception:
raise QueryError(exception) from exception


def output_json(json_output: str, fikl_query: FIKLSelectQuery):
""" Writes the provided json to the provided path."""
if fikl_query["output_type"] == FIKLOutputType.PATH:
Expand All @@ -91,6 +94,7 @@ def output_json(json_output: str, fikl_query: FIKLSelectQuery):

return "Unknown output type"


def do_group_by(records, fikl_query: FIKLSelectQuery):
"""Groups records by the provided group property."""
if "group" in fikl_query and fikl_query["group"]:
Expand Down Expand Up @@ -322,12 +326,15 @@ def execute_delete_query(fikl_query: FIKLUpdateQuery) -> int:

count = 0

for doc in docs:
try:
doc.reference.delete()
count += 1
except Exception:
pass
if len(docs) > 0:
with typer.progressbar(label="Deleting", length=len(docs)) as progress:
for doc in docs:
try:
doc.reference.delete()
count += 1
progress.update(1)
except Exception:
pass

return count

Expand All @@ -344,12 +351,15 @@ def execute_update_query(fikl_query: FIKLUpdateQuery) -> int:
count = 0
new_values = merge_setters(fikl_query["set"])

for doc in docs:
try:
doc.reference.update(new_values)
count += 1
except Exception:
pass
if len(docs) > 0:
with typer.progressbar(label="Updating", length=len(docs)) as progress:
for doc in docs:
try:
doc.reference.update(new_values)
count += 1
progress.update(1)
except Exception:
pass

return count

Expand Down

0 comments on commit 489b495

Please sign in to comment.