@@ -182,62 +182,55 @@ <h4 class="panel-title">{% trans "Existing Rate Limits" %}</h4>
182182 </ div >
183183 </ div >
184184
185- {% if consumer.current_state %}
186185 < div class ="row ">
187186 < div class ="col-xs-12 ">
188187 < h2 > {% trans "Current Usage" %}
189188 < button type ="button " id ="refreshUsageBtn " class ="btn btn-sm btn-info pull-right " onclick ="refreshUsageStats() ">
190- < span class ="glyphicon glyphicon-refresh "> </ span > {% trans "Refresh (10s )" %}
189+ < span class ="glyphicon glyphicon-refresh "> </ span > {% trans "Auto Refresh (5s )" %}
191190 </ button >
192191 </ h2 >
193192 < div class ="panel panel-info " id ="usageStatsPanel ">
194193 < div class ="panel-body " id ="usageStatsContent ">
195- {% if consumer.current_state.per_hour %}
196194 < div class ="row " id ="usageStatsRow ">
197- {% if consumer.current_state.per_second %}
198195 < div class ="col-xs-6 col-sm-2 " data-period ="per_second ">
199196 < strong > {% trans "Per Second" %}</ strong > < br >
200- < span class ="text-info usage-calls "> {{ consumer.current_state. per_second.calls_made }} calls made</ span > < br >
201- < small class ="text-muted usage-reset "> Resets in {{ consumer.current_state. per_second.reset_in_seconds }} seconds</ small >
197+ < span class ="text-info usage-calls "> {% if current_usage.per_second.calls_made|add:"0" == -1 %}Not tracked{% else %}{{ current_usage. per_second.calls_made }} calls made{% endif %} </ span > < br >
198+ < small class ="text-muted usage-reset "> {% if current_usage.per_second.reset_in_seconds|add:"0" == -1 %}Not tracked{% else %} Resets in {{ current_usage. per_second.reset_in_seconds }} seconds{% endif %} </ small >
202199 </ div >
203- {% endif %}
204- {% if consumer.current_state.per_minute %}
205200 < div class ="col-xs-6 col-sm-2 " data-period ="per_minute ">
206201 < strong > {% trans "Per Minute" %}</ strong > < br >
207- < span class ="text-info usage-calls "> {{ consumer.current_state. per_minute.calls_made }} calls made</ span > < br >
208- < small class ="text-muted usage-reset "> Resets in {{ consumer.current_state. per_minute.reset_in_seconds }} seconds</ small >
202+ < span class ="text-info usage-calls "> {% if current_usage.per_minute.calls_made|add:"0" == -1 %}Not tracked{% else %}{{ current_usage. per_minute.calls_made }} calls made{% endif %} </ span > < br >
203+ < small class ="text-muted usage-reset "> {% if current_usage.per_minute.reset_in_seconds|add:"0" == -1 %}Not tracked{% else %} Resets in {{ current_usage. per_minute.reset_in_seconds }} seconds{% endif %} </ small >
209204 </ div >
210- {% endif %}
211205 < div class ="col-xs-6 col-sm-2 " data-period ="per_hour ">
212206 < strong > {% trans "Per Hour" %}</ strong > < br >
213- < span class ="text-info usage-calls "> {{ consumer.current_state. per_hour.calls_made }} calls made</ span > < br >
214- < small class ="text-muted usage-reset "> Resets in {{ consumer.current_state. per_hour.reset_in_seconds }} seconds</ small >
207+ < span class ="text-info usage-calls "> {% if current_usage.per_hour.calls_made|add:"0" == -1 %}Not tracked{% else %}{{ current_usage. per_hour.calls_made }} calls made{% endif %} </ span > < br >
208+ < small class ="text-muted usage-reset "> {% if current_usage.per_hour.reset_in_seconds|add:"0" == -1 %}Not tracked{% else %} Resets in {{ current_usage. per_hour.reset_in_seconds }} seconds{% endif %} </ small >
215209 </ div >
216210 < div class ="col-xs-6 col-sm-2 " data-period ="per_day ">
217211 < strong > {% trans "Per Day" %}</ strong > < br >
218- < span class ="text-info usage-calls "> {{ consumer.current_state. per_day.calls_made }} calls made</ span > < br >
219- < small class ="text-muted usage-reset "> Resets in {{ consumer.current_state. per_day.reset_in_seconds }} seconds</ small >
212+ < span class ="text-info usage-calls "> {% if current_usage.per_day.calls_made|add:"0" == -1 %}Not tracked{% else %}{{ current_usage. per_day.calls_made }} calls made{% endif %} </ span > < br >
213+ < small class ="text-muted usage-reset "> {% if current_usage.per_day.reset_in_seconds|add:"0" == -1 %}Not tracked{% else %} Resets in {{ current_usage. per_day.reset_in_seconds }} seconds{% endif %} </ small >
220214 </ div >
221215 < div class ="col-xs-6 col-sm-2 " data-period ="per_week ">
222216 < strong > {% trans "Per Week" %}</ strong > < br >
223- < span class ="text-info usage-calls "> {{ consumer.current_state. per_week.calls_made }} calls made</ span > < br >
224- < small class ="text-muted usage-reset "> Resets in {{ consumer.current_state. per_week.reset_in_seconds }} seconds</ small >
217+ < span class ="text-info usage-calls "> {% if current_usage.per_week.calls_made|add:"0" == -1 %}Not tracked{% else %}{{ current_usage. per_week.calls_made }} calls made{% endif %} </ span > < br >
218+ < small class ="text-muted usage-reset "> {% if current_usage.per_week.reset_in_seconds|add:"0" == -1 %}Not tracked{% else %} Resets in {{ current_usage. per_week.reset_in_seconds }} seconds{% endif %} </ small >
225219 </ div >
226220 < div class ="col-xs-6 col-sm-2 " data-period ="per_month ">
227221 < strong > {% trans "Per Month" %}</ strong > < br >
228- < span class ="text-info usage-calls "> {{ consumer.current_state. per_month.calls_made }} calls made</ span > < br >
229- < small class ="text-muted usage-reset "> Resets in {{ consumer.current_state. per_month.reset_in_seconds }} seconds</ small >
222+ < span class ="text-info usage-calls "> {% if current_usage.per_month.calls_made|add:"0" == -1 %}Not tracked{% else %}{{ current_usage. per_month.calls_made }} calls made{% endif %} </ span > < br >
223+ < small class ="text-muted usage-reset "> {% if current_usage.per_month.reset_in_seconds|add:"0" == -1 %}Not tracked{% else %} Resets in {{ current_usage. per_month.reset_in_seconds }} seconds{% endif %} </ small >
230224 </ div >
231225 </ div >
232- {% endif %}
233226 < div id ="refreshProgress " class ="progress " style ="display: none; margin-top: 15px; ">
234227 < div class ="progress-bar progress-bar-info progress-bar-striped active " id ="progressBar " style ="width: 0% "> </ div >
235228 </ div >
229+
236230 </ div >
237231 </ div >
238232 </ div >
239233 </ div >
240- {% endif %}
241234 < div class ="row ">
242235 < div class ="col-xs-12 ">
243236 < div id ="consumers-detail-consumer_id ">
@@ -352,6 +345,14 @@ <h2>{% trans "Current Usage" %}
352345 { % if consumer . to_date % }
353346 $ ( 'body' ) . attr ( 'data-to-date' , '{{ consumer.to_date }}' ) ;
354347 { % endif % }
348+
349+ // Debug: Log basic debugging info
350+ console . log ( 'Current usage data available:' , { % if current_usage % } true { % else % } false { % endif % } ) ;
351+
352+ // Initial load of usage data
353+ setTimeout ( function ( ) {
354+ fetchUsageData ( ) ;
355+ } , 1000 ) ;
355356 } ) ;
356357
357358 // Global functions for CRUD operations - attached to window for global access
@@ -469,17 +470,17 @@ <h2>{% trans "Current Usage" %}
469470 // Global variables for refresh functionality
470471 let refreshInterval = null ;
471472 let refreshCount = 0 ;
472- const MAX_REFRESH_COUNT = 10 ;
473+ const MAX_REFRESH_COUNT = 5 ;
473474
474475 // Function to refresh usage statistics
475- function refreshUsageStats ( ) {
476+ window . refreshUsageStats = function ( ) {
476477 const button = document . getElementById ( 'refreshUsageBtn' ) ;
477478 const progressDiv = document . getElementById ( 'refreshProgress' ) ;
478479 const progressBar = document . getElementById ( 'progressBar' ) ;
479480
480481 // Disable button and show progress
481482 button . disabled = true ;
482- button . innerHTML = '<span class="glyphicon glyphicon-refresh glyphicon-spin"></span> Refreshing...' ;
483+ button . innerHTML = '<span class="glyphicon glyphicon-refresh glyphicon-spin"></span> Auto Refreshing...' ;
483484 progressDiv . style . display = 'block' ;
484485
485486 // Reset counters
@@ -488,13 +489,15 @@ <h2>{% trans "Current Usage" %}
488489 // Start refresh cycle
489490 refreshInterval = setInterval ( fetchUsageData , 1000 ) ;
490491 fetchUsageData ( ) ; // Initial fetch
491- }
492+ } ;
492493
493494 // Function to fetch usage data via AJAX
494495 function fetchUsageData ( ) {
495496 const consumerId = '{{ consumer.consumer_id }}' ;
496497 const panel = document . getElementById ( 'usageStatsPanel' ) ;
497498
499+ console . log ( 'Fetching usage data for consumer:' , consumerId ) ;
500+
498501 // Add refreshing effect to panel
499502 panel . classList . add ( 'panel-refreshing' ) ;
500503
@@ -507,6 +510,7 @@ <h2>{% trans "Current Usage" %}
507510 } ,
508511 timeout : 5000 , // 5 second timeout
509512 success : function ( data ) {
513+ console . log ( 'Usage data received:' , data ) ;
510514 updateUsageDisplay ( data ) ;
511515 refreshCount ++ ;
512516
@@ -516,15 +520,15 @@ <h2>{% trans "Current Usage" %}
516520 progressBar . style . width = progress + '%' ;
517521 progressBar . textContent = refreshCount + '/' + MAX_REFRESH_COUNT ;
518522
519- // Stop after 10 seconds
523+ // Stop after 5 seconds
520524 if ( refreshCount >= MAX_REFRESH_COUNT ) {
521525 clearInterval ( refreshInterval ) ;
522526 resetRefreshButton ( ) ;
523527 panel . classList . remove ( 'panel-refreshing' ) ;
524528 }
525529 } ,
526530 error : function ( xhr , status , error ) {
527- console . error ( 'Error fetching usage data:' , error ) ;
531+ console . error ( 'Error fetching usage data:' , error , xhr . responseText ) ;
528532 showRefreshError ( error ) ;
529533
530534 refreshCount ++ ;
@@ -562,26 +566,27 @@ <h2>{% trans "Current Usage" %}
562566
563567 // Function to update usage display with new data
564568 function updateUsageDisplay ( data ) {
565- if ( data && data . current_state ) {
566- const currentState = data . current_state ;
569+ console . log ( 'Updating display with data:' , data ) ;
570+ if ( data ) {
567571 const periods = [ 'per_second' , 'per_minute' , 'per_hour' , 'per_day' , 'per_week' , 'per_month' ] ;
568572
569573 periods . forEach ( function ( period ) {
570- const periodData = currentState [ period ] ;
574+ const periodData = data [ period ] ;
571575 if ( periodData ) {
572576 const periodDiv = document . querySelector ( '[data-period="' + period + '"]' ) ;
573577 if ( periodDiv ) {
574578 const callsSpan = periodDiv . querySelector ( '.usage-calls' ) ;
575579 const resetSpan = periodDiv . querySelector ( '.usage-reset' ) ;
576580
577581 if ( callsSpan ) {
578- const oldCalls = callsSpan . textContent . match ( / \d + / ) ;
582+ const oldCalls = callsSpan . textContent . match ( / - ? \d + / ) ;
579583 const newCalls = periodData . calls_made ;
584+ const displayCalls = newCalls === - 1 ? 'Not tracked' : newCalls + ' calls made' ;
580585
581586 // Check if calls increased
582- const callsIncreased = oldCalls && parseInt ( oldCalls [ 0 ] ) < newCalls ;
587+ const callsIncreased = oldCalls && parseInt ( oldCalls [ 0 ] ) < newCalls && newCalls !== - 1 ;
583588
584- callsSpan . textContent = newCalls + ' calls made' ;
589+ callsSpan . textContent = displayCalls ;
585590
586591 // Add visual feedback
587592 callsSpan . classList . add ( 'updating' ) ;
@@ -604,7 +609,8 @@ <h2>{% trans "Current Usage" %}
604609 }
605610
606611 if ( resetSpan ) {
607- resetSpan . textContent = 'Resets in ' + periodData . reset_in_seconds + ' seconds' ;
612+ const resetText = periodData . reset_in_seconds === - 1 ? 'Not tracked' : 'Resets in ' + periodData . reset_in_seconds + ' seconds' ;
613+ resetSpan . textContent = resetText ;
608614 // Add subtle animation to reset timer
609615 resetSpan . style . opacity = '0.7' ;
610616 setTimeout ( function ( ) {
@@ -621,7 +627,7 @@ <h2>{% trans "Current Usage" %}
621627 }
622628
623629 // Function to update last refresh time
624- function updateLastRefreshTime ( ) {
630+ window . updateLastRefreshTime = function ( ) {
625631 let timeDiv = document . getElementById ( 'lastRefreshTime' ) ;
626632 if ( ! timeDiv ) {
627633 timeDiv = document . createElement ( 'small' ) ;
@@ -638,13 +644,12 @@ <h2>{% trans "Current Usage" %}
638644 }
639645
640646 // Function to reset refresh button
641- function resetRefreshButton ( ) {
647+ window . resetRefreshButton = function ( ) {
642648 const button = document . getElementById ( 'refreshUsageBtn' ) ;
643649 const progressDiv = document . getElementById ( 'refreshProgress' ) ;
644- const panel = document . getElementById ( 'usageStatsPanel' ) ;
645650
646651 button . disabled = false ;
647- button . innerHTML = '<span class="glyphicon glyphicon-refresh"></span> {% trans " Refresh (10s)" %} ' ;
652+ button . innerHTML = '<span class="glyphicon glyphicon-refresh"></span> Auto Refresh (5s) ' ;
648653 progressDiv . style . display = 'none' ;
649654 document . getElementById ( 'progressBar' ) . style . width = '0%' ;
650655 document . getElementById ( 'progressBar' ) . textContent = '' ;
@@ -654,7 +659,7 @@ <h2>{% trans "Current Usage" %}
654659 if ( errorDiv && errorDiv . parentNode ) {
655660 errorDiv . parentNode . removeChild ( errorDiv ) ;
656661 }
657- }
662+ } ;
658663 </ script >
659664{% endblock extrajs %}
660665
0 commit comments