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

io.fabric8.insight.log.rest.LogRequest Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright 2005-2015 Red Hat, Inc.
 *
 *  Red Hat licenses this file to you 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 io.fabric8.insight.log.rest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonCreator;

public class LogRequest {
	private long from;
	private long size;
	private List> sort;
	private Map query;

	public static LogRequest newInstance(Long maxLogSeq) {
		List> s = new ArrayList();
		s.add(keyValueMap("timestamp", "desc"));
		s.add(keyValueMap("seq", "desc"));

		Map q = new HashMap();
		Map constant_score = new HashMap();
		Map filter = new HashMap();

		q.put("constant_score", constant_score);
		constant_score.put("filter", filter);

		List listOfTerms = new ArrayList();
		//listOfTerms.add(createSearchTerm("host", "root"));
		listOfTerms.add(createSearchTerms("level", "error", "warn", "info"));

		if (maxLogSeq != null) {
			listOfTerms.add(createSearchRangeGt("seq", maxLogSeq));
		}

		filter.put("and", listOfTerms);

		return new LogRequest(0, 50, s, q);
	}

	private static Map keyValueMap(String key, String value) {
		Map answer = new HashMap();
		answer.put(key, value);
		return answer;
	}

	protected static Map createSearchRangeGt(String name, Object value) {
		return createSearchRange(name, "gt", value);
	}

	protected static Map createSearchRange(String name, String compareOperation, Object value) {
		Map answer = new HashMap();
		Map range = new HashMap();
		Map compare = new HashMap();
		answer.put("range", range);
		range.put(compareOperation,  compare);
		compare.put(name, value);
		return answer;
	}

	protected static Map createSearchTerm(String name, String value) {
		Map answer = new HashMap();
		Map term = new HashMap();
		answer.put("term", term);
		term.put(name, value);
		return answer;
	}

	protected static Map createSearchTerms(String name, String... values) {
		Map answer = new HashMap();
		Map term = new HashMap();
		answer.put("terms", term);
		term.put(name, new ArrayList(Arrays.asList(values)));
		return answer;
	}

	@JsonCreator
	public LogRequest(long from, long size, List> sort, Map query) {
		this.from = from;
		this.size = size;
		this.sort = sort;
		this.query = query;
	}
	public long getFrom() {
		return from;
	}
	public void setFrom(long from) {
		this.from = from;
	}
	public long getSize() {
		return size;
	}
	public void setSize(long size) {
		this.size = size;
	}
	public List> getSort() {
		return sort;
	}
	public void setSort(List> sort) {
		this.sort = sort;
	}

	public Map getQuery() {
		return query;
	}

	public void setQuery(Map query) {
		this.query = query;
	}



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy