com.algodefu.yeti.web.ClusterServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.algodefu.yeti.web.ClusterServlet.java

Source

/*
 * Copyright 2014 Algodefu
 *
 * 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 com.algodefu.yeti.web;

import com.algodefu.yeti.data.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public class ClusterServlet extends HttpServlet {
    private DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json");
        response.setHeader("Cache-Control", "nocache");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        ClusterFormat clusterFormat = new ClusterFormat();

        // for JSON
        String errMsg = "";
        String clusterTitle = "";
        List<Cluster> clusterData = new ArrayList<>();
        String symbol = "";

        try {
            symbol = request.getParameter("symbol");
            int count = Integer.parseInt(request.getParameter("count"));
            LocalDateTime beginDt = LocalDateTime.parse(request.getParameter("beginDt"), dtf);
            LocalDateTime endDt = LocalDateTime.parse(request.getParameter("endDt"), dtf);

            ClusterType clusterType = ClusterType.fromName(request.getParameter("cluster-type"));
            clusterFormat.setType(clusterType);
            clusterFormat.setSymbol(symbol);
            clusterFormat.setBeginDt(beginDt);
            clusterFormat.setEndDt(endDt);
            clusterFormat.setCount(count);

            switch (clusterType) {
            case TIMEFRAME:
            case TICKTIME:
                int timeInterval = Integer.parseInt(request.getParameter("time-interval"));
                ChronoUnit chronoUnit = ChronoUnit.valueOf(request.getParameter("chronoUnit"));
                clusterFormat.setTimeInterval(timeInterval);
                clusterFormat.setChronoUnit(chronoUnit);
                break;
            case PRICERANGE:
                double priceRange = Double.parseDouble(request.getParameter("price-range"));
                clusterFormat.setPriceRange(priceRange);
                break;
            case VOLUME:
                long clusterVolume = Long.parseLong(request.getParameter("cluster-volume"));
                clusterFormat.setClusterVolume(clusterVolume);
                break;
            case TRADECOUNT:
                long maxTicksInCluster = Long.parseLong(request.getParameter("maxNumTicksInCluster"));
                clusterFormat.setMaxTicksInCluster(maxTicksInCluster);
                break;
            }

            ClusterSource clusterSource = new ClusterSource(clusterFormat);
            Instant start = Instant.now();
            clusterData = clusterSource.getClusters();
            Instant stop = Instant.now();
            System.out.printf("%d clusters of type %s generated in %d ms. \n", clusterData.size(),
                    clusterFormat.getType().toString(), Duration.between(start, stop).toMillis());

            // add clusterType as title
            if (clusterFormat.getType() != null)
                clusterTitle = clusterFormat.getType().toString() + " CLUSTERS";

        } catch (Exception e) {
            errMsg = e.toString().replace("\"", " ");
            e.printStackTrace();
        }

        Hashtable<String, Object> hashtable = new Hashtable<>();
        hashtable.put("errMsg", errMsg);
        hashtable.put("symbol", symbol);
        hashtable.put("clusterTitle", clusterTitle);
        hashtable.put("clusterData", clusterData.toArray());

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Cluster.class, new ClusterTypeAdapter());
        Gson gson = gsonBuilder.create();

        out.println(gson.toJson(hashtable));
        out.flush();
        out.close();
    }

}