|
| 1 | +import logging |
| 2 | +from aind_data_access_api.rds_tables import RDSCredentials, Client |
| 3 | +from aind_data_access_api.document_db import MetadataDbClient |
| 4 | +import pandas as pd |
| 5 | +import panel as pn |
| 6 | + |
| 7 | +# Configure logging |
| 8 | +logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | +# Redshift settings |
| 12 | +REDSHIFT_SECRETS = "/aind/prod/redshift/credentials/readwrite" |
| 13 | +RDS_TABLE_NAME = "metadata_upgrade_status_prod" |
| 14 | + |
| 15 | + |
| 16 | +extra_columns = { |
| 17 | + "_id": 1, |
| 18 | + "data_description.data_level": 1, |
| 19 | + "data_description.project_name": 1, |
| 20 | + "name": 1, |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +@pn.cache() |
| 25 | +def get_extra_col_df(): |
| 26 | + client = MetadataDbClient( |
| 27 | + host="api.allenneuraldynamics.org", |
| 28 | + version="v1", |
| 29 | + ) |
| 30 | + records = client.retrieve_docdb_records( |
| 31 | + filter_query={}, |
| 32 | + projection=extra_columns, |
| 33 | + limit=0, |
| 34 | + ) |
| 35 | + for i, record in enumerate(records): |
| 36 | + data_description = record.get("data_description", {}) |
| 37 | + if data_description: |
| 38 | + record["data_level"] = data_description.get("data_level", None) |
| 39 | + record["project_name"] = data_description.get("project_name", None) |
| 40 | + record.pop("data_description") |
| 41 | + |
| 42 | + record.pop("_id") |
| 43 | + records[i] = record |
| 44 | + return pd.DataFrame(records) |
| 45 | + |
| 46 | + |
| 47 | +@pn.cache() |
| 48 | +def get_redshift_table(): |
| 49 | + rds_client = Client( |
| 50 | + credentials=RDSCredentials( |
| 51 | + aws_secrets_name=REDSHIFT_SECRETS, |
| 52 | + ), |
| 53 | + ) |
| 54 | + df = rds_client.read_table(RDS_TABLE_NAME) |
| 55 | + return df |
| 56 | + |
| 57 | + |
| 58 | +@pn.cache() |
| 59 | +def get_data(): |
| 60 | + logger.info("Loading extra columns from DocDB...") |
| 61 | + extra_col_df = get_extra_col_df() |
| 62 | + logger.info("Loading Redshift table...") |
| 63 | + df = get_redshift_table() |
| 64 | + if df is None or len(df) == 0: |
| 65 | + return pn.pane.Markdown("**Table is empty or could not be read**") |
| 66 | + logger.info("Merging extra columns...") |
| 67 | + df = df.merge(extra_col_df, how="left", left_on="v1_id", right_on="_id") |
| 68 | + return df |
| 69 | + |
| 70 | + |
| 71 | +def build_panel_app(): |
| 72 | + col = pn.Column("# Metadata Upgrade Status Table", sizing_mode="stretch_width") |
| 73 | + button = pn.widgets.Button(name="Load Table", button_type="primary") |
| 74 | + |
| 75 | + def load_table(event): |
| 76 | + col.loading = True |
| 77 | + df = get_data() |
| 78 | + tab = pn.widgets.Tabulator( |
| 79 | + df, |
| 80 | + sizing_mode="stretch_width", |
| 81 | + height=800, |
| 82 | + header_filters=True, |
| 83 | + disabled=True, |
| 84 | + page_size=500, |
| 85 | + show_index=False, |
| 86 | + ) |
| 87 | + col[:] = ["# Metadata Upgrade Status Table", tab] |
| 88 | + col.loading = False |
| 89 | + |
| 90 | + button.on_click(load_table) |
| 91 | + col.append(button) |
| 92 | + return col |
| 93 | + |
| 94 | + |
| 95 | +app = build_panel_app() |
| 96 | +app.servable(title="Upgrade Status") |
0 commit comments