Files
siakad/app/Support/Legacy/SystemOverview.php
T
2026-11-11 18:26:23 +07:00

147 lines
6.2 KiB
PHP

<?php
namespace App\Support\Legacy;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Throwable;
/**
* Ringkasan Superuser dari DB lama: jumlah akun (t_account), pegawai per otoritas, riwayat masuk per tahun
* (t_historilogin), serta kondisi koneksi basis data untuk halaman Kesehatan sistem. Hanya baca.
*/
final class SystemOverview
{
/** Riwayat masuk puluhan tahun dan berhenti bertambah; agregatnya cukup dihitung sekali per jam. */
private const int LOGIN_CACHE_SECONDS = 3600;
private const int TOP_AUTHORITIES = 8;
private const int YEARS = 8;
private function db(): ConnectionInterface
{
return DB::connection('untan');
}
/**
* @return array{total: int, aktif: int, beku: int, pegawai: int, mahasiswa: int, tanpaPeran: int, superuser: int}
*/
public function accounts(): array
{
$count = [];
foreach ($this->db()->table('t_account')->selectRaw('jenis, case when status = 0 then 0 else 1 end as beku, count(*) as jumlah')->groupBy('jenis')->groupByRaw('case when status = 0 then 0 else 1 end')->get() as $row) {
$count[(int) $row->jenis][(int) $row->beku] = (int) $row->jumlah;
}
$of = fn (int $jenis): int => array_sum($count[$jenis] ?? []);
$beku = array_sum(array_column($count, 1));
// Pegawai tanpa otoritas dan bukan dosen ditolak saat masuk (LegacyRole).
$tanpaPeran = $this->db()->table('t_account as a')
->where('a.jenis', LegacyRole::JENIS_PEGAWAI)
->whereNotExists(fn ($sub) => $sub->selectRaw('1')->from('t_otoritasuser as o')->whereColumn('o.iduser', 'a.iduser'))
->whereNotExists(fn ($sub) => $sub->selectRaw('1')->from('t_dosen as d')->whereColumn('d.idpegawai', 'a.identitas'))
->count();
return [
'total' => $of(LegacyRole::JENIS_PEGAWAI) + $of(LegacyRole::JENIS_MAHASISWA),
'aktif' => $of(LegacyRole::JENIS_PEGAWAI) + $of(LegacyRole::JENIS_MAHASISWA) - $beku,
'beku' => $beku,
'pegawai' => $of(LegacyRole::JENIS_PEGAWAI),
'mahasiswa' => $of(LegacyRole::JENIS_MAHASISWA),
'tanpaPeran' => $tanpaPeran,
'superuser' => $this->db()->table('t_otoritasuser')->where('idotoritas', 1)->distinct()->count('iduser'),
];
}
/**
* Pemegang otoritas terbanyak (gabungan sisanya dalam "Lainnya").
*
* @return array<string, int> nama otoritas => jumlah pemegang
*/
public function authorities(): array
{
$rows = $this->db()->table('t_otoritasuser as o')
->join('t_otoritas as t', 't.idotoritas', '=', 'o.idotoritas')
->selectRaw('t.otoritas, count(*) as jumlah')
->groupBy('t.otoritas')
->orderByDesc('jumlah')->orderBy('t.otoritas')
->get();
$out = [];
foreach ($rows->take(self::TOP_AUTHORITIES) as $row) {
$out[trim((string) $row->otoritas)] = (int) $row->jumlah;
}
if ($rows->count() > self::TOP_AUTHORITIES) {
$out['Lainnya'] = (int) $rows->slice(self::TOP_AUTHORITIES)->sum('jumlah');
}
return $out;
}
/**
* Jumlah masuk per tahun pada tahun-tahun terakhir yang tercatat, dan waktu masuk terakhir.
*
* @return array{tahun: array<int, int>, terakhir: ?string}
*/
public function logins(): array
{
return Cache::remember('superuser.logins.'.md5((string) config('database.connections.untan.database')), self::LOGIN_CACHE_SECONDS, function (): array {
$latest = $this->db()->table('t_historilogin')->max('logintimestamp');
if ($latest === null) {
return ['tahun' => [], 'terakhir' => null];
}
$last = Carbon::parse($latest)->year;
$year = $this->db()->getDriverName() === 'sqlite' ? "cast(strftime('%Y', logintimestamp) as integer)" : 'extract(year from logintimestamp)::int';
$rows = $this->db()->table('t_historilogin')
->where('logintimestamp', '>=', Carbon::create($last - self::YEARS + 1)->startOfYear())
->selectRaw("$year as tahun, count(*) as jumlah")
->groupByRaw($year)->orderByRaw($year)
->pluck('jumlah', 'tahun');
$perYear = [];
for ($y = $last - self::YEARS + 1; $y <= $last; $y++) {
$perYear[$y] = (int) ($rows[$y] ?? 0);
}
return ['tahun' => $perYear, 'terakhir' => LoginHistory::time($latest)];
});
}
/**
* Kondisi koneksi ke DB lama untuk Kesehatan sistem: label => nilai. Galat apa pun dilaporkan, tidak dilempar.
*
* @return array{ok: bool, info: array<string, string>, catatan: string}
*/
public function database(): array
{
$start = hrtime(true);
try {
$this->db()->selectOne('select 1');
$ms = round((hrtime(true) - $start) / 1e6);
$sqlite = $this->db()->getDriverName() === 'sqlite';
$info = [
'Server' => $sqlite ? 'SQLite '.$this->db()->selectOne('select sqlite_version() as v')->v : (string) preg_replace('/\s+on\s.*$/', '', $this->db()->selectOne('select version() as v')->v),
'Basis data' => (string) ($sqlite ? 'tiruan' : $this->db()->selectOne('select current_database() as d')->d),
'Waktu respons' => $ms.' ms',
];
if (! $sqlite) {
$info['Ukuran basis data'] = (string) $this->db()->selectOne('select pg_size_pretty(pg_database_size(current_database())) as s')->s;
$info['Jumlah tabel'] = number_format((int) $this->db()->selectOne("select count(*) as c from information_schema.tables where table_schema = 'public' and table_type = 'BASE TABLE'")->c, 0, ',', '.');
}
return ['ok' => true, 'info' => $info, 'catatan' => 'Terhubung ke basis data SIAKAD.'];
} catch (Throwable) {
// Pesan asli bisa memuat alamat server dan kredensial; cukup dilaporkan tidak terjangkau.
return ['ok' => false, 'info' => [], 'catatan' => 'Basis data SIAKAD tidak terjangkau dari server ini.'];
}
}
}