<commit_before>﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using spectator.Configuration.Overrides;
using spectator.Infrastructure;

namespace spectator.Configuration
{
    public class JsonSpectatorConfiguration : ISpectatorOverrideConfiguration, ISpectatorConfiguration
    {
        public string StatsdHost { get; set; }

        int ISpectatorConfiguration.StatsdPort => StatsdPort ?? 0;

        public int? StatsdPort { get; set; }

        public string MetricPrefix { get; set; }

        TimeSpan ISpectatorConfiguration.Interval => Interval ?? TimeSpan.Zero;

        public TimeSpan? Interval { get; set; }

        public IList<MetricConfiguration> Metrics { get; set; }

        public static ISpectatorConfiguration LoadConfigFrom(string path)
        {
            var fileAdapter = new FileAdapter();

            try
            {
                var configContents = fileAdapter.ReadAllText(path);

                return LoadConfigFromString(configContents);
            }
            catch
            {
                return new EmptyConfiguration();
            }
        }

        public static JsonSpectatorConfiguration LoadConfigFromString(string contents)
        {
            return JsonConvert.DeserializeObject<JsonSpectatorConfiguration>(contents);
        }

        public static ISpectatorOverrideConfiguration LoadOverrideFrom(string path)
        {
            var fileAdapter = new FileAdapter();

            try
            {
                var configContents = fileAdapter.ReadAllText(path);

                return LoadConfigFromString(configContents);
            }
            catch
            {
                return new EmptyOverrideConfiguration();
            }
        }
    }
}<commit_msg>More debug logging statements so it's easier to figure out what's happening with config loading.<commit_after>﻿using System;
using System.Collections.Generic;
using log4net;
using Newtonsoft.Json;
using spectator.Configuration.Overrides;
using spectator.Infrastructure;

namespace spectator.Configuration
{
    public class JsonSpectatorConfiguration : ISpectatorOverrideConfiguration, ISpectatorConfiguration
    {
        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public string StatsdHost { get; set; }

        int ISpectatorConfiguration.StatsdPort => StatsdPort ?? 0;

        public int? StatsdPort { get; set; }

        public string MetricPrefix { get; set; }

        TimeSpan ISpectatorConfiguration.Interval => Interval ?? TimeSpan.Zero;

        public TimeSpan? Interval { get; set; }

        public IList<MetricConfiguration> Metrics { get; set; }

        public static ISpectatorConfiguration LoadConfigFrom(string path)
        {
            Log.Debug($"Loading JSON configuration from '{path}'");

            return LoadJsonConfig<ISpectatorConfiguration>(path, () => new EmptyConfiguration(), LoadConfigFromString);
        }

        private static T LoadJsonConfig<T>(string path, Func<T> emptyConfigurationFactory, Func<string, T> loaderMethod)
        {
            var fileAdapter = new FileAdapter();

            try
            {
                var configContents = fileAdapter.ReadAllText(path);

                return loaderMethod(configContents);
            }
            catch(Exception ex)
            {
                Log.Warn($"Exception occurred loading JSON configuration file from '{path}' ('{ex.Message}'), assuming an empty configuration.");

                return emptyConfigurationFactory();
            }
        }

        public static JsonSpectatorConfiguration LoadConfigFromString(string contents)
        {
            return JsonConvert.DeserializeObject<JsonSpectatorConfiguration>(contents);
        }

        public static ISpectatorOverrideConfiguration LoadOverrideFrom(string path)
        {
            Log.Debug($"Loading JSON override configuration from '{path}'");

            return LoadJsonConfig<ISpectatorOverrideConfiguration>(path, () => new EmptyOverrideConfiguration(), LoadConfigFromString);
        }
    }
}
{'fraction_non_alphanumeric': 0.07451169520135037, 'fraction_numerical': 0.0007234145165179648, 'mean_word_length': 2.5182357930449535, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 18, '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': '3134564', 'n_tokens_mistral': 991, 'n_tokens_neox': 927, 'n_words': 242}