<commit_before>package water.api;

import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;

import hex.CreateFrame;
import hex.GridSearch.GridSearchProgress;
import hex.KMeans2;
import hex.KMeans2.KMeans2ModelView;
import hex.KMeans2.KMeans2Progress;
import hex.ReBalance;
import hex.deeplearning.DeepLearning;
import hex.drf.DRF;
import hex.gapstat.GapStatistic;
import hex.gapstat.GapStatisticModelView;
import hex.gbm.GBM;
import hex.glm.*;
import hex.nb.NBModelView;
import hex.nb.NBProgressPage;
import hex.gapstat.GapStatisticProgressPage;
import hex.nb.NaiveBayes;
import hex.pca.PCA;
import hex.pca.PCAModelView;
import hex.pca.PCAProgressPage;
import hex.pca.PCAScore;
import hex.singlenoderf.SpeeDRF;
import hex.singlenoderf.SpeeDRFModelView;
import hex.singlenoderf.SpeeDRFProgressPage;
import water.AutoBuffer;
import water.Boot;
import water.H2O;
import water.NanoHTTPD;
import water.api.Upload.PostFile;
import water.api.handlers.ModelBuildersMetadataHandlerV1;
import water.deploy.LaunchJar;
import water.schemas.HTTP404V1;
import water.schemas.HTTP500V1;
import water.schemas.Schema;
import water.util.Log;
import water.util.Log.Tag.Sys;
import water.util.RString;
import water.util.Utils.ExpectedExceptionForDebug;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/** This is a simple web server. */
public class RequestServer extends NanoHTTPD {
  private static final int LATEST_VERSION = 2;

  public enum API_VERSION {
    V_1(1, "/"),
    V_2(2, "/2/"); // FIXME: better should be /v2/
    final private int _version;
    final private String _prefix;
    public final String prefix() { return _prefix; }
    private API_VERSION(int version, String prefix) { _version = version; _prefix = prefix; }
  }
  static RequestServer SERVER;

  // cache of all loaded resources
  private static final ConcurrentHashMap<String,byte[]> _cache = new ConcurrentHashMap();
  protected static final HashMap<String,Request> _requests = new HashMap();

  // An array of regexs-over-URLs and handling Methods.
  // The list is searched in-order, first match gets dispatched.
  protected static final LinkedHashMap<String,Method> _handlers = new LinkedHashMap<String,Method>();

  static final Request _http404;
  static final Request _http500;

  public static final Response response404(NanoHTTPD server, Properties parms) { return _http404.serve(server, parms, Request.RequestType.www); }
  public static final Response response500(NanoHTTPD server, Properties parms) { return _http500.serve(server, parms, Request.RequestType.www); }

