/*
 * Copyright 2012-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.glowroot.sandbox.ui;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.google.common.base.Charsets;
import com.google.common.base.Stopwatch;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;

import org.glowroot.container.AppUnderTest;
import org.glowroot.container.Container;
import org.glowroot.container.config.CoarseProfilingConfig;
import org.glowroot.container.config.FineProfilingConfig;
import org.glowroot.container.config.GeneralConfig;
import org.glowroot.container.config.StorageConfig;
import org.glowroot.container.javaagent.JavaagentContainer;
import org.glowroot.container.local.LocalContainer;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * @author Trask Stalnaker
 * @since 0.5
 */
public class UiSandboxMain {

    private static final JsonFactory jsonFactory = new JsonFactory();

    // need to use javaagent when testing pointcuts.html, otherwise class/method auto completion
    // won't be available
    private static final boolean useJavaagent = false;
    private static final int INITIAL_UI_PORT = 4001;
    private static final boolean rollOverQuickly = false;

    static {
        System.setProperty("glowroot.internal.collector.aggregateInterval", "15");
    }

    private UiSandboxMain() {}

    public static void main(String... args) throws Exception {
        Container container;
        File dataDir = new File("target");
        File configFile = new File(dataDir, "config.json");
        if (!configFile.exists()) {
            writeConfigJson(dataDir, INITIAL_UI_PORT);
        }
        if (useJavaagent) {
            container = new JavaagentContainer(dataDir, true, false, false);
        } else {
            container = new LocalContainer(dataDir, true, false);
        }
        // set thresholds low so there will be lots of data to view
        GeneralConfig generalConfig = container.getConfigService().getGeneralConfig();
        container.getConfigService().updateGeneralConfig(generalConfig);
        CoarseProfilingConfig coarseProfilingConfig = container.getConfigService()
                .getCoarseProfilingConfig();
        coarseProfilingConfig.setInitialDelayMillis(500);
        coarseProfilingConfig.setIntervalMillis(500);
        coarseProfilingConfig.setTotalSeconds(2);
        container.getConfigService().updateCoarseProfilingConfig(coarseProfilingConfig);
        FineProfilingConfig fineProfilingConfig = container.getConfigService()
                .getFineProfilingConfig();
        fineProfilingConfig.setTracePercentage(50);
        fineProfilingConfig.setIntervalMillis(10);
        fineProfilingConfig.setTotalSeconds(1);
        container.getConfigService().updateFineProfilingConfig(fineProfilingConfig);
        if (rollOverQuickly) {
            StorageConfig storageConfig = container.getConfigService().getStorageConfig();
            storageConfig.setRollingSizeMb(10);
            container.getConfigService().updateStorageConfig(storageConfig);
        }
        container.executeAppUnderTest(GenerateTraces.class);
    }

    private static void writeConfigJson(File dataDir, int uiPort) throws IOException {
        StringBuilder sb = new StringBuilder();
        JsonGenerator jg = jsonFactory.createGenerator(CharStreams.asWriter(sb));
        jg.writeStartObject();
        jg.writeObjectFieldStart("ui");
        jg.writeNumberField("port", uiPort);
        jg.writeEndObject();
        jg.writeEndObject();
        jg.close();
        Files.write(sb, new File(dataDir, "config.json"), Charsets.UTF_8);
    }

    public static class GenerateTraces implements AppUnderTest {
        @Override
        public void executeApp() throws InterruptedException {
            while (true) {
                Stopwatch stopwatch = Stopwatch.createStarted();
                while (stopwatch.elapsed(SECONDS) < 30) {
                    // a very short trace that will have an empty merged stack tree
                    new NestableCall(1, 10, 100).execute();
                    // a trace that will have merged stack tree with only a single leaf
                    new NestableCall(1, 100, 100).execute();
                    new NestableCall(new NestableCall(10, 50, 5000), 20, 50, 5000).execute();
                    new NestableCall(new NestableCall(5, 50, 5000), 5, 50, 5000).execute();
                    new NestableCall(new NestableCall(10, 50, 5000), 10, 50, 5000).execute();
                    new NestableCall(new NestableCall(20, 50, 5000), 5, 50, 5000).execute();
                }
                new NestableCall(new NestableCall(5000, 50, 5000), 100, 50, 5000).execute();
                Thread.sleep(1000);
            }
        }
    }
}

@@ -668,132 +668,8 @@
 le;%0A
-import java.io.IOException;%0A%0Aimport com.fasterxml.jackson.core.JsonFactory;%0Aimport com.fasterxml.jackson.core.JsonGenerator;
 %0Aimp
@@ -750,49 +750,8 @@
 ch;%0A
-import com.google.common.io.CharStreams;%0A
 impo
@@ -1339,202 +1339,8 @@
  %7B%0A%0A
-    private static final JsonFactory jsonFactory = new JsonFactory();%0A%0A    // need to use javaagent when testing pointcuts.html, otherwise class/method auto completion%0A    // won't be available%0A
     
@@ -1884,48 +1884,93 @@
     
-writeConfigJson(dataDir, INITIAL_UI_PORT
+Files.write(%22%7B%5C%22ui%5C%22:%7B%5C%22port%5C%22:%22 + INITIAL_UI_PORT + %22%7D%7D%22, configFile, Charsets.UTF_8
 );%0A 
@@ -3470,501 +3470,8 @@
  %7D%0A%0A
-    private static void writeConfigJson(File dataDir, int uiPort) throws IOException %7B%0A        StringBuilder sb = new StringBuilder();%0A        JsonGenerator jg = jsonFactory.createGenerator(CharStreams.asWriter(sb));%0A        jg.writeStartObject();%0A        jg.writeObjectFieldStart(%22ui%22);%0A        jg.writeNumberField(%22port%22, uiPort);%0A        jg.writeEndObject();%0A        jg.writeEndObject();%0A        jg.close();%0A        Files.write(sb, new File(dataDir, %22config.json%22), Charsets.UTF_8);%0A    %7D%0A%0A
     

Simplify sandbox initial ui port config