Monitoring Settings

This commit is contained in:
headlessdev 2025-05-31 12:36:58 +02:00
parent 307062b5be
commit 6c1fca1377
12 changed files with 303 additions and 22 deletions

View File

@ -3,8 +3,8 @@ import prisma from "@/app/prisma";
import { z } from "zod/v4";
const schema = z.object({
frequency: z.number(),
checksUntilOffline: z.number(),
frequency: z.number().min(1, { message: "Frequency must be greater than 0" }).max(1000, { message: "Frequency must be less than 1000" }),
checksUntilOffline: z.number().min(1, { message: "Checks until offline must be greater than 0" }).max(1000, { message: "Checks until offline must be less than 1000" }),
})
export async function POST(request: NextRequest) {

View File

@ -0,0 +1,18 @@
import { NextRequest, NextResponse } from "next/server";
import prisma from "@/app/prisma";
export async function GET(request: NextRequest) {
try {
const generalSettings = await prisma.generalApplicationMonitoringSettings.findFirst({
where: {
applicationSettings: {
none: {}
}
}
});
return NextResponse.json(generalSettings, { status: 200 });
} catch (error) {
return NextResponse.json({ error: "Failed to get general settings" }, { status: 500 });
}
}

View File

@ -3,8 +3,8 @@ import prisma from "@/app/prisma";
import { z } from "zod/v4";
const schema = z.object({
frequency: z.number(),
checksUntilOffline: z.number(),
frequency: z.number().min(1, { message: "Frequency must be greater than 0" }).max(1000, { message: "Frequency must be less than 1000" }),
checksUntilOffline: z.number().min(1, { message: "Checks until offline must be greater than 0" }).max(1000, { message: "Checks until offline must be less than 1000" }),
})
export async function POST(request: NextRequest) {

View File

@ -0,0 +1,18 @@
import { NextRequest, NextResponse } from "next/server";
import prisma from "@/app/prisma";
export async function GET(request: NextRequest) {
try {
const generalSettings = await prisma.generalServerMonitoringSettings.findFirst({
where: {
serverSettings: {
none: {}
}
}
});
return NextResponse.json(generalSettings, { status: 200 });
} catch (error) {
return NextResponse.json({ error: "Failed to get general settings" }, { status: 500 });
}
}

View File

@ -1,8 +1,8 @@
"use client"
import { useEffect, useState } from "react"
import useNotifications from "@/hooks/useNotifications"
import { Check, CircleHelp, Copy, Server, Smartphone } from "lucide-react"
import useMonitoring from "@/hooks/useMonitoring"
import { Check, CircleHelp, Copy, Server, Settings, Smartphone } from "lucide-react"
interface MonitoringSettingsProps {
onError: (message: string) => void
@ -10,5 +10,178 @@ interface MonitoringSettingsProps {
}
export const MonitoringSettings = ({ onError, onSuccess }: MonitoringSettingsProps) => {
return <div>MonitoringSettings</div>
const { getGeneralApplicationMonitoringSettings, getGeneralServerMonitoringSettings, editGeneralApplicationMonitoringSettings, editGeneralServerMonitoringSettings } = useMonitoring();
const [generalApplicationMonitoringSettings, setGeneralApplicationMonitoringSettings] = useState<any>(null);
const [generalServerMonitoringSettings, setGeneralServerMonitoringSettings] = useState<any>(null);
const fetchGeneralMonitoringSettings = async () => {
const generalApplicationMonitoringSettings = await getGeneralApplicationMonitoringSettings();
const generalServerMonitoringSettings = await getGeneralServerMonitoringSettings();
if (typeof generalApplicationMonitoringSettings === 'string' || typeof generalServerMonitoringSettings === 'string') {
onError(typeof generalApplicationMonitoringSettings === 'string' ? generalApplicationMonitoringSettings : generalServerMonitoringSettings as string);
return;
}
setGeneralApplicationMonitoringSettings(
generalApplicationMonitoringSettings || {
frequency: 10,
checksUntilOffline: 1,
}
);
setGeneralServerMonitoringSettings(
generalServerMonitoringSettings || {
frequency: 10,
checksUntilOffline: 1,
}
);
}
useEffect(() => {
fetchGeneralMonitoringSettings();
}, []);
return(
<div className="w-full bg-base-200 p-6 rounded-2xl border border-stone-800">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 mb-6">
<div className="flex items-center gap-3">
<div className="p-2 bg-primary/10 rounded-lg">
<Settings className="w-6 h-6 text-primary" />
</div>
<div>
<h2 className="text-xl font-bold">Monitoring Settings</h2>
<p className="text-sm opacity-70">Manage your monitoring settings</p>
</div>
</div>
</div>
<div className="space-y-8">
{/* Server Monitoring Section */}
<div className="card bg-base-100 shadow-lg border border-base-300">
<div className="card-body p-6">
<div className="flex items-center gap-3 mb-6">
<div className="p-2 bg-primary/10 rounded-lg">
<Server className="w-6 h-6 text-primary" />
</div>
<div>
<h3 className="text-lg font-bold">Server Monitoring</h3>
<p className="text-sm opacity-70">Configure server monitoring frequency and thresholds</p>
</div>
</div>
<div className="space-y-4">
<div className="card bg-base-200">
<div className="card-body p-4">
<div className="flex flex-col lg:flex-row lg:items-center gap-4">
<div className="flex-1">
<label className="label">
<span className="label-text font-medium">Check Frequency (every x seconds)</span>
</label>
<input
type="number"
className="input input-bordered w-full"
value={generalServerMonitoringSettings?.frequency}
onChange={(e) => setGeneralServerMonitoringSettings({
...generalServerMonitoringSettings,
frequency: parseInt(e.target.value)
})}
min="1"
/>
</div>
<div className="flex-1">
<label className="label">
<span className="label-text font-medium">Checks Until Offline</span>
</label>
<input
type="number"
className="input input-bordered w-full"
value={generalServerMonitoringSettings?.checksUntilOffline}
onChange={(e) => setGeneralServerMonitoringSettings({
...generalServerMonitoringSettings,
checksUntilOffline: parseInt(e.target.value)
})}
min="1"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{/* Application Monitoring Section */}
<div className="card bg-base-100 shadow-lg border border-base-300">
<div className="card-body p-6">
<div className="flex items-center gap-3 mb-6">
<div className="p-2 bg-secondary/10 rounded-lg">
<Smartphone className="w-6 h-6 text-secondary" />
</div>
<div>
<h3 className="text-lg font-bold">Application Monitoring</h3>
<p className="text-sm opacity-70">Configure application monitoring frequency and thresholds</p>
</div>
</div>
<div className="space-y-4">
<div className="card bg-base-200">
<div className="card-body p-4">
<div className="flex flex-col lg:flex-row lg:items-center gap-4">
<div className="flex-1">
<label className="label">
<span className="label-text font-medium">Check Frequency (every x seconds)</span>
</label>
<input
type="number"
className="input input-bordered w-full"
value={generalApplicationMonitoringSettings?.frequency}
onChange={(e) => setGeneralApplicationMonitoringSettings({
...generalApplicationMonitoringSettings,
frequency: parseInt(e.target.value)
})}
min="1"
/>
</div>
<div className="flex-1">
<label className="label">
<span className="label-text font-medium">Checks Until Offline</span>
</label>
<input
type="number"
className="input input-bordered w-full"
value={generalApplicationMonitoringSettings?.checksUntilOffline}
onChange={(e) => setGeneralApplicationMonitoringSettings({
...generalApplicationMonitoringSettings,
checksUntilOffline: parseInt(e.target.value)
})}
min="1"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{/* Save Button */}
<div className="flex justify-end">
<button
className="btn btn-primary"
onClick={async () => {
const serverResponse = await editGeneralServerMonitoringSettings(generalServerMonitoringSettings);
const appResponse = await editGeneralApplicationMonitoringSettings(generalApplicationMonitoringSettings);
if (typeof serverResponse === 'string' || typeof appResponse === 'string') {
onError(typeof serverResponse === 'string' ? serverResponse : appResponse as string);
return;
}
onSuccess('Monitoring settings saved successfully');
}}
>
Save Settings
</button>
</div>
</div>
</div>
)
}

62
hooks/useMonitoring.ts Normal file
View File

@ -0,0 +1,62 @@
import { useState, useEffect, useCallback } from "react";
import axios from "axios";
interface MonitoringSettings {
frequency: number;
checksUntilOffline: number;
}
const useMonitoring = () => {
const getGeneralApplicationMonitoringSettings = (): Promise<any> | string => {
return axios.get('/api/monitoring/applications/get_general')
.then((response) => {
if(response.data) {
return response.data
} else {
return
}
})
.catch((error) => {
return error.response.data.error
});
}
const getGeneralServerMonitoringSettings = (): Promise<any> | string => {
return axios.get('/api/monitoring/servers/get_general')
.then((response) => {
if(response.data) {
return response.data
} else {
return
}
})
.catch((error) => {
return error.response.data.error
});
}
const editGeneralApplicationMonitoringSettings = (settings: MonitoringSettings): Promise<any> | string => {
return axios.post('/api/monitoring/applications/edit_general', settings)
.then((response) => {
return response.data
})
.catch((error) => {
return error.response.data.error
});
}
const editGeneralServerMonitoringSettings = (settings: MonitoringSettings): Promise<any> | string => {
return axios.post('/api/monitoring/servers/edit_general', settings)
.then((response) => {
return response.data
})
.catch((error) => {
return error.response.data.error
});
}
return { getGeneralApplicationMonitoringSettings, getGeneralServerMonitoringSettings, editGeneralApplicationMonitoringSettings, editGeneralServerMonitoringSettings };
};
export default useMonitoring;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
{
"name": "prisma-client-f1235714bbffddc1d2e6784ff382367dc9d016612d156baafb10f4a6136ed602",
"name": "prisma-client-d1e43606f7a3013467af671b0e6bb25d802256e22d857bc4d3e40d92038c4fbf",
"main": "index.js",
"types": "index.d.ts",
"browser": "index-browser.js",

Binary file not shown.

View File

@ -5,8 +5,9 @@
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
generator client {
provider = "prisma-client-js"
output = "./generated/prisma"
provider = "prisma-client-js"
output = "./generated/prisma"
binaryTargets = ["native", "windows"]
}
datasource db {

View File

@ -7,6 +7,7 @@
generator client {
provider = "prisma-client-js"
output = "./generated/prisma"
binaryTargets = ["native", "windows"]
}
datasource db {