Display Used Permits in addition to permits available

Add Used Permits to the "Display rate limits statistics" board, since
with the introduction of the warning mechanism, it makes more sense to
show Used Permits too.

Feature: Issue 10303
Change-Id: I9fd793129b244cbf5e3b85a577adbedfc6e5fb52
diff --git a/src/main/java/com/googlesource/gerrit/plugins/ratelimiter/ListCommand.java b/src/main/java/com/googlesource/gerrit/plugins/ratelimiter/ListCommand.java
index 9f6c946..3971039 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/ratelimiter/ListCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/ratelimiter/ListCommand.java
@@ -1,36 +1,5 @@
-// Copyright (C) 2017 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.googlesource.gerrit.plugins.ratelimiter;
-
-import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE;
-import static com.googlesource.gerrit.plugins.ratelimiter.Module.UPLOAD_PACK_PER_HOUR;
-
-import com.google.common.cache.LoadingCache;
-import com.google.gerrit.common.data.GlobalCapability;
-import com.google.gerrit.extensions.annotations.RequiresCapability;
-import com.google.gerrit.sshd.AdminHighPriorityCommand;
-import com.google.gerrit.sshd.CommandMetaData;
-import com.google.gerrit.sshd.SshCommand;
-import com.google.inject.Inject;
-import com.google.inject.name.Named;
-import java.time.Duration;
-import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.concurrent.TimeUnit;
-
 @AdminHighPriorityCommand
 @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
 @CommandMetaData(
@@ -38,23 +7,17 @@
     description = "Display rate limits statistics",
     runsAt = MASTER_OR_SLAVE)
 final class ListCommand extends SshCommand {
-  private static final String FORMAT = "%-26s %-17s %-19s %s";
+  private static final String FORMAT = "%-26s %-17s %-19s %-15s %s";
   private static final String DASHED_LINE =
-      "------------------------------------------------------------------------------";
-
+      "---------------------------------------------------------------------------------------------";
   private final LoadingCache<String, RateLimiter> uploadPackPerHour;
   private final UserResolver userResolver;
-
   @Inject
   ListCommand(
       @Named(UPLOAD_PACK_PER_HOUR) LoadingCache<String, RateLimiter> uploadPackPerHour,
       UserResolver userResolver) {
     this.uploadPackPerHour = uploadPackPerHour;
     this.userResolver = userResolver;
-  }
-
-  @Override
-  protected void run() throws UnloggedFailure {
     try {
       stdout.println(DASHED_LINE);
       stdout.println("* " + UPLOAD_PACK_PER_HOUR + " *");
@@ -65,6 +28,7 @@
               "Account Id/IP (username)",
               "Permits Per Hour",
               "Available Permits",
+              "Used Permits",
               "Replenish in"));
       stdout.println(DASHED_LINE);
       uploadPackPerHour.asMap().entrySet().stream()
@@ -75,7 +39,6 @@
       throw die(e);
     }
   }
-
   private void printEntry(Entry<String, RateLimiter> entry) {
     stdout.println(
         String.format(
@@ -83,13 +46,12 @@
             getDisplayValue(entry.getKey()),
             permits(entry.getValue().permitsPerHour()),
             permits(entry.getValue().availablePermits()),
+            permits(entry.getValue().usedPermits()),
             Duration.ofSeconds(entry.getValue().remainingTime(TimeUnit.SECONDS))));
   }
-
   private String permits(int value) {
     return value == Integer.MAX_VALUE ? "unlimited" : Integer.toString(value);
   }
-
   private String getDisplayValue(String key) {
     Optional<String> currentUser = userResolver.getUserName(key);
     return currentUser.map(name -> key + " (" + name + ")").orElse(key);
diff --git a/src/main/resources/Documentation/cmd-list.md b/src/main/resources/Documentation/cmd-list.md
index d541cfa..c5116a7 100644
--- a/src/main/resources/Documentation/cmd-list.md
+++ b/src/main/resources/Documentation/cmd-list.md
@@ -1,36 +1,16 @@
-@PLUGIN@ list
-=================
-
-NAME
-----
-@PLUGIN@ list display rate limit statistics
-
-SYNOPSIS
---------
->     ssh -p <port> <host> @PLUGIN@ list
-
-DESCRIPTION
------------
-Displays rate limit statistics: account id (or IP if request is anonymous),
-permits per hour, remaining permits and when they will be replenished.
-
-The time before permits are replenished is represented using ISO-8601 seconds
 based representation, such as PT59M30S (59 minutes and 30 seconds).
-
 ACCESS
 ------
 Gerrit Administrators only.
-
 EXAMPLES
 --------
-
 >     $ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ list
->     ------------------------------------------------------------------------------
+>     ---------------------------------------------------------------------------------------------
 >     * upload_pack_per_hour *
->     ------------------------------------------------------------------------------
->     Account Id (or IP)   Permits Per Hour  Available Permits   Replenish in
->     ------------------------------------------------------------------------------
->     1000001              unlimited         unlimited           PT0S
->     1000002              1000              999                 PT59M30S
->     127.0.0.1            1000              123                 PT10M26S
->     ------------------------------------------------------------------------------
+>     ---------------------------------------------------------------------------------------------
+>     Account Id/IP (username)   Permits Per Hour  Available Permits   Used Permits    Replenish in
+>     ---------------------------------------------------------------------------------------------
+>     1000000 (admin)            unlimited         unlimited           0               PT0S
+>     1000001 (test_user)        1000              999                 1               PT59M30S
+>     127.0.0.1                  1000              123                 877             PT10M26S
+>     ---------------------------------------------------------------------------------------------