<commit_before>/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2010-2011 <NAME> / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.web.controller.runtime;

import java.io.File;
import java.io.IOException;
import java.util.TreeMap;

import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.Textbox;

import com.jaeksoft.searchlib.Logging;
import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.web.controller.CommonController;

public class LogsController extends CommonController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5390275562492575445L;

	private String selectedFile;

	private String currentLog;

	private String[] logFileList;

	public LogsController() throws SearchLibException {
		super();
		reset();
	}

	@Override
	protected void reset() throws SearchLibException {
		selectedFile = null;
		currentLog = null;
		logFileList = null;
	}

	private final static String[] buildOrderedLogFiles()
			throws SearchLibException {
		File[] files = Logging.getLogFiles();
		if (files == null)
			return null;
		TreeMap<Long, File> map = new TreeMap<Long, File>();
		for (File f : files)
			map.put(f.lastModified(), f);
		String[] names = new String[map.size()];
		int i = names.length;
		for (File f : map.values()) {
			i--;
			names[i] = f.getName();
		}
		return names;
	}

	public String[] getLogFiles() throws SearchLibException {
		if (logFileList == null)
			logFileList = buildOrderedLogFiles();
		return logFileList;
	}

	public String getCurrentLog() throws IOException, SearchLibException {
		if (currentLog == null && selectedFile != null)
			currentLog = Logging.readLogs(100000, selectedFile);
		return currentLog;
	}<commit_msg>New feature 3483681: Add a download button for log files<commit_after>/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2010-2011 <NAME> / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.web.controller.runtime;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.Textbox;

import com.jaeksoft.searchlib.Logging;
import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.web.controller.CommonController;

public class LogsController extends CommonController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5390275562492575445L;

	private String selectedFile;

	private String currentLog;

	private String[] logFileList;

	public LogsController() throws SearchLibException {
		super();
		reset();
	}

	@Override
	protected void reset() throws SearchLibException {
		selectedFile = null;
		currentLog = null;
		logFileList = null;
	}

	private final static String[] buildOrderedLogFiles()
			throws SearchLibException {
		File[] files = Logging.getLogFiles();
		if (files == null)
			return null;
		Arrays.sort(files, new Comparator<File>() {
			@Override
			public int compare(File f1, File f2) {
				Long l1 = f1.lastModified();
				Long l2 = f2.lastModified();
				return l2.compareTo(l1);
			}
		});
		String[] names = new String[files.length];
		int i = 0;
		for (File file : files)
			names[i++] = file.getName();
		return names;
	}

	public String[] getLogFiles() throws SearchLibException {
		if (logFileList == null)
			logFileList = buildOrderedLogFiles();
		return logFileList;
	}

	public String getCurrentLog() throws IOException, SearchLibException {
		if (currentLog == null && selectedFile != null)
			currentLog = Logging.readLogs(100000, selectedFile);
		return currentLog;
	}
{'fraction_non_alphanumeric': 0.09868295476235923, 'fraction_numerical': 0.01603359419736591, 'mean_word_length': 4.097276264591439, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3958725', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1547, 'n_words': 504}