Skip to content

Instantly share code, notes, and snippets.

Code Snippets — Multi-Tenant Genie

Lift-and-use code examples for the moving pieces of the architecture. Every snippet here is pulled from working code in this repo — links to the canonical source file are included so you can read the full context.

Pattern note: snippets that depend on identity propagation come in two flavors:

  • Pattern A — SP-per-client (uses session_user()). Working today on any Databricks workspace. Shown by default.
  • Pattern B — Shared SP + custom claims (uses current_oauth_custom_identity_claims()). Pending Genie surface validation; shown where it differs.

from dotenv import load_dotenv
import os
import requests
import json
from typing import Optional
# Load environment variables
load_dotenv('../../../keys.env')
# Constants
from flask import Flask, request, jsonify
import threading
import requests
# Initialize Flask application
app = Flask(__name__)
@app.route('/')
def index():
return '''
# Databricks notebook source
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# Load the dataset
iris = load_iris()
X = iris.data
#!/usr/bin/env python3
import os
import requests
import time
import logging
import json
from urllib.parse import urljoin, urlencode
from concurrent.futures import ThreadPoolExecutor, as_completed
import pyarrow as pa
#!/usr/bin/env python3
import json
import os
import requests
import time
from urllib.parse import urljoin, urlencode
from concurrent.futures import ThreadPoolExecutor, as_completed
# Set debug level for HTTP debug logging
{
"datasets": [
{
"name": "1d8e639a",
"displayName": "query-history-trends",
"query": "with query_history as \n(SELECT\nstatement_id,\nexecuted_by,\nstatement_text,\naccount_id, \nworkspace_id, \nwarehouse_id,\nexecution_status,\nf.statement_type,\nclient_application,\nCOALESCE(error_message, 'None') AS error_message,\nCOALESCE(try_divide(total_duration_ms, 1000), 0) AS QueryRuntimeSeconds,\nCOALESCE(try_divide(total_task_duration_ms, 1000), 0) AS CPUTotalExecutionTime,\nCOALESCE(try_divide(execution_duration_ms, 1000), 0) AS ExecutionQueryTime,\nCOALESCE(try_divide(compilation_duration_ms, 1000), 0) AS CompilationQueryTime,\nCOALESCE(try_divide(waiting_at_capacity_duration_ms, 1000), 0) AS QueueQueryTime,\nCOALESCE(try_divide(waiting_for_compute_duration_ms, 1000), 0) AS StartUpQueryTime,\nCOALESCE(try_divide(result_fetch_duration_ms, 1000), 0) AS ResultFetchTime,\nstart_time,\nend_time,\nupdate_time,\nCOALESCE(read_bytes, 0) AS read_bytes,\nCOALESCE(read_io_cache_percent, 0) AS read_