<commit_before>#region Using Directives

using System;
using System.Net;
using Endeca.Control.EacToolkit;
using EndecaControl.EacToolkit.Core;
using EndecaControl.EacToolkit.Services;

#endregion

namespace EndecaControl.EacToolkit.Components
{
    public sealed class DgraphComponent : ServerComponent
    {
        private const int WebReqTimeout = 600000; // 10 min
        internal bool IndexApplied;

        internal bool UpdateApplied;
        private string _updateDir;

        //private const string DeleteBackupIndexFolderTemplate = @"del /s /q {0}.bak\. > nul && rd /s /q {0}.bak > nul";
        private const string BackUpExistingIndexTemplate = @"move {0} {0}.bak > nul";
       // private const string DeleteExistingBackupTemplate = @"rd /s /q {0}.bak > nul";
        private const string MoveExistingIndexToBackupTemplate = @"move {0} {1} > nul";

        internal DgraphComponent(string compId, string appId, HostType host) : base(compId, appId, host)
        {
        }


        /// <summary>
        ///     Partial update log files folder
        /// </summary>
        public string UpdateLogDir { get; set; }

        /// <summary>
        ///     Partial update folder
        /// </summary>
        public string UpdateDir
        {
            get { return _updateDir; }
            set { _updateDir = value; }
        }

        /// <summary>
        ///     Index distribution local temporary folder
        /// </summary>
        public string IndexDistributionDir { set; get; }

        /// <summary>
        ///     Stops MDEX engine, copy files to the input folder and starts the engine
        /// </summary>
        /// <returns>true if successful</returns>
        public bool ApplyIndex()
        {
            if (IsActive)
            {
                StopComponent(true);
            }
            Logger.Debug(String.Format("{0}:{1} - Deleting backup index folder.", hostName, port));
            CleanDir(string.Format(InputDirectory.Substring(0, InputDirectory.LastIndexOf(@"\", StringComparison.InvariantCulture)), ".bak"));

            Logger.Debug(String.Format("{0}:{1} - Backing up existing index.", hostName, port));
            BackupFiles(CustomProps["localIndexDir"], 1);
            // BackUpExistingIndex();

            Logger.Debug(String.Format("{0}:{1} - Move index from the local index distribution folder.", hostName, port));
            MoveIndexToInputFolder();

            var token = CopyFiles(HostId, IndexDistributionDir, InputDirectory);

            Logger.Debug(String.Format("{0}:{1} - Re-creating the local index distribution folder.", hostName, port));
            ReCreateLocalIndexDistributionFolder();

            Logger.Debug(String.Format("{0}:{1} - Attempting to start Dgraph.", hostName, port));

            if (WaitUtilityComplete(token))
            {
                ArchiveLog(true);
                Start(true);
            }
            else
            {
                Logger.Error(FailureMessage);
            }
            IndexApplied = IsActive;
            return IndexApplied;
        }

        private void ReCreateLocalIndexDistributionFolder()
        {
            const string reCreateLocalIndexDistributionFolderTemplate = @"mkdir {0}";
            var cmd = String.Format(reCreateLocalIndexDistributionFolderTemplate, IndexDistributionDir);
            var token = EacGateway.Instance.ShellCmd(AppId, HostId, cmd);
            if (WaitUtilityComplete(token))
            {
                Logger.Debug(String.Format("Re-created local index distribution folder on host {0}. Token: {1}.",
                                           HostId, token));
            }
            else
            {
                throw new ControlScriptException(
                    String.Format("Re-creating local index distribution failed on host {0}.", HostId));
            }
        }

        private void MoveIndexToInputFolder()
        {
            var cmd = String.Format(MoveExistingIndexToBackupTemplate, IndexDistributionDir,
                                    InputDirectory.Substring(0, InputDirectory.LastIndexOf(@"\")));
            var token = EacGateway.Instance.ShellCmd(AppId, HostId, cmd);
            if (WaitUtilityComplete(token))
            {
                Logger.Debug(String.Format("Deleted backup index folder on host {0}. Token: {1}.", HostId, token));
            }
            else
            {
                throw new ControlScriptException(
                    String.Format("Local index distribution folder cleaning failed on host {0}.", HostId));
            }
        }

        private void BackUpExistingIndex()
        {
            var inputDir = InputDirectory.Substring(0, InputDirectory.LastIndexOf(@"\", StringComparison.InvariantCulture));
            var cmd = String.Format(BackUpExistingIndexTemplate, inputDir);
            var token = EacGateway.Instance.ShellCmd(AppId, HostId, cmd);
            if (WaitUtilityComplete(token))
            {
                Logger.Debug(String.Format("Backed up existing index on host {0}. Token: {1} & {2}.", HostId, token,
                                           token));
            }
            else
            {
                throw new ControlScriptException(
                    String.Format("Existing index backup failed on host {0}.", HostId));
            }
        }

        /// <summary>
        ///     Cleans partial updates folder
        /// </summary>
        public void CleanUpdateDir()
        {
            CleanDir(_updateDir);
        }

        /// <summary>
        ///     Applies partial update
        /// </summary>
        public bool ApplyUpdate()
        {
            //TODO: Async????????
            Logger.Debug(String.Format("{0}:{1} - Applying updates.", hostName, port));
            var req = String.Format("http://{0}:{1}/admin?op=update", HostName, Port);
            var updateReq = (HttpWebRequest) WebRequest.Create(req);
            updateReq.Credentials = CredentialCache.DefaultCredentials;
            updateReq.Timeout = WebReqTimeout;
            try
            {
                var response = (HttpWebResponse) updateReq.GetResponse();
                UpdateApplied = response.StatusCode == HttpStatusCode.OK;
            }
            catch (WebException e)<commit_msg>refined the dgraph distribute and apply process<commit_after>#region Using Directives

using System;
using System.IO;
using System.Net;
using Endeca.Control.EacToolkit;
using EndecaControl.EacToolkit.Core;
using EndecaControl.EacToolkit.Services;

#endregion

namespace EndecaControl.EacToolkit.Components
{
    public sealed class DgraphComponent : ServerComponent
    {
        private const int WebReqTimeout = 600000; // 10 min
        internal bool IndexApplied;

        internal bool UpdateApplied;
        private string _updateDir;

        private const string BackUpExistingIndexTemplate = @"move {0} {0}.bak > nul";
        private const string MoveFoldersTemplate = @"move {0} {1} > nul";

        internal DgraphComponent(string compId, string appId, HostType host) : base(compId, appId, host)
        {
        }


        /// <summary>
        ///     Partial update log files folder
        /// </summary>
        public string UpdateLogDir { get; set; }

        /// <summary>
        ///     Partial update folder
        /// </summary>
        public string UpdateDir
        {
            get { return _updateDir; }
            set { _updateDir = value; }
        }

        /// <summary>
        ///     Index distribution local temporary folder
        /// </summary>
        public string IndexDistributionDir { set; get; }

        /// <summary>
        ///     Stops MDEX engine, copy files to the input folder and starts the engine
        /// </summary>
        /// <returns>true if successful</returns>
        public bool ApplyIndex()
        {
            if (IsActive)
            {
                StopComponent(true);
            }

            var inputFolder = InputDirectory.Substring(0, InputDirectory.LastIndexOf(@"\"));

            Logger.Debug(String.Format("{0}:{1} - Backing up existing index.", hostName, port));
            BackupFiles(inputFolder, 1);

            var token = CopyFiles(HostId, IndexDistributionDir, inputFolder);

            if (WaitUtilityComplete(token))
            {
                Logger.Debug(String.Format("{0}:{1} - Archiving logs.", hostName, port));
                ArchiveLog(true);

                Logger.Debug(String.Format("{0}:{1} - Attempting to start Dgraph.", hostName, port));
                Start(true);
            }
            else
            {
                Logger.Error(FailureMessage);
            }
            IndexApplied = IsActive;
            return IndexApplied;
        }

        /// <summary>
        ///     Cleans partial updates folder
        /// </summary>
        public void CleanUpdateDir()
        {
            CleanDir(_updateDir);
        }

        /// <summary>
        ///     Applies partial update
        /// </summary>
        public bool ApplyUpdate()
        {
            //TODO: Async????????
            Logger.Debug(String.Format("{0}:{1} - Applying updates.", hostName, port));
            var req = String.Format("http://{0}:{1}/admin?op=update", HostName, Port);
            var updateReq = (HttpWebRequest) WebRequest.Create(req);
            updateReq.Credentials = CredentialCache.DefaultCredentials;
            updateReq.Timeout = WebReqTimeout;
            try
            {
                var response = (HttpWebResponse) updateReq.GetResponse();
                UpdateApplied = response.StatusCode == HttpStatusCode.OK;
            }
            catch (WebException e)
{'fraction_non_alphanumeric': 0.1077524162039893, 'fraction_numerical': 0.006991568990335184, 'mean_word_length': 1.7895038715227989, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2969756', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2402, 'n_words': 612}