Skip to content

Commit

Permalink
Merge pull request #674 from lantanagroup/LNK-1825
Browse files Browse the repository at this point in the history
LNK-1825
  • Loading branch information
MontaltoNick committed Jan 29, 2024
2 parents b49162a + 73cfc23 commit 18a0ba2
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions core/src/main/java/com/lantanagroup/link/time/StopwatchManager.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.lantanagroup.link.time;

import com.lantanagroup.link.db.SharedService;
import com.lantanagroup.link.db.model.MetricData;
import com.lantanagroup.link.db.model.Metrics;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.*;

import com.lantanagroup.link.db.SharedService;

@Component
public class StopwatchManager {
private static final int COUNT_WIDTH = 6;
private static final int DURATION_WIDTH = 11;

private static final int CATEGORY_WIDTH_MIN = 15;
private static final int TASK_WIDTH_MIN = 11;
private SharedService sharedService;

private final Map<String, List<Duration>> durationsByTask = new LinkedHashMap<>();
Expand All @@ -36,15 +35,28 @@ public synchronized String getStatistics() {
int taskWidth = durationsByTask.keySet().stream()
.mapToInt(String::length)
.max()
.orElse(DURATION_WIDTH);
Formatter formatter = new Formatter(taskWidth, COUNT_WIDTH, DURATION_WIDTH);
.orElse(0);

taskWidth = Integer.max(taskWidth, TASK_WIDTH_MIN);

var categoryWidth = durationsByTask.entrySet()
.stream()
.mapToInt(s -> s.getKey().substring(s.getKey().indexOf(":") + 1).length())
.max()
.orElse(0);

categoryWidth = Integer.max(categoryWidth, CATEGORY_WIDTH_MIN);

Formatter formatter = new Formatter(categoryWidth, taskWidth, COUNT_WIDTH, DURATION_WIDTH);
StringBuilder statistics = new StringBuilder();
statistics.append(formatter.getHeaderLine("Task", "Count", "Total", "Min", "Mean", "Max"));
statistics.append(formatter.getHeaderLine("Category","Task", "Count", "Total", "Min", "Mean", "Max"));
for (Map.Entry<String, List<Duration>> entry : durationsByTask.entrySet()) {
String key = entry.getKey();
String task = key.substring(0, key.indexOf(":"));
String category = key.substring(key.indexOf(":") + 1);
List<Duration> durations = entry.getValue();
statistics.append(formatter.getValueLine(
category,
task,
durations.size(),
getTotal(durations),
Expand Down Expand Up @@ -105,15 +117,18 @@ private static class Formatter {
private final int taskWidth;
private final int countWidth;
private final int durationWidth;
private final int categoryWidth;

public Formatter(int taskWidth, int countWidth, int durationWidth) {
public Formatter(int categoryWidth, int taskWidth, int countWidth, int durationWidth) {
this.categoryWidth = categoryWidth;
this.taskWidth = taskWidth;
this.countWidth = countWidth;
this.durationWidth = durationWidth;
}

public String getHeaderLine(String task, String count, String... durations) {
public String getHeaderLine(String category, String task, String count, String... durations) {
List<String> headers = new ArrayList<>();
headers.add(StringUtils.center(category, categoryWidth));
headers.add(StringUtils.center(task, taskWidth));
headers.add(StringUtils.center(count, countWidth));
for (String duration : durations) {
Expand All @@ -122,8 +137,9 @@ public String getHeaderLine(String task, String count, String... durations) {
return String.join(SEPARATOR, headers) + System.lineSeparator();
}

public String getValueLine(String task, int count, Duration... durations) {
public String getValueLine(String category, String task, int count, Duration... durations) {
List<String> values = new ArrayList<>();
values.add(StringUtils.rightPad(category, categoryWidth));
values.add(StringUtils.rightPad(task, taskWidth));
values.add(StringUtils.leftPad(Integer.toString(count), countWidth));
for (Duration duration : durations) {
Expand Down

0 comments on commit 18a0ba2

Please sign in to comment.