package com.griddynamics.jagger.webclient.client.data;

import com.griddynamics.jagger.webclient.client.dto.MetricDto;
import com.griddynamics.jagger.webclient.client.dto.MetricNameDto;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: kirilkadurilka
 * Date: 22.04.13
 * Time: 10:15
 * To change this template use File | Settings | File Templates.
 */
public class MetricRankingProvider {
    private static List<String> patterns = Arrays.asList(
            "Iterations",
            "Duration",
            "Throughput",
            "Latency",
            "Success rate",
            "Latency .+ %"
    );

    protected static int compare(String o1, String o2){
        Comparable o1Rank = getRank(o1);
        Comparable o2Rank = getRank(o2);
        if (o1Rank.compareTo(0)==0 && o2Rank.compareTo(0)==0){
            return o1.compareTo(o2);
        }
        return o1Rank.compareTo(o2Rank);
    }

    protected static Integer getRank(String o){
        int rank = 0;
        for (String pattern : patterns){
            if (pattern.matches(o)){
                return new Integer(patterns.size()-rank);
            }
            rank++;
        }
        return 0;
    }

    public static void sortMetricNames(List<MetricNameDto> list){
        Collections.sort(list, new Comparator<MetricNameDto>() {
            @Override
            public int compare(MetricNameDto o, MetricNameDto o2) {
                return (-1)*MetricRankingProvider.compare(o.getName(), o2.getName());
            }
        });
    }

    public static void sortMetrics(List<MetricDto> list){
        Collections.sort(list, new Comparator<MetricDto>() {
            @Override
            public int compare(MetricDto metricDto, MetricDto metricDto2) {
                return (-1)*MetricRankingProvider.compare(metricDto.getMetricName().getName(), metricDto2.getMetricName().getName());
            }
        });
    }
}

@@ -49,16 +49,60 @@
 .data;%0A%0A
+import com.google.gwt.regexp.shared.RegExp;%0A
 import c
@@ -156,16 +156,16 @@
 ricDto;%0A
-
 import c
@@ -467,22 +467,22 @@
 ic List%3C
-String
+RegExp
 %3E patter
@@ -513,16 +513,31 @@
         
+RegExp.compile(
 %22Iterati
@@ -540,16 +540,17 @@
 rations%22
+)
 ,%0A      
@@ -555,16 +555,31 @@
         
+RegExp.compile(
 %22Duratio
@@ -580,16 +580,17 @@
 uration%22
+)
 ,%0A      
@@ -595,16 +595,31 @@
         
+RegExp.compile(
 %22Through
@@ -622,16 +622,17 @@
 oughput%22
+)
 ,%0A      
@@ -641,31 +641,23 @@
     
-%22Latency%22,%0A            
+RegExp.compile(
 %22Suc
@@ -666,16 +666,17 @@
 ss rate%22
+)
 ,%0A      
@@ -685,16 +685,31 @@
     
+RegExp.compile(
 %22Latency
  .+ 
@@ -708,14 +708,10 @@
 ency
- .+ %25
 %22
+)
 %0A   
@@ -955,32 +955,121 @@
 (o2);%0A        %7D%0A
+        if (o1Rank.compareTo(o2Rank)==0)%7B%0A            return o1.compareTo(o2);%0A        %7D%0A
         return o
@@ -1183,22 +1183,22 @@
    for (
-String
+RegExp
  pattern
@@ -1239,15 +1239,12 @@
 ern.
-match
+t
 es
+t
 (o))

fix metrics sort