1 | |
2 |
|
3 | const COLUMNS = [ |
4 | "id", |
5 | "status", |
6 | "finishStatus", |
7 | "loadedSize", |
8 | "unloadedSize", |
9 | "name", |
10 | "note", |
11 | "sites", |
12 | "fromArchive", |
13 | "loading", |
14 | "locked", |
15 | "deviceAddedCount", |
16 | "deviceRemovedCount", |
17 | "interfaceActiveCount", |
18 | "interfaceCount", |
19 | "interfaceEdgeCount", |
20 | "totalDevCount", |
21 | "isLastSnapshot", |
22 | "tsChange", |
23 | "tsEnd", |
24 | "tsStart", |
25 | "userCount", |
26 | ] |
27 |
|
28 | |
29 | * List snapshots |
30 | * List one page of snapshots from the snapshot table (tables/management/snapshots), newest first by default. Filters use the table filter syntax, e.g. {"and": [{"locked": ["eq", true]}]}. Returns the rows, the table metadata and next_start (null on the last page). |
31 | */ |
32 | export async function main( |
33 | auth: RT.IpFabric, |
34 | filters: { [key: string]: any } | undefined, |
35 | sort_order: "asc" | "desc" | undefined, |
36 | limit: number | undefined, |
37 | start: number | undefined |
38 | ) { |
39 | const pageLimit = limit || 100 |
40 | const pageStart = start || 0 |
41 |
|
42 | const version = (auth.api_version ?? "").trim() |
43 | |
44 | const match = version.match(/^v?(\d+)(?:\.(\d+))?/) |
45 | if (version && !match) { |
46 | throw new Error( |
47 | `api_version "${version}" is not valid, use v#.# (e.g. v7.2)` |
48 | ) |
49 | } |
50 | const prefix = match ? `api/v${match[1]}.${match[2] ?? "0"}/` : "api/" |
51 | const url = new URL( |
52 | `${prefix}tables/management/snapshots`, |
53 | auth.base_url.replace(/\/*$/, "/") |
54 | ) |
55 |
|
56 | const response = await fetch(url, { |
57 | method: "POST", |
58 | headers: { |
59 | "X-API-Token": auth.token, |
60 | "Content-Type": "application/json", |
61 | Accept: "application/json", |
62 | }, |
63 | body: JSON.stringify({ |
64 | columns: COLUMNS, |
65 | filters: filters ?? {}, |
66 | sort: { column: "tsEnd", order: sort_order || "desc" }, |
67 | pagination: { limit: pageLimit, start: pageStart }, |
68 | }), |
69 | }) |
70 |
|
71 | if (!response.ok) { |
72 | throw new Error(`${response.status} ${await response.text()}`) |
73 | } |
74 |
|
75 | const result = (await response.json()) as { |
76 | data: { [key: string]: any }[] |
77 | _meta: { [key: string]: any } |
78 | } |
79 | return { |
80 | ...result, |
81 | next_start: |
82 | result.data.length === pageLimit ? pageStart + result.data.length : null, |
83 | } |
84 | } |
85 |
|