<commit_before>﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Collections.Generic;
using Hudl.Ffmpeg.BaseTypes;
using Hudl.Ffmpeg.Command.BaseTypes;
using Hudl.Ffmpeg.Command.Managers;
using Hudl.Ffmpeg.Common;
using Hudl.Ffmpeg.Filters.BaseTypes;
using log4net;

namespace Hudl.Ffmpeg.Command
{
    public class FfmpegCommand
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(FfmpegCommand).Name);

        private FfmpegCommand(CommandFactory owner)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            Owner = owner;
            Id = Guid.NewGuid().ToString();
            Objects = CommandObjects.Create(this);
            OutputManager = CommandOutputManager.Create(this);
            ResourceManager = CommandResourceManager.Create(this);
            FilterchainManager = CommandFilterchainManager.Create(this);
        }

        public static FfmpegCommand Create(CommandFactory owner)
        {
            return new FfmpegCommand(owner);    
        }

        internal CommandObjects Objects { get; set; }

        public ReadOnlyCollection<CommandOutput> Outputs { get { return Objects.Outputs.AsReadOnly(); } }

        public ReadOnlyCollection<CommandResource> Resources { get { return Objects.Inputs.AsReadOnly(); } }

        public ReadOnlyCollection<Filterchain> Filtergraph { get { return Objects.Filtergraph.FilterchainList.AsReadOnly(); } }

        public CommandOutputManager OutputManager { get; set; }

        public CommandResourceManager ResourceManager { get; set; }

        public CommandFilterchainManager FilterchainManager { get; set; }

        /// <summary>
        /// Renders the command stream with the defualt command processor
        /// </summary>
        public List<CommandOutput> Render()
        {
            return RenderWith<CmdProcessorReciever>();
        }

        /// <summary>
        /// Renders the command stream with a new command processor
        /// </summary>
        public List<CommandOutput> RenderWith<TProcessor>()
            where TProcessor : ICommandProcessor, new()
        {
            var commandProcessor = new TProcessor();

            if (!commandProcessor.Open(Owner.Configuration))
            {
                throw new FfmpegRenderingException(commandProcessor.Error);
            }

            var returnType = RenderWith(commandProcessor);

            if (!commandProcessor.Close())
            {
                throw new FfmpegRenderingException(commandProcessor.Error);
            }

            return returnType;
        }

        /// <summary>
        /// Renders the command stream with an existing command processor
        /// </summary>
        public List<CommandOutput> RenderWith<TProcessor>(TProcessor commandProcessor)
            where TProcessor : ICommandProcessor
        {
            if (commandProcessor == null)
            {
                throw new ArgumentNullException("commandProcessor");
            }

            var commandBuilder = new CommandBuilder();
            commandBuilder.WriteCommand(this);
            
            if (!commandProcessor.Send(commandBuilder.ToString()))
            {
                throw new FfmpegRenderingException(commandProcessor.Error);
            }

            return Objects.Outputs;
        }

        #region Internals
        internal string Id { get; set; }
        internal CommandFactory Owner { get; set; }

        internal List<CommandResource> ResourcesFromReceipts(params CommandReceipt[] receipts)
        {
            return ResourcesFromReceipts(new List<CommandReceipt>(receipts));
        }
        internal List<CommandResource> ResourcesFromReceipts(List<CommandReceipt> receipts)
        {
            if (receipts == null || receipts.Count == 0)
            {
                throw new ArgumentException("Receipts cannot be null or empty for a retrieve.", "receipts");
            }

            return receipts.Select(receipt =>
                {
                    CommandResource resource = null;
                    switch (receipt.Type)
                    {
                        case CommandReceiptType.Input:
                            resource = Objects.Inputs.FirstOrDefault(r => r.Resource.Map == receipt.Map);
                            break;
                        case CommandReceiptType.Stream:
                            var filterchain = Objects.Filtergraph.FilterchainList.FirstOrDefault(f => f.GetReceipts().Any(r => r.Equals(receipt)));
                            var filterchainOutput = filterchain.Outputs(this).FirstOrDefault(r => r.Resource.Map == receipt.Map);
                            resource = CommandResource.Create(filterchainOutput.Resource);
                            resource.Id = filterchainOutput.Id; 
                            resource.Owner = this;
                            break;
                    }
                    if (resource == null)
                    {
                        throw new KeyNotFoundException("Resource was not found in the command list.");
                    }
                    return resource; 
                }).ToList();<commit_msg>TENT-305 issue in length calculation that was called when it shouldnt have been.<commit_after>﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Collections.Generic;
using Hudl.Ffmpeg.BaseTypes;
using Hudl.Ffmpeg.Command.BaseTypes;
using Hudl.Ffmpeg.Command.Managers;
using Hudl.Ffmpeg.Common;
using Hudl.Ffmpeg.Filters.BaseTypes;
using log4net;

namespace Hudl.Ffmpeg.Command
{
    public class FfmpegCommand
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(FfmpegCommand).Name);

        private FfmpegCommand(CommandFactory owner)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            Owner = owner;
            Id = Guid.NewGuid().ToString();
            Objects = CommandObjects.Create(this);
            OutputManager = CommandOutputManager.Create(this);
            ResourceManager = CommandResourceManager.Create(this);
            FilterchainManager = CommandFilterchainManager.Create(this);
        }

        public static FfmpegCommand Create(CommandFactory owner)
        {
            return new FfmpegCommand(owner);    
        }

        internal CommandObjects Objects { get; set; }

        public ReadOnlyCollection<CommandOutput> Outputs { get { return Objects.Outputs.AsReadOnly(); } }

        public ReadOnlyCollection<CommandResource> Resources { get { return Objects.Inputs.AsReadOnly(); } }

        public ReadOnlyCollection<Filterchain> Filtergraph { get { return Objects.Filtergraph.FilterchainList.AsReadOnly(); } }

        public CommandOutputManager OutputManager { get; set; }

        public CommandResourceManager ResourceManager { get; set; }

        public CommandFilterchainManager FilterchainManager { get; set; }

        /// <summary>
        /// Renders the command stream with the defualt command processor
        /// </summary>
        public List<CommandOutput> Render()
        {
            return RenderWith<CmdProcessorReciever>();
        }

        /// <summary>
        /// Renders the command stream with a new command processor
        /// </summary>
        public List<CommandOutput> RenderWith<TProcessor>()
            where TProcessor : ICommandProcessor, new()
        {
            var commandProcessor = new TProcessor();

            if (!commandProcessor.Open(Owner.Configuration))
            {
                throw new FfmpegRenderingException(commandProcessor.Error);
            }

            var returnType = RenderWith(commandProcessor);

            if (!commandProcessor.Close())
            {
                throw new FfmpegRenderingException(commandProcessor.Error);
            }

            return returnType;
        }

        /// <summary>
        /// Renders the command stream with an existing command processor
        /// </summary>
        public List<CommandOutput> RenderWith<TProcessor>(TProcessor commandProcessor)
            where TProcessor : ICommandProcessor
        {
            if (commandProcessor == null)
            {
                throw new ArgumentNullException("commandProcessor");
            }

            var commandBuilder = new CommandBuilder();
            commandBuilder.WriteCommand(this);
            
            if (!commandProcessor.Send(commandBuilder.ToString()))
            {
                throw new FfmpegRenderingException(commandProcessor.Error);
            }

            return Objects.Outputs;
        }

        #region Internals
        internal string Id { get; set; }
        internal CommandFactory Owner { get; set; }

        internal List<CommandResource> ResourcesFromReceipts(params CommandReceipt[] receipts)
        {
            return ResourcesFromReceipts(new List<CommandReceipt>(receipts));
        }
        internal List<CommandResource> ResourcesFromReceipts(List<CommandReceipt> receipts)
        {
            if (receipts == null || receipts.Count == 0)
            {
                throw new ArgumentException("Receipts cannot be null or empty for a retrieve.", "receipts");
            }

            return receipts.Select(receipt =>
                {
                    CommandResource resource = null;
                    switch (receipt.Type)
                    {
                        case CommandReceiptType.Input:
                            resource = Objects.Inputs.FirstOrDefault(r => r.Resource.Map == receipt.Map);
                            break;
                        case CommandReceiptType.Stream:
                            var filterchain = Objects.Filtergraph.FilterchainList.FirstOrDefault(f => f.GetReceipts().Any(r => r.Equals(receipt)));
                            var filterchainOutput = filterchain.OutputList.FirstOrDefault(r => r.Resource.Map == receipt.Map);
                            resource = CommandResource.Create(filterchainOutput.Resource);
                            resource.Id = filterchainOutput.Id; 
                            resource.Owner = this;
                            break;
                    }
                    if (resource == null)
                    {
                        throw new KeyNotFoundException("Resource was not found in the command list.");
                    }
                    return resource; 
                }).ToList();
{'fraction_non_alphanumeric': 0.08287710024542194, 'fraction_numerical': 0.0006607513686992637, 'mean_word_length': 1.8321304464047046, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 51, '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': '6504746', 'n_tokens_mistral': 2533, 'n_tokens_neox': 2410, 'n_words': 567}