Skip to content

Commit

Permalink
GH-44 Improve stats filtering
Browse files Browse the repository at this point in the history
GH-81 Support stats filtering using text pattern (Resolve #81)
GH-80 Replace fixed stats limiter with dynamic algorithm (Resolve #80)
  • Loading branch information
dzikoysk committed May 26, 2020
1 parent 2b8ea97 commit 00fd8d7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/main/java/org/panda_lang/reposilite/console/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.panda_lang.reposilite.console;

import io.vavr.control.Try;
import org.panda_lang.reposilite.Reposilite;
import org.panda_lang.reposilite.auth.KeygenCommand;
import org.panda_lang.reposilite.auth.RevokeCommand;
Expand Down Expand Up @@ -67,7 +68,13 @@ public boolean execute(String command) throws Exception {

switch (command.toLowerCase()) {
case "stats":
return new StatsCommand(elements.length == 1 ? 2 : Integer.parseInt(elements[1])).call(reposilite);
if (elements.length == 1) {
return new StatsCommand(-1).call(reposilite);
}

return Try.ofSupplier(() -> new StatsCommand(Long.parseLong(elements[1])))
.getOrElse(new StatsCommand(elements[1]))
.call(reposilite);
case "keygen":
return new KeygenCommand(elements[1], elements[2]).call(reposilite);
case "revoke":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public boolean call(Reposilite reposilite) {
Reposilite.getLogger().info("Reposilite " + ReposiliteConstants.VERSION + " Commands:");
Reposilite.getLogger().info(" help - List available commands");
Reposilite.getLogger().info(" status - Display summary status of app health");
Reposilite.getLogger().info(" stats [<limiter> = 2] - Display collected metrics and (optional) filter them using the given limiter");
Reposilite.getLogger().info(" stats [<limiter>/<pattern>] - Display collected metrics and (optional) filter them using the given limiter or pattern");
Reposilite.getLogger().info(" tokens - List all generated tokens");
Reposilite.getLogger().info(" keygen <path> <alias> - Generate a new access token for the given path");
Reposilite.getLogger().info(" revoke <alias> - Revoke token");
Expand Down
33 changes: 28 additions & 5 deletions src/main/java/org/panda_lang/reposilite/stats/StatsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,51 @@

import org.panda_lang.reposilite.Reposilite;
import org.panda_lang.reposilite.console.NanoCommand;
import org.panda_lang.utilities.commons.StringUtils;
import org.panda_lang.utilities.commons.console.Effect;

import java.util.Map;
import java.util.Map.Entry;

public final class StatsCommand implements NanoCommand {

private final int limiter;
private final String pattern;
private Long limiter;

public StatsCommand(int limiter) {
public StatsCommand(long limiter, String pattern) {
this.limiter = limiter;
this.pattern = pattern;
}

public StatsCommand(long limiter) {
this(limiter, "");
}

public StatsCommand(String pattern) {
this(0, pattern);
}

@Override
public boolean call(Reposilite reposilite) {
StatsService statsService = reposilite.getStatsService();
Map<String, Integer> stats = statsService.fetchStats(entry -> entry.getValue() >= limiter);
int count = statsService.countRecords();
int sum = statsService.sumRecords();

if (limiter == -1) {
double avg = sum / (double) count;
limiter = Math.round(avg + (0.2 * avg));
}

Map<String, Integer> stats = statsService.fetchStats(entry -> entry.getValue() >= limiter && entry.getKey().contains(pattern));

Reposilite.getLogger().info("");
Reposilite.getLogger().info("Statistics: ");
Reposilite.getLogger().info(" Requests count: " + statsService.countRecords() + " (sum: " + statsService.sumRecords() + ")");
Reposilite.getLogger().info(" Recorded: " + (stats.isEmpty() ? "[] " : "") + "(list filtered by at least " + Effect.BLACK_BOLD + limiter + Effect.RESET + " recorded requests)");
Reposilite.getLogger().info(" Requests count: " + count + " (sum: " + sum + ")");
Reposilite.getLogger().info(StringUtils.join(
" Recorded: " + (stats.isEmpty() ? "[] " : ""),
" (limiter: " + Effect.BLACK_BOLD + limiter + Effect.RESET, ", pattern: '" + Effect.BLACK_BOLD + pattern + Effect.RESET + "')"
));

int order = 0;

for (Entry<String, Integer> entry : stats.entrySet()) {
Expand Down

0 comments on commit 00fd8d7

Please sign in to comment.