Current state, whatever that means

This commit is contained in:
2022-03-17 20:04:12 -04:00
parent fb6dbdfaa7
commit 60edbee0b8
22 changed files with 1087 additions and 156 deletions

View File

@@ -15,7 +15,7 @@ namespace PartSource.Automation.Jobs
private readonly ILogger<ExecuteSsisPackages> _logger;
// TODO: set from config
private readonly string[] _ssisPackages = { "Parts Availability", "Parts Price" };
private readonly string[] _ssisPackages = { "Parts Availability" };
public ExecuteSsisPackages(IConfiguration configuration, SsisService ssisService, ILogger<ExecuteSsisPackages> logger)
{

View File

@@ -0,0 +1,75 @@
using PartSource.Data.Nexpart;
using PartSource.Services;
using Ratermania.Automation.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs
{
public class GetNexpartMenuItems : IAutomationJob
{
private readonly NexpartService _nexpartService;
public GetNexpartMenuItems(NexpartService nexpartService)
{
_nexpartService = nexpartService;
}
public async Task Run()
{
IList<string> rows = new List<string>
{
"\"Level 1\", \"Level 2\", \"Level 3\", \"Menu ID\""
};
MenuNodesLookup menuNodesLookup = new MenuNodesLookup
{
MenuId = 1,
NumberOfLevels = 1
};
MenuNodesLookupResponse categoryResponse = await _nexpartService.SendRequest<MenuNodesLookup, MenuNodesLookupResponse>(menuNodesLookup);
foreach (MenuNode categoryNode in categoryResponse.ResponseBody.MenuNode)
{
rows.Add($"\"{categoryNode.Description}\",\"\",\"\",{categoryNode.Id}");
MenuNodesLookup subgroupLookup = new MenuNodesLookup
{
MenuId = 1,
NumberOfLevels = 1,
ParentMenuNodeId = categoryNode.Id
};
MenuNodesLookupResponse subgroupResponse = await _nexpartService.SendRequest<MenuNodesLookup, MenuNodesLookupResponse>(subgroupLookup);
foreach (MenuNode subgroupNode in subgroupResponse.ResponseBody.MenuNode)
{
rows.Add($"\"{categoryNode.Description}\",\"{subgroupNode.Description}\",\"\",{subgroupNode.Id}");
MenuNodesLookup thirdLookup = new MenuNodesLookup
{
MenuId = 1,
NumberOfLevels = 1,
ParentMenuNodeId = subgroupNode.Id
};
MenuNodesLookupResponse thirdResponse = await _nexpartService.SendRequest<MenuNodesLookup, MenuNodesLookupResponse>(thirdLookup);
foreach (MenuNode thirdNode in thirdResponse.ResponseBody.MenuNode)
{
rows.Add($"\"{categoryNode.Description}\",\"{subgroupNode.Description}\",\"{thirdNode.Description}\",{thirdNode.Id}");
}
}
}
await File.WriteAllLinesAsync("C:\\users\\Tommy\\desktop\\Partsource Menu Items.csv", rows);
;
}
}
}

View File

@@ -0,0 +1,65 @@
using Ratermania.Automation.Interfaces;
using Ratermania.Shopify;
using Ratermania.Shopify.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs.POC
{
public class FixMultipleSeoTables : IAutomationJob
{
private readonly ShopifyClient _shopifyClient;
public FixMultipleSeoTables(ShopifyClient shopifyClient)
{
_shopifyClient = shopifyClient;
}
public async Task Run()
{
IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", "CA112-SC137-FL13750_Intake Manifolds" } });
while (products != null && products.Any())
{
foreach (Product product in products)
{
try
{
string[] parts = product.BodyHtml.Split("<div id=\"seoData\">");
if (parts.Length > 2)
{
string ul = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
string seoData = "<div id=\"seoData\">" + parts[1].Substring(0, parts[1].IndexOf("</table>") + "</table>".Length) + "</div>";
string vehicleIds = new Regex("<div id=\"vehicleIDs\".*</div>").Match(product.BodyHtml).Value;
product.BodyHtml = ul + seoData + vehicleIds;
await _shopifyClient.Products.Update(product);
}
}
catch
{
Console.WriteLine($"Failed to update {product.Id}");
}
}
try
{
Console.WriteLine("Did 250");
products = await _shopifyClient.Products.GetNext();
}
catch (Exception ex)
{
products = await _shopifyClient.Products.GetPrevious();
}
}
}
}
}

View File

@@ -0,0 +1,145 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PartSource.Automation.Models;
using PartSource.Data;
using PartSource.Data.Contexts;
using PartSource.Data.Models;
using PartSource.Services;
using Ratermania.Automation.Interfaces;
using Ratermania.Shopify;
using Ratermania.Shopify.Exceptions;
using Ratermania.Shopify.Resources;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs
{
public class UpdateFitmentHtml : IAutomationJob
{
private readonly ILogger<UpdateFitmentHtml> _logger;
private readonly ShopifyClient _shopifyClient;
private readonly PartSourceContext _partSourceContext;
private readonly FitmentContext _fitmentContext;
private readonly VehicleService _vehicleService;
public UpdateFitmentHtml(ILogger<UpdateFitmentHtml> logger, PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleService vehicleService)
{
_logger = logger;
_partSourceContext = partSourceContext;
_fitmentContext = fitmentContext;
_shopifyClient = shopifyClient;
_vehicleService = vehicleService;
}
public async Task Run()
{
IEnumerable<Product> products = null;
try
{
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "vendor", "FRAM" } });
}
catch (Exception ex)
{
_logger.LogError("Failed to get products from Shopify", ex);
throw;
}
int i = 1;
while (products != null && products.Any())
{
foreach (Product product in products)
{
ImportData importData = null;
try
{
importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.VariantSku == product.Variants[0].Sku);
if (importData == null)
{
continue;
}
product.BodyHtml = importData.BodyHtml;
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
if (ymmFitment.Count > 0)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("<table><tr><th colspan=\"2\">This Part Fits</th></tr>");
foreach (string fitment in ymmFitment)
{
try
{
string[] parts = fitment.Split(' ', 2);
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
}
catch
{
// This is still a POC at this point. Oh well...
}
}
stringBuilder.AppendLine("</table>");
product.BodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
}
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
if (vehicleIdFitment.Count > 0)
{
string vehicleIdString = string.Join('-', vehicleIdFitment.Select(j => $"v{j}"));
product.BodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
}
List<string> tags = new List<string>
{
importData.LineCode,
importData.PartNumber,
"zzzIsFitment=true"
};
product.Tags = string.Join(',', tags);
await _shopifyClient.Products.Update(product);
}
catch (Exception ex)
{
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
}
}
try
{
Console.WriteLine(i);
products = await _shopifyClient.Products.GetNext();
i++;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
products = await _shopifyClient.Products.GetPrevious();
}
}
}
}
}

