|
1 | 1 | <script> |
2 | 2 | import { onMount } from 'svelte'; |
| 3 | + import Swal from 'sweetalert2'; |
3 | 4 | import { Card, CardBody, Input, Button } from '@sveltestrap/sveltestrap'; |
4 | | - import { getAgentRuleOptions } from '$lib/services/agent-service'; |
| 5 | + import { getAgentRuleOptions, generateAgentCodeScript } from '$lib/services/agent-service'; |
5 | 6 | import Markdown from '$lib/common/markdown/Markdown.svelte'; |
6 | 7 | import BotsharpTooltip from '$lib/common/tooltip/BotsharpTooltip.svelte'; |
7 | | - import { ADMIN_ROLES } from '$lib/helpers/constants'; |
| 8 | + import LoadingToComplete from '$lib/common/LoadingToComplete.svelte'; |
| 9 | + import { ADMIN_ROLES } from '$lib/helpers/constants'; |
| 10 | + import { AgentCodeScriptType } from '$lib/helpers/enums'; |
8 | 11 |
|
9 | 12 | const limit = 100; |
10 | 13 | const textLimit = 1024; |
11 | | -
|
| 14 | + |
| 15 | + /** @type {boolean} */ |
| 16 | + let isLoading = false; |
| 17 | + /** @type {boolean} */ |
| 18 | + let isComplete = false; |
| 19 | + /** @type {boolean} */ |
| 20 | + let isError = false; |
| 21 | + /** @type {number} */ |
| 22 | + let duration = 2000; |
12 | 23 | let windowWidth = 0; |
13 | 24 | let windowHeight = 0; |
14 | 25 |
|
| 26 | + /** @type {string} */ |
| 27 | + let successText = ''; |
| 28 | + let errorText = ''; |
| 29 | +
|
15 | 30 | /** @type {import('$agentTypes').AgentModel} */ |
16 | 31 | export let agent; |
17 | 32 |
|
|
145 | 160 | handleAgentChange(); |
146 | 161 | } |
147 | 162 |
|
| 163 | + /** |
| 164 | + * @param {import("$agentTypes").AgentRule} rule |
| 165 | + */ |
| 166 | + function compileCodeScript(rule) { |
| 167 | + Swal.fire({ |
| 168 | + title: 'Are you sure?', |
| 169 | + html: ` |
| 170 | + <div> |
| 171 | + <p>Are you sure you want to generate code script <b>"${rule.trigger_name}_rule.py"</b>?</p> |
| 172 | + <p>This action will overwrite existing code script if any.</p> |
| 173 | + </div> |
| 174 | + `, |
| 175 | + icon: 'warning', |
| 176 | + showCancelButton: true, |
| 177 | + cancelButtonText: 'No', |
| 178 | + confirmButtonText: 'Yes' |
| 179 | + }).then(async (result) => { |
| 180 | + if (result.value) { |
| 181 | + generateCodeScript(rule); |
| 182 | + } |
| 183 | + }); |
| 184 | + } |
| 185 | +
|
| 186 | + /** |
| 187 | + * @param {import("$agentTypes").AgentRule} rule |
| 188 | + */ |
| 189 | + function generateCodeScript(rule) { |
| 190 | + return new Promise((resolve, reject) => { |
| 191 | + isLoading = true; |
| 192 | + generateAgentCodeScript(agent.id, { |
| 193 | + text: rule.criteria, |
| 194 | + options: { |
| 195 | + save_to_db: true, |
| 196 | + script_name: `${rule.trigger_name}_rule.py`, |
| 197 | + script_type: AgentCodeScriptType.Src, |
| 198 | + // to do: |
| 199 | + // agent_id: agent.id, |
| 200 | + // template_name: "rule" |
| 201 | + } |
| 202 | + }).then(res => { |
| 203 | + if (res?.success) { |
| 204 | + isLoading = false; |
| 205 | + isComplete = true; |
| 206 | + successText = "Code script has been generated!"; |
| 207 | + setTimeout(() => { |
| 208 | + isComplete = false; |
| 209 | + successText = ""; |
| 210 | + }, duration); |
| 211 | + resolve(res); |
| 212 | + } else { |
| 213 | + throw "error when generating code script."; |
| 214 | + } |
| 215 | + }).catch(() => { |
| 216 | + isLoading = false; |
| 217 | + isComplete = false; |
| 218 | + isError = true; |
| 219 | + errorText = "Failed to generate code script."; |
| 220 | + setTimeout(() => { |
| 221 | + isError = false; |
| 222 | + errorText = ""; |
| 223 | + }, duration); |
| 224 | + reject(); |
| 225 | + }); |
| 226 | + }); |
| 227 | + } |
| 228 | +
|
148 | 229 |
|
149 | 230 | /** @param {import('$agentTypes').AgentRule[]} list */ |
150 | 231 | function innerRefresh(list) { |
|
176 | 257 |
|
177 | 258 | <svelte:window on:resize={() => resizeWindow()}/> |
178 | 259 |
|
| 260 | +<LoadingToComplete {isLoading} {isComplete} {isError} {successText} {errorText} /> |
| 261 | +
|
179 | 262 | <Card> |
180 | 263 | <CardBody> |
181 | 264 | <div class="text-center"> |
|
248 | 331 | data-bs-placement="top" |
249 | 332 | title="Compile code script" |
250 | 333 | > |
251 | | - <i class="mdi mdi-file-code" /> |
| 334 | + <i |
| 335 | + class="mdi mdi-file-code" |
| 336 | + role="link" |
| 337 | + tabindex="0" |
| 338 | + on:keydown={() => {}} |
| 339 | + on:click={() => compileCodeScript(rule)} |
| 340 | + /> |
252 | 341 | </div> |
253 | 342 | {/if} |
254 | 343 | </div> |
|
0 commit comments