mirror of
https://github.com/fankes/beszel.git
synced 2025-10-20 10:19:27 +08:00
use new batch api for setting global alerts
This commit is contained in:
@@ -70,6 +70,10 @@ func (h *Hub) Run() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
// set general settings
|
||||||
|
settings := h.app.Settings()
|
||||||
|
// batch requests (for global alerts)
|
||||||
|
settings.Batch.Enabled = true
|
||||||
// set auth settings
|
// set auth settings
|
||||||
usersCollection, err := h.app.FindCollectionByNameOrId("users")
|
usersCollection, err := h.app.FindCollectionByNameOrId("users")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -5,7 +5,6 @@ import { AlertInfo, AlertRecord, SystemRecord } from "@/types"
|
|||||||
import { lazy, Suspense, useRef, useState } from "react"
|
import { lazy, Suspense, useRef, useState } from "react"
|
||||||
import { toast } from "../ui/use-toast"
|
import { toast } from "../ui/use-toast"
|
||||||
import { RecordOptions } from "pocketbase"
|
import { RecordOptions } from "pocketbase"
|
||||||
import { newQueue, Queue } from "@henrygd/queue"
|
|
||||||
import { Trans, t, Plural } from "@lingui/macro"
|
import { Trans, t, Plural } from "@lingui/macro"
|
||||||
|
|
||||||
interface AlertData {
|
interface AlertData {
|
||||||
@@ -20,8 +19,6 @@ interface AlertData {
|
|||||||
|
|
||||||
const Slider = lazy(() => import("@/components/ui/slider"))
|
const Slider = lazy(() => import("@/components/ui/slider"))
|
||||||
|
|
||||||
let queue: Queue
|
|
||||||
|
|
||||||
const failedUpdateToast = () =>
|
const failedUpdateToast = () =>
|
||||||
toast({
|
toast({
|
||||||
title: t`Failed to update alert`,
|
title: t`Failed to update alert`,
|
||||||
@@ -49,7 +46,7 @@ export function SystemAlert({
|
|||||||
} else if (checked) {
|
} else if (checked) {
|
||||||
pb.collection("alerts").create({
|
pb.collection("alerts").create({
|
||||||
system: system.id,
|
system: system.id,
|
||||||
user: pb.authStore.model!.id,
|
user: pb.authStore.record!.id,
|
||||||
name: data.key,
|
name: data.key,
|
||||||
value: value,
|
value: value,
|
||||||
min: min,
|
min: min,
|
||||||
@@ -88,11 +85,7 @@ export function SystemAlertGlobal({
|
|||||||
data.checked = false
|
data.checked = false
|
||||||
data.val = data.min = 0
|
data.val = data.min = 0
|
||||||
|
|
||||||
data.updateAlert = (checked: boolean, value: number, min: number) => {
|
data.updateAlert = async (checked: boolean, value: number, min: number) => {
|
||||||
if (!queue) {
|
|
||||||
queue = newQueue(5)
|
|
||||||
}
|
|
||||||
|
|
||||||
const { set, populatedSet } = systemsWithExistingAlerts.current
|
const { set, populatedSet } = systemsWithExistingAlerts.current
|
||||||
|
|
||||||
// if overwrite checked, make sure all alerts will be overwritten
|
// if overwrite checked, make sure all alerts will be overwritten
|
||||||
@@ -105,48 +98,57 @@ export function SystemAlertGlobal({
|
|||||||
min,
|
min,
|
||||||
triggered: false,
|
triggered: false,
|
||||||
}
|
}
|
||||||
for (let system of systems) {
|
|
||||||
// if overwrite is false and system is in set (alert existed), skip
|
|
||||||
if (!overwrite && set.has(system.id)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// find matching existing alert
|
|
||||||
const existingAlert = alerts.find((alert) => alert.system === system.id && data.key === alert.name)
|
|
||||||
// if first run, add system to set (alert already existed when global panel was opened)
|
|
||||||
if (existingAlert && !populatedSet && !overwrite) {
|
|
||||||
set.add(system.id)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
const requestOptions: RecordOptions = {
|
|
||||||
requestKey: system.id,
|
|
||||||
}
|
|
||||||
|
|
||||||
// checked - make sure alert is created or updated
|
// we can only send 50 in one batch
|
||||||
if (checked) {
|
let done = 0
|
||||||
if (existingAlert) {
|
|
||||||
// console.log('updating', system.name)
|
while (done < systems.length) {
|
||||||
queue
|
const batch = pb.createBatch()
|
||||||
.add(() => pb.collection("alerts").update(existingAlert.id, recordData, requestOptions))
|
let batchSize = 0
|
||||||
.catch(failedUpdateToast)
|
|
||||||
} else {
|
for (let i = done; i < Math.min(done + 50, systems.length); i++) {
|
||||||
// console.log('creating', system.name)
|
const system = systems[i]
|
||||||
queue
|
// if overwrite is false and system is in set (alert existed), skip
|
||||||
.add(() =>
|
if (!overwrite && set.has(system.id)) {
|
||||||
pb.collection("alerts").create(
|
continue
|
||||||
{
|
|
||||||
system: system.id,
|
|
||||||
user: pb.authStore.model!.id,
|
|
||||||
name: data.key,
|
|
||||||
...recordData,
|
|
||||||
},
|
|
||||||
requestOptions
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.catch(failedUpdateToast)
|
|
||||||
}
|
}
|
||||||
} else if (existingAlert) {
|
// find matching existing alert
|
||||||
// console.log('deleting', system.name)
|
const existingAlert = alerts.find((alert) => alert.system === system.id && data.key === alert.name)
|
||||||
queue.add(() => pb.collection("alerts").delete(existingAlert.id)).catch(failedUpdateToast)
|
// if first run, add system to set (alert already existed when global panel was opened)
|
||||||
|
if (existingAlert && !populatedSet && !overwrite) {
|
||||||
|
set.add(system.id)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
batchSize++
|
||||||
|
const requestOptions: RecordOptions = {
|
||||||
|
requestKey: system.id,
|
||||||
|
}
|
||||||
|
|
||||||
|
// checked - make sure alert is created or updated
|
||||||
|
if (checked) {
|
||||||
|
if (existingAlert) {
|
||||||
|
batch.collection("alerts").update(existingAlert.id, recordData, requestOptions)
|
||||||
|
} else {
|
||||||
|
batch.collection("alerts").create(
|
||||||
|
{
|
||||||
|
system: system.id,
|
||||||
|
user: pb.authStore.record!.id,
|
||||||
|
name: data.key,
|
||||||
|
...recordData,
|
||||||
|
},
|
||||||
|
requestOptions
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else if (existingAlert) {
|
||||||
|
batch.collection("alerts").delete(existingAlert.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
batchSize && batch.send()
|
||||||
|
} catch (e) {
|
||||||
|
failedUpdateToast()
|
||||||
|
} finally {
|
||||||
|
done += 50
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
systemsWithExistingAlerts.current.populatedSet = true
|
systemsWithExistingAlerts.current.populatedSet = true
|
||||||
|
Reference in New Issue
Block a user