perf: improve rate limiting resilience and reduce cache invalidation overhead

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-11 18:53:18 +07:00
parent b1412d7824
commit 4f894f255e
18 changed files with 0 additions and 3317 deletions

View File

@@ -1,387 +0,0 @@
/* ==========================================================================
Peptide Search AI — Frontend Styles
========================================================================== */
/* --- Search Form --- */
.psa-search-wrap {
max-width: 720px;
margin: 2rem auto;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
}
.psa-search-form {
margin-bottom: 1.5rem;
}
.psa-search-input-wrap {
display: flex;
gap: 0;
border: 2px solid #2563eb;
border-radius: 8px;
overflow: hidden;
transition: border-color 0.2s;
}
.psa-search-input-wrap:focus-within {
border-color: #1d4ed8;
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
.psa-search-input {
flex: 1;
padding: 14px 18px;
font-size: 16px;
border: none;
outline: none;
background: #fff;
color: #1e293b;
}
.psa-search-input::placeholder {
color: #94a3b8;
}
.psa-search-input:focus {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
.psa-search-btn {
padding: 14px 24px;
background: #2563eb;
color: #fff;
border: none;
font-size: 15px;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
white-space: nowrap;
}
.psa-search-btn:hover {
background: #1d4ed8;
}
.psa-search-btn:focus-visible {
outline: 2px solid #fff;
outline-offset: -2px;
}
.psa-search-icon {
margin-right: 4px;
}
/* --- Search Results --- */
.psa-search-results {
margin-top: 1rem;
}
.psa-result-count {
font-size: 14px;
color: #64748b;
margin-bottom: 1rem;
}
.psa-result-item {
display: block;
padding: 16px 20px;
margin-bottom: 8px;
background: #fff;
border: 1px solid #e2e8f0;
border-radius: 8px;
text-decoration: none;
color: inherit;
transition: border-color 0.2s, box-shadow 0.2s;
}
.psa-result-item:hover {
border-color: #2563eb;
box-shadow: 0 2px 8px rgba(37, 99, 235, 0.1);
}
.psa-result-title {
font-size: 18px;
font-weight: 600;
color: #1e293b;
margin: 0 0 6px;
}
.psa-result-meta {
font-size: 13px;
color: #64748b;
margin-bottom: 6px;
}
.psa-result-meta span {
margin-right: 16px;
}
.psa-result-excerpt {
font-size: 14px;
color: #475569;
line-height: 1.5;
margin: 0;
}
.psa-result-badge {
display: inline-block;
font-size: 11px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
padding: 2px 8px;
border-radius: 4px;
margin-left: 8px;
vertical-align: middle;
}
.psa-badge-ai {
background: #fef3c7;
color: #92400e;
}
.psa-badge-verified {
background: #d1fae5;
color: #065f46;
}
.psa-badge-manual {
background: #dbeafe;
color: #1e40af;
}
/* --- Checking / Loading State --- */
.psa-checking {
margin-top: 1.5rem;
}
.psa-checking-inner {
text-align: center;
padding: 32px 24px;
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 12px;
}
.psa-checking-inner p {
margin: 12px 0 0;
font-size: 15px;
color: #64748b;
}
/* --- Spinner --- */
.psa-spinner {
display: inline-block;
width: 36px;
height: 36px;
border: 4px solid #bfdbfe;
border-top-color: #2563eb;
border-radius: 50%;
animation: psa-spin 0.8s linear infinite;
}
@keyframes psa-spin {
to { transform: rotate(360deg); }
}
/* --- Pending / Being Added Page --- */
.psa-pending {
margin-top: 1.5rem;
}
.psa-pending-inner {
text-align: center;
padding: 40px 32px;
background: linear-gradient(135deg, #eff6ff 0%, #f0f9ff 100%);
border: 1px solid #bfdbfe;
border-radius: 16px;
}
.psa-pending-icon {
font-size: 48px;
line-height: 1;
margin-bottom: 8px;
}
.psa-pending-inner h3 {
margin: 0 0 12px;
font-size: 21px;
color: #1e40af;
font-weight: 700;
}
.psa-pending-inner > p {
margin: 0 0 28px;
font-size: 15px;
color: #3b82f6;
max-width: 500px;
margin-left: auto;
margin-right: auto;
line-height: 1.6;
}
/* --- Invalid Peptide --- */
.psa-invalid {
margin-top: 1.5rem;
}
.psa-invalid-inner {
text-align: center;
padding: 32px 24px;
background: #fafafa;
border: 1px solid #e2e8f0;
border-radius: 12px;
}
.psa-invalid-inner h3 {
margin: 0 0 12px;
font-size: 20px;
color: #334155;
}
.psa-invalid-inner p {
margin: 0;
font-size: 15px;
color: #64748b;
max-width: 500px;
margin-left: auto;
margin-right: auto;
line-height: 1.6;
}
/* --- Error State --- */
.psa-error {
margin-top: 1.5rem;
padding: 16px 20px;
background: #fef2f2;
border: 1px solid #fca5a5;
border-radius: 8px;
color: #991b1b;
font-size: 14px;
}
/* --- Single Peptide Page Data --- */
.psa-peptide-data {
margin-top: 2rem;
}
.psa-quick-facts {
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 10px;
padding: 20px 24px;
margin-bottom: 2rem;
}
.psa-quick-facts h3 {
margin: 0 0 12px;
font-size: 16px;
font-weight: 700;
color: #334155;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.psa-facts-table {
width: 100%;
border-collapse: collapse;
}
.psa-facts-table th {
text-align: left;
padding: 8px 16px 8px 0;
font-weight: 600;
color: #475569;
width: 180px;
vertical-align: top;
border-bottom: 1px solid #e2e8f0;
font-size: 14px;
}
.psa-facts-table td {
padding: 8px 0;
color: #1e293b;
border-bottom: 1px solid #e2e8f0;
font-size: 14px;
}
.psa-facts-table tr:last-child th,
.psa-facts-table tr:last-child td {
border-bottom: none;
}
.psa-sequence {
font-family: "SF Mono", "Fira Code", "Fira Mono", Menlo, Consolas, monospace;
font-size: 13px;
word-break: break-all;
background: #f1f5f9;
padding: 4px 8px;
border-radius: 4px;
}
.psa-section {
margin-bottom: 2rem;
}
.psa-section h3 {
font-size: 18px;
font-weight: 700;
color: #1e293b;
margin: 0 0 12px;
padding-bottom: 8px;
border-bottom: 2px solid #2563eb;
}
.psa-section-content {
font-size: 15px;
line-height: 1.77;
color: #334155;
}
.psa-source-badge {
margin-top: 2rem;
padding: 12px 16px;
border-radius: 8px;
font-size: 13px;
line-height: 1.5;
}
.psa-source-badge.psa-badge-ai {
background: #fffbeb;
border: 1px solid #fde68a;
color: #92400e;
}
.psa-source-badge.psa-badge-verified {
background: #ecfdf5;
border: 1px solid #6ee7b7;
color: #065f46;
}
.psa-source-badge.psa-badge-manual {
background: #eff6ff;
border: 1px solid #93c5fd;
color: #1e40af;
}
/* --- Responsive --- */
@media (max-width: 600px) {
.psa-search-input-wrap,
.psa-notify-input-wrap {
flex-direction: column;
}
.psa-search-btn,
.psa-notify-btn {
border-radius: 0;
}
.psa-facts-table th {
width: 120px;
}
.psa-pending-inner {
padding: 28px 20px;
}
}

View File

@@ -1,214 +0,0 @@
/**
* Peptide Search AI — Frontend JavaScript
*
* Supports multiple shortcode instances on the same page by using
* class-based selectors scoped to each .psa-search-wrap container.
*
* Flow:
* 1. User searches -> AJAX call to backend
* 2. If found -> show results
* 3. If not found -> backend automatically validates + queues generation
* -> user sees "pending" page
* 4. If invalid peptide name -> show "not recognized" message
*/
(function ($) {
'use strict';
/**
* Initialize a single search instance scoped to its .psa-search-wrap container.
*/
function initSearchInstance($wrap) {
var $form = $wrap.find('.psa-search-form');
var $input = $wrap.find('.psa-search-input');
var $results = $wrap.find('.psa-search-results');
var $checking = $wrap.find('.psa-checking');
var $pending = $wrap.find('.psa-pending');
var $invalid = $wrap.find('.psa-invalid');
var $error = $wrap.find('.psa-error');
function debounce(fn, delay) {
var timer;
return function () {
var args = arguments, ctx = this;
clearTimeout(timer);
timer = setTimeout(function () { fn.apply(ctx, args); }, delay);
};
}
function hideAll() {
$results.hide();
$results.attr('aria-busy', 'false');
$checking.hide();
$pending.hide();
$invalid.hide();
$error.hide();
}
function showError(msg) {
hideAll();
$error.html('<strong>Error:</strong> ' + esc(msg)).show();
$error.attr('role', 'alert');
}
function esc(str) {
var d = document.createElement('div');
d.appendChild(document.createTextNode(str));
return d.innerHTML;
}
function sourceBadge(source) {
var map = {
'pubchem': { label: 'Verified', cls: 'psa-badge-verified' },
'manual': { label: 'Curated', cls: 'psa-badge-manual' }
};
var b = map[source];
if (!b) return '';
return '<span class="psa-result-badge ' + b.cls + '">' + b.label + '</span>';
}
/**
* Render search results.
*/
function renderResults(data) {
hideAll();
var html = '<p class="psa-result-count">' + data.total + ' peptide' +
(data.total !== 1 ? 's' : '') + ' found</p>';
data.results.forEach(function (item) {
html += '<a href="' + esc(item.url) + '" class="psa-result-item">';
html += '<h4 class="psa-result-title">' + esc(item.title);
if (item.source) html += sourceBadge(item.source);
html += '</h4>';
var meta = [];
if (item.molecular_weight) meta.push('<span>MW: ' + esc(item.molecular_weight) + '</span>');
if (item.molecular_formula) meta.push('<span>Formula: ' + esc(item.molecular_formula) + '</span>');
if (item.sequence && item.sequence.length <= 40) meta.push('<span>Seq: <code>' + esc(item.sequence) + '</code></span>');
if (meta.length) html += '<div class="psa-result-meta">' + meta.join('') + '</div>';
if (item.excerpt) html += '<p class="psa-result-excerpt">' + esc(item.excerpt) + '</p>';
html += '</a>';
});
$results.html(html).show();
}
/**
* Show the pending/in-progress message.
*/
function showPending(name) {
hideAll();
$pending.find('.psa-pending-inner p').html(
'<strong>' + esc(name) + '</strong> is currently being added to our database. Please check back again later.'
);
$pending.show();
}
/**
* Show the invalid peptide message.
*/
function showInvalid(name, reason) {
hideAll();
$invalid.find('.psa-invalid-text').html(
'<strong>' + esc(name) + '</strong> does not appear to be a recognized peptide. Please check the spelling or try a different search term.'
);
$invalid.show();
}
/**
* Perform the search. The backend handles everything:
* - If found: returns results
* - If not found + valid peptide: validates, creates placeholder, queues generation
* - If not found + invalid: returns invalid status
*/
function doSearch(query) {
if (query.length < 2) {
hideAll();
return;
}
hideAll();
$checking.show();
$results.attr('aria-busy', 'true');
$.ajax({
url: psaAjax.ajaxurl,
method: 'GET',
data: {
action: 'psa_search',
nonce: psaAjax.nonce,
q: query
},
timeout: 30000,
success: function (response) {
$results.attr('aria-busy', 'false');
if (!response.success) {
showError(response.data || 'Search failed.');
return;
}
var d = response.data;
switch (d.status) {
case 'found':
renderResults(d.data);
break;
case 'pending':
showPending(d.peptide_name);
break;
case 'invalid':
showInvalid(query, d.message);
break;
case 'rate_limited':
showError(d.message || 'Too many requests. Please try again later.');
break;
default:
showError('Unexpected response.');
}
},
error: function (jqXHR, textStatus, errorThrown) {
$results.attr('aria-busy', 'false');
var errorMsg = 'Could not connect to the server. Please try again.';
if (textStatus === 'timeout') {
errorMsg = 'Request timed out. The server took too long to respond. Please try again.';
} else if (textStatus === 'error') {
if (jqXHR.status === 0) {
errorMsg = 'Network error. Please check your connection and try again.';
} else if (jqXHR.status === 500) {
errorMsg = 'Server error. Please try again later.';
} else if (jqXHR.status === 503) {
errorMsg = 'Server is temporarily unavailable. Please try again later.';
}
}
showError(errorMsg);
}
});
}
// --- Event Handlers (scoped to this instance) ---
$form.on('submit', function (e) {
e.preventDefault();
doSearch($input.val().trim());
});
var debouncedSearch = debounce(function () {
var q = $input.val().trim();
if (q.length >= 2) {
doSearch(q);
} else {
hideAll();
}
}, 400);
$input.on('input', debouncedSearch);
}
// --- Initialize all search instances on page ---
$(document).ready(function () {
$('.psa-search-wrap').each(function () {
initSearchInstance($(this));
});
});
})(jQuery);