<commit_before>package fr.issamax.essearch.admin.river.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.IdsQueryBuilder;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import fr.issamax.essearch.admin.river.data.FSRiver;
import fr.issamax.essearch.admin.river.data.FSRiverHelper;

@Component
public class RiverService {
	@Autowired Client client;

	/**
	 * Get the river definition by its name
	 * @param name
	 * @return
	 */
	public FSRiver get(String name) {
		if (name == null) return null;
		
		// TODO Be a little more fancy
		List<FSRiver> rivers = get();
		
		for (Iterator<FSRiver> iterator = rivers.iterator(); iterator.hasNext();) {
			FSRiver fsRiver = iterator.next();
			if (name.equals(fsRiver.getName())) return fsRiver;
		}
		
		return null;
	}

	/**
	 * Get all active rivers
	 * @return
	 */
	public List<FSRiver> get() {
		List<FSRiver> rivers = new ArrayList<FSRiver>();
		
		SearchRequestBuilder srb = new SearchRequestBuilder(client);

		try {
			IdsQueryBuilder iqb = new IdsQueryBuilder((String[]) null);
			iqb.addIds("_meta");
			srb.setIndices("_river");
			srb.setQuery(iqb);
			
			SearchResponse response = srb.execute().actionGet();
			
			if (response.hits().totalHits() > 0) {
				
				for (int i = 0; i < response.hits().hits().length; i++) {
					SearchHit hit = response.hits().hits()[i];

					Map<String, Object> meta = (Map<String, Object>) hit.sourceAsMap();
					if (meta.containsKey("fs")) {
						// We only manage FS rivers
						FSRiver fsriver = FSRiverHelper.toFSRiver(meta);
						rivers.add(fsriver);
					}
				}
			}
			
		} catch (IndexMissingException e) {
			// That's a common use case. We started with an empty index
		}
		
		return rivers;
	}

	/**
	 * Update (or add) a river
	 * @param river
	 */
	public void update(FSRiver river) {
		//TODO
		System.out.println("TODO DPI Update ES River : " + river.toString());
	}
	
	/**
	 * Remove river
	 * @param river
	 */
	public void remove(FSRiver river) {
		//TODO
		System.out.println("TODO DPI Remove ES River : " + river.toString());
	}


}<commit_msg>Add a start and stop for all existing rivers<commit_after>package fr.issamax.essearch.admin.river.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.admin.indices.close.CloseIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.IdsQueryBuilder;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import fr.issamax.essearch.admin.river.data.FSRiver;
import fr.issamax.essearch.admin.river.data.FSRiverHelper;

@Component
public class RiverService {
	@Autowired Client client;

	/**
	 * Get the river definition by its name
	 * @param name
	 * @return
	 */
	public FSRiver get(String name) {
		if (name == null) return null;
		
		// TODO Be a little more fancy
		List<FSRiver> rivers = get();
		
		for (Iterator<FSRiver> iterator = rivers.iterator(); iterator.hasNext();) {
			FSRiver fsRiver = iterator.next();
			if (name.equals(fsRiver.getName())) return fsRiver;
		}
		
		return null;
	}

	/**
	 * Get all active rivers
	 * @return
	 */
	public List<FSRiver> get() {
		List<FSRiver> rivers = new ArrayList<FSRiver>();
		
		SearchRequestBuilder srb = new SearchRequestBuilder(client);

		try {
			IdsQueryBuilder iqb = new IdsQueryBuilder((String[]) null);
			iqb.addIds("_meta");
			srb.setIndices("_river");
			srb.setQuery(iqb);
			
			SearchResponse response = srb.execute().actionGet();
			
			if (response.hits().totalHits() > 0) {
				
				for (int i = 0; i < response.hits().hits().length; i++) {
					SearchHit hit = response.hits().hits()[i];

					Map<String, Object> meta = (Map<String, Object>) hit.sourceAsMap();
					if (meta.containsKey("fs")) {
						// We only manage FS rivers
						FSRiver fsriver = FSRiverHelper.toFSRiver(meta);
						rivers.add(fsriver);
					}
				}
			}
			
		} catch (IndexMissingException e) {
			// That's a common use case. We started with an empty index
		}
		
		return rivers;
	}

	/**
	 * Update (or add) a river
	 * @param river
	 */
	public void update(FSRiver river) {
		//TODO
		System.out.println("TODO DPI Update ES River : " + river.toString());
	}
	
	/**
	 * Remove river
	 * @param river
	 */
	public void remove(FSRiver river) {
		//TODO
		System.out.println("TODO DPI Remove ES River : " + river.toString());
	}

	/**
	 * Stop all rivers
	 */
	public void stop() {
		CloseIndexRequestBuilder irb = new CloseIndexRequestBuilder(client.admin().indices());

		irb.setIndex("_river");
		CloseIndexResponse response = irb.execute().actionGet();
		
		if (!response.acknowledged()) {
			System.err.println("Pb when clausing rivers.");
		}
	}
	
	/**
	 * (Re)Start all rivers
	 */
	public void start() {
		OpenIndexRequestBuilder irb = new OpenIndexRequestBuilder(client.admin().indices());

		irb.setIndex("_river");
		OpenIndexResponse response = irb.execute().actionGet();
		
		if (!response.acknowledged()) {
			System.err.println("Pb when clausing rivers.");
		}
	}
	

}
{'fraction_non_alphanumeric': 0.13236780060626474, 'fraction_numerical': 0.0006736274840013472, 'mean_word_length': 3.74740207833733, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3111107', 'n_tokens_mistral': 2128, 'n_tokens_neox': 1860, 'n_words': 428}