Skip to content

Commit b3f4cfe

Browse files
feature/modified the **Current Usage** section in the GUI to display
rate limits alongside current usage counts in the format "X of Y calls made" How It Works: Based on your example data: - **Rate Limit 1**: Per Second=10, Per Minute=5, Per Hour=-1, Per Day=2340, Per Week=-1, Per Month=-1 - **Rate Limit 2**: Per Second=1, Per Minute=26, Per Hour=450, Per Day=-1, Per Week=4124, Per Month=23000 The system will now display: - **Per Second**: "1 of 11 calls made" (10+1=11) - **Per Minute**: "7 of 31 calls made" (5+26=31) - **Per Hour**: "26 of 450 calls made" (only limit 2 has a value) - **Per Day**: "109 of 2340 calls made" (only limit 1 has a value) - **Per Week**: "109 of 4124 calls made" (only limit 2 has a value) - **Per Month**: "109 of 23000 calls made" (only limit 2 has a value)
1 parent defeab3 commit b3f4cfe

1 file changed

Lines changed: 75 additions & 3 deletions

File tree

apimanager/consumers/templates/consumers/detail.html

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,82 @@ <h2>{% trans "Current Usage" %}
349349
// Debug: Log basic debugging info
350350
console.log('Current usage data available:', {% if current_usage %}true{% else %}false{% endif %});
351351

352-
// Initial load of usage data
352+
// Load rate limits data into JavaScript
353+
window.rateLimits = [
354+
{% if call_limits and call_limits.limits %}
355+
{% for limit in call_limits.limits %}
356+
{
357+
per_second_call_limit: {{ limit.per_second_call_limit|default:"-1" }},
358+
per_minute_call_limit: {{ limit.per_minute_call_limit|default:"-1" }},
359+
per_hour_call_limit: {{ limit.per_hour_call_limit|default:"-1" }},
360+
per_day_call_limit: {{ limit.per_day_call_limit|default:"-1" }},
361+
per_week_call_limit: {{ limit.per_week_call_limit|default:"-1" }},
362+
per_month_call_limit: {{ limit.per_month_call_limit|default:"-1" }}
363+
}{% if not forloop.last %},{% endif %}
364+
{% endfor %}
365+
{% endif %}
366+
];
367+
368+
// Initial load and display of usage data
353369
setTimeout(function() {
370+
console.log('Rate limits loaded:', window.rateLimits);
371+
updateUsageDisplayWithLimits();
354372
fetchUsageData();
355-
}, 1000);
373+
}, 100);
356374
});
357375

376+
// Function to calculate effective limit for a period by summing all active limits
377+
function getEffectiveLimit(period) {
378+
if (!window.rateLimits || window.rateLimits.length === 0) {
379+
return null;
380+
}
381+
382+
let total = 0;
383+
let hasLimits = false;
384+
385+
window.rateLimits.forEach(function(limit) {
386+
let limitValue;
387+
switch(period) {
388+
case 'per_second': limitValue = limit.per_second_call_limit; break;
389+
case 'per_minute': limitValue = limit.per_minute_call_limit; break;
390+
case 'per_hour': limitValue = limit.per_hour_call_limit; break;
391+
case 'per_day': limitValue = limit.per_day_call_limit; break;
392+
case 'per_week': limitValue = limit.per_week_call_limit; break;
393+
case 'per_month': limitValue = limit.per_month_call_limit; break;
394+
default: limitValue = -1;
395+
}
396+
397+
// Convert to integer and check if it's a valid positive limit
398+
let parsedValue = parseInt(limitValue);
399+
if (!isNaN(parsedValue) && parsedValue > 0) {
400+
total += parsedValue;
401+
hasLimits = true;
402+
}
403+
});
404+
405+
console.log('Effective limit for', period, ':', hasLimits ? total : 'unlimited');
406+
return hasLimits ? total : null;
407+
}
408+
409+
// Function to update usage display with rate limits
410+
function updateUsageDisplayWithLimits() {
411+
$('#usageStatsRow .usage-calls').each(function() {
412+
let $this = $(this);
413+
let period = $this.closest('[data-period]').data('period');
414+
let text = $this.text().trim();
415+
416+
// Only update if it shows "X calls made" format (not "Unlimited" or "Not tracked")
417+
let match = text.match(/^(\d+) calls made$/);
418+
if (match) {
419+
let calls = match[1];
420+
let effectiveLimit = getEffectiveLimit(period);
421+
if (effectiveLimit && effectiveLimit > 0) {
422+
$this.text(calls + ' of ' + effectiveLimit + ' calls made');
423+
}
424+
}
425+
});
426+
}
427+
358428
// Global functions for CRUD operations - attached to window for global access
359429
window.showAddRateLimitForm = function() {
360430
// Reset form for new entry
@@ -580,7 +650,9 @@ <h2>{% trans "Current Usage" %}
580650
if (callsSpan) {
581651
const oldCalls = callsSpan.textContent.match(/-?\d+/);
582652
const newCalls = periodData ? periodData.calls_made : null;
583-
const displayCalls = !periodData ? 'Unlimited' : (newCalls === -1 ? 'Not tracked' : newCalls + ' calls made');
653+
const effectiveLimit = getEffectiveLimit(period);
654+
const limitText = effectiveLimit ? ' of ' + effectiveLimit : '';
655+
const displayCalls = !periodData ? 'Unlimited' : (newCalls === -1 ? 'Not tracked' : newCalls + limitText + ' calls made');
584656

585657
// Check if calls increased (only if periodData exists)
586658
const callsIncreased = periodData && oldCalls && parseInt(oldCalls[0]) < newCalls && newCalls !== -1;

0 commit comments

Comments
 (0)