() as table =>
let
Url = NexalisApiBaseUrl & "/find",
Token = NexalisApiReadToken,
ShowAttr = true,
SortMeta = true,
Raw = Web.Contents(
Url,
[
Headers = [
#"X-Warp10-Token" = Token
],
Query = [
selector = "nx.value{.app=nexalis}",
showattr = if ShowAttr then "true" else "false",
sortmeta = if SortMeta then "true" else "false"
]
]
),
// /find returns TEXT lines: class{labels}{attrs}
Txt = Text.FromBinary(Raw),
Lines = List.Select(
Lines.FromText(Txt),
each Text.Trim(_) <> "" and not Text.StartsWith(Text.Trim(_), "#")
),
T0 = Table.FromList(Lines, Splitter.SplitByNothing(), {"line"}, null, ExtraValues.Error),
// helper: extract the Nth {...} segment
GetSeg = (s as text, n as number) as nullable text =>
let
parts = Text.Split(s, "{"),
seg = if List.Count(parts) > n then Text.BeforeDelimiter(parts{n}, "}") else null
in
seg,
// labels are the 1st {...} and attrs are the 2nd {...}
T1 = Table.AddColumn(T0, "labelsText", each GetSeg([line], 1), type text),
T2 = Table.AddColumn(T1, "attrsText", each GetSeg([line], 2), type text),
// URL decode helper (turns %3D into =, %2F into /, etc.)
UrlDecode = (s as nullable text) as nullable text =>
if s = null then null
else try Uri.Parts("http://x/?" & "p=" & s)[Query][p] otherwise s,
// Convert "k=v,k2=v2" => record [k=v, k2=v2] (with URL decoding)
ToRecord = (kv as nullable text) as record =>
let
txt = if kv = null or Text.Trim(kv) = "" then "" else kv,
pairs = if txt = "" then {} else Text.Split(txt, ","),
kvs = List.Transform(pairs, each Text.Split(_, "=")),
good = List.Select(kvs, each List.Count(_) = 2),
keys = List.Transform(good, each UrlDecode(_{0})),
vals = List.Transform(good, each UrlDecode(_{1})),
rec = if List.Count(good) = 0 then [] else Record.FromList(vals, keys)
in
rec,
T3 = Table.AddColumn(T2, "labels_rec", each ToRecord([labelsText]), type record),
T4 = Table.AddColumn(T3, "attrs_rec", each ToRecord([attrsText]), type record),
// Pull required label fields (safe if missing)
T5 = Table.AddColumn(T4, "dataPoint", each try Record.Field([labels_rec], "dataPoint") otherwise null, type text),
T6 = Table.AddColumn(T5, "deviceID", each try Record.Field([labels_rec], "deviceID") otherwise null, type text),
T7 = Table.AddColumn(T6, "deviceModel", each try Record.Field([labels_rec], "deviceModel") otherwise null, type text),
T8 = Table.AddColumn(T7, "siteName", each try Record.Field([labels_rec], "siteName") otherwise null, type text),
// Pull required attribute fields (safe if missing)
T9 = Table.AddColumn(T8, "dataObject", each try Record.Field([attrs_rec], "dataObject") otherwise null, type text),
T10 = Table.AddColumn(T9, "subDataObject", each try Record.Field([attrs_rec], "subDataObject") otherwise null, type text),
T11 = Table.AddColumn(T10, "protocol", each try Record.Field([attrs_rec], "protocol") otherwise null, type text),
T12 = Table.AddColumn(T11, "multiplier", each try Record.Field([attrs_rec], "multiplier") otherwise null, type text),
T13 = Table.AddColumn(T12, "logicalNode", each try Record.Field([attrs_rec], "logicalNode") otherwise null, type text),
T14 = Table.AddColumn(T13, "measurementType", each try Record.Field([attrs_rec], "measurementType") otherwise null, type text),
T15 = Table.AddColumn(T14, "description", each try Record.Field([attrs_rec], "description") otherwise null, type text),
T16 = Table.AddColumn(T15, "engUnits", each try Record.Field([attrs_rec], "engUnits") otherwise null, type text),
T17 = Table.AddColumn(T16, "adder", each try Record.Field([attrs_rec], "adder") otherwise null, type text),
T18 = Table.AddColumn(T17, "nx-agent-id", each try Record.Field([attrs_rec], "nx-agent-id") otherwise null, type text),
T19 = Table.AddColumn(T18, "assetType", each try Record.Field([attrs_rec], "assetType") otherwise null, type text),
T20 = Table.AddColumn(T19, "subDeviceID", each try Record.Field([attrs_rec], "subDeviceID") otherwise null, type text),
Result = Table.SelectColumns(
T20,
{
"dataPoint","deviceID","deviceModel","siteName",
"dataObject","subDataObject","protocol","multiplier","logicalNode",
"measurementType","description","engUnits","adder","nx-agent-id",
"assetType","subDeviceID"
}
)
in
Result