1 | |
2 |
|
3 | |
4 | * Create search job |
5 | * Start an SPL search job and return its search ID (sid). A query that does not start with "search" or "|" gets "search " prepended, so a query starting with a macro that expands to a generating command needs its leading "|". |
6 | */ |
7 | export async function main( |
8 | auth: RT.Splunk, |
9 | query: string, |
10 | earliest_time: string | undefined, |
11 | latest_time: string | undefined, |
12 | exec_mode: "normal" | "blocking" | undefined |
13 | ) { |
14 | const base = auth.base_url.replace(/\/+$/, "") |
15 | const trimmed = query.trim() |
16 | |
17 | |
18 | |
19 | const search = /^(search(\s|$)|\|)/i.test(trimmed) |
20 | ? trimmed |
21 | : `search ${trimmed}` |
22 |
|
23 | const body = new URLSearchParams({ search, output_mode: "json" }) |
24 | if (earliest_time) body.append("earliest_time", earliest_time) |
25 | if (latest_time) body.append("latest_time", latest_time) |
26 | if (exec_mode) body.append("exec_mode", exec_mode) |
27 |
|
28 | const response = await fetch(`${base}/services/search/jobs`, { |
29 | method: "POST", |
30 | headers: { |
31 | Authorization: `Bearer ${auth.token}`, |
32 | "Content-Type": "application/x-www-form-urlencoded", |
33 | Accept: "application/json", |
34 | }, |
35 | body, |
36 | }) |
37 |
|
38 | if (!response.ok) { |
39 | throw new Error(`${response.status} ${await response.text()}`) |
40 | } |
41 |
|
42 | return await response.json() |
43 | } |
44 |
|