Compare commits
5 Commits
amalia/27-
...
amalia/07-
| Author | SHA1 | Date | |
|---|---|---|---|
| 5fd5c15394 | |||
| cb565ba0bd | |||
| 940fa5a5b7 | |||
| 0b9f07e543 | |||
| 8440374424 |
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "Village" ADD COLUMN "isDummy" BOOLEAN NOT NULL DEFAULT false;
|
||||
@@ -51,6 +51,7 @@ model Village {
|
||||
name String
|
||||
desc String @db.Text
|
||||
isActive Boolean @default(true)
|
||||
isDummy Boolean @default(false)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
Group Group[]
|
||||
|
||||
628
src/app/api/monitoring/[[...slug]]/route.ts
Normal file
628
src/app/api/monitoring/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,628 @@
|
||||
import formatDateTime from "@/lib/formatDateTime";
|
||||
import { prisma } from "@/module/_global";
|
||||
import cors from "@elysiajs/cors";
|
||||
import { swagger } from "@elysiajs/swagger";
|
||||
import Elysia, { t } from "elysia";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import "moment/locale/id";
|
||||
|
||||
// Gabungkan semua ke dalam satu instance server yang dipasang di /api/monitoring
|
||||
const MonitoringServer = new Elysia({ prefix: "/api/monitoring" })
|
||||
.use(cors({
|
||||
origin: "*",
|
||||
methods: ["GET", "POST", "OPTIONS"],
|
||||
}))
|
||||
.use(swagger({
|
||||
path: "/docs", // Karena prefix instance adalah /api/monitoring, maka ini akan diakses di /api/monitoring/docs
|
||||
documentation: {
|
||||
info: {
|
||||
title: "Des Plus - Monitoring API",
|
||||
version: "1.0.0",
|
||||
description: "API Khusus untuk kebutuhan Dashboard Monitoring",
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
.get(
|
||||
"/grid-overview",
|
||||
async ({ query, set }) => {
|
||||
try {
|
||||
const version = await prisma.setting.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
value: true
|
||||
}
|
||||
});
|
||||
|
||||
const result_version = Object.fromEntries(version.map(item => [item.id, item.value]));
|
||||
|
||||
const activity_today = await prisma.userLog.count({
|
||||
where: {
|
||||
createdAt: {
|
||||
gte: moment().subtract(1, 'days').toDate(),
|
||||
lte: moment().toDate(),
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const activity_yesterday = await prisma.userLog.count({
|
||||
where: {
|
||||
createdAt: {
|
||||
gte: moment().subtract(2, 'days').toDate(),
|
||||
lte: moment().subtract(1, 'days').toDate(),
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
const activity_increase = (activity_today - activity_yesterday);
|
||||
const percentage_increase = (activity_increase / activity_yesterday) * 100
|
||||
|
||||
const total_village = await prisma.village.findMany({
|
||||
where: {
|
||||
isDummy: false
|
||||
}
|
||||
})
|
||||
|
||||
const total_village_active = total_village.filter((item) => item.isActive).length
|
||||
const total_village_inactive = total_village.filter((item) => !item.isActive).length
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
data: {
|
||||
version: result_version,
|
||||
activity: {
|
||||
today: activity_today,
|
||||
increase: _.isNaN(percentage_increase) ? 0 : percentage_increase.toFixed(2),
|
||||
},
|
||||
village: {
|
||||
active: total_village_active,
|
||||
inactive: total_village_inactive,
|
||||
},
|
||||
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("[overview] grid-overview error:", error);
|
||||
set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan pada server",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
detail: {
|
||||
summary: "Grid Overview",
|
||||
description: "Menu Overview - Mendapatkan daftar versi aplikasi.",
|
||||
tags: ["overview"],
|
||||
},
|
||||
}
|
||||
)
|
||||
.get(
|
||||
"/daily-activity",
|
||||
async ({ query, set }) => {
|
||||
try {
|
||||
// const data = await prisma.userLog.findMany({
|
||||
// where: {
|
||||
// User: {
|
||||
// Village: {
|
||||
// isDummy: false
|
||||
// }
|
||||
// },
|
||||
// createdAt: {
|
||||
// gte: moment().subtract(7, 'days').toDate(),
|
||||
// lte: moment().toDate(),
|
||||
// }
|
||||
// },
|
||||
// select: {
|
||||
// createdAt: true,
|
||||
// }
|
||||
// })
|
||||
|
||||
const data = await prisma.$queryRaw`
|
||||
SELECT
|
||||
DATE(ul."createdAt") AS tanggal,
|
||||
COUNT(*) AS total
|
||||
FROM "UserLog" ul
|
||||
JOIN "User" u ON ul."idUser" = u."id"
|
||||
JOIN "Village" v ON u."idVillage" = v."id"
|
||||
WHERE v."isDummy" = false
|
||||
AND ul."createdAt" >= NOW() - INTERVAL '7 days'
|
||||
GROUP BY tanggal
|
||||
ORDER BY tanggal;` as any[];
|
||||
|
||||
const result = [];
|
||||
|
||||
// ubah data ke map biar gampang lookup
|
||||
const map = data.reduce((acc: any, item: any) => {
|
||||
const key = moment(item.tanggal).format('YYYY-MM-DD');
|
||||
acc[key] = Number(item.total);
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
// generate 7 hari terakhir
|
||||
for (let i = 6; i >= 0; i--) {
|
||||
const date = moment().subtract(i, 'days');
|
||||
|
||||
const key = date.format('YYYY-MM-DD');
|
||||
|
||||
result.push({
|
||||
date: date.format('DD MMM'),
|
||||
logs: map[key] || 0
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("[overview] daily-activity error:", error);
|
||||
set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan pada server",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
detail: {
|
||||
summary: "Daily Activity",
|
||||
description: "Menu Overview - Mendapatkan data grafik aktivitas harian semua desa.",
|
||||
tags: ["overview"],
|
||||
},
|
||||
}
|
||||
)
|
||||
.get(
|
||||
"/comparison-activity",
|
||||
async ({ query, set }) => {
|
||||
try {
|
||||
const data = await prisma.$queryRaw`
|
||||
SELECT
|
||||
v."name",
|
||||
COUNT(ul."id") AS total_logs
|
||||
FROM "UserLog" ul
|
||||
JOIN "User" u ON ul."idUser" = u."id"
|
||||
JOIN "Village" v ON u."idVillage" = v."id"
|
||||
WHERE v."isDummy" = false
|
||||
AND ul."createdAt" >= NOW() - INTERVAL '7 days'
|
||||
GROUP BY v."id", v."name"
|
||||
ORDER BY total_logs DESC;
|
||||
` as any[];
|
||||
|
||||
const result = data.map(item => ({
|
||||
village: item.name,
|
||||
activity: Number(item.total_logs)
|
||||
}));
|
||||
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("[overview] comparison-activity error:", error);
|
||||
set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan pada server",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
detail: {
|
||||
summary: "Comparison Activity",
|
||||
description: "Menu Overview - Mendapatkan data grafik perbandingan aktivitas desa selama 7 hari terakhir.",
|
||||
tags: ["overview"],
|
||||
},
|
||||
}
|
||||
)
|
||||
.get(
|
||||
"/get-villages",
|
||||
async ({ query, set }) => {
|
||||
const { search, page } = query;
|
||||
const pageNum = Number(page ?? 1);
|
||||
try {
|
||||
const data = await prisma.village.findMany({
|
||||
where: {
|
||||
isDummy: false,
|
||||
...(search && { name: { contains: search, mode: 'insensitive' } })
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
isActive: true,
|
||||
createdAt: true,
|
||||
User: {
|
||||
where: {
|
||||
idUserRole: "supadmin"
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
take: 1,
|
||||
},
|
||||
},
|
||||
skip: (pageNum - 1) * 10,
|
||||
take: 10,
|
||||
})
|
||||
|
||||
const result = data.map((village) => ({
|
||||
id: village.id,
|
||||
name: village.name,
|
||||
isActive: village.isActive,
|
||||
createdAt: formatDateTime(village.createdAt),
|
||||
perbekel: village.User[0]?.name || null,
|
||||
}));
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("[villages] get-villages error:", error);
|
||||
set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan pada server",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
query: t.Object({
|
||||
search: t.Optional(t.String({ description: "Kata kunci pencarian nama desa" })),
|
||||
page: t.Optional(t.String({ description: "Halaman data (default: 1)" })),
|
||||
}),
|
||||
detail: {
|
||||
summary: "Get Villages",
|
||||
description: "Menu Villages - Mendapatkan semua data desa.",
|
||||
tags: ["villages"],
|
||||
},
|
||||
}
|
||||
)
|
||||
.get(
|
||||
"/info-villages",
|
||||
async ({ query, set }) => {
|
||||
const { id } = query;
|
||||
try {
|
||||
const data = await prisma.village.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
isActive: true,
|
||||
createdAt: true,
|
||||
User: {
|
||||
where: {
|
||||
idUserRole: "supadmin"
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
take: 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!data) {
|
||||
set.status = 404;
|
||||
return {
|
||||
success: false,
|
||||
message: "Desa tidak ditemukan",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
const result = data ? {
|
||||
id: data?.id,
|
||||
name: data?.name,
|
||||
isActive: data?.isActive,
|
||||
createdAt: data?.createdAt ? formatDateTime(data.createdAt) : null,
|
||||
perbekel: data?.User[0]?.name || null,
|
||||
} : null;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("[detail-villages] info-villages error:", error);
|
||||
set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan pada server",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
query: t.Object({
|
||||
id: t.Optional(t.String({ description: "ID desa" })),
|
||||
}),
|
||||
detail: {
|
||||
summary: "Info Villages",
|
||||
description: "Menu Detail Villages - Mendapatkan info data desa untuk header dan kolom Informasi Sistem.",
|
||||
tags: ["detail-villages"],
|
||||
},
|
||||
}
|
||||
)
|
||||
.get(
|
||||
"/grid-villages",
|
||||
async ({ query, set }) => {
|
||||
const { id } = query;
|
||||
|
||||
try {
|
||||
const village = await prisma.village.findUnique({
|
||||
where: { id: id }
|
||||
});
|
||||
|
||||
if (!village) {
|
||||
set.status = 404;
|
||||
return {
|
||||
success: false,
|
||||
message: "Desa tidak ditemukan",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
const dataUser = await prisma.user.findMany({
|
||||
where: {
|
||||
idVillage: id,
|
||||
NOT: {
|
||||
idUserRole: "developer"
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const dataGroup = await prisma.group.findMany({
|
||||
where: {
|
||||
idVillage: id,
|
||||
}
|
||||
})
|
||||
|
||||
const dataDivision = await prisma.division.findMany({
|
||||
where: {
|
||||
idVillage: id,
|
||||
}
|
||||
})
|
||||
|
||||
const dataProject = await prisma.project.findMany({
|
||||
where: {
|
||||
idVillage: id
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
const result = {
|
||||
user: {
|
||||
active: dataUser.filter((user) => user.isActive).length,
|
||||
nonActive: dataUser.filter((user) => !user.isActive).length,
|
||||
},
|
||||
group: {
|
||||
active: dataGroup.filter((group) => group.isActive).length,
|
||||
nonActive: dataGroup.filter((group) => !group.isActive).length,
|
||||
},
|
||||
division: {
|
||||
active: dataDivision.filter((division) => division.isActive).length,
|
||||
nonActive: dataDivision.filter((division) => !division.isActive).length,
|
||||
},
|
||||
project: {
|
||||
active: dataProject.filter((project) => project.isActive).length,
|
||||
nonActive: dataProject.filter((project) => !project.isActive).length,
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("[detail-villages] grid-villages error:", error);
|
||||
set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan pada server",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
query: t.Object({
|
||||
id: t.Optional(t.String({ description: "ID desa" })),
|
||||
}),
|
||||
detail: {
|
||||
summary: "Grid Villages",
|
||||
description: "Menu Grid Villages - Mendapatkan info data desa untuk 4 grid untuk halaman detail desa.",
|
||||
tags: ["detail-villages"],
|
||||
},
|
||||
}
|
||||
)
|
||||
.get("/log-villages", async ({ query, set }) => {
|
||||
const { id, time } = query;
|
||||
|
||||
try {
|
||||
const village = await prisma.village.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!village) {
|
||||
set.status = 404;
|
||||
return {
|
||||
success: false,
|
||||
message: "Desa tidak ditemukan",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
let startDate: Date;
|
||||
|
||||
if (time === "daily") {
|
||||
startDate = new Date();
|
||||
startDate.setDate(now.getDate() - 13); // 14 hari
|
||||
} else if (time === "monthly") {
|
||||
startDate = new Date(now.getFullYear(), 0, 1); // awal tahun
|
||||
} else if (time === "yearly") {
|
||||
startDate = new Date(now.getFullYear() - 4, 0, 1); // 5 tahun terakhir (opsional)
|
||||
} else {
|
||||
startDate = new Date(0);
|
||||
}
|
||||
|
||||
const dataLog = await prisma.userLog.findMany({
|
||||
where: {
|
||||
createdAt: {
|
||||
gte: startDate,
|
||||
},
|
||||
User: {
|
||||
idVillage: id,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
// =========================
|
||||
// 🔥 GROUPING
|
||||
// =========================
|
||||
const map: Record<string, number> = {};
|
||||
|
||||
dataLog.forEach((log) => {
|
||||
const date = new Date(log.createdAt);
|
||||
|
||||
let label = "";
|
||||
|
||||
if (time === "daily") {
|
||||
label = date.toLocaleDateString("id-ID", {
|
||||
day: "2-digit",
|
||||
month: "short",
|
||||
});
|
||||
} else if (time === "monthly") {
|
||||
label = date.toLocaleDateString("id-ID", {
|
||||
month: "short",
|
||||
});
|
||||
} else if (time === "yearly") {
|
||||
label = date.getFullYear().toString();
|
||||
}
|
||||
|
||||
map[label] = (map[label] || 0) + 1;
|
||||
});
|
||||
|
||||
// =========================
|
||||
// 🔥 FORMAT FINAL
|
||||
// =========================
|
||||
let result: any[] = [];
|
||||
|
||||
if (time === "daily") {
|
||||
for (let i = 13; i >= 0; i--) {
|
||||
const d = new Date();
|
||||
d.setDate(d.getDate() - i);
|
||||
|
||||
const label = d.toLocaleDateString("id-ID", {
|
||||
day: "2-digit",
|
||||
month: "short",
|
||||
});
|
||||
|
||||
result.push({
|
||||
label,
|
||||
aktivitas: map[label] || 0,
|
||||
});
|
||||
}
|
||||
} else if (time === "monthly") {
|
||||
const year = now.getFullYear();
|
||||
for (let m = 0; m <= 11; m++) {
|
||||
const d = new Date(year, m, 1);
|
||||
|
||||
const label = d.toLocaleDateString("id-ID", {
|
||||
month: "short",
|
||||
});
|
||||
|
||||
result.push({
|
||||
label,
|
||||
aktivitas: map[label] || 0,
|
||||
});
|
||||
}
|
||||
} else if (time === "yearly") {
|
||||
const years = Object.keys(map).map(Number);
|
||||
|
||||
if (years.length === 0) {
|
||||
const currentYear = new Date().getFullYear();
|
||||
|
||||
result = [
|
||||
{ label: currentYear.toString(), aktivitas: 0 }
|
||||
];
|
||||
} else {
|
||||
const minYear = Math.min(...years);
|
||||
const maxYear = Math.max(...years);
|
||||
|
||||
result = [];
|
||||
|
||||
for (let y = minYear; y <= maxYear; y++) {
|
||||
const label = y.toString();
|
||||
|
||||
result.push({
|
||||
label,
|
||||
aktivitas: map[label] || 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("[log-villages] error:", error);
|
||||
set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan pada server",
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
query: t.Object({
|
||||
id: t.String({ description: "ID desa" }),
|
||||
time: t.Enum(
|
||||
{
|
||||
daily: "daily",
|
||||
monthly: "monthly",
|
||||
yearly: "yearly",
|
||||
},
|
||||
{
|
||||
description: "Rentang waktu (daily = 14 hari, monthly = 1 tahun, yearly = per tahun)",
|
||||
}
|
||||
),
|
||||
}),
|
||||
detail: {
|
||||
summary: "Log Villages",
|
||||
description:
|
||||
"Mendapatkan data log aktivitas desa berdasarkan rentang waktu (harian, bulanan, tahunan)",
|
||||
tags: ["detail-villages"],
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
;
|
||||
|
||||
|
||||
export const GET = MonitoringServer.handle;
|
||||
export const POST = MonitoringServer.handle;
|
||||
@@ -2,7 +2,7 @@ import { NextResponse } from "next/server";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
return NextResponse.json({ success: true, version: "2.1.8", tahap: "beta", update: "-api untuk dashboard noc; -perbaikan otp" }, { status: 200 });
|
||||
return NextResponse.json({ success: true, version: "2.1.9", tahap: "beta", update: "-api untuk dashboard monitoring" }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, version: "Gagal mendapatkan version, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
|
||||
11
src/lib/formatDateTime.ts
Normal file
11
src/lib/formatDateTime.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
function formatDateTime(date: Date) {
|
||||
return new Intl.DateTimeFormat('id-ID', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
day: '2-digit',
|
||||
month: 'short',
|
||||
year: 'numeric',
|
||||
}).format(date);
|
||||
}
|
||||
|
||||
export default formatDateTime
|
||||
Reference in New Issue
Block a user