add lazy ping option

This commit is contained in:
seriousm4x
2023-08-14 16:10:55 +02:00
parent c1dbde574a
commit d9b14adc8d
4 changed files with 78 additions and 5 deletions

View File

@@ -21,9 +21,9 @@ func RunPing(app *pocketbase.PocketBase) {
// init cronjob
CronPing = cron.New()
CronPing.AddFunc(settingsPrivateRecords[0].GetString("interval"), func() {
// skip cron if no realtime clients connected
// skip cron if no realtime clients connected and lazy_ping is turned on
realtimeClients := len(app.SubscriptionsBroker().Clients())
if realtimeClients == 0 {
if realtimeClients == 0 && settingsPrivateRecords[0].GetBool("lazy_ping") {
return
}
// expand ports field

View File

@@ -0,0 +1,48 @@
package migrations
import (
"encoding/json"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/models/schema"
)
func init() {
m.Register(func(db dbx.Builder) error {
dao := daos.New(db);
collection, err := dao.FindCollectionByNameOrId("nmj3ko20gzkg8n3")
if err != nil {
return err
}
// add
new_lazy_ping := &schema.SchemaField{}
json.Unmarshal([]byte(`{
"system": false,
"id": "yjyq5pvg",
"name": "lazy_ping",
"type": "bool",
"required": false,
"unique": false,
"options": {}
}`), new_lazy_ping)
collection.Schema.AddField(new_lazy_ping)
return dao.SaveCollection(collection)
}, func(db dbx.Builder) error {
dao := daos.New(db);
collection, err := dao.FindCollectionByNameOrId("nmj3ko20gzkg8n3")
if err != nil {
return err
}
// remove
collection.Schema.RemoveField("yjyq5pvg")
return dao.SaveCollection(collection)
})
}

View File

@@ -9,5 +9,6 @@ export type SettingsPublic = Record & {
export type SettingsPrivate = Record & {
interval: number;
lazy_ping: boolean;
scan_range: string;
};

View File

@@ -67,8 +67,17 @@
.collection('settings_public')
.update(settingsPubClone.id, settingsPubClone)
.then((res) => {
toast.success('Saved settings');
settingsPub.set(res as SettingsPublic);
})
.catch((err) => {
toast.error(err.message);
});
await $pocketbase
.collection('settings_private')
.update(settingsPrivClone.id, settingsPrivClone)
.then((res) => {
toast.success('Saved settings');
settingsPriv.set(res as SettingsPrivate);
goto('/');
})
.catch((err) => {
@@ -90,7 +99,7 @@
class="badge">@every 3s</span
>.
</p>
<p class="mb-2">
<p>
Learn more about the correct syntax for cron on
<a target="_blank" class="link" href="https://en.wikipedia.org/wiki/Cron">Wikipedia</a>
or refer to the
@@ -98,7 +107,7 @@
>package documentation</a
>.
</p>
<div class="form-control w-full">
<div class="form-control w-full mt-2">
<input
type="text"
placeholder="e.g. '@every 5s' or '@every 1m'"
@@ -106,6 +115,21 @@
bind:value={settingsPrivClone.interval}
/>
</div>
<h2 class="card-title mt-2">Lazy ping</h2>
<p class="mt-2">
When lazy ping is turned on, UpSnap will only ping devices if there is an active user
visiting the website. If it's turned off, UpSnap will always ping devices.
</p>
<div class="form-control w-fit">
<label class="label cursor-pointer gap-2">
<input
type="checkbox"
class="checkbox checkbox-primary"
bind:checked={settingsPrivClone.lazy_ping}
/>
<span class="label-text">Enable</span>
</label>
</div>
</div>
</div>
<div class="card w-full bg-base-300 shadow-xl mt-6">