View File

@@ -0,0 +1,144 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PartSource.Automation.Models;
using PartSource.Data;
using PartSource.Data.Contexts;
using PartSource.Data.Dtos;
using PartSource.Data.Models;
using PartSource.Services;
using Ratermania.Automation.Interfaces;
using Ratermania.Shopify;
using Ratermania.Shopify.Exceptions;
using Ratermania.Shopify.Resources;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs.POC
{
public class UpdateFitmentScratchpad : IAutomationJob
{
private readonly ILogger<UpdateFitmentScratchpad> _logger;
private readonly ShopifyClient _shopifyClient;
private readonly PartSourceContext _partSourceContext;
private readonly FitmentContext _fitmentContext;
private readonly VehicleService _vehicleService;
public UpdateFitmentScratchpad(ILogger<UpdateFitmentScratchpad> logger, PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleService vehicleService)
{
_logger = logger;
_partSourceContext = partSourceContext;
_fitmentContext = fitmentContext;
_shopifyClient = shopifyClient;
_vehicleService = vehicleService;
}
public async Task Run()
{
IList<string> productTypes = new List<string>
{
"C172-S231-F23107_(PS) Wipers - TRICO Neoform",
"CA172-SC231-FL23106_Wiper Blades - Bosch Icon",
"CA172-SC231-FL23107_(PS) Wipers - TRICO Neoform",
"CA172-SC231-FL23109_(PS) Wipers - TRICO Tech/Exact Fit",
"CA172-SC231-FL23110_Wiper Accessories",
"CA172-SC231-FL23114_Wiper Blades - Rear",
"CA172-SC231-FL23116_(PS) Wipers - Bosch Insight (Hybrid)",
"CA172-SC231-FL23117_(PS) Wipers - Bosch Clear Advantage (Beam)",
"CA172-SC231-FL23118_(PS) Wipers - Winter",
"CA172-SC231-FL23125_Wiper Blades - Bosch Areotwin"
};
IList<string> csvData = new List<string>
{
"\"Line Code\", \"Part Number\", \"Year\", \"Make\", \"Model\", \"Position\""
};
foreach (string type in productTypes)
{
IEnumerable<Product> products = null;
try
{
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", type } });
}
catch (Exception ex)
{
_logger.LogError("Failed to get products from Shopify", ex);
throw;
}
while (products != null && products.Any())
{
foreach (Product product in products)
{
ImportData importData = null;
try
{
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
importData = new ImportData
{
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0").Value ?? string.Empty,
PartNumber = product.Title.Split(' ')[0],
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
};
string csvRow = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
IList<VehicleFitmentDto> vehicles = _vehicleService.GetVehicleFitmentForPart(importData.PartNumber, importData.LineCode);
if (vehicles.Count > 0)
{
foreach (VehicleFitmentDto vehicle in vehicles)
{
try
{
csvData.Add($"\"{importData.LineCode}\", \"{importData.PartNumber}\", \"{vehicle.Vehicle.Year}\", \"{vehicle.Vehicle.MakeName}\", \"{vehicle.Vehicle.ModelName}\", \"{vehicle.Fitment.Position}\"");
}
catch
{
// This is still a POC at this point. Oh well...
}
}
}
}
catch (Exception ex)
{
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
}
}
try
{
Console.WriteLine($"Did an iteration of {type}");
products = await _shopifyClient.Products.GetNext();
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
products = await _shopifyClient.Products.GetPrevious();
}
}
}
await File.WriteAllLinesAsync("C:\\users\\Tommy\\desktop\\Wiper Fitment.csv", csvData);
;
}
}
}

View File

@@ -11,6 +11,7 @@ using System.Data;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@@ -24,6 +25,8 @@ namespace PartSource.Automation.Jobs
private readonly FtpConfiguration _ftpConfiguration;
private readonly SeoDataType _seoDataType;
private readonly IDictionary<string, string> _noteDictionary;
public ProcessWhiFitment(IConfiguration configuration, ILogger<ProcessWhiFitment> logger, WhiSeoService whiSeoService)
{
_logger = logger;
@@ -32,48 +35,68 @@ namespace PartSource.Automation.Jobs
_seoDataType = SeoDataType.Fitment;
_ftpConfiguration = configuration.GetSection("ftpServers:WhiConfiguration").Get<FtpConfiguration>();
_noteDictionary = new ConcurrentDictionary<string, string>();
}
public async Task Run()
{
_whiSeoService.Truncate();
_whiSeoService.GetFiles(_seoDataType);
_whiSeoService.TruncateFitmentTables();
// _whiSeoService.GetFiles(_seoDataType);
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
IEnumerable<IGrouping<string, FileInfo>> fileGroups = directoryInfo.GetFiles().Where(f => f.Name.EndsWith("csv.gz")).GroupBy(x => x.Name.Split('_').Last());
ConcurrentQueue<IGrouping<string, FileInfo>> fileGroups = new ConcurrentQueue<IGrouping<string, FileInfo>>();
foreach (IGrouping<string, FileInfo> fileGroup in fileGroups)
foreach (IGrouping<string, FileInfo> fileGroup in directoryInfo.GetFiles().Where(f => f.Name.EndsWith("csv.gz")).GroupBy(x => x.Name.Split('_').Last()))
{
foreach (FileInfo fileInfo in fileGroup)
fileGroups.Enqueue(fileGroup);
}
while (fileGroups.Count > 0)
{
Parallel.For(0, 2, i =>
{
try
bool result = fileGroups.TryDequeue(out IGrouping<string, FileInfo> fileGroup);
if (!result)
{
string filename = Decompress(fileInfo);
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
return;
DataTable dataTable = GetDataTable(filename);
_whiSeoService.BulkCopy(_seoDataType, dataTable, tableName);
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
File.Delete(filename);
}
catch (Exception ex)
foreach (FileInfo fileInfo in fileGroup)
{
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
try
{
string filename = Decompress(fileInfo);
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
DataTable dataTable = GetDataTable(filename);
_whiSeoService.BulkCopyFitment(dataTable, tableName);
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
File.Delete(filename);
}
catch (Exception ex)
{
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
}
}
}
string fitmentTable = fileGroup.Key.Substring(0, fileGroup.Key.IndexOf('.'));
_whiSeoService.CreateFitmentTable(fitmentTable);
string fitmentTable = fileGroup.Key.Substring(0, fileGroup.Key.IndexOf('.'));
_whiSeoService.CreateFitmentTable(fitmentTable);
_logger.LogInformation($"Created fitment table for part group {fitmentTable}.");
_logger.LogInformation($"Created fitment table for part group {fitmentTable}.");
});
}
_whiSeoService.CreateFitmentView();
_whiSeoService.SaveNotes(_noteDictionary);
}
public string Decompress(FileInfo fileInfo)
@@ -96,7 +119,7 @@ namespace PartSource.Automation.Jobs
dataTable.Columns.Add("BaseVehicleId", typeof(int));
dataTable.Columns.Add("EngineConfigId", typeof(int));
dataTable.Columns.Add("Position", typeof(string));
dataTable.Columns.Add("NoteText", typeof(string));
dataTable.Columns.Add("FitmentNoteHash", typeof(string));
using StreamReader reader = new StreamReader(filename);
string line = reader.ReadLine(); // Burn the header row
@@ -112,20 +135,45 @@ namespace PartSource.Automation.Jobs
}
string lineCode = Regex.Replace(columns[0], "[^a-zA-Z0-9]", string.Empty).Trim();
string partNumber = Regex.Replace(columns[1], "[^a-zA-Z0-9]", string.Empty).Trim();
string partNumber = Regex.Replace(columns[1], "[^a-zA-Z0-9\\-]", string.Empty).Trim();
string position = columns[7].Trim();
string noteText = columns[4].Trim();
string noteTextHash = GetMD5Hash(noteText);
if (!_noteDictionary.ContainsKey(noteTextHash))
{
_noteDictionary.Add(noteTextHash, noteText);
}
if (!string.IsNullOrEmpty(lineCode)
&& !string.IsNullOrEmpty(partNumber)
&& int.TryParse(columns[5], out int baseVehicleId)
&& int.TryParse(columns[6], out int engineConfigId))
{
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId, position, noteText });
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId, position, noteTextHash });
}
}
return dataTable;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5351:Do Not Use Broken Cryptographic Algorithms", Justification = "Not used for security")]
private string GetMD5Hash(string input)
{
using MD5 md5 = MD5.Create();
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
stringBuilder.Append(hashBytes[i].ToString("X2"));
}
return stringBuilder.ToString();
}
}
}

View File

@@ -0,0 +1,126 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using PartSource.Automation.Models.Configuration;
using PartSource.Automation.Models.Enums;
using PartSource.Automation.Services;
using Ratermania.Automation.Interfaces;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs
{
public class ProcessWhiVehicles : IAutomationJob
{
private readonly ILogger<ProcessWhiVehicles> _logger;
private readonly WhiSeoService _whiSeoService;
private readonly FtpConfiguration _ftpConfiguration;
private readonly SeoDataType _seoDataType;
public ProcessWhiVehicles(IConfiguration configuration, ILogger<ProcessWhiVehicles> logger, WhiSeoService whiSeoService)
{
_logger = logger;
_whiSeoService = whiSeoService;
_seoDataType = SeoDataType.Vehicle;
_ftpConfiguration = configuration.GetSection("ftpServers:WhiConfiguration").Get<FtpConfiguration>();
}
public async Task Run()
{
_whiSeoService.TruncateVehicleTable();
_whiSeoService.GetFiles(_seoDataType);
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
IEnumerable<FileInfo> files = directoryInfo.GetFiles().Where(f => f.Name.StartsWith("seo_aces_vehicle_feed"));
foreach (FileInfo fileInfo in files)
{
try
{
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
DataTable dataTable = GetDataTable(fileInfo.FullName);
_whiSeoService.BulkCopyVehicle(dataTable, tableName);
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
File.Delete(fileInfo.FullName);
}
catch (Exception ex)
{
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
}
}
_whiSeoService.CreateVehicleTable();
_logger.LogInformation($"Created vehicle table.");
}
private DataTable GetDataTable(string filename)
{
using DataTable dataTable = new DataTable();
dataTable.Columns.Add("Year", typeof(int));
dataTable.Columns.Add("MakeId", typeof(int));
dataTable.Columns.Add("MakeName", typeof(string));
dataTable.Columns.Add("ModelId", typeof(int));
dataTable.Columns.Add("ModelName", typeof(string));
dataTable.Columns.Add("EngineConfigId", typeof(int));
dataTable.Columns.Add("EngineDescription", typeof(string));
dataTable.Columns.Add("BaseVehicleId", typeof(int));
dataTable.Columns.Add("VehicleToEngineConfigId", typeof(int));
dataTable.Columns.Add("SubmodelId", typeof(int));
dataTable.Columns.Add("SubmodelName", typeof(string));
using StreamReader reader = new StreamReader(filename);
string line = reader.ReadLine(); // Burn the header row
while (reader.Peek() > 0)
{
line = reader.ReadLine();
string[] columns = line.Split("\",\"");
for (int i = 0; i < columns.Length; i++)
{
columns[i] = columns[i].Replace("\"", string.Empty);
}
string makeName = columns[4].Trim();
string modelName = columns[6].Trim();
string submodelName = columns[34].Trim();
string engineDescription = columns[51].Trim();
if (!string.IsNullOrEmpty(makeName)
&& !string.IsNullOrEmpty(modelName)
&& !string.IsNullOrEmpty(submodelName)
&& !string.IsNullOrEmpty(engineDescription)
&& int.TryParse(columns[0], out int baseVehicleId)
&& int.TryParse(columns[2], out int year)
&& int.TryParse(columns[3], out int makeId)
&& int.TryParse(columns[5], out int modelId)
&& int.TryParse(columns[33], out int submodelId)
&& int.TryParse(columns[35], out int engineConfigId)
&& int.TryParse(columns[36], out int vehicleToEngineConfigId))
{
dataTable.Rows.Add(new object[] { year, makeId, makeName, modelId, modelName, engineConfigId, engineDescription, baseVehicleId, vehicleToEngineConfigId, submodelId, submodelName });
}
}
return dataTable;
}
}
}

View File

@@ -14,6 +14,7 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@@ -38,14 +39,21 @@ namespace PartSource.Automation.Jobs
public async Task Run()
{
IEnumerable<Product> products = null;
try
IList<string> productTypes = new List<string>
{
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });//, {"product_type", "CA142-SC130-FL13029_Certified Brake Pads" } });
"CA108-SC349-FL34907_CV Shafts, New"
};
foreach (string type in productTypes)
{
IEnumerable<Product> products = null;
try
{
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", "CA108-SC349-FL34907_CV Shafts, New" } });
}
catch (Exception ex)
catch (Exception ex)
{
_logger.LogError("Failed to get products from Shopify", ex);
throw;
@@ -61,14 +69,25 @@ namespace PartSource.Automation.Jobs
try
{
importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.ShopifyId == product.Id);
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
if (importData == null)
//importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.ShopifyId == product.Id);
//if (importData == null)
//{
// continue;
importData = new ImportData
{
continue;
}
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0").Value ?? string.Empty,
PartNumber = product.Title.Split(' ')[0],
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
};
// }
//importData.PartNumber = product.Title.Split(' ')[0];
bool isFitment = false;
string bodyHtml = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
@@ -84,6 +103,10 @@ namespace PartSource.Automation.Jobs
if (vehicleIdFitment.Count > 0)
{
string vehicleIdString = string.Join('-', vehicleIdFitment.Select(j => $"v{j}"));
bodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
isFitment = true;
string json = JsonConvert.SerializeObject(vehicleIdFitment);
@@ -114,6 +137,28 @@ namespace PartSource.Automation.Jobs
{
isFitment = true;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("<table><tr><th colspan=\"2\">This Part Fits</th></tr>");
foreach (string fitment in ymmFitment)
{
try
{
string[] parts = fitment.Split(' ', 2);
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
}
catch
{
// This is still a POC at this point. Oh well...
}
}
stringBuilder.AppendLine("</table>");
bodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
string json = JsonConvert.SerializeObject(ymmFitment);
if (json.Length < 100000)
{
@@ -149,12 +194,32 @@ namespace PartSource.Automation.Jobs
await _shopifyClient.Metafields.Add(isFitmentMetafield);
List<string> tags = new List<string>
Metafield lineCodeMetafield = new Metafield
{
importData.LineCode,
importData.PartNumber
Namespace = "google",
Key = "custom_label_0",
Value = importData.LineCode,
ValueType = "string",
OwnerResource = "product",
OwnerId = product.Id
};
//await _shopifyClient.Metafields.Add(lineCodeMetafield);
Metafield partNumberMetafield = new Metafield
{
Namespace = "google",
Key = "custom_label_1",
Value = importData.PartNumber,
ValueType = "string",
OwnerResource = "product",
OwnerId = product.Id
};
// await _shopifyClient.Metafields.Add(partNumberMetafield);
List<string> tags = new List<string>();
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
{
tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
@@ -173,8 +238,8 @@ namespace PartSource.Automation.Jobs
tags.Add(zzzIsFitment);
product.Tags = string.Join(',', tags);
//product.Tags = string.Join(',', tags);
product.BodyHtml = bodyHtml;
await _shopifyClient.Products.Update(product);
importData.IsFitment = isFitment;
@@ -205,5 +270,7 @@ namespace PartSource.Automation.Jobs
}
}
}
;
}
}
}

View File

@@ -47,7 +47,14 @@ namespace PartSource.Automation.Jobs
{
try
{
ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == product.Variants[0].Sku);
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
ImportData importData = new ImportData
{
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0").Value ?? string.Empty,
PartNumber = product.Title.Split(' ')[0],
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
};
if (importData == null || importData?.LineCode == "SVG") // Headlights go in front, DUH
{
@@ -87,40 +94,41 @@ namespace PartSource.Automation.Jobs
await SavePositionMetafield(product, vehicleIds, currentPosition);
IList<string> notes = fitments.Select(f => f.NoteText)
.Distinct()
.ToList();
//IList<string> notes = fitments.Select(f => f.NoteText)
IList<object> vehicleNotes = new List<object>();
// .Distinct()
// .ToList();
foreach (string noteText in notes)
{
vehicleIds = fitments.Where(f => f.NoteText == noteText)
.Select(f => new { f.EngineConfigId, f.BaseVehicleId })
.SelectMany(f => vehicles.Where(v => v.BaseVehicleId == f.BaseVehicleId && v.EngineConfigId == f.EngineConfigId))
.Select(v => v.VehicleToEngineConfigId)
.ToList();
//IList<object> vehicleNotes = new List<object>();
vehicleNotes.Add(new { noteText, vehicleIds });
}
//foreach (string noteText in notes)
//{
// vehicleIds = fitments.Where(f => f.NoteText == noteText)
// .Select(f => new { f.EngineConfigId, f.BaseVehicleId })
// .SelectMany(f => vehicles.Where(v => v.BaseVehicleId == f.BaseVehicleId && v.EngineConfigId == f.EngineConfigId))
// .Select(v => v.VehicleToEngineConfigId)
// .ToList();
string json = JsonConvert.SerializeObject(vehicleNotes);
if (json.Length >= 100000)
{
continue;
}
// vehicleNotes.Add(new { noteText, vehicleIds });
//}
Metafield vehicleMetafield = new Metafield
{
Namespace = "fitment",
Key = "note_text",
Value = json,
ValueType = "json_string",
OwnerResource = "product",
OwnerId = product.Id
};
//string json = JsonConvert.SerializeObject(vehicleNotes);
//if (json.Length >= 100000)
//{
// continue;
//}
await _shopifyClient.Metafields.Add(vehicleMetafield);
//Metafield vehicleMetafield = new Metafield
//{
// Namespace = "fitment",
// Key = "note_text",
// Value = json,
// ValueType = "json_string",
// OwnerResource = "product",
// OwnerId = product.Id
//};
//await _shopifyClient.Metafields.Add(vehicleMetafield);
//importData.UpdatedAt = DateTime.Now;
//importData.UpdateType = "Positioning";
@@ -148,7 +156,7 @@ namespace PartSource.Automation.Jobs
private IList<Fitment> GetPositionOrderedFitments(string partNumber, string lineCode)
{
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]\\-", string.Empty);
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
.Where(d => d.LineCode == lineCode)

View File

@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PartSource.Automation.Models.Jobs;
using PartSource.Automation.Services;
using PartSource.Data.Contexts;
using PartSource.Data.Models;
@@ -9,7 +10,9 @@ using Ratermania.Shopify.Resources;
using Ratermania.Shopify.Resources.Enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs
@@ -31,15 +34,14 @@ namespace PartSource.Automation.Jobs
public async Task Run()
{
List<UpdatePricingResult> pricingReport = new List<UpdatePricingResult>();
IEnumerable<Product> products = null;
IEnumerable<PartPrice> prices = null;
int updateCount = 0;
try
{
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
prices = await _partSourceContext.PartPrices.ToListAsync();
prices = await _partSourceContext.PartPrices.AsNoTracking().ToListAsync();
}
catch (Exception ex)
@@ -53,6 +55,8 @@ namespace PartSource.Automation.Jobs
{
foreach (Product product in products)
{
List<UpdatePricingResult> productPricingUpdate = new List<UpdatePricingResult>();
if (product.Variants.Length > 0)
{
bool hasUpdate = false;
@@ -69,6 +73,15 @@ namespace PartSource.Automation.Jobs
if (product.Variants[i].Price.ToString("G29") != partPrice.Your_Price.Value.ToString("G29") || product.Variants[i].CompareAtPrice.ToString("G29") != partPrice.Compare_Price.Value.ToString("G29"))
{
productPricingUpdate.Add(new UpdatePricingResult
{
Sku = variant.Sku,
OldPrice = product.Variants[i].Price,
NewPrice = partPrice.Your_Price.Value,
OldCompareAt = product.Variants[i].CompareAtPrice,
NewCompareAt = partPrice.Compare_Price.Value
});
product.Variants[i].Price = partPrice.Your_Price.Value;
product.Variants[i].CompareAtPrice = partPrice.Compare_Price.Value;
@@ -95,8 +108,8 @@ namespace PartSource.Automation.Jobs
{
//await _shopifyClient.Metafields.Add(metafield);
await _shopifyClient.Products.Update(product);
updateCount++;
pricingReport.AddRange(productPricingUpdate);
}
catch (Exception ex)
@@ -111,7 +124,7 @@ namespace PartSource.Automation.Jobs
{
products = await _shopifyClient.Products.GetNext();
_logger.LogInformation($"Total updated: {updateCount}");
_logger.LogInformation($"Total updated: {pricingReport.Count}");
}
catch (Exception ex)
@@ -121,7 +134,40 @@ namespace PartSource.Automation.Jobs
}
}
_emailService.Send("Pricing Update Completed", $"The pricing update has completed. Total updated: {updateCount}");
Attachment attachment = GetPricingReportAttachment(pricingReport);
_emailService.Send("Pricing Update Completed", $"The pricing update has completed. Total updated: {pricingReport.Count}", attachment);
}
private Attachment GetPricingReportAttachment(IList<UpdatePricingResult> pricingReport)
{
string directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Pricing Reports");
string filename = Path.Combine(directory, $"Pricing Update {DateTime.Now.ToString("yyyy-MM-dd")}.csv");
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (File.Exists(filename))
{
File.Delete(filename);
}
using FileStream fileStream = File.OpenWrite(filename);
using StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
streamWriter.WriteLine("SKU, Old Price, New Price, Old Compare At, New Compare At");
foreach (UpdatePricingResult pricingResult in pricingReport)
{
streamWriter.WriteLine($"{pricingResult.Sku},{pricingResult.OldPrice},{pricingResult.NewPrice},{pricingResult.OldCompareAt},{pricingResult.NewCompareAt}");
}
streamWriter.Close();
fileStream.Close();
return new Attachment(filename);
}
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace PartSource.Automation.Models.Jobs
{
public class UpdatePricingResult
{
public string Sku { get; set; }
public decimal OldPrice { get; set; }
public decimal NewPrice { get; set; }
public decimal OldCompareAt { get; set; }
public decimal NewCompareAt { get; set; }
}
}

View File

@@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using PartSource.Automation.Jobs;
using PartSource.Automation.Jobs.POC;
using PartSource.Automation.Services;
using PartSource.Data;
using PartSource.Data.AutoMapper;
@@ -21,8 +22,7 @@ namespace PartSource.Automation
{
class Program
{
static async Task Main(string[] args)
{
static async Task Main(string[] args){
try
{
using IHost host = CreateHostBuilder().Build();
@@ -55,7 +55,11 @@ namespace PartSource.Automation
)
.AddDbContext<FitmentContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("FitmentDatabase"), opts => opts.EnableRetryOnFailure())
options.UseSqlServer(builder.Configuration.GetConnectionString("FitmentDatabase"), opts =>
{
opts.EnableRetryOnFailure();
opts.CommandTimeout(600);
})
)
.AddShopify(options =>
@@ -64,33 +68,44 @@ namespace PartSource.Automation
options.ApiSecret = builder.Configuration["Shopify:ApiSecret"];
options.ApiVersion = "2020-01";
options.ShopDomain = builder.Configuration["Shopify:ShopDomain"];
//options.ApiKey = "9a533dad460321c6ce8f30bf5b8691ed";
//options.ApiSecret = "dc9e28365d9858e544d57ac7af43fee7";
//options.ApiVersion = "2020-01";
//options.ShopDomain = "dev-partsource.myshopify.com";
})
.AddAutomation(options =>
{
options.HasBaseInterval(new TimeSpan(0, 15, 0))
.HasMaxFailures(5)
//.HasJob<TestJob>(options => options.HasInterval(new TimeSpan(7, 0, 0, 0)));
//
//.HasJob<SyncronizeProducts>(options => options.HasInterval(new TimeSpan(24, 0, 0)))
.HasJob<ProcessWhiFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .HasDependency<SyncronizeProducts>()
// .HasJob<UpdateFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .HasDependency<ProcessWhiFitment>()
//.HasDependency<SyncronizeProducts>()
// .StartsAt(DateTime.Today.AddHours(8))
//) ;
//.HasJob<StatusCheck>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .StartsAt(DateTime.Parse("2021-04-01 08:00:00"))
//)
// .HasJob<ExecuteSsisPackages>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .StartsAt(DateTime.Today.AddHours(26))
// )
// .HasJob<UpdatePricing>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .HasDependency<ExecuteSsisPackages>()
// .StartsAt(DateTime.Today.AddHours(27)
// )
);
.HasMaxFailures(1)
//.HasJob<TestJob>(options => options.HasInterval(new TimeSpan(7, 0, 0, 0)));
//
//.HasJob<SyncronizeProducts>(options => options.HasInterval(new TimeSpan(24, 0, 0)))
// .HasJob<ProcessWhiFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0)));
//.HasJob<ProcessWhiVehicles>(options => options.HasInterval(new TimeSpan(24, 0, 0))
//.HasDependency<SyncronizeProducts>()
.HasJob<UpdateFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0)));
//.HasJob<UpdatePositioning>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .HasDependency<UpdateFitment>()
// .HasDependency<ProcessWhiFitment>()
// .HasDependency<SyncronizeProducts>()
// .StartsAt(DateTime.Today.AddHours(8))
//) ;
//.HasJob<StatusCheck>(options => options.HasInterval(new TimeSpan(24, 0, 0))
// .StartsAt(DateTime.Parse("2021-04-01 08:00:00"))
//)
//.HasJob<ExecuteSsisPackages>(options =>
// options.HasInterval(new TimeSpan(24, 0, 0))
// .StartsAt(DateTime.Today.AddHours(5))
// );
//.StartsAt(DateTime.Today.AddHours(26))
//)
//.HasJob<UpdatePricing>(options => options.HasInterval(new TimeSpan(24, 0, 0))
//.HasDependency<ExecuteSsisPackages>()
//.StartsAt(DateTime.Today.AddHours(27)
//)
//);
//.AddApiServer();
})
@@ -98,6 +113,7 @@ namespace PartSource.Automation
.AddSingleton<SsisService>()
.AddSingleton<WhiSeoService>()
.AddSingleton<VehicleService>()
.AddSingleton<NexpartService>()
.AddAutoMapper(typeof(PartSourceProfile));

View File

@@ -3,6 +3,7 @@ using PartSource.Automation.Models.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
@@ -19,7 +20,7 @@ namespace PartSource.Automation.Services
_emailConfiguration = configuration.GetSection("emailConfiguration").Get<EmailConfiguration>();
}
public void Send(string subject, string body)
public void Send(string subject, string body, Attachment attachment = null)
{
using SmtpClient smtpClient = new SmtpClient
{
@@ -31,15 +32,20 @@ namespace PartSource.Automation.Services
From = new MailAddress(_emailConfiguration.From),
Subject = subject,
Body = body,
IsBodyHtml = true
IsBodyHtml = true,
};
if (attachment != null)
{
mailMessage.Attachments.Add(attachment);
}
foreach (string address in _emailConfiguration.To.Split(','))
{
mailMessage.To.Add(address);
}
// smtpClient.Send(mailMessage);
smtpClient.Send(mailMessage);
}
public void Send(string to, string subject, string body)
@@ -54,7 +60,7 @@ namespace PartSource.Automation.Services
From = new MailAddress(_emailConfiguration.From),
Subject = subject,
Body = body,
IsBodyHtml = false
IsBodyHtml = false,
};
foreach (string address in _emailConfiguration.To.Split(','))
@@ -62,7 +68,7 @@ namespace PartSource.Automation.Services
mailMessage.To.Add(to);
}
// smtpClient.Send(mailMessage);
smtpClient.Send(mailMessage);
}
}
}

View File

@@ -41,7 +41,7 @@ namespace PartSource.Automation.Services
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{filename}"));
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
request.Method = WebRequestMethods.Ftp.DownloadFile;
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using Stream responseStream = response.GetResponseStream();
using FileStream fileStream = new FileStream($"{_ftpConfiguration.Destination}\\{filename.Replace("/", "\\")}", FileMode.Create);

View File

@@ -35,7 +35,7 @@ namespace PartSource.Automation.Services
foreach (string file in files)
{
if (file.EndsWith("csv.gz"))
if (file.Contains(".csv"))
{
try
{
@@ -53,7 +53,16 @@ namespace PartSource.Automation.Services
}
}
public void Truncate()
public void TruncateVehicleTable()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"truncate table dbo.Vehicle", connection);
command.ExecuteNonQuery();
}
public void TruncateFitmentTables()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
@@ -62,11 +71,36 @@ namespace PartSource.Automation.Services
command.ExecuteNonQuery();
}
public void BulkCopy(SeoDataType seoDataType, DataTable dataTable, string tableName)
public void SaveNotes(IDictionary<string, string> notes)
{
using DataTable dataTable = new DataTable();
dataTable.Columns.Add("NoteText", typeof(string));
dataTable.Columns.Add("Hash", typeof(string));
foreach (KeyValuePair<string, string> note in notes)
{
dataTable.Rows.Add(new string[] { note.Value, note.Key });
}
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"FitmentNote",
BulkCopyTimeout = 14400
};
bulk.WriteToServer(dataTable);
}
public void BulkCopyFitment(DataTable dataTable, string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
string sql = string.Empty;
using SqlCommand command = new SqlCommand($"EXEC CreateFitmentTempTable @tableName = '{tableName}'", connection);
command.ExecuteNonQuery();
@@ -74,6 +108,25 @@ namespace PartSource.Automation.Services
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"FitmentTemp.{tableName}",
BulkCopyTimeout = 1
};
bulk.WriteToServer(dataTable);
}
public void BulkCopyVehicle(DataTable dataTable, string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
string sql = string.Empty;
using SqlCommand command = new SqlCommand($"EXEC CreateVehicleTempTable @tableName = '{tableName}'", connection);
command.ExecuteNonQuery();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"VehicleTemp.{tableName}",
BulkCopyTimeout = 14400
};
@@ -97,6 +150,21 @@ namespace PartSource.Automation.Services
using SqlCommand command = new SqlCommand($"exec CreateFitmentView", connection);
command.ExecuteNonQuery();
using SqlCommand command2 = new SqlCommand($"exec CreateFitmentIndexes", connection);
command2.ExecuteNonQuery();
}
public void CreateVehicleTable()
{
return;
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"exec CreateVehicleTable", connection);
command.CommandTimeout = 1800;
command.ExecuteNonQuery();
}
}
}

View File

@@ -6,6 +6,7 @@
"emailConfiguration": {
"From": "alerts@ps-automation.eastus2.cloudapp.azure.com",
"To": "tom@soundpress.com,Anas.Bajwa@Partsource.ca,josh@soundpress.com,alex.au@partsource.ca,michael.massara@partsource.ca",
//"To": "tom@tomraterman.com",
"SmtpHost": "localhost"
},
"FtpServers": {
@@ -34,7 +35,8 @@
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information",
// "Microsoft.EntityFrameworkCore.Database.Command": "Information"
},
"EventLog": {
"LogLevel": {