  // initialization ------------------------------------------------------------
  static {
    boolean USE_NEW_TAB = true;

    _http404 = registerRequest(new HTTP404());
    _http500 = registerRequest(new HTTP500());


//    Request.addToNavbar(registerRequest(new Inspect4UX()),  "NEW Inspect",                "Data"); //disable for now
    // REST API helper
    // Request.addToNavbar(registerRequest(new GBMSchemaV1()),    "GBM API call", "REST");
    // Request.addToNavbar(registerRequest(new GBMSchemaBloody()), "GBM Bloody", "REST");
    registerGET("/1/metadata/modelbuilders/.*", ModelBuildersMetadataHandlerV1.class, "show");
    registerGET("/1/metadata/modelbuilders", ModelBuildersMetadataHandlerV1.class, "list");

    // Data
    Request.addToNavbar(registerRequest(new ImportFiles2()),  "Import Files",           "Data");
    Request.addToNavbar(registerRequest(new Upload2()),       "Upload",                 "Data");
    Request.addToNavbar(registerRequest(new Parse2()),        "Parse",                  "Data");
    Request.addToNavbar(registerRequest(new Inspector()),     "Inspect",                "Data");
    Request.addToNavbar(registerRequest(new SummaryPage2()),  "Summary",                "Data");
    Request.addToNavbar(registerRequest(new QuantilesPage()), "Quantiles",              "Data");
    Request.addToNavbar(registerRequest(new StoreView()),     "View All",               "Data");
    Request.addToNavbar(registerRequest(new ExportFiles()),   "Export Files",           "Data");
    // Register Inspect2 just for viewing frames
    registerRequest(new Inspect2());

    // Not supported for now
//    Request.addToNavbar(registerRequest(new ExportS3()),    "Export S3",                  "Data");
//    Request.addToNavbar(registerRequest(new ExportHdfs()),  "Export HDFS",                "Data");

    // Remove VA based algos from GUI
//    Request.addToNavbar(registerRequest(new GLM()),         "GLM",                        "Model");
//    Request.addToNavbar(registerRequest(new GLMGrid()),     "GLM Grid",                   "Model");
//    Request.addToNavbar(registerRequest(new KMeans()),      "KMeans",                     "Model");
//    Request.addToNavbar(registerRequest(new RF()),          "Single Node RF",             "Model");

    // FVec models
    Request.addToNavbar(registerRequest(new PCA()),         "PCA",                      "Model");
    Request.addToNavbar(registerRequest(new GBM()),         "GBM",                      "Model");
    Request.addToNavbar(registerRequest(new DeepLearning()),"Deep Learning",            "Model");
    Request.addToNavbar(registerRequest(new DRF()),         "Distributed RF",           "Model");
    Request.addToNavbar(registerRequest(new GLM2()),        "GLM",                      "Model");
    Request.addToNavbar(registerRequest(new SpeeDRF()),     "SpeeDRF (Beta)",           "Model");
    Request.addToNavbar(registerRequest(new KMeans2()),     "KMeans (Beta)",            "Model");
    Request.addToNavbar(registerRequest(new NaiveBayes()),  "Naive Bayes (Beta)",       "Model");

    // FVec scoring
    Request.addToNavbar(registerRequest(new Predict()),     "Predict",                  "Score");
    Request.addToNavbar(registerRequest(new ConfusionMatrix()), "Confusion Matrix",     "Score");
    Request.addToNavbar(registerRequest(new AUC()),         "AUC",                      "Score");
    Request.addToNavbar(registerRequest(new HitRatio()),    "HitRatio",                 "Score");
    Request.addToNavbar(registerRequest(new PCAScore()),    "PCAScore",                 "Score");
    Request.addToNavbar(registerRequest(new Steam()),    "Multi-model Scoring (Beta)", "Score");

    // Admin
    Request.addToNavbar(registerRequest(new Jobs()),        "Jobs",                     "Admin");
    Request.addToNavbar(registerRequest(new Cloud()),       "Cluster Status",           "Admin");
    Request.addToNavbar(registerRequest(new IOStatus()),    "Cluster I/O",              "Admin");
    Request.addToNavbar(registerRequest(new Timeline()),    "Timeline",                 "Admin");
    Request.addToNavbar(registerRequest(new JProfile()),    "Profiler",                 "Admin");
    Request.addToNavbar(registerRequest(new JStack()),      "Stack Dump",               "Admin");
    Request.addToNavbar(registerRequest(new Debug()),       "Debug Dump",               "Admin");
    Request.addToNavbar(registerRequest(new LogView()),     "Inspect Log",              "Admin");
    Request.addToNavbar(registerRequest(new UnlockKeys()),  "Unlock Keys",              "Admin");
    Request.addToNavbar(registerRequest(new Shutdown()),    "Shutdown",                 "Admin");

    // Help and Tutorials
    Request.addToNavbar(registerRequest(new Documentation()),       "H2O Documentation",      "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new Tutorials()),           "Tutorials Home",         "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new TutorialGBM()),         "GBM Tutorial",           "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new TutorialDeepLearning()),"Deep Learning Tutorial", "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new TutorialRFIris()),      "Random Forest Tutorial", "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new TutorialGLMProstate()), "GLM Tutorial",           "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new TutorialKMeans()),      "KMeans Tutorial",        "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new AboutH2O()),            "About H2O",              "Help");

    // Beta things should be reachable by the API and web redirects, but not put in the menu.
    if(H2O.OPT_ARGS.beta == null) {
      registerRequest(new hex.LR2());
      registerRequest(new ReBalance());
      registerRequest(new FrameSplitPage());
      registerRequest(new NFoldFrameExtractPage());
      registerRequest(new GapStatistic());
      registerRequest(new CreateFrame());
      registerRequest(new KillMinus3());
      registerRequest(new SaveModel());
      registerRequest(new LoadModel());
    } else {
      Request.addToNavbar(registerRequest(new hex.LR2()),              "Linear Regression2",   "Beta");
      Request.addToNavbar(registerRequest(new ReBalance()),            "ReBalance",            "Beta");
      Request.addToNavbar(registerRequest(new FrameSplitPage()),       "Split frame",          "Beta");
      Request.addToNavbar(registerRequest(new NFoldFrameExtractPage()),"N-Fold frame extract", "Beta");
      Request.addToNavbar(registerRequest(new Console()),              "Console",              "Beta");
      Request.addToNavbar(registerRequest(new GapStatistic()),         "Gap Statistic",        "Beta");
      Request.addToNavbar(registerRequest(new CreateFrame()),          "Create Frame",         "Beta");
      Request.addToNavbar(registerRequest(new KillMinus3()),           "Kill Minus 3",         "Beta");
      Request.addToNavbar(registerRequest(new SaveModel()),            "Save Model",           "Beta");
      Request.addToNavbar(registerRequest(new LoadModel()),            "Load Model",           "Beta");
//      Request.addToNavbar(registerRequest(new ExportModel()),     "Export Model",         "Beta (FluidVecs!)");
//      Request.addToNavbar(registerRequest(new ImportModel()),     "Import Model",         "Beta (FluidVecs!)");
    }<commit_msg>Move Split Frame from Beta to Data section.<commit_after>package water.api;

import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;

import hex.CreateFrame;
import hex.GridSearch.GridSearchProgress;
import hex.KMeans2;
import hex.KMeans2.KMeans2ModelView;
import hex.KMeans2.KMeans2Progress;
import hex.ReBalance;
import hex.deeplearning.DeepLearning;
import hex.drf.DRF;
import hex.gapstat.GapStatistic;
import hex.gapstat.GapStatisticModelView;
import hex.gbm.GBM;
import hex.glm.*;
import hex.nb.NBModelView;
import hex.nb.NBProgressPage;
import hex.gapstat.GapStatisticProgressPage;
import hex.nb.NaiveBayes;
import hex.pca.PCA;
import hex.pca.PCAModelView;
import hex.pca.PCAProgressPage;
import hex.pca.PCAScore;
import hex.singlenoderf.SpeeDRF;
import hex.singlenoderf.SpeeDRFModelView;
import hex.singlenoderf.SpeeDRFProgressPage;
import water.AutoBuffer;
import water.Boot;
import water.H2O;
import water.NanoHTTPD;
import water.api.Upload.PostFile;
import water.api.handlers.ModelBuildersMetadataHandlerV1;
import water.deploy.LaunchJar;
import water.schemas.HTTP404V1;
import water.schemas.HTTP500V1;
import water.schemas.Schema;
import water.util.Log;
import water.util.Log.Tag.Sys;
import water.util.RString;
import water.util.Utils.ExpectedExceptionForDebug;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/** This is a simple web server. */
public class RequestServer extends NanoHTTPD {
  private static final int LATEST_VERSION = 2;

  public enum API_VERSION {
    V_1(1, "/"),
    V_2(2, "/2/"); // FIXME: better should be /v2/
    final private int _version;
    final private String _prefix;
    public final String prefix() { return _prefix; }
    private API_VERSION(int version, String prefix) { _version = version; _prefix = prefix; }
  }
  static RequestServer SERVER;

  // cache of all loaded resources
  private static final ConcurrentHashMap<String,byte[]> _cache = new ConcurrentHashMap();
  protected static final HashMap<String,Request> _requests = new HashMap();

  // An array of regexs-over-URLs and handling Methods.
  // The list is searched in-order, first match gets dispatched.
  protected static final LinkedHashMap<String,Method> _handlers = new LinkedHashMap<String,Method>();

  static final Request _http404;
  static final Request _http500;

  public static final Response response404(NanoHTTPD server, Properties parms) { return _http404.serve(server, parms, Request.RequestType.www); }
  public static final Response response500(NanoHTTPD server, Properties parms) { return _http500.serve(server, parms, Request.RequestType.www); }

  // initialization ------------------------------------------------------------
  static {
    boolean USE_NEW_TAB = true;

    _http404 = registerRequest(new HTTP404());
    _http500 = registerRequest(new HTTP500());


//    Request.addToNavbar(registerRequest(new Inspect4UX()),  "NEW Inspect",                "Data"); //disable for now
    // REST API helper
    // Request.addToNavbar(registerRequest(new GBMSchemaV1()),    "GBM API call", "REST");
    // Request.addToNavbar(registerRequest(new GBMSchemaBloody()), "GBM Bloody", "REST");
    registerGET("/1/metadata/modelbuilders/.*", ModelBuildersMetadataHandlerV1.class, "show");
    registerGET("/1/metadata/modelbuilders", ModelBuildersMetadataHandlerV1.class, "list");

    // Data
    Request.addToNavbar(registerRequest(new ImportFiles2()),  "Import Files",           "Data");
    Request.addToNavbar(registerRequest(new Upload2()),       "Upload",                 "Data");
    Request.addToNavbar(registerRequest(new Parse2()),        "Parse",                  "Data");
    Request.addToNavbar(registerRequest(new Inspector()),     "Inspect",                "Data");
    Request.addToNavbar(registerRequest(new SummaryPage2()),  "Summary",                "Data");
    Request.addToNavbar(registerRequest(new QuantilesPage()), "Quantiles",              "Data");
    Request.addToNavbar(registerRequest(new FrameSplitPage()),"Split frame",            "Data");
    Request.addToNavbar(registerRequest(new StoreView()),     "View All",               "Data");
    Request.addToNavbar(registerRequest(new ExportFiles()),   "Export Files",           "Data");
    // Register Inspect2 just for viewing frames
    registerRequest(new Inspect2());

    // Not supported for now
//    Request.addToNavbar(registerRequest(new ExportS3()),    "Export S3",                  "Data");
//    Request.addToNavbar(registerRequest(new ExportHdfs()),  "Export HDFS",                "Data");

    // Remove VA based algos from GUI
//    Request.addToNavbar(registerRequest(new GLM()),         "GLM",                        "Model");
//    Request.addToNavbar(registerRequest(new GLMGrid()),     "GLM Grid",                   "Model");
//    Request.addToNavbar(registerRequest(new KMeans()),      "KMeans",                     "Model");
//    Request.addToNavbar(registerRequest(new RF()),          "Single Node RF",             "Model");

    // FVec models
    Request.addToNavbar(registerRequest(new PCA()),         "PCA",                      "Model");
    Request.addToNavbar(registerRequest(new GBM()),         "GBM",                      "Model");
    Request.addToNavbar(registerRequest(new DeepLearning()),"Deep Learning",            "Model");
    Request.addToNavbar(registerRequest(new DRF()),         "Distributed RF",           "Model");
    Request.addToNavbar(registerRequest(new GLM2()),        "GLM",                      "Model");
    Request.addToNavbar(registerRequest(new SpeeDRF()),     "SpeeDRF (Beta)",           "Model");
    Request.addToNavbar(registerRequest(new KMeans2()),     "KMeans (Beta)",            "Model");
    Request.addToNavbar(registerRequest(new NaiveBayes()),  "Naive Bayes (Beta)",       "Model");

    // FVec scoring
    Request.addToNavbar(registerRequest(new Predict()),     "Predict",                  "Score");
    Request.addToNavbar(registerRequest(new ConfusionMatrix()), "Confusion Matrix",     "Score");
    Request.addToNavbar(registerRequest(new AUC()),         "AUC",                      "Score");
    Request.addToNavbar(registerRequest(new HitRatio()),    "HitRatio",                 "Score");
    Request.addToNavbar(registerRequest(new PCAScore()),    "PCAScore",                 "Score");
    Request.addToNavbar(registerRequest(new Steam()),    "Multi-model Scoring (Beta)", "Score");

    // Admin
    Request.addToNavbar(registerRequest(new Jobs()),        "Jobs",                     "Admin");
    Request.addToNavbar(registerRequest(new Cloud()),       "Cluster Status",           "Admin");
    Request.addToNavbar(registerRequest(new IOStatus()),    "Cluster I/O",              "Admin");
    Request.addToNavbar(registerRequest(new Timeline()),    "Timeline",                 "Admin");
    Request.addToNavbar(registerRequest(new JProfile()),    "Profiler",                 "Admin");
    Request.addToNavbar(registerRequest(new JStack()),      "Stack Dump",               "Admin");
    Request.addToNavbar(registerRequest(new Debug()),       "Debug Dump",               "Admin");
    Request.addToNavbar(registerRequest(new LogView()),     "Inspect Log",              "Admin");
    Request.addToNavbar(registerRequest(new UnlockKeys()),  "Unlock Keys",              "Admin");
    Request.addToNavbar(registerRequest(new Shutdown()),    "Shutdown",                 "Admin");

    // Help and Tutorials
    Request.addToNavbar(registerRequest(new Documentation()),       "H2O Documentation",      "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new Tutorials()),           "Tutorials Home",         "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new TutorialGBM()),         "GBM Tutorial",           "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new TutorialDeepLearning()),"Deep Learning Tutorial", "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new TutorialRFIris()),      "Random Forest Tutorial", "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new TutorialGLMProstate()), "GLM Tutorial",           "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new TutorialKMeans()),      "KMeans Tutorial",        "Help", USE_NEW_TAB);
    Request.addToNavbar(registerRequest(new AboutH2O()),            "About H2O",              "Help");

    // Beta things should be reachable by the API and web redirects, but not put in the menu.
    if(H2O.OPT_ARGS.beta == null) {
      registerRequest(new hex.LR2());
      registerRequest(new ReBalance());
      registerRequest(new NFoldFrameExtractPage());
      registerRequest(new GapStatistic());
      registerRequest(new CreateFrame());
      registerRequest(new KillMinus3());
      registerRequest(new SaveModel());
      registerRequest(new LoadModel());
    } else {
      Request.addToNavbar(registerRequest(new hex.LR2()),              "Linear Regression2",   "Beta");
      Request.addToNavbar(registerRequest(new ReBalance()),            "ReBalance",            "Beta");
      Request.addToNavbar(registerRequest(new NFoldFrameExtractPage()),"N-Fold frame extract", "Beta");
      Request.addToNavbar(registerRequest(new Console()),              "Console",              "Beta");
      Request.addToNavbar(registerRequest(new GapStatistic()),         "Gap Statistic",        "Beta");
      Request.addToNavbar(registerRequest(new CreateFrame()),          "Create Frame",         "Beta");
      Request.addToNavbar(registerRequest(new KillMinus3()),           "Kill Minus 3",         "Beta");
      Request.addToNavbar(registerRequest(new SaveModel()),            "Save Model",           "Beta");
      Request.addToNavbar(registerRequest(new LoadModel()),            "Load Model",           "Beta");
//      Request.addToNavbar(registerRequest(new ExportModel()),     "Export Model",         "Beta (FluidVecs!)");
//      Request.addToNavbar(registerRequest(new ImportModel()),     "Import Model",         "Beta (FluidVecs!)");
    }
{'fraction_non_alphanumeric': 0.13232459521973786, 'fraction_numerical': 0.007517347725520432, 'mean_word_length': 3.1908319870759287, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4658261', 'n_tokens_mistral': 6048, 'n_tokens_neox': 5874, 'n_words': 1246}