<commit_before>﻿/*
Copyright 2015-2018 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TWCore.Diagnostics.Status;
using TWCore.Serialization;
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable SwitchStatementMissingSomeCases

namespace TWCore.Diagnostics.Log.Storages
{
    /// <summary>
    /// ElasticSearch log storage
    /// </summary>
    [StatusName("ElasticSearch Log")]
    public class ElasticSearchLogStorage : ILogStorage
    {
        private readonly Timer _timer;
        private readonly Uri _url;
        private readonly string _indexFormat;
        private volatile bool _processing;
        private int _count;
        private readonly BlockingCollection<SourceData> _logItems;
        private bool _enabled = true;
        private StringBuilder _builderBuffer = new StringBuilder();
        private JsonTextSerializer serializer = new JsonTextSerializer
        {
            EnumsAsStrings = true
        };
        private WebClient _client;

        #region .ctor
        /// <summary>
        /// ElasticSearch log storage
        /// </summary>
        /// <param name="url">Elastic search json api url</param>
        /// <param name="indexFormat">Index format</param>
        /// <param name="periodInSeconds">Fetch period in seconds</param>
        public ElasticSearchLogStorage(string url, string indexFormat, int periodInSeconds = 10)
        {
            _url = new UriBuilder(url) { Path = "_bulk" }.Uri;
            _indexFormat = indexFormat;
            _client = new WebClient();
            _logItems = new BlockingCollection<SourceData>();
            var period = TimeSpan.FromSeconds(periodInSeconds);
            _timer = new Timer(TimerCallback, this, period, period);
            Core.Status.Attach(collection =>
            {
                collection.Add("Configuration",
                    new StatusItemValueItem("Url", _url),
                    new StatusItemValueItem("IndexFormat", _indexFormat),
                    new StatusItemValueItem("Period In Seconds", periodInSeconds)
                );
                collection.Add("Remaining Items", _count);
            });

        }
        /// <summary>
        /// ElasticSearch log storage finalizer
        /// </summary>
        ~ElasticSearchLogStorage()<commit_msg>changes.<commit_after>﻿/*
Copyright 2015-2018 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TWCore.Diagnostics.Status;
using TWCore.Serialization;
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable SwitchStatementMissingSomeCases

namespace TWCore.Diagnostics.Log.Storages
{
    /// <summary>
    /// ElasticSearch log storage
    /// </summary>
    [StatusName("ElasticSearch Log")]
    public class ElasticSearchLogStorage : ILogStorage
    {
        private readonly Timer _timer;
        private readonly string _originalUrl;
        private readonly Uri _url;
        private readonly string _indexFormat;
        private volatile bool _processing;
        private int _count;
        private readonly BlockingCollection<SourceData> _logItems;
        private bool _enabled = true;
        private StringBuilder _builderBuffer = new StringBuilder();
        private JsonTextSerializer serializer = new JsonTextSerializer
        {
            EnumsAsStrings = true
        };
        private WebClient _client;

        #region .ctor
        /// <summary>
        /// ElasticSearch log storage
        /// </summary>
        /// <param name="url">Elastic search json api url</param>
        /// <param name="indexFormat">Index format</param>
        /// <param name="periodInSeconds">Fetch period in seconds</param>
        public ElasticSearchLogStorage(string url, string indexFormat, int periodInSeconds = 10)
        {
            _originalUrl = url;
            _url = new UriBuilder(url) { Path = "_bulk" }.Uri;
            _indexFormat = indexFormat;
            _client = new WebClient();
            _logItems = new BlockingCollection<SourceData>();
            var period = TimeSpan.FromSeconds(periodInSeconds);
            _timer = new Timer(TimerCallback, this, period, period);
            Core.Status.Attach(collection =>
            {
                collection.Add("Configuration",
                    new StatusItemValueItem("Url", _originalUrl),
                    new StatusItemValueItem("IndexFormat", _indexFormat),
                    new StatusItemValueItem("Period In Seconds", periodInSeconds)
                );
                collection.Add("Remaining Items", _count);
            }, this);
        }
        /// <summary>
        /// ElasticSearch log storage finalizer
        /// </summary>
        ~ElasticSearchLogStorage()
{'fraction_non_alphanumeric': 0.08419188667095943, 'fraction_numerical': 0.004507405022537025, 'mean_word_length': 2.622740524781341, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4554299', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1512, 'n_words': 490}