Skip to content

Commit

Permalink
Wrapper around metabase-import-export lib
Browse files Browse the repository at this point in the history
  • Loading branch information
seocam committed Nov 8, 2020
1 parent 6e48f8e commit 95bf2f7
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 11 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,21 @@ After creating the collection you will have to access it in order to get the col
Now we are ready to run the collection import script:

```
./scripts/metabase-import-export.py \
kanban-dash metabase --import \
--username=<your-user-name> \
import \
--collection-id=<your-collection-id> \
--import-file=kanbandash/kanban-dashboards.json
--url=<your-metabase-url>
```

or using our docker container:

```
docker run -it cravefood/kanban-dash kanban-dash metabase --import \
--username=<your-user-name> \
--collection-id=<your-collection-id> \
--url=<your-metabase-url>
```


After running the script you should be able to access the collection and see the imported reports. Select "Kanban" in the dashboards tab to see the dashboard without any data.

Expand Down
60 changes: 58 additions & 2 deletions kanbandash/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
from alembic import command
from alembic.config import Config

from metabase_import_export import (
export_collection,
import_collection,
metabase_login,
set_metabase_url,
)

from . import models

from .populate_with_test_data import generate_data
Expand Down Expand Up @@ -37,12 +44,14 @@ def get_tables():

def drop_tables():
tables = get_tables()
models.Base.metadata.drop_all(models.SQLALCHEMY_ENGINE, tables=tables)
engine = models.get_db_engine()
models.Base.metadata.drop_all(engine, tables=tables)


def create_tables():
tables = get_tables()
models.Base.metadata.create_all(models.SQLALCHEMY_ENGINE, tables=tables)
engine = models.get_db_engine()
models.Base.metadata.create_all(engine, tables=tables)
migrate_schema()


Expand All @@ -66,12 +75,59 @@ def manage_models(args):
migrate_schema()


def manage_metabase(args):
set_metabase_url(args.url)
metabase_login(args.username)

if args.export:
print("Where to you want to store your Kanban definitions?")
kanban_definitions_file = input(">> ")
export_collection(kanban_definitions_file, args.collection_id)
else:
kanban_definitions_file = os.path.join(SCRIPT_DIR, "kanban-dashboards.json")
import_collection(kanban_definitions_file, args.collection_id)


def get_argparser():
parser = argparse.ArgumentParser(
description="Metabase Kanban Dashboard manager",
)
subparsers = parser.add_subparsers()

# metabase subparsers
metabase = subparsers.add_parser("metabase")
import_export_group = metabase.add_mutually_exclusive_group(required=True)
import_export_group.add_argument(
"--import",
action="store_true",
help=("Import the Kanban dashboard definitions to a Metabase instance."),
)
import_export_group.add_argument(
"--export",
action="store_true",
help=("Export the Kanban dashboard definitions from a Metabase instance."),
)
metabase.add_argument(
"--collection-id",
type=int,
help=(
"The id of the collection where the data will be imported to or "
"exported from."
),
required=True,
)
metabase.add_argument(
"--url",
help="Metabase base URL",
default="http://localhost:3000",
)
metabase.add_argument(
"--username",
help="Metabase admin user",
required=True,
)
metabase.set_defaults(func=manage_metabase)

# models subparsers
models = subparsers.add_parser("models")
models_group = models.add_mutually_exclusive_group(required=True)
Expand Down
24 changes: 21 additions & 3 deletions kanbandash/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,29 @@

from . import settings

# SQLAlchemy
SQLALCHEMY_ENGINE = create_engine(settings.POSTGRES_DATABASE_URL, echo=False)
SQLALCHEMY_CACHE = {}

Base = declarative_base()
Session = sessionmaker(bind=SQLALCHEMY_ENGINE)


def get_db_engine():
if "engine" in SQLALCHEMY_CACHE:
return SQLALCHEMY_CACHE["engine"]

engine = create_engine(settings.POSTGRES_DATABASE_URL, echo=False)
SQLALCHEMY_CACHE["engine"] = engine
return engine


def get_db_session_class():
if "session" in SQLALCHEMY_CACHE:
return SQLALCHEMY_CACHE["session"]

engine = get_db_engine()
Session = sessionmaker(bind=engine)
SQLALCHEMY_CACHE["session"] = Session

return Session


class KanbanClassOfService(Base):
Expand Down
25 changes: 23 additions & 2 deletions kanbandash/populate_with_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
KanbanClassOfService,
KanbanColumn,
KanbanDay,
Session,
get_db_session_class,
)

factories_session = Session()
FACTORY_SESSION_CACHE = {}

START_DATE = datetime.date(2019, 1, 1)
TODAY = datetime.datetime.now().date()
Expand All @@ -34,7 +34,20 @@
}


def get_factory_session():
if "session" in FACTORY_SESSION_CACHE:
return FACTORY_SESSION_CACHE["session"]

Session = get_db_session_class()
factories_session = Session()
FACTORY_SESSION_CACHE["session"] = factories_session

return factories_session


def generate_boards(n):
factories_session = get_factory_session()

boards = []
for letter in string.ascii_uppercase:
name = "Board {}".format(letter)
Expand Down Expand Up @@ -68,6 +81,8 @@ def generate_kanban_columns(board):


def generate_kanban_days():
factories_session = get_factory_session()

class_of_services = get_object_ids(KanbanClassOfService)
columns_query = factories_session.query(KanbanColumn)
date = START_DATE
Expand Down Expand Up @@ -109,6 +124,8 @@ def generate_kanban_days():


def generate_class_of_services():
factories_session = get_factory_session()

class_of_services = CLASS_OF_SERVICES.keys()

for name in class_of_services:
Expand All @@ -119,11 +136,15 @@ def generate_class_of_services():


def get_object_ids(Model):
factories_session = get_factory_session()

raw_query_data = factories_session.query(Model.id).all()
return list(chain(*raw_query_data))


def generate_kanban_cards(n, m):
factories_session = get_factory_session()

date = START_DATE
boards = get_object_ids(KanbanBoard)
class_of_services = get_object_ids(KanbanClassOfService)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = kanbandash
version = 0.2.0
version = 0.2.2

[options]
packages = find:
Expand Down

0 comments on commit 95bf2f7

Please sign in to comment.