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

com.thematchbox.river.rest.RestIndexerStatusAction 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.IndexKey;
import com.thematchbox.river.indexers.MatchBoxIndexerManager;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.rest.*;

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

public class RestIndexerStatusAction extends BaseRestHandler {
    public static final String INDEX_NAME = "indexName";
    public static final String INDEX_TYPE = "indexType";

    private MatchBoxIndexerManager riverIndexingManager;

    @Inject
    public RestIndexerStatusAction(Settings settings, Client client, RestController controller, MatchBoxIndexerManager riverIndexingManager) {
        super(settings, controller, client);
        this.riverIndexingManager = riverIndexingManager;
        controller.registerHandler(RestRequest.Method.GET, "/_matchboxIndexerStatus", this);
        controller.registerHandler(RestRequest.Method.GET, "/_matchboxIndexerStatus/{" + INDEX_NAME + "}", this);
        controller.registerHandler(RestRequest.Method.GET, "/_matchboxIndexerStatus/{" + INDEX_NAME + "}/{"+ INDEX_TYPE + "}", this);
    }

    @Override
    protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
        final String indexName = request.hasParam(INDEX_NAME) ? request.param(INDEX_NAME) : null;
        final String indexType = request.hasParam(INDEX_TYPE) ? request.param(INDEX_TYPE) : null;

        List indexingStatuses = new ArrayList<>();
        List keys = riverIndexingManager.getMatchingKeys(indexName, indexType);
        for (IndexKey key : keys) {
            indexingStatuses.addAll(riverIndexingManager.getStatus(key));
        }

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY-HH:mm:ss:SSS");
            DecimalFormat decimalFormat = new DecimalFormat("0.##");
            XContentBuilder builder = JsonXContent.contentBuilder();
            builder.startObject();
            builder.field("states");
            builder.startArray();
            for (IndexFeedback indexingState : indexingStatuses) {
                RestHelper.appendIndexingStatus(dateFormat, decimalFormat, builder, indexingState);
            }
            builder.endArray();
            builder.endObject();
            channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
        } catch (IOException e) {
            failed(channel, e);
        }
    }

    private void failed(RestChannel channel, Throwable e) {
        try {
            channel.sendResponse(new BytesRestResponse(channel, e));
        } catch (IOException e1) {
            logger.error("Failed to send failure response", e1);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy