package com.yahoo.omid.notifications.metrics;

import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yammer.metrics.reporting.ConsoleReporter;
import com.yammer.metrics.reporting.CsvReporter;

public class MetricsUtils {

    static Logger logger = LoggerFactory.getLogger(MetricsUtils.class);
    static final Pattern METRICS_CONFIG_PATTERN = Pattern
            .compile("(csv:.+|console):(\\d+):(DAYS|HOURS|MICROSECONDS|MILLISECONDS|MINUTES|NANOSECONDS|SECONDS)");

    public static void initMetrics(String metricsConfig) {
        if (metricsConfig == null || metricsConfig.equals("")) {
            metricsConfig = "console:1:MINUTES";
        }
        Matcher matcher = METRICS_CONFIG_PATTERN.matcher(metricsConfig);
        if (!matcher.matches()) {
            logger.error(
                    "Invalid metrics configuration [{}]. Metrics configuration must match the pattern [{}]. Metrics reporting disabled.",
                    metricsConfig, METRICS_CONFIG_PATTERN);
        } else {
            String group1 = matcher.group(1);

            if (group1.startsWith("csv")) {
                String outputDir = group1.substring("csv:".length());
                long period = Long.valueOf(matcher.group(2));
                TimeUnit timeUnit = TimeUnit.valueOf(matcher.group(3));
                if (!(new File(outputDir).exists())) {
                    logger.error("output dir {} does not exist", outputDir);
                }
                logger.info("Reporting metrics through csv files in directory [{}] with frequency of [{}] [{}]",
                        new String[] { outputDir, String.valueOf(period), timeUnit.name() });
                CsvReporter.enable(new File(outputDir), period, timeUnit);
            } else {
                long period = Long.valueOf(matcher.group(2));
                TimeUnit timeUnit = TimeUnit.valueOf(matcher.group(3));
                logger.info("Reporting metrics on the console with frequency of [{}] [{}]",
                        new String[] { String.valueOf(period), timeUnit.name() });
                ConsoleReporter.enable(period, timeUnit);
            }
        }
    }
}

@@ -1513,16 +1513,15 @@
 ger.
-error(%22o
+warn(%22O
 utpu
@@ -1526,16 +1526,28 @@
 put dir 
+for metrics 
 %7B%7D does 
@@ -1555,16 +1555,29 @@
 ot exist
+! Creating...
 %22, outpu
@@ -1583,16 +1583,294 @@
 utDir);%0A
+                    boolean success = (new File(outputDir)).mkdirs();%0A                    if (!success) %7B%0A                        logger.error(%22Output dir for metrics %7B%7D cannot be created! NO metrics provided!%22, outputDir);%0A                        return;%0A                    %7D%0A
         

Create metrisch dir if it doesn't exsist