<commit_before>package il.co.topq.report.model;

import il.co.topq.difido.model.execution.Execution;
import il.co.topq.difido.model.execution.MachineNode;
import il.co.topq.difido.model.execution.Node;
import il.co.topq.difido.model.execution.ScenarioNode;
import il.co.topq.difido.model.execution.TestNode;
import il.co.topq.difido.model.test.TestDetails;
import il.co.topq.report.Common;
import il.co.topq.report.Configuration;
import il.co.topq.report.Configuration.ConfigProps;
import il.co.topq.report.controller.listener.ListenersManager;
import il.co.topq.report.controller.listener.ResourceChangedListener;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public enum ExecutionManager implements ResourceChangedListener {
	INSTANCE;

	private final Logger log = LoggerFactory.getLogger(ExecutionManager.class);

	// Package private for unit testing
	Map<Integer, ExecutionMetaData> executionsCache;;

	private static Object lockObject = new Object();

	private static Object fileAccessLockObject = new Object();

	private static final String EXECUTION_FILE_NAME = "reports/meta.json";

	public int addExecution() {
		readExecutionMeta();
		Execution execution = new Execution();
		final Date executionDate = new Date();
		final ExecutionMetaData metaData = new ExecutionMetaData(
				Common.ELASTIC_SEARCH_TIMESTAMP_STRING_FORMATTER.format(executionDate), execution);
		metaData.setTime(Common.API_TIME_FORMATTER.format(executionDate));
		metaData.setDate(Common.API_DATE_FORMATTER.format(executionDate));

		int maxId = getMaxId();
		metaData.setId(++maxId);
		metaData.setFolderName(Common.EXECUTION_REPORT_FOLDER_PREFIX + "_"
				+ Common.EXECUTION_REPROT_TIMESTAMP_FORMATTER.format(new Date()));
		metaData.setUri(Common.REPORTS_FOLDER_NAME + "/" + metaData.getFolderName() + "/index.html");
		metaData.setActive(true);
		executionsCache.put(metaData.getId(), metaData);
		writeExecutionMeta();
		ListenersManager.INSTANCE.notifyExecutionAdded(metaData.getId(), execution);
		return metaData.getId();
	}

	private void writeExecutionMeta() {
		if (null == executionsCache) {
			return;
		}
		synchronized (fileAccessLockObject) {
			try {
				new ObjectMapper().writeValue(getExecutionMetaFile(), executionsCache);
			} catch (IOException e) {
				log.error("Failed writing execution meta data", e);
			}
		}
	}

	private File getExecutionMetaFile() {
		return new File(Configuration.INSTANCE.readString(ConfigProps.DOC_ROOT_FOLDER), EXECUTION_FILE_NAME);
	}

	private int getMaxId() {
		int maxId = 0;
		for (Integer i : executionsCache.keySet()) {
			if (i > maxId) {
				maxId = i;
			}
		}
		return maxId;
	}

	private void readExecutionMeta() {<commit_msg>Fixed exception java.io.FileNotFoundException: docRoot\reports\meta.json (The system cannot find the path specified). issue #22 and issue #30<commit_after>package il.co.topq.report.model;

import il.co.topq.difido.model.execution.Execution;
import il.co.topq.difido.model.execution.MachineNode;
import il.co.topq.difido.model.execution.Node;
import il.co.topq.difido.model.execution.ScenarioNode;
import il.co.topq.difido.model.execution.TestNode;
import il.co.topq.difido.model.test.TestDetails;
import il.co.topq.report.Common;
import il.co.topq.report.Configuration;
import il.co.topq.report.Configuration.ConfigProps;
import il.co.topq.report.controller.listener.ListenersManager;
import il.co.topq.report.controller.listener.ResourceChangedListener;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public enum ExecutionManager implements ResourceChangedListener {
	INSTANCE;

	private final Logger log = LoggerFactory.getLogger(ExecutionManager.class);

	// Package private for unit testing
	Map<Integer, ExecutionMetaData> executionsCache;;

	private static Object lockObject = new Object();

	private static Object fileAccessLockObject = new Object();

	private static final String EXECUTION_FILE_NAME = "reports/meta.json";

	public int addExecution() {
		readExecutionMeta();
		Execution execution = new Execution();
		final Date executionDate = new Date();
		final ExecutionMetaData metaData = new ExecutionMetaData(
				Common.ELASTIC_SEARCH_TIMESTAMP_STRING_FORMATTER.format(executionDate), execution);
		metaData.setTime(Common.API_TIME_FORMATTER.format(executionDate));
		metaData.setDate(Common.API_DATE_FORMATTER.format(executionDate));

		int maxId = getMaxId();
		metaData.setId(++maxId);
		metaData.setFolderName(Common.EXECUTION_REPORT_FOLDER_PREFIX + "_"
				+ Common.EXECUTION_REPROT_TIMESTAMP_FORMATTER.format(new Date()));
		metaData.setUri(Common.REPORTS_FOLDER_NAME + "/" + metaData.getFolderName() + "/index.html");
		metaData.setActive(true);
		executionsCache.put(metaData.getId(), metaData);
		writeExecutionMeta();
		ListenersManager.INSTANCE.notifyExecutionAdded(metaData.getId(), execution);
		return metaData.getId();
	}

	private void writeExecutionMeta() {
		if (null == executionsCache) {
			return;
		}
		synchronized (fileAccessLockObject) {
			final File executionMetaFile = getExecutionMetaFile();
			if (!executionMetaFile.exists()){
				if (!executionMetaFile.getParentFile().exists()){
					if (!executionMetaFile.getParentFile().mkdirs()){
						log.error("Failed creating folder for execution meta file ");
						return;
					}
					try {
						if (!executionMetaFile.createNewFile()){
							log.error("Failed creating execution meta file");
							return;
						}
					} catch (IOException e) {
						log.error("Failed creating execution meta file",e);
						return;
					}
				}
			}
			try {
				new ObjectMapper().writeValue(getExecutionMetaFile(), executionsCache);
			} catch (IOException e) {
				log.error("Failed writing execution meta data", e);
			}
		}
	}

	private File getExecutionMetaFile() {
		return new File(Configuration.INSTANCE.readString(ConfigProps.DOC_ROOT_FOLDER), EXECUTION_FILE_NAME);
	}

	private int getMaxId() {
		int maxId = 0;
		for (Integer i : executionsCache.keySet()) {
			if (i > maxId) {
				maxId = i;
			}
		}
		return maxId;
	}

	private void readExecutionMeta() {
{'fraction_non_alphanumeric': 0.11273640661938535, 'fraction_numerical': 0.0014775413711583924, 'mean_word_length': 5.081761006289308, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '7594835', 'n_tokens_mistral': 2336, 'n_tokens_neox': 2099, 'n_words': 404}