<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using log4net;
using Newtonsoft.Json;
using RoboticsTxt.Lib.Contracts;
using RoboticsTxt.Lib.Contracts.Configuration;

namespace RoboticsTxt.Lib.Components.Sequencer
{
    internal class PositionStorageAccessor
    {
        private readonly ApplicationConfiguration applicationConfiguration;
        private readonly ILog logger = LogManager.GetLogger(typeof(PositionStorageAccessor));

        public PositionStorageAccessor(ApplicationConfiguration applicationConfiguration)
        {
            this.applicationConfiguration = applicationConfiguration;
        }

        private const string FileNamePattern = "Positions_{0}.json";

        public bool WritePositionsToFile(IEnumerable<Position> positions)
        {
            try
            {
                var positionsJson = JsonConvert.SerializeObject(positions, Formatting.Indented);

                var stream = new FileStream(string.Format(FileNamePattern, applicationConfiguration.ApplicationName), FileMode.Create);
                var streamWriter = new StreamWriter(stream);

                streamWriter.Write(positionsJson);
                streamWriter.Flush();
            }
            catch (Exception exception)
            {
                this.logger.Error(exception);
                return false;
            }

            return true;
        }

        public List<Position> LoadPositionsFromFile()
        {
            var positions = new List<Position>();

            try
            {
                var stream = new FileStream(string.Format(FileNamePattern, applicationConfiguration.ApplicationName), FileMode.Open);
                var streamReader = new StreamReader(stream);

                var positionsJson = streamReader.ReadToEnd();

                positions = JsonConvert.DeserializeObject<List<Position>>(positionsJson);
            }
            catch (Exception exception)
            {
                this.logger.Error(exception);<commit_msg>pre-check for positions file existence<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using log4net;
using Newtonsoft.Json;
using RoboticsTxt.Lib.Contracts;
using RoboticsTxt.Lib.Contracts.Configuration;

namespace RoboticsTxt.Lib.Components.Sequencer
{
    internal class PositionStorageAccessor
    {
        private readonly ApplicationConfiguration applicationConfiguration;
        private readonly ILog logger = LogManager.GetLogger(typeof(PositionStorageAccessor));

        public PositionStorageAccessor(ApplicationConfiguration applicationConfiguration)
        {
            this.applicationConfiguration = applicationConfiguration;
        }

        private const string FileNamePattern = "Positions_{0}.json";

        public bool WritePositionsToFile(IEnumerable<Position> positions)
        {
            try
            {
                var positionsJson = JsonConvert.SerializeObject(positions, Formatting.Indented);

                var stream = new FileStream(string.Format(FileNamePattern, applicationConfiguration.ApplicationName), FileMode.Create);
                var streamWriter = new StreamWriter(stream);

                streamWriter.Write(positionsJson);
                streamWriter.Flush();
            }
            catch (Exception exception)
            {
                this.logger.Error(exception);
                return false;
            }

            return true;
        }

        public List<Position> LoadPositionsFromFile()
        {
            var positions = new List<Position>();

            var filePath = string.Format(FileNamePattern, applicationConfiguration.ApplicationName);

            if (!File.Exists(filePath))
                return positions;

            try
            {
                var stream = new FileStream(filePath, FileMode.Open);
                var streamReader = new StreamReader(stream);

                var positionsJson = streamReader.ReadToEnd();

                positions = JsonConvert.DeserializeObject<List<Position>>(positionsJson);
            }
            catch (Exception exception)
            {
                this.logger.Error(exception);
{'fraction_non_alphanumeric': 0.07183634633682208, 'fraction_numerical': 0.0009514747859181732, 'mean_word_length': 2.2622187742435997, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '5999741', 'n_tokens_mistral': 957, 'n_tokens_neox': 899, 'n_words': 198}