Repository navigation
fix(counter): percepat statistik pengunjung dengan index dan cache - #1818
Open
pandigresik wants to merge 1 commit into
Open
pandigresik wants to merge 1 commit into
pandigresik wants to merge 1 commit into
Conversation
Tabel visitors (±537 ribu baris) tidak memiliki index, sehingga halaman Statistik Pengunjung dan lookup middleware memindai seluruh tabel. - Migrasi: tambah kolom url_hash (sha256 dari url) dengan backfill, dan index pada visited_at, url_hash, serta (ip_address, url_hash, visited_at) - Visitor: url_hash terisi otomatis; filter tanggal memakai rentang; summaryStats() menggabungkan enam periode dalam satu query; cachedSummaryStats() dan countAllVisitors() di-cache 5 menit; getTopPagesVisited() dikelompokkan per url_hash - TrackVisitors: lookup kunjungan harian memakai url_hash - CounterController: memakai ringkasan dan Top 10 yang di-cache - Test CounterPerformanceTest: index, url_hash, summaryStats, dan batas jumlah query di halaman counter Closes #1817 Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Contributor
|
🔄 AI PR Review sedang antri di server...
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request: fix: percepat halaman Statistik Pengunjung dengan index dan cache pada tabel visitors
Description
Halaman Statistik Pengunjung (
/counter) sangat lambat karena tabelvisitors(±537 ribu baris) tidak memiliki index, sehingga setiap statistik memindai seluruh tabel. Middleware pelacak pengunjung juga menjalankan query ke tabel yang sama di setiap request GET, sehingga kelambatan ini ikut memengaruhi seluruh situs. PR ini menambahkan kolomurl_hashdan index, menggabungkan query statistik, serta menambahkan cache 5 menit.Changes made:
2026_11_26_000000_add_url_hash_and_indexes_to_visitors_table.phpmenambahkan kolomurl_hash(sha256 dari url), mengisi data lama denganSHA2(url, 256), dan menambahkan index padavisited_at,url_hash, serta(ip_address, url_hash, visited_at).Visitor:url_hashdiisi otomatis saaturlberubah. Filter tanggal memakai rentang[awal, akhir)agar index terpakai. DitambahkansummaryStats()(enam periode dalam satu query) dancachedSummaryStats()(cache 5 menit).countAllVisitors()memakai cache yang sama.getTopPagesVisited()dikelompokkan perurl_hash.TrackVisitors: lookup kunjungan harian memakaiurl_hashdan filter tanggalTODAY, bukan membandingkan kolom TEXTurl.CounterController: memakaiVisitor::cachedSummaryStats()dan meng-cache Top 10 halaman selama 5 menit.tests/Feature/CounterPerformanceTest.php: 10 test untuk index, penulisanurl_hash,summaryStats,getTopPagesVisited, dan jumlah query halaman counter (maksimal 3 query kevisitors).Reason for change:
COUNT(DISTINCT),GROUP BY url(TEXT), danGROUP BY DATE_FORMATmemindai seluruh tabel tanpa index. Pengukuran lokal: Top 10 halaman ±6,9 detik, grafik harian ±3,6 detik, lookup middleware ±1,2 detik per request.countAllVisitors()di setiap halaman admin, sekarang memakai cache yang sama.Impact of change:
✅ Halaman counter: jumlah query ke
visitorsdari 8 menjadi maksimal 3 saat cache dingin, dan 1 saat cache hangat (hanya grafik harian yang tidak di-cache).✅ Middleware: lookup per request memakai index, sehingga beban di halaman publik ikut turun.
✅ Sidebar admin: tidak lagi memindai tabel di setiap halaman.
Related Issue
#1817
Steps to Reproduce
Before fix (problem):
After fix (solution):
php artisan migrate.Testing on related features:
CounterExportTestlulus)Checklist
Technical Details
Technical Explanation
Visitor::filter(TODAY)->where('ip_address', ..)->where('url_hash', sha256(url)), memakaivisitors_ip_url_visited_index.SELECTdenganSUM(CASE WHEN visited_at >= ? AND visited_at < ? ...).counter.summarydancounter.top_pages, TTL 5 menit, driver cache aplikasi.visitors_url_hash_index, filter tanggal memakaivisitors_visited_at_index.Configuration changes
Tidak ada perubahan konfigurasi.
Dependencies added
No new dependencies
Testing
Manual Testing
Automated Testing
tests/Feature/CounterPerformanceTest.php(10 test, lulus)tests/Feature/CounterExportTest.php(lulus)UserControllerTestgagal karena data email sisa di database testing, danPendudukExportTestgagal karena batas waktu (3,79 detik lawan 3 detik). Keduanya tidak menyentuh kode counter. Belum diverifikasi apakah keduanya juga gagal dirilis-devtanpa perubahan ini.Browser Compatibility (if applicable)
Tidak berlaku (perubahan backend).
Catatan Deploy
UPDATEpada seluruh baris dan membuat tiga index. Jalankan saat trafik rendah.🤖 Generated with Claude Code