// Path: dotTrace-common/src/main/java/jetbrains/buildServer/dotTrace/Constants.java
// public class Constants {
//     public static final String USE_VAR = "dot_trace";
//     public static final String PATH_VAR = "dot_trace_path";
//     public static final String THRESHOLDS_VAR = "dot_trace_thresholds";
//     public static final String MEASURE_TYPE_VAR = "dot_trace_measure_type";
//     public static final String PROFILE_CHILD_PROCESSES_VAR = "dot_trace_profile_child_processes";
//     public static final String PROCESS_FILTERS_VAR = "dot_trace_process_filters";
//     public static final String SNAPSHOTS_PATH_VAR = "dot_trace_snapshots_path";
// }
// 
// Path: dotTrace-common/src/main/java/jetbrains/buildServer/dotTrace/StatisticMessage.java
// public class StatisticMessage extends MessageWithAttributes {
//   public static final String MESSAGE_NAME = "dotTraceStatistic";
//   private static final String METHOD_STATISTIC_MESSAGE_ATTR = "methodName";
//   private static final String THRESHOLD_TOTAL_TIME_STATISTIC_MESSAGE_ATTR = "totalTimeThreshold";
//   private static final String THRESHOLD_OWN_TIME_STATISTIC_MESSAGE_ATTR = "ownTimeThreshold";
//   private static final String MEASURED_TOTAL_TIME_STATISTIC_MESSAGE_ATTR = "measuredTotalTime";
//   private static final String MEASURED_OWN_TIME_STATISTIC_MESSAGE_ATTR = "measuredOwnTime";
// 
//   public StatisticMessage(
//     @NotNull final String methodName,
//     @NotNull final String totalTimeThreshold,
//     @NotNull final String ownTimeThreshold,
//     @NotNull final String measuredTotalTime,
//     @NotNull final String measuredOwnTime) {
//     super(
//       MESSAGE_NAME,
//       CollectionsUtil.asMap(
//         METHOD_STATISTIC_MESSAGE_ATTR, methodName,
//         THRESHOLD_TOTAL_TIME_STATISTIC_MESSAGE_ATTR, totalTimeThreshold,
//         THRESHOLD_OWN_TIME_STATISTIC_MESSAGE_ATTR, ownTimeThreshold,
//         MEASURED_TOTAL_TIME_STATISTIC_MESSAGE_ATTR, measuredTotalTime,
//         MEASURED_OWN_TIME_STATISTIC_MESSAGE_ATTR, measuredOwnTime));
//   }
// 
//   private StatisticMessage(@NotNull final Map<String, String> attributes) {
//     super(MESSAGE_NAME, attributes);
//   }
// 
//   @Nullable
//   public static StatisticMessage tryParse(@NotNull final ServiceMessage message) {
//     if(!MESSAGE_NAME.equalsIgnoreCase(message.getMessageName())) {
//       return null;
//     }
// 
//     return new StatisticMessage(message.getAttributes());
//   }
// 
//   @NotNull
//   public String getMethodName() {
//     return getAttributes().get(METHOD_STATISTIC_MESSAGE_ATTR);
//   }
// 
//   @NotNull
//   public String getOwnTimeThreshold() {
//     return getAttributes().get(THRESHOLD_OWN_TIME_STATISTIC_MESSAGE_ATTR);
//   }
// 
//   @NotNull
//   public String getTotalTimeThreshold() {
//     return getAttributes().get(THRESHOLD_TOTAL_TIME_STATISTIC_MESSAGE_ATTR);
//   }
// 
//   @NotNull
//   public String getMeasuredOwnTime() {
//     return getAttributes().get(MEASURED_OWN_TIME_STATISTIC_MESSAGE_ATTR);
//   }
// 
//   @NotNull
//   public String getMeasuredTotalTime() {
//     return getAttributes().get(MEASURED_TOTAL_TIME_STATISTIC_MESSAGE_ATTR);
//   }
// 
//   @Override
//   public boolean equals(final Object o) {
//     if (this == o) return true;
//     if (o == null || getClass() != o.getClass()) return false;
// 
//     final StatisticMessage statisticMessage = (StatisticMessage)o;
// 
//     if (!getMethodName().equals(statisticMessage.getMethodName())) return false;
//     if (!getTotalTimeThreshold().equals(statisticMessage.getTotalTimeThreshold())) return false;
//     if (!getOwnTimeThreshold().equals(statisticMessage.getOwnTimeThreshold())) return false;
//     if (!getMeasuredTotalTime().equals(statisticMessage.getMeasuredTotalTime())) return false;
//     return getMeasuredOwnTime().equals(statisticMessage.getMeasuredOwnTime());
// 
//   }
// 
//   @Override
//   public int hashCode() {
//     int result = getMethodName().hashCode();
//     result = 31 * result + getTotalTimeThreshold().hashCode();
//     result = 31 * result + getOwnTimeThreshold().hashCode();
//     result = 31 * result + getMeasuredTotalTime().hashCode();
//     result = 31 * result + getMeasuredOwnTime().hashCode();
//     return result;
//   }
// }


import com.intellij.openapi.util.text.StringUtil;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import jetbrains.buildServer.dotNet.buildRunner.agent.*;
import jetbrains.buildServer.dotTrace.Constants;
import jetbrains.buildServer.dotTrace.StatisticMessage;
import org.jetbrains.annotations.NotNull;
    myThresholdsParser = thresholdsParser;
    myAfterBuildPublisher = afterBuildPublisher;
    myParametersService = parametersService;
    myFileService = fileService;
    myLoggerService = loggerService;
  }

  @Override
  public void publishBeforeBuildFile(@NotNull final CommandLineExecutionContext commandLineExecutionContext, @NotNull final File file, @NotNull final String content) {
  }

  @Override
  public void publishAfterBuildArtifactFile(@NotNull final CommandLineExecutionContext commandLineExecutionContext, @NotNull final File reportFile) {
    myAfterBuildPublisher.publishAfterBuildArtifactFile(commandLineExecutionContext, reportFile);

    Metrics thresholdValues;
    String thresholdsStr = myParametersService.tryGetRunnerParameter(Constants.THRESHOLDS_VAR);
    if(!StringUtil.isEmptyOrSpaces(thresholdsStr)) {
      thresholdValues = myThresholdsParser.parse(thresholdsStr);
    }
    else {
      return;
    }

    try {
      final String reportContent = myFileService.readAllTextFile(reportFile);
      final Metrics measuredValues = myReportParser.parse(reportContent);
      for (Metric measuredValue : measuredValues.getMetrics()) {
        for (final Metric thresholdValue : thresholdValues.getMetrics()) {
          if(measuredValue.getMethodName().startsWith(thresholdValue.getMethodName())) {
// Path: dotTrace-common/src/main/java/jetbrains/buildServer/dotTrace/Constants.java
// public class Constants {
//     public static final String USE_VAR = "dot_trace";
//     public static final String PATH_VAR = "dot_trace_path";
//     public static final String THRESHOLDS_VAR = "dot_trace_thresholds";
//     public static final String MEASURE_TYPE_VAR = "dot_trace_measure_type";
//     public static final String PROFILE_CHILD_PROCESSES_VAR = "dot_trace_profile_child_processes";
//     public static final String PROCESS_FILTERS_VAR = "dot_trace_process_filters";
//     public static final String SNAPSHOTS_PATH_VAR = "dot_trace_snapshots_path";
// }
// 
// Path: dotTrace-common/src/main/java/jetbrains/buildServer/dotTrace/StatisticMessage.java
// public class StatisticMessage extends MessageWithAttributes {
//   public static final String MESSAGE_NAME = "dotTraceStatistic";
//   private static final String METHOD_STATISTIC_MESSAGE_ATTR = "methodName";
//   private static final String THRESHOLD_TOTAL_TIME_STATISTIC_MESSAGE_ATTR = "totalTimeThreshold";
//   private static final String THRESHOLD_OWN_TIME_STATISTIC_MESSAGE_ATTR = "ownTimeThreshold";
//   private static final String MEASURED_TOTAL_TIME_STATISTIC_MESSAGE_ATTR = "measuredTotalTime";
//   private static final String MEASURED_OWN_TIME_STATISTIC_MESSAGE_ATTR = "measuredOwnTime";
// 
//   public StatisticMessage(
//     @NotNull final String methodName,
//     @NotNull final String totalTimeThreshold,
//     @NotNull final String ownTimeThreshold,
//     @NotNull final String measuredTotalTime,
//     @NotNull final String measuredOwnTime) {
//     super(
//       MESSAGE_NAME,
//       CollectionsUtil.asMap(
//         METHOD_STATISTIC_MESSAGE_ATTR, methodName,
//         THRESHOLD_TOTAL_TIME_STATISTIC_MESSAGE_ATTR, totalTimeThreshold,
//         THRESHOLD_OWN_TIME_STATISTIC_MESSAGE_ATTR, ownTimeThreshold,
//         MEASURED_TOTAL_TIME_STATISTIC_MESSAGE_ATTR, measuredTotalTime,
//         MEASURED_OWN_TIME_STATISTIC_MESSAGE_ATTR, measuredOwnTime));
//   }
// 
//   private StatisticMessage(@NotNull final Map<String, String> attributes) {
//     super(MESSAGE_NAME, attributes);
//   }
// 
//   @Nullable
//   public static StatisticMessage tryParse(@NotNull final ServiceMessage message) {
//     if(!MESSAGE_NAME.equalsIgnoreCase(message.getMessageName())) {
//       return null;
//     }
// 
//     return new StatisticMessage(message.getAttributes());
//   }
// 
//   @NotNull
//   public String getMethodName() {
//     return getAttributes().get(METHOD_STATISTIC_MESSAGE_ATTR);
//   }
// 
//   @NotNull
//   public String getOwnTimeThreshold() {
//     return getAttributes().get(THRESHOLD_OWN_TIME_STATISTIC_MESSAGE_ATTR);
//   }
// 
//   @NotNull
//   public String getTotalTimeThreshold() {
//     return getAttributes().get(THRESHOLD_TOTAL_TIME_STATISTIC_MESSAGE_ATTR);
//   }
// 
//   @NotNull
//   public String getMeasuredOwnTime() {
//     return getAttributes().get(MEASURED_OWN_TIME_STATISTIC_MESSAGE_ATTR);
//   }
// 
//   @NotNull
//   public String getMeasuredTotalTime() {
//     return getAttributes().get(MEASURED_TOTAL_TIME_STATISTIC_MESSAGE_ATTR);
//   }
// 
//   @Override
//   public boolean equals(final Object o) {
//     if (this == o) return true;
//     if (o == null || getClass() != o.getClass()) return false;
// 
//     final StatisticMessage statisticMessage = (StatisticMessage)o;
// 
//     if (!getMethodName().equals(statisticMessage.getMethodName())) return false;
//     if (!getTotalTimeThreshold().equals(statisticMessage.getTotalTimeThreshold())) return false;
//     if (!getOwnTimeThreshold().equals(statisticMessage.getOwnTimeThreshold())) return false;
//     if (!getMeasuredTotalTime().equals(statisticMessage.getMeasuredTotalTime())) return false;
//     return getMeasuredOwnTime().equals(statisticMessage.getMeasuredOwnTime());
// 
//   }
// 
//   @Override
//   public int hashCode() {
//     int result = getMethodName().hashCode();
//     result = 31 * result + getTotalTimeThreshold().hashCode();
//     result = 31 * result + getOwnTimeThreshold().hashCode();
//     result = 31 * result + getMeasuredTotalTime().hashCode();
//     result = 31 * result + getMeasuredOwnTime().hashCode();
//     return result;
//   }
// }


// Path: dotTrace-agent/src/main/java/jetbrains/buildServer/dotTrace/agent/BuildPublisher.java
import com.intellij.openapi.util.text.StringUtil;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import jetbrains.buildServer.dotNet.buildRunner.agent.*;
import jetbrains.buildServer.dotTrace.Constants;
import jetbrains.buildServer.dotTrace.StatisticMessage;
import org.jetbrains.annotations.NotNull;

    myThresholdsParser = thresholdsParser;
    myAfterBuildPublisher = afterBuildPublisher;
    myParametersService = parametersService;
    myFileService = fileService;
    myLoggerService = loggerService;
  }

  @Override
  public void publishBeforeBuildFile(@NotNull final CommandLineExecutionContext commandLineExecutionContext, @NotNull final File file, @NotNull final String content) {
  }

  @Override
  public void publishAfterBuildArtifactFile(@NotNull final CommandLineExecutionContext commandLineExecutionContext, @NotNull final File reportFile) {
    myAfterBuildPublisher.publishAfterBuildArtifactFile(commandLineExecutionContext, reportFile);

    Metrics thresholdValues;
    String thresholdsStr = myParametersService.tryGetRunnerParameter(Constants.THRESHOLDS_VAR);
    if(!StringUtil.isEmptyOrSpaces(thresholdsStr)) {
      thresholdValues = myThresholdsParser.parse(thresholdsStr);
    }
    else {
      return;
    }

    try {
      final String reportContent = myFileService.readAllTextFile(reportFile);
      final Metrics measuredValues = myReportParser.parse(reportContent);
      for (Metric measuredValue : measuredValues.getMetrics()) {
        for (final Metric thresholdValue : thresholdValues.getMetrics()) {
          if(measuredValue.getMethodName().startsWith(thresholdValue.getMethodName())) {
            myLoggerService.onMessage(new StatisticMessage(measuredValue.getMethodName(), thresholdValue.getTotalTime(), thresholdValue.getOwnTime(), measuredValue.getTotalTime(), measuredValue.getOwnTime()));