630 lines
28 KiB
Lua
630 lines
28 KiB
Lua
-- Databricks, through its CLI, which reads ~/.databrickscfg and signs in as it's set up to, so
|
|
-- the extension never sees a token. "dbx" offers Search, Runs and Jobs: "dbx search sales" (or
|
|
-- just "dbx sales") finds catalogs, schemas, tables and volumes, "dbx runs" the latest job runs,
|
|
-- newest first ("dbx runs failed", "dbx runs ingest" filter them), and "dbx jobs" the jobs. Enter
|
|
-- shows one: a table's columns, a schema's tables, a run's tasks, a job's runs; ⌘K opens it in
|
|
-- Databricks or copies its name. Which workspaces it searches is a setting, the profiles in
|
|
-- ~/.databrickscfg to choose from. Its text is in locales/.
|
|
|
|
local cache = toolshed.cache_dir()
|
|
-- index.sh beside this file walks a workspace, as no one call lists what's in it.
|
|
local folder = package.path:match("^(.-)/%?%.lua")
|
|
-- The index is walked again in the background once it's this old, searches using it meanwhile.
|
|
local stale_seconds = 24 * 3600
|
|
-- Runs change by the minute but not by the keystroke: asked again once they're this old.
|
|
local runs_seconds = 20
|
|
-- Search shows at most this many, as a launcher list shows no more.
|
|
local most = 40
|
|
|
|
local escape = toolshed.escape
|
|
|
|
local quoted = toolshed.shell_quote
|
|
|
|
local modified = toolshed.file.modified
|
|
|
|
local read = toolshed.file.read
|
|
|
|
-- The CLI nags on stderr about skills of its own, which says nothing about what went wrong.
|
|
local function reason(err)
|
|
local lines = {}
|
|
for line in tostring(err):gmatch("[^\n]+") do
|
|
if not line:find("aitools") then table.insert(lines, line) end
|
|
end
|
|
return (table.concat(lines, " "):gsub("^.-databricks: ", ""))
|
|
end
|
|
|
|
-- workspaces are the profiles in ~/.databrickscfg that are a workspace, not an account: name
|
|
-- and address, in the file's order.
|
|
local function workspaces()
|
|
local list, current = {}, nil
|
|
for line in (read(os.getenv("HOME") .. "/.databrickscfg") or ""):gmatch("[^\n]+") do
|
|
local name = line:match("^%s*%[(.-)%]")
|
|
if name then
|
|
current = { name = name }
|
|
table.insert(list, current)
|
|
elseif current then
|
|
local key, value = line:match("^%s*([%w_]+)%s*=%s*(.-)%s*$")
|
|
if key then current[key] = value end
|
|
end
|
|
end
|
|
local found = {}
|
|
for _, p in ipairs(list) do
|
|
if p.host and not p.account_id then
|
|
local host = p.host:gsub("/+$", "")
|
|
if not host:find("^https?://") then host = "https://" .. host end
|
|
table.insert(found, { name = p.name, host = host, id = host:match("adb%-(%d+)%.") })
|
|
end
|
|
end
|
|
return found
|
|
end
|
|
|
|
local function workspace(name)
|
|
for _, w in ipairs(workspaces()) do
|
|
if w.name == name then return w end
|
|
end
|
|
error(toolshed.t("error.no-profile", { profile = name }))
|
|
end
|
|
|
|
-- chosen are the workspaces the setting names: one, or all of them.
|
|
local function chosen(settings)
|
|
local all = workspaces()
|
|
if settings.workspace == "*" then return all end
|
|
for _, w in ipairs(all) do
|
|
if w.name == settings.workspace then return { w } end
|
|
end
|
|
return {}
|
|
end
|
|
|
|
local function cli(profile, ...)
|
|
local args = { "databricks", ... }
|
|
table.insert(args, "-p")
|
|
table.insert(args, profile)
|
|
table.insert(args, "-o")
|
|
table.insert(args, "json")
|
|
return toolshed.json.decode(toolshed.exec(args))
|
|
end
|
|
|
|
-- address is where Databricks shows a thing, with the workspace's id so the right one opens.
|
|
local function address(w, path)
|
|
return w.host .. path .. (w.id and ("?o=" .. w.id) or "")
|
|
end
|
|
|
|
local function problem(text)
|
|
return { title = text, icon = "info", actions = {} }
|
|
end
|
|
|
|
-- The index ------------------------------------------------------------------------------------
|
|
|
|
local function index_file(profile) return cache .. "/index-" .. profile:gsub("[^%w%-_]", "_") .. ".tsv" end
|
|
|
|
local function index_key(profile) return "index-" .. profile:lower():gsub("[^%w_-]", "-") end
|
|
|
|
-- index_in_background walks the workspace again, quietly, unless that's already under way.
|
|
local function index_in_background(profile)
|
|
toolshed.activities.set {
|
|
key = index_key(profile), title = toolshed.t("jobs.index.title", { workspace = profile }), description = toolshed.t("jobs.index.description"), quiet = true, timeout = 1800,
|
|
run = { "sh", folder .. "/index.sh", profile, index_file(profile) },
|
|
}
|
|
end
|
|
|
|
local indexes = {} -- by profile: what's read from its file, and when
|
|
|
|
-- index is what's known of a workspace, its entries { kind, name, detail, comment }, or nil and
|
|
-- what to say instead: that it's being walked, or why that failed.
|
|
local function index(profile)
|
|
local file = index_file(profile)
|
|
local at = modified(file)
|
|
if not at then
|
|
local last = toolshed.activities.get(index_key(profile))
|
|
if last and (last.state == "failed" or last.state == "timed-out") then
|
|
return nil, toolshed.t("error.index", { reason = reason(last.reason) })
|
|
end
|
|
index_in_background(profile)
|
|
return nil, toolshed.t("indexing", { workspace = profile })
|
|
end
|
|
if os.time() - at > stale_seconds then index_in_background(profile) end
|
|
local known = indexes[profile]
|
|
if known and known.at == at then return known.entries end
|
|
local entries = {}
|
|
for line in (read(file) or ""):gmatch("[^\n]+") do
|
|
local kind, name, detail, comment = line:match("^([^\t]*)\t([^\t]*)\t([^\t]*)\t?(.*)$")
|
|
if kind then table.insert(entries, { kind = kind, name = name, detail = detail, comment = comment }) end
|
|
end
|
|
indexes[profile] = { at = at, entries = entries }
|
|
return entries
|
|
end
|
|
|
|
local kind_icons = { catalog = "database", schema = "layers", table = "table-2", volume = "hard-drive", job = "workflow" }
|
|
|
|
-- A catalog, schema, table or volume as a row: its name, what it is, and where.
|
|
local function item_jig(w, e, several)
|
|
local kind = e.kind
|
|
if kind == "table" and (e.detail == "VIEW" or e.detail == "MATERIALIZED_VIEW") then kind = "view" end
|
|
local tags = { { text = toolshed.t("kind." .. kind) } }
|
|
if several then table.insert(tags, { text = w.name, tint = "blue" }) end
|
|
local short = e.name:match("([^.]+)$")
|
|
local within = e.name:match("^(.*)%.[^.]+$")
|
|
local about = {}
|
|
if within then table.insert(about, within) end
|
|
if e.comment ~= "" then table.insert(about, e.comment) end
|
|
local actions = { "show", "open", "copy-name" }
|
|
if e.kind == "table" then table.insert(actions, "copy-select") end
|
|
if e.kind == "volume" then table.insert(actions, "copy-path") end
|
|
return {
|
|
key = w.name .. ":" .. e.name, title = short, subtitle = table.concat(about, " · "), icon = kind_icons[e.kind],
|
|
tags = tags, tool = "item", actions = actions,
|
|
data = { profile = w.name, kind = e.kind, name = e.name },
|
|
}
|
|
end
|
|
|
|
local function job_jig(w, e, several)
|
|
local tags = several and { { text = w.name, tint = "blue" } } or nil
|
|
return {
|
|
key = w.name .. ":job:" .. e.detail, title = e.name, subtitle = toolshed.t("job-id", { id = e.detail }), icon = "workflow",
|
|
tags = tags, tool = "job", data = { profile = w.name, id = e.detail, name = e.name },
|
|
}
|
|
end
|
|
|
|
-- found are the index's entries of these kinds that match what's typed, best first.
|
|
local function found(settings, typed, kinds, as_jig)
|
|
local wanted = {}
|
|
for _, k in ipairs(kinds) do wanted[k] = true end
|
|
local list, said = {}, {}
|
|
local ws = chosen(settings)
|
|
if #ws == 0 then return { problem(toolshed.t("error.no-workspace")) } end
|
|
for _, w in ipairs(ws) do
|
|
local entries, why = index(w.name)
|
|
if not entries then
|
|
table.insert(said, problem(why))
|
|
else
|
|
for _, e in ipairs(entries) do
|
|
if wanted[e.kind] then
|
|
local score = toolshed.match(typed, e.name)
|
|
if score > 0 then table.insert(list, { score = score, w = w, e = e }) end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
table.sort(list, function(a, b)
|
|
if a.score ~= b.score then return a.score > b.score end
|
|
return a.e.name < b.e.name
|
|
end)
|
|
local jigs = said
|
|
for i = 1, math.min(#list, most) do table.insert(jigs, as_jig(list[i].w, list[i].e, #ws > 1)) end
|
|
return jigs
|
|
end
|
|
|
|
-- Runs -----------------------------------------------------------------------------------------
|
|
|
|
local runs_known = {} -- by profile: the latest runs, and when they were asked for
|
|
|
|
local function latest_runs(profile)
|
|
local known = runs_known[profile]
|
|
if known and os.time() - known.at < runs_seconds then return known.runs end
|
|
local runs = cli(profile, "jobs", "list-runs", "--limit", "50")
|
|
runs_known[profile] = { at = os.time(), runs = runs }
|
|
return runs
|
|
end
|
|
|
|
-- status is how a run or task went, as a word and a tint.
|
|
local function status(state)
|
|
state = state or {}
|
|
local life, result = state.life_cycle_state, state.result_state
|
|
if life == "RUNNING" or life == "PENDING" or life == "QUEUED" or life == "TERMINATING" or life == "BLOCKED" or life == "WAITING_FOR_RETRY" then
|
|
return "running", "blue"
|
|
end
|
|
if result == "SUCCESS" then return "succeeded", "green" end
|
|
if result == "SUCCESS_WITH_FAILURES" then return "partly", "orange" end
|
|
if result == "CANCELED" or result == "UPSTREAM_CANCELED" then return "cancelled", "gray" end
|
|
if result == "SKIPPED" or result == "EXCLUDED" or life == "SKIPPED" then return "skipped", "gray" end
|
|
if result or life == "INTERNAL_ERROR" then return "failed", "red" end
|
|
return "running", "blue"
|
|
end
|
|
|
|
-- The words that filter runs by how they went, in the languages Toolshed speaks.
|
|
local status_words = {
|
|
failed = "failed", fail = "failed", failing = "failed", feilet = "failed", feil = "failed",
|
|
running = "running", ["kjører"] = "running", active = "running",
|
|
succeeded = "succeeded", success = "succeeded", ok = "succeeded", vellykket = "succeeded",
|
|
cancelled = "cancelled", canceled = "cancelled", avbrutt = "cancelled", skipped = "skipped",
|
|
}
|
|
|
|
local function duration(ms)
|
|
local s = math.floor((ms or 0) / 1000)
|
|
if s < 60 then return s .. " s" end
|
|
if s < 3600 then return math.floor(s / 60) .. " m " .. s % 60 .. " s" end
|
|
return math.floor(s / 3600) .. " h " .. math.floor(s % 3600 / 60) .. " m"
|
|
end
|
|
|
|
local function ago(ms)
|
|
local s = math.max(0, os.time() - math.floor((ms or 0) / 1000))
|
|
if s < 60 then return toolshed.t("ago.now") end
|
|
if s < 3600 then return toolshed.t("ago.minutes", { count = math.floor(s / 60) }) end
|
|
if s < 86400 then return toolshed.t("ago.hours", { count = math.floor(s / 3600) }) end
|
|
return toolshed.t("ago.days", { count = math.floor(s / 86400) })
|
|
end
|
|
|
|
local function when(ms)
|
|
return os.date("%Y-%m-%d %H:%M", math.floor((ms or 0) / 1000))
|
|
end
|
|
|
|
local function run_length(run)
|
|
if run.run_duration and run.run_duration > 0 then return run.run_duration end
|
|
local ended = run.end_time and run.end_time > 0 and run.end_time or os.time() * 1000
|
|
return ended - (run.start_time or ended)
|
|
end
|
|
|
|
local function run_jig(w, run, several)
|
|
local how, tint = status(run.state)
|
|
local tags = { { text = toolshed.t("status." .. how), tint = tint } }
|
|
if several then table.insert(tags, { text = w.name, tint = "blue" }) end
|
|
return {
|
|
key = w.name .. ":run:" .. tostring(run.run_id), title = run.run_name or toolshed.t("run"), icon = "play",
|
|
subtitle = ago(run.start_time) .. " · " .. duration(run_length(run)), tags = tags, tool = "run",
|
|
data = { profile = w.name, id = string.format("%.0f", run.run_id), url = run.run_page_url },
|
|
}
|
|
end
|
|
|
|
-- run_rows are the latest runs of the chosen workspaces, newest first, those that are of how
|
|
-- typed says they went and named like the rest of it.
|
|
local function run_rows(settings, typed)
|
|
local only, words = nil, {}
|
|
for word in typed:lower():gmatch("%S+") do
|
|
if status_words[word] then only = status_words[word] else table.insert(words, word) end
|
|
end
|
|
local named = table.concat(words, " ")
|
|
local list, jigs = {}, {}
|
|
local ws = chosen(settings)
|
|
if #ws == 0 then return { problem(toolshed.t("error.no-workspace")) } end
|
|
for _, w in ipairs(ws) do
|
|
local ok, runs = pcall(latest_runs, w.name)
|
|
if not ok then
|
|
table.insert(jigs, problem(toolshed.t("error.runs", { workspace = w.name, reason = reason(runs) })))
|
|
else
|
|
for _, run in ipairs(runs) do
|
|
if (not only or status(run.state) == only) and (named == "" or toolshed.match(named, run.run_name or "") > 0) then
|
|
table.insert(list, { w = w, run = run })
|
|
end
|
|
end
|
|
end
|
|
end
|
|
table.sort(list, function(a, b) return (a.run.start_time or 0) > (b.run.start_time or 0) end)
|
|
for i = 1, math.min(#list, most) do table.insert(jigs, run_jig(list[i].w, list[i].run, #ws > 1)) end
|
|
if #jigs == 0 then table.insert(jigs, problem(toolshed.t("no-runs"))) end
|
|
return jigs
|
|
end
|
|
|
|
-- Pages ----------------------------------------------------------------------------------------
|
|
|
|
local function card(title, icon, tint, facts)
|
|
local rows = {}
|
|
for _, f in ipairs(facts) do
|
|
if f[2] and f[2] ~= "" then table.insert(rows, "<dt>" .. escape(toolshed.t(f[1])) .. "</dt><dd>" .. f[2] .. "</dd>") end
|
|
end
|
|
return '<div class="card wide" data-tint="' .. tint .. '"><div class="card-head"><span class="tile small" data-icon="' .. icon ..
|
|
'"></span>' .. escape(title) .. '</div><dl class="kv">' .. table.concat(rows) .. "</dl></div>"
|
|
end
|
|
|
|
local function page(html) return '<div class="page">' .. html .. "</div>" end
|
|
|
|
local function rows_at(section) return '<div data-rows="' .. escape(section) .. '" style="margin-top: 8px"></div>' end
|
|
|
|
local function tag(text, tint)
|
|
return '<span class="tag" data-tint="' .. tint .. '">' .. escape(text) .. "</span>"
|
|
end
|
|
|
|
-- within lists what the index has directly inside a catalog or schema, as rows of the page.
|
|
local function within(w, name, section, jigs)
|
|
local entries = index(w.name) or {}
|
|
local depth = select(2, name:gsub("%.", "")) + 1
|
|
local inside = {}
|
|
for _, e in ipairs(entries) do
|
|
if e.kind ~= "job" and e.name:sub(1, #name + 1) == name .. "." and select(2, e.name:gsub("%.", "")) == depth then
|
|
table.insert(inside, e)
|
|
end
|
|
end
|
|
table.sort(inside, function(a, b) return a.name < b.name end)
|
|
for _, e in ipairs(inside) do
|
|
local row = item_jig(w, e, false)
|
|
row.section = section
|
|
table.insert(jigs, row)
|
|
end
|
|
return #inside
|
|
end
|
|
|
|
-- type_icons show a column's type at a glance, by Unity Catalog's type_name: numbers, text,
|
|
-- yes or no, dates and times, and what holds other values.
|
|
local type_icons = {
|
|
BYTE = { "hash", "blue" }, SHORT = { "hash", "blue" }, INT = { "hash", "blue" }, LONG = { "hash", "blue" },
|
|
FLOAT = { "hash", "blue" }, DOUBLE = { "hash", "blue" }, DECIMAL = { "hash", "blue" },
|
|
STRING = { "type", "green" }, CHAR = { "type", "green" }, BINARY = { "binary", "gray" },
|
|
BOOLEAN = { "toggle-right", "purple" },
|
|
DATE = { "calendar", "orange" }, TIMESTAMP = { "calendar-clock", "orange" }, TIMESTAMP_NTZ = { "calendar-clock", "orange" },
|
|
INTERVAL = { "timer", "orange" },
|
|
ARRAY = { "brackets", "teal" }, STRUCT = { "braces", "teal" }, MAP = { "braces", "teal" }, VARIANT = { "braces", "teal" },
|
|
GEOGRAPHY = { "map-pin", "pink" }, GEOMETRY = { "map-pin", "pink" },
|
|
}
|
|
|
|
local function type_tile(type_name)
|
|
local icon = type_icons[type_name] or { "circle", "gray" }
|
|
return '<span class="tile small" data-icon="' .. icon[1] .. '" data-tint="' .. icon[2] .. '"></span>'
|
|
end
|
|
|
|
local function item_page(ctx)
|
|
local d = ctx.data
|
|
local w = workspace(d.profile)
|
|
-- html is the card; below, what's under it across the page: a table's columns, a schema's tables.
|
|
local jigs, html, below = {}, "", ""
|
|
if d.kind == "table" then
|
|
local t = cli(w.name, "tables", "get", d.name)
|
|
html = card(t.full_name, "table-2", "red", {
|
|
{ "fact.type", escape(t.table_type) .. (t.data_source_format and (" · " .. escape(t.data_source_format)) or "") },
|
|
{ "fact.comment", t.comment and escape(t.comment) },
|
|
{ "fact.owner", escape(t.owner) },
|
|
{ "fact.created", t.created_at and escape(when(t.created_at) .. " · " .. (t.created_by or "")) },
|
|
{ "fact.updated", t.updated_at and escape(when(t.updated_at) .. " · " .. (t.updated_by or "")) },
|
|
{ "fact.location", t.storage_location and escape(t.storage_location) },
|
|
})
|
|
-- Keys are tagged on their columns; the tables foreign keys point to are rows to open.
|
|
local primary, foreign, references = {}, {}, {}
|
|
for _, constraint in ipairs(t.table_constraints or {}) do
|
|
for _, name in ipairs((constraint.primary_key_constraint or {}).child_columns or {}) do primary[name] = true end
|
|
local fk = constraint.foreign_key_constraint
|
|
if fk then
|
|
for i, name in ipairs(fk.child_columns or {}) do
|
|
foreign[name] = fk.parent_table .. "." .. ((fk.parent_columns or {})[i] or "")
|
|
end
|
|
if not references[fk.parent_table] then
|
|
references[fk.parent_table] = true
|
|
local row = item_jig(w, { kind = "table", name = fk.parent_table, detail = "", comment = "" }, false)
|
|
row.section, row.subtitle = toolshed.t("references"), table.concat(fk.child_columns or {}, ", ") .. " → " .. fk.parent_table
|
|
table.insert(jigs, row)
|
|
end
|
|
end
|
|
end
|
|
-- The columns are facts to read, nothing to open, so they're a table rather than rows.
|
|
local lines = {}
|
|
for _, c in ipairs(t.columns or {}) do
|
|
local keys = ""
|
|
if primary[c.name] then keys = keys .. ' <span class="tag" data-tint="yellow" data-icon="key-round">PK</span>' end
|
|
if foreign[c.name] then
|
|
keys = keys .. ' <span class="tag" data-tint="blue" data-icon="link" title="' .. escape(foreign[c.name]) .. '">FK</span>'
|
|
end
|
|
table.insert(lines, '<div class="line"><div class="name"><span class="text">' .. escape(c.name) .. "</span>" .. keys ..
|
|
'</div><div class="name dim" style="font-size: inherit">' .. type_tile(c.type_name) .. '<span class="text">' .. escape(c.type_text or c.type_name) ..
|
|
'</span></div><div class="dim ellipsis">' .. escape(c.comment) .. "</div></div>")
|
|
end
|
|
if #lines > 0 then
|
|
below = '<div style="--columns: minmax(0, 1fr) minmax(0, 12em) minmax(0, 1.5fr); margin-top: 16px"><div class="head"><div>' ..
|
|
escape(toolshed.t("columns", { count = #lines })) .. "</div><div>" .. escape(toolshed.t("fact.type")) .. "</div><div>" ..
|
|
escape(toolshed.t("fact.comment")) .. "</div></div>" .. table.concat(lines) .. "</div>"
|
|
end
|
|
if #jigs > 0 then below = below .. rows_at(toolshed.t("references")) end
|
|
elseif d.kind == "volume" then
|
|
local v = cli(w.name, "volumes", "read", d.name)
|
|
html = card(v.full_name, "hard-drive", "red", {
|
|
{ "fact.type", escape(v.volume_type) },
|
|
{ "fact.path", escape("/Volumes/" .. d.name:gsub("%.", "/")) },
|
|
{ "fact.comment", v.comment and escape(v.comment) },
|
|
{ "fact.owner", escape(v.owner) },
|
|
{ "fact.created", v.created_at and escape(when(v.created_at)) },
|
|
{ "fact.location", v.storage_location and escape(v.storage_location) },
|
|
})
|
|
else
|
|
local what = d.kind == "schema" and cli(w.name, "schemas", "get", d.name) or cli(w.name, "catalogs", "get", d.name)
|
|
html = card(d.name, kind_icons[d.kind], "red", {
|
|
{ "fact.type", d.kind == "catalog" and what.catalog_type and escape(what.catalog_type) },
|
|
{ "fact.comment", what.comment and escape(what.comment) },
|
|
{ "fact.owner", escape(what.owner) },
|
|
{ "fact.created", what.created_at and escape(when(what.created_at)) },
|
|
{ "fact.location", what.storage_root and escape(what.storage_root) },
|
|
})
|
|
local section = toolshed.t(d.kind == "schema" and "inside.schema" or "inside.catalog")
|
|
if within(w, d.name, section, jigs) > 0 then below = rows_at(section) end
|
|
end
|
|
return { html = page('<div class="cards">' .. html .. "</div>" .. below), jigs = jigs }
|
|
end
|
|
|
|
local function explorer(w, d)
|
|
if d.kind == "volume" then return address(w, "/explore/data/volumes/" .. d.name:gsub("%.", "/")) end
|
|
return address(w, "/explore/data/" .. d.name:gsub("%.", "/"))
|
|
end
|
|
|
|
local function task_jig(w, task, section)
|
|
local how, tint = status(task.state)
|
|
local about = { duration(run_length(task)) }
|
|
-- A task tried again is listed once a try, so they're told apart.
|
|
if (task.attempt_number or 0) > 0 then table.insert(about, 1, toolshed.t("retry", { count = task.attempt_number })) end
|
|
local message = (task.state or {}).state_message
|
|
if message and message ~= "" then table.insert(about, message) end
|
|
return {
|
|
title = task.task_key, subtitle = table.concat(about, " · "),
|
|
icon = "play", section = section, tags = { { text = toolshed.t("status." .. how), tint = tint } },
|
|
tool = "run", action = "open", data = { profile = w.name, url = task.run_page_url },
|
|
}
|
|
end
|
|
|
|
local function run_page(ctx)
|
|
local w = workspace(ctx.data.profile)
|
|
local run = cli(w.name, "jobs", "get-run", ctx.data.id)
|
|
local how, tint = status(run.state)
|
|
local figures = '<div class="figures">' ..
|
|
'<div class="figure" data-tint="' .. tint .. '"><span class="tile" data-icon="play"></span><div class="value">' ..
|
|
escape(toolshed.t("status." .. how)) .. '</div><div class="label">' .. escape(toolshed.t("fact.status")) .. "</div></div>" ..
|
|
'<div class="figure" data-tint="blue"><span class="tile" data-icon="clock"></span><div class="value">' ..
|
|
escape(when(run.start_time)) .. '</div><div class="label">' .. escape(ago(run.start_time)) .. "</div></div>" ..
|
|
'<div class="figure" data-tint="blue"><span class="tile" data-icon="timer"></span><div class="value">' ..
|
|
escape(duration(run_length(run))) .. '</div><div class="label">' .. escape(toolshed.t("fact.duration")) .. "</div></div></div>"
|
|
local message = (run.state or {}).state_message
|
|
local note = message and message ~= "" and ('<div class="note">' .. escape(message) .. "</div>") or ""
|
|
-- A failed run says only to see the output, so the last failed task's error is fetched.
|
|
local failed
|
|
for _, task in ipairs(run.tasks or {}) do
|
|
if status(task.state) == "failed" then failed = task end
|
|
end
|
|
if failed then
|
|
local ok, out = pcall(cli, w.name, "jobs", "get-run-output", string.format("%.0f", failed.run_id))
|
|
if ok and out.error and out.error ~= "" then
|
|
note = note .. '<div class="note">' .. escape(toolshed.t("error-in", { task = failed.task_key })) .. '</div><div class="log">' ..
|
|
escape(out.error) .. "</div>"
|
|
end
|
|
end
|
|
local jigs = {}
|
|
local section = toolshed.t("tasks", { count = #(run.tasks or {}) })
|
|
for _, task in ipairs(run.tasks or {}) do table.insert(jigs, task_jig(w, task, section)) end
|
|
table.insert(jigs, 1, {
|
|
title = toolshed.t("action.open-job"), icon = "workflow", tool = "job",
|
|
data = { profile = w.name, id = string.format("%.0f", run.job_id), name = run.run_name },
|
|
})
|
|
return {
|
|
html = page(figures .. note .. (#jigs > 1 and rows_at(section) or "")),
|
|
jigs = jigs, refresh = how == "running" and 10 or nil,
|
|
}
|
|
end
|
|
|
|
local function job_page(ctx)
|
|
local w = workspace(ctx.data.profile)
|
|
local job = cli(w.name, "jobs", "get", ctx.data.id)
|
|
local s = job.settings or {}
|
|
local schedule = s.schedule and (s.schedule.quartz_cron_expression .. " · " .. (s.schedule.timezone_id or "") ..
|
|
(s.schedule.pause_status == "PAUSED" and (" " .. tag(toolshed.t("paused"), "gray")) or "")) or nil
|
|
local html = card(s.name or ctx.data.name, "workflow", "red", {
|
|
{ "fact.id", escape(ctx.data.id) },
|
|
{ "fact.schedule", schedule and escape(schedule) },
|
|
{ "fact.tasks", s.tasks and tostring(#s.tasks) },
|
|
{ "fact.created", job.created_time and escape(when(job.created_time)) },
|
|
})
|
|
local runs = cli(w.name, "jobs", "list-runs", "--job-id", ctx.data.id, "--limit", "25")
|
|
table.sort(runs, function(a, b) return (a.start_time or 0) > (b.start_time or 0) end)
|
|
local section = toolshed.t("recent-runs")
|
|
local jigs = {}
|
|
for _, run in ipairs(runs) do
|
|
local row = run_jig(w, run, false)
|
|
row.section = section
|
|
table.insert(jigs, row)
|
|
end
|
|
return { html = page('<div class="cards">' .. html .. "</div>" .. (#jigs > 0 and rows_at(section) or "")), jigs = jigs }
|
|
end
|
|
|
|
-- dbx ------------------------------------------------------------------------------------------
|
|
|
|
local verbs = {
|
|
{ name = "search", icon = "search" },
|
|
{ name = "runs", icon = "play" },
|
|
{ name = "jobs", icon = "workflow" },
|
|
}
|
|
|
|
-- verb_rows are the verbs what's typed could become, all of them for nothing: each types itself
|
|
-- after dbx, to go on from there.
|
|
local function verb_rows(word)
|
|
local rows = {}
|
|
for _, v in ipairs(verbs) do
|
|
if v.name:sub(1, #word) == word then
|
|
table.insert(rows, {
|
|
key = v.name, title = toolshed.t("verb." .. v.name .. ".title"), subtitle = toolshed.t("verb." .. v.name .. ".description"),
|
|
icon = v.icon, prefix = v.name .. " ", actions = {},
|
|
})
|
|
end
|
|
end
|
|
return rows
|
|
end
|
|
|
|
local function catalog_rows(settings, typed)
|
|
if typed == "" then return { problem(toolshed.t("search-hint")) } end
|
|
return found(settings, typed, { "catalog", "schema", "table", "volume" }, item_jig)
|
|
end
|
|
|
|
-- listed is what's typed after dbx: a verb and what to look for, or straight away what to find
|
|
-- in the catalogs. A word on its own also offers the verbs it could become ("ru" → runs).
|
|
local function listed(rest, settings)
|
|
local word, typed = rest:match("^(%S+)%s*(.-)%s*$")
|
|
if not word then return verb_rows("") end
|
|
word = word:lower()
|
|
if word == "search" then return catalog_rows(settings, typed) end
|
|
if word == "runs" or word == "run" then return run_rows(settings, typed) end
|
|
if word == "jobs" or word == "job" then
|
|
return found(settings, typed, { "job" }, job_jig)
|
|
end
|
|
local rows = typed == "" and verb_rows(word) or {}
|
|
for _, row in ipairs(catalog_rows(settings, rest:match("^%s*(.-)%s*$"))) do table.insert(rows, row) end
|
|
return rows
|
|
end
|
|
|
|
local function copy(text)
|
|
toolshed.clipboard.copy(text)
|
|
return toolshed.t("copied", { text = text })
|
|
end
|
|
|
|
return toolshed.toolbox {
|
|
key = "databricks",
|
|
title = "Databricks",
|
|
description = "i18n:toolbox.description",
|
|
icon = "assets/databricks.svg",
|
|
tint = "red",
|
|
requires = { { command = "databricks" }, { command = "jq" }, { path = "~/.databrickscfg" } },
|
|
settings = {
|
|
{
|
|
key = "workspace", title = "i18n:setting.workspace.title", description = "i18n:setting.workspace.description",
|
|
type = "choice", default = "DEFAULT",
|
|
options_from = function()
|
|
local options = {}
|
|
for _, w in ipairs(workspaces()) do
|
|
table.insert(options, { value = w.name, title = w.name .. " · " .. w.host:gsub("^https://", "") })
|
|
end
|
|
table.insert(options, { value = "*", title = toolshed.t("setting.workspace.all") })
|
|
return options
|
|
end,
|
|
},
|
|
},
|
|
tools = {
|
|
{
|
|
key = "search",
|
|
title = "i18n:tool.search.title",
|
|
description = "i18n:tool.search.description",
|
|
words = { "dbx", "databricks" },
|
|
keywords = { "databricks", "unity catalog", "catalog", "table", "schema", "volume", "job", "runs" },
|
|
examples = { "dbx sales", "dbx runs", "dbx runs failed", "dbx jobs ingest" },
|
|
items = true,
|
|
search = function(rest, settings) return listed(rest, settings) end,
|
|
-- Its rows are the other tools' (a table, a run, a job), which do what they're chosen for.
|
|
actions = { { key = "none", title = "i18n:tool.search.title", run = function() end } },
|
|
},
|
|
{
|
|
key = "item", title = "i18n:tool.item.title", hidden = true, items = true,
|
|
actions = {
|
|
{ key = "show", title = "i18n:action.show", icon = "info", run = item_page },
|
|
{ key = "open", title = "i18n:action.open", icon = "external-link", run = function(ctx)
|
|
toolshed.open(explorer(workspace(ctx.data.profile), ctx.data))
|
|
end },
|
|
{ key = "copy-name", title = "i18n:action.copy-name", icon = "copy", shortcut = "cmd+shift+c", run = function(ctx)
|
|
return copy(ctx.data.name)
|
|
end },
|
|
{ key = "copy-select", title = "i18n:action.copy-select", icon = "copy", run = function(ctx)
|
|
return copy("SELECT * FROM " .. ctx.data.name .. " LIMIT 100")
|
|
end },
|
|
{ key = "copy-path", title = "i18n:action.copy-path", icon = "copy", run = function(ctx)
|
|
return copy("/Volumes/" .. ctx.data.name:gsub("%.", "/"))
|
|
end },
|
|
},
|
|
},
|
|
{
|
|
key = "run", title = "i18n:tool.run.title", hidden = true, items = true,
|
|
actions = {
|
|
{ key = "show", title = "i18n:action.show", icon = "info", run = run_page },
|
|
{ key = "open", title = "i18n:action.open", icon = "external-link", run = function(ctx) toolshed.open(ctx.data.url) end },
|
|
{ key = "copy-url", title = "i18n:action.copy-url", icon = "copy", run = function(ctx) return copy(ctx.data.url) end },
|
|
},
|
|
},
|
|
{
|
|
key = "job", title = "i18n:tool.job.title", hidden = true, items = true,
|
|
actions = {
|
|
{ key = "show", title = "i18n:action.show", icon = "info", run = job_page },
|
|
{ key = "open", title = "i18n:action.open", icon = "external-link", run = function(ctx)
|
|
toolshed.open(address(workspace(ctx.data.profile), "/jobs/" .. ctx.data.id))
|
|
end },
|
|
{ key = "copy-name", title = "i18n:action.copy-name", icon = "copy", run = function(ctx) return copy(ctx.data.name) end },
|
|
},
|
|
},
|
|
},
|
|
}
|