All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thematchbox.river.rest.RestHelper Maven / Gradle / Ivy

Go to download

This project contains an abstract implementation of an ElasticSearch River and is used as a basis for custom river implementations.

There is a newer version: 1.1.3
Show newest version
package com.thematchbox.river.rest;

/* Copyright 2015 theMatchBox

   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.
*/

import com.thematchbox.river.actions.IndexFeedback;
import com.thematchbox.river.actions.IndexJob;
import com.thematchbox.river.actions.IndexKey;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

public class RestHelper {
    public static void appendRequest(XContentBuilder builder, IndexJob indexJob) throws IOException {
        builder.startObject()
                .field("indexName", indexJob.indexKey.indexName)
                .field("indexType", indexJob.indexKey.indexType)
                .field("actionType", indexJob.actionType.name());
        builder.endObject();
    }

    public static void appendResponseMap(XContentBuilder builder, Map responseMap) throws IOException {
        builder.startObject();
        builder.field("results");
        builder.startArray();
        for (IndexKey indexKey : responseMap.keySet()) {
            int count = responseMap.get(indexKey);
            builder.startObject();
            builder.field("indexName", indexKey.indexName);
            builder.field("indexType", indexKey.indexType);
            builder.field("clearedStates", count);
            builder.endObject();
        }
        builder.endArray();
        builder.endObject();
    }

    public static void appendFeedBack(XContentBuilder builder, IndexResponse indexResponse) throws IOException {
        builder.startObject();
        builder.field("request");
        appendRequest(builder, indexResponse.indexJob);
        builder.field("accepted", indexResponse.jobAdded);
        builder.endObject();
    }

    public static void appendFeedBackList(XContentBuilder builder, List indexResponseList) throws IOException {
        builder.startObject();
        builder.field("offeredRequests");
        builder.startArray();
        for (IndexResponse indexResponse : indexResponseList) {
            appendFeedBack(builder, indexResponse);
        }
        builder.endArray();
        builder.endObject();
    }

    public static void appendIndexingStatus(SimpleDateFormat dateFormat, DecimalFormat decimalFormat, XContentBuilder builder, IndexFeedback indexFeedback) throws IOException {
        builder.startObject();
        builder.field("request");
        appendRequest(builder, indexFeedback.getIndexJob());

        builder.field("state", indexFeedback.getState().name());
        if (indexFeedback.getErrorMessage() != null) {
            builder.field("error", indexFeedback.getErrorMessage());
        }

        builder.field("total", indexFeedback.getTotal());
        builder.field("successCount", indexFeedback.getSuccessCount());
        builder.field("failCount", indexFeedback.getFailCount());
        builder.field("started", dateFormat.format(indexFeedback.getStartTime()));
        if (indexFeedback.getTotal() > 0) {
            if (indexFeedback.getState() == IndexFeedback.State.FINISHED) {
                builder.field("ended", dateFormat.format(indexFeedback.getCurrentTime()));
            } else {
                long eta = indexFeedback.getETA();
                builder.field("eta", eta > 0 ? dateFormat.format(eta) : "undefined");
            }
        }
        builder.field("progress", decimalFormat.format(indexFeedback.getProgress() * 100) + "%");
        builder.field("throughput", decimalFormat.format(indexFeedback.getThroughputPerSecond()) + " items/sec");
        builder.endObject();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy