<commit_before>/*
 * Copyright 2012-2013 Continuuity,Inc. All Rights Reserved.
 */
package com.continuuity.metrics.query;

import com.continuuity.common.conf.CConfiguration;
import com.continuuity.common.conf.Constants;
import com.continuuity.common.guice.ConfigModule;
import com.continuuity.common.guice.DiscoveryRuntimeModule;
import com.continuuity.common.guice.LocationRuntimeModule;
import com.continuuity.common.metrics.MetricsCollectionService;
import com.continuuity.data.runtime.DataFabricLevelDBModule;
import com.continuuity.metrics.MetricsConstants;
import com.continuuity.metrics.guice.MetricsClientRuntimeModule;
import com.continuuity.metrics.guice.MetricsQueryRuntimeModule;
import com.continuuity.weave.discovery.Discoverable;
import com.continuuity.weave.discovery.DiscoveryServiceClient;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import java.io.File;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

/**
 *
 */
public class BaseMetricsQueryTest {

  private static File dataDir;
  private static Injector injector;
  protected static MetricsCollectionService collectionService;
  protected static MetricsQueryService queryService;

  protected InetSocketAddress getMetricsQueryEndpoint() throws InterruptedException {
    Iterable<Discoverable> endpoints = injector.getInstance(DiscoveryServiceClient.class)
                                               .discover(Constants.Service.METRICS);
    Iterator<Discoverable> itor = endpoints.iterator();
    while (!itor.hasNext()) {
      TimeUnit.SECONDS.sleep(1);
      itor = endpoints.iterator();
    }
    return itor.next().getSocketAddress();
  }

  @BeforeClass
  public static void init() {
    dataDir = Files.createTempDir();
    System.out.println(dataDir);

    CConfiguration cConf = CConfiguration.create();
    cConf.set(MetricsConstants.ConfigKeys.SERVER_PORT, "0");

    injector = Guice.createInjector(
      new ConfigModule(cConf),
      new DataFabricLevelDBModule(),
      new LocationRuntimeModule().getSingleNodeModules(),
      new DiscoveryRuntimeModule().getSingleNodeModules(),
      new MetricsClientRuntimeModule().getSingleNodeModules(),
      new MetricsQueryRuntimeModule().getSingleNodeModules()
    );

    collectionService = injector.getInstance(MetricsCollectionService.class);<commit_msg>metrics query tests were not using a temp folder so they could clash with each other.<commit_after>/*
 * Copyright 2012-2013 Continuuity,Inc. All Rights Reserved.
 */
package com.continuuity.metrics.query;

import com.continuuity.common.conf.CConfiguration;
import com.continuuity.common.conf.Constants;
import com.continuuity.common.guice.ConfigModule;
import com.continuuity.common.guice.DiscoveryRuntimeModule;
import com.continuuity.common.guice.LocationRuntimeModule;
import com.continuuity.common.metrics.MetricsCollectionService;
import com.continuuity.data.runtime.DataFabricLevelDBModule;
import com.continuuity.metrics.MetricsConstants;
import com.continuuity.metrics.guice.MetricsClientRuntimeModule;
import com.continuuity.metrics.guice.MetricsQueryRuntimeModule;
import com.continuuity.weave.discovery.Discoverable;
import com.continuuity.weave.discovery.DiscoveryServiceClient;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import java.io.File;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

/**
 *
 */
public class BaseMetricsQueryTest {

  private static File dataDir;
  private static Injector injector;
  protected static MetricsCollectionService collectionService;
  protected static MetricsQueryService queryService;

  protected InetSocketAddress getMetricsQueryEndpoint() throws InterruptedException {
    Iterable<Discoverable> endpoints = injector.getInstance(DiscoveryServiceClient.class)
                                               .discover(Constants.Service.METRICS);
    Iterator<Discoverable> itor = endpoints.iterator();
    while (!itor.hasNext()) {
      TimeUnit.SECONDS.sleep(1);
      itor = endpoints.iterator();
    }
    return itor.next().getSocketAddress();
  }

  @BeforeClass
  public static void init() {
    dataDir = Files.createTempDir();
    System.out.println(dataDir);

    CConfiguration cConf = CConfiguration.create();
    cConf.set(MetricsConstants.ConfigKeys.SERVER_PORT, "0");
    cConf.set(Constants.CFG_DATA_LEVELDB_DIR, dataDir.getAbsolutePath());

    injector = Guice.createInjector(
      new ConfigModule(cConf),
      new DataFabricLevelDBModule(cConf),
      new LocationRuntimeModule().getSingleNodeModules(),
      new DiscoveryRuntimeModule().getSingleNodeModules(),
      new MetricsClientRuntimeModule().getSingleNodeModules(),
      new MetricsQueryRuntimeModule().getSingleNodeModules()
    );

    collectionService = injector.getInstance(MetricsCollectionService.class);
{'fraction_non_alphanumeric': 0.09523809523809523, 'fraction_numerical': 0.0037943464238284954, 'mean_word_length': 4.977324263038549, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5834684', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1455, 'n_words': 264}