use new batch api for setting global alerts

This commit is contained in:
Henry Dollman
2024-11-27 17:07:44 -05:00
parent ed01752546
commit 87f7390eca
2 changed files with 55 additions and 49 deletions

View File

@@ -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 {

View File

@@ -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,7 +98,16 @@ export function SystemAlertGlobal({
min, min,
triggered: false, triggered: false,
} }
for (let system of systems) {
// we can only send 50 in one batch
let done = 0
while (done < systems.length) {
const batch = pb.createBatch()
let batchSize = 0
for (let i = done; i < Math.min(done + 50, systems.length); i++) {
const system = systems[i]
// if overwrite is false and system is in set (alert existed), skip // if overwrite is false and system is in set (alert existed), skip
if (!overwrite && set.has(system.id)) { if (!overwrite && set.has(system.id)) {
continue continue
@@ -117,6 +119,7 @@ export function SystemAlertGlobal({
set.add(system.id) set.add(system.id)
continue continue
} }
batchSize++
const requestOptions: RecordOptions = { const requestOptions: RecordOptions = {
requestKey: system.id, requestKey: system.id,
} }
@@ -124,29 +127,28 @@ export function SystemAlertGlobal({
// checked - make sure alert is created or updated // checked - make sure alert is created or updated
if (checked) { if (checked) {
if (existingAlert) { if (existingAlert) {
// console.log('updating', system.name) batch.collection("alerts").update(existingAlert.id, recordData, requestOptions)
queue
.add(() => pb.collection("alerts").update(existingAlert.id, recordData, requestOptions))
.catch(failedUpdateToast)
} else { } else {
// console.log('creating', system.name) batch.collection("alerts").create(
queue
.add(() =>
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,
...recordData, ...recordData,
}, },
requestOptions requestOptions
) )
)
.catch(failedUpdateToast)
} }
} else if (existingAlert) { } else if (existingAlert) {
// console.log('deleting', system.name) batch.collection("alerts").delete(existingAlert.id)
queue.add(() => pb.collection("alerts").delete(existingAlert.id)).catch(failedUpdateToast) }
}
try {
batchSize && batch.send()
} catch (e) {
failedUpdateToast()
} finally {
done += 50
} }
} }
systemsWithExistingAlerts.current.populatedSet = true systemsWithExistingAlerts.current.populatedSet = true