<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling;
using Stardust.Manager.Extensions;
using Stardust.Manager.Helpers;
using Stardust.Manager.Interfaces;
using Stardust.Manager.Models;

namespace Stardust.Manager
{
	public class JobRepository : IJobRepository
	{
		private readonly RetryPolicyProvider _retryPolicyProvider;
		private readonly RetryPolicy _retryPolicy;
		private readonly string _connectionString;

		public JobRepository(string connectionString, RetryPolicyProvider retryPolicyProvider)
		{
			_connectionString = connectionString;
			_retryPolicyProvider = retryPolicyProvider;
			_retryPolicy = retryPolicyProvider.GetPolicy();
		}

		private void runner(Action funcToRun, string faliureMessage)
		{
			var policy = _retryPolicyProvider.GetPolicy();
			applyLoggingOnRetries(policy);
			try
			{
				policy.ExecuteAction(funcToRun);
			}
			catch (Exception ex)
			{
				this.Log().ErrorWithLineNumber(ex.Message + faliureMessage);
			}
		}

		private void applyLoggingOnRetries(RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy> policy)
		{
			policy.Retrying += (sender, args) =>
			{
				var msg = String.Format("Retry - Count:{0}, Delay:{1}, Exception:{2}", args.CurrentRetryCount, args.Delay,
					args.LastException);
				this.Log().ErrorWithLineNumber(msg);
			};
		}

		public void Add(JobDefinition job)
		{
			using (var connection = new SqlConnection(_connectionString))
			{
				SqlCommand jobHistoryCommand = connection.CreateCommand();
				jobHistoryCommand.CommandText = "INSERT INTO [Stardust].JobHistory (JobId, Name, CreatedBy, Serialized, Type) VALUES(@Id, @Name, @By, @Serialized, @Type)";
				jobHistoryCommand.Parameters.AddWithValue("@Id", job.Id);
				jobHistoryCommand.Parameters.AddWithValue("@Name", job.Name);
				jobHistoryCommand.Parameters.AddWithValue("@By", job.UserName);
				jobHistoryCommand.Parameters.AddWithValue("@Serialized", job.Serialized);
				jobHistoryCommand.Parameters.AddWithValue("@Type", job.Type);

				SqlCommand jobDefinitionCommand = connection.CreateCommand();
				jobDefinitionCommand.CommandText = "INSERT INTO [Stardust].JobDefinitions (Id, Name, Serialized, Type, userName) VALUES(@Id, @Name, @Serialized, @Type, @UserName)";
				jobDefinitionCommand.Parameters.AddWithValue("@Id", job.Id);
				jobDefinitionCommand.Parameters.AddWithValue("@Name", job.Name);
				jobDefinitionCommand.Parameters.AddWithValue("@UserName", job.UserName);
				jobDefinitionCommand.Parameters.AddWithValue("@Serialized", job.Serialized);
				jobDefinitionCommand.Parameters.AddWithValue("@Type", job.Type);
				try
				{<commit_msg>remove runner<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling;
using Stardust.Manager.Extensions;
using Stardust.Manager.Helpers;
using Stardust.Manager.Interfaces;
using Stardust.Manager.Models;

namespace Stardust.Manager
{
	public class JobRepository : IJobRepository
	{
		private readonly RetryPolicyProvider _retryPolicyProvider;
		private readonly RetryPolicy _retryPolicy;
		private readonly string _connectionString;

		public JobRepository(string connectionString, RetryPolicyProvider retryPolicyProvider)
		{
			_connectionString = connectionString;
			_retryPolicyProvider = retryPolicyProvider;
			_retryPolicy = retryPolicyProvider.GetPolicy();
		}
		
		
		public void Add(JobDefinition job)
		{
			using (var connection = new SqlConnection(_connectionString))
			{
				SqlCommand jobHistoryCommand = connection.CreateCommand();
				jobHistoryCommand.CommandText = "INSERT INTO [Stardust].JobHistory (JobId, Name, CreatedBy, Serialized, Type) VALUES(@Id, @Name, @By, @Serialized, @Type)";
				jobHistoryCommand.Parameters.AddWithValue("@Id", job.Id);
				jobHistoryCommand.Parameters.AddWithValue("@Name", job.Name);
				jobHistoryCommand.Parameters.AddWithValue("@By", job.UserName);
				jobHistoryCommand.Parameters.AddWithValue("@Serialized", job.Serialized);
				jobHistoryCommand.Parameters.AddWithValue("@Type", job.Type);

				SqlCommand jobDefinitionCommand = connection.CreateCommand();
				jobDefinitionCommand.CommandText = "INSERT INTO [Stardust].JobDefinitions (Id, Name, Serialized, Type, userName) VALUES(@Id, @Name, @Serialized, @Type, @UserName)";
				jobDefinitionCommand.Parameters.AddWithValue("@Id", job.Id);
				jobDefinitionCommand.Parameters.AddWithValue("@Name", job.Name);
				jobDefinitionCommand.Parameters.AddWithValue("@UserName", job.UserName);
				jobDefinitionCommand.Parameters.AddWithValue("@Serialized", job.Serialized);
				jobDefinitionCommand.Parameters.AddWithValue("@Type", job.Type);
				try
				{
{'fraction_non_alphanumeric': 0.10862944162436548, 'fraction_numerical': 0.0006091370558375635, 'mean_word_length': 5.841666666666667, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '2208228', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1401, 'n_words': 269}