Hourly inventory updates
This commit is contained in:
@@ -8,6 +8,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PartSource.Automation.Models.Configuration;
|
||||
using PartSource.Automation.Models.Ftp;
|
||||
using PartSource.Automation.Services;
|
||||
@@ -18,35 +19,49 @@ namespace PartSource.Automation.Jobs.POC
|
||||
public class BulkUpdateInventory : IAutomationJob
|
||||
{
|
||||
private readonly FtpService _ftpService;
|
||||
private readonly ILogger<BulkUpdateInventory> _logger;
|
||||
|
||||
public BulkUpdateInventory(IConfiguration configuration)
|
||||
public BulkUpdateInventory(IConfiguration configuration, ILogger<BulkUpdateInventory> logger)
|
||||
{
|
||||
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:AzureConfiguration").Get<FtpConfiguration>();
|
||||
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:AutomationConfiguration").Get<FtpConfiguration>();
|
||||
_ftpService = new FtpService(ftpConfiguration);
|
||||
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
{
|
||||
FtpFileInfo lastUploadedFile = _ftpService.ListFilesExtended()
|
||||
.Where(f => f.FileType == FtpFileType.File && f.Filename.IndexOf("Availability") > -1)
|
||||
.Where(f => f.FileType == FtpFileType.File && f.Filename.IndexOf("Availability Full") > -1)
|
||||
.OrderByDescending(f => f.Modified)
|
||||
.First();
|
||||
.FirstOrDefault();
|
||||
|
||||
string file = _ftpService.Download(lastUploadedFile.Filename, Path.GetTempPath());
|
||||
if (lastUploadedFile == null)
|
||||
{
|
||||
_logger.LogInformation($"No full inventory file available.");
|
||||
return;
|
||||
}
|
||||
|
||||
string file = _ftpService.Download(lastUploadedFile.Filename);
|
||||
|
||||
DataTable dataTable = GetDataTable(file);
|
||||
|
||||
using SqlConnection connection = new SqlConnection("Server=tcp:ps-automation-stage.eastus2.cloudapp.azure.com,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=stageuser;Password=]FXepK^cFYS|[H<;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;");
|
||||
using SqlConnection connection = new SqlConnection("Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-test;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
|
||||
connection.Open();
|
||||
|
||||
using SqlCommand command = new SqlCommand("TRUNCATE TABLE PartAvailability", connection);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
|
||||
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||
{
|
||||
DestinationTableName = $"PartAvailability",
|
||||
DestinationTableName = "PartAvailability",
|
||||
BulkCopyTimeout = 14400
|
||||
};
|
||||
|
||||
bulk.WriteToServer(dataTable);
|
||||
|
||||
_ftpService.Delete(lastUploadedFile.Filename);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,20 +32,21 @@ namespace PartSource.Automation.Jobs.POC
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
{
|
||||
FtpFileInfo lastUploadedFile = _ftpService.ListFilesExtended()
|
||||
.Where(f => f.FileType == FtpFileType.File && f.Modified >= DateTime.Now.AddHours(-24) && f.Filename.IndexOf("Availability Partial") > -1)
|
||||
.Where(f => f.FileType == FtpFileType.File && f.Filename.IndexOf("Availability Partial") > -1)
|
||||
.OrderByDescending(f => f.Modified)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (lastUploadedFile == null)
|
||||
{
|
||||
_logger.LogInformation($"No partial inventory file available for the time period {DateTime.Now.AddHours(-24)} - {DateTime.Now}");
|
||||
_logger.LogInformation($"No partial inventory file available.");
|
||||
return;
|
||||
}
|
||||
|
||||
string file = _ftpService.Download($"{lastUploadedFile.Filename}", "C:\\Users\\Tom\\Desktop");
|
||||
_logger.LogInformation("Processing {filename}", lastUploadedFile.Filename);
|
||||
|
||||
string file = _ftpService.Download($"{lastUploadedFile.Filename}");
|
||||
|
||||
using SqlConnection connection = new SqlConnection("Server=tcp:ps-automation-stage.eastus2.cloudapp.azure.com,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=stageuser;Password=]FXepK^cFYS|[H<;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;");
|
||||
using SqlConnection connection = new SqlConnection("Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-test;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
|
||||
connection.Open();
|
||||
|
||||
using StreamReader reader = new StreamReader(file);
|
||||
@@ -55,17 +56,17 @@ namespace PartSource.Automation.Jobs.POC
|
||||
{
|
||||
line = reader.ReadLine();
|
||||
|
||||
string[] columns = line.Split(",");
|
||||
string[] columns = line.Split("|");
|
||||
for (int i = 0; i < columns.Length; i++)
|
||||
{
|
||||
columns[i] = columns[i].Replace("\"", string.Empty);
|
||||
}
|
||||
|
||||
if (int.TryParse(columns[0], out int store)
|
||||
&& int.TryParse(columns[1], out int quantity)
|
||||
&& int.TryParse(columns[2], out int sku))
|
||||
&& int.TryParse(columns[1], out int sku)
|
||||
&& int.TryParse(columns[2], out int quantity))
|
||||
{
|
||||
using SqlCommand sqlCommand = new SqlCommand("UPDATE Inventory SET QTY = @qty WHERE SKU = @sku AND Store = @store", connection);
|
||||
using SqlCommand sqlCommand = new SqlCommand("UPDATE PartAvailability SET QTY = @qty WHERE SKU = @sku AND Store = @store", connection);
|
||||
sqlCommand.Parameters.Add(new SqlParameter("qty", quantity));
|
||||
sqlCommand.Parameters.Add(new SqlParameter("sku", sku));
|
||||
sqlCommand.Parameters.Add(new SqlParameter("store", store));
|
||||
@@ -74,6 +75,8 @@ namespace PartSource.Automation.Jobs.POC
|
||||
}
|
||||
}
|
||||
|
||||
_ftpService.Delete(lastUploadedFile.Filename);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
@@ -7,12 +6,10 @@ using Microsoft.Extensions.Logging;
|
||||
using PartSource.Automation.Jobs;
|
||||
using PartSource.Automation.Jobs.POC;
|
||||
using PartSource.Automation.Services;
|
||||
using PartSource.Data;
|
||||
using PartSource.Data.AutoMapper;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Services;
|
||||
using Ratermania.Automation.DependencyInjection;
|
||||
using Ratermania.Automation.Logging;
|
||||
using Ratermania.Shopify.DependencyInjection;
|
||||
using System;
|
||||
using System.IO;
|
||||
@@ -22,7 +19,8 @@ namespace PartSource.Automation
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static async Task Main(string[] args){
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
using IHost host = CreateHostBuilder().Build();
|
||||
@@ -95,9 +93,13 @@ namespace PartSource.Automation
|
||||
//.HasJob<StatusCheck>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
||||
// .StartsAt(DateTime.Parse("2021-04-01 08:00:00"))
|
||||
//)
|
||||
.HasJob<ProcessWhiVehicles>(options =>
|
||||
options.HasInterval(new TimeSpan(24, 0, 0))
|
||||
.StartsAt(DateTime.Today)
|
||||
.HasJob<BulkUpdateInventory>(options =>
|
||||
options.HasInterval(new TimeSpan(1, 0, 0))
|
||||
.StartsAt(DateTime.Today.AddHours(-27))
|
||||
)
|
||||
.HasJob<PartialInventoryUpdate>(options =>
|
||||
options.HasInterval(new TimeSpan(1, 0, 0))
|
||||
.StartsAt(DateTime.Today.AddHours(-27).AddMinutes(30))
|
||||
);
|
||||
|
||||
//.HasJob<PartialInventoryUpdate>(options => options.HasInterval(new TimeSpan(1, 0, 0))
|
||||
|
||||
@@ -85,8 +85,6 @@ namespace PartSource.Automation.Services
|
||||
|
||||
public string Download(string filename, string destination = null)
|
||||
{
|
||||
|
||||
|
||||
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{filename}"));
|
||||
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
||||
request.Method = WebRequestMethods.Ftp.DownloadFile;
|
||||
@@ -116,5 +114,15 @@ namespace PartSource.Automation.Services
|
||||
|
||||
return destination;
|
||||
}
|
||||
|
||||
public void Delete(string filename)
|
||||
{
|
||||
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{filename}"));
|
||||
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
||||
request.Method = WebRequestMethods.Ftp.DeleteFile;
|
||||
|
||||
|
||||
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"FitmentDatabase": "Data Source=localhost;Initial Catalog=WhiFitment;Integrated Security=true;TrustServerCertificate=True",
|
||||
//"FitmentDatabase": "Data Source=localhost;Initial Catalog=WhiFitment;Integrated Security=true;TrustServerCertificate=True",
|
||||
//"FitmentDatabase": "Data Source=localhost;Initial Catalog=WhiFitment;User ID=stageuser;Password=FXepK^cFYS|[H<;Encrypt=True;TrustServerCertificate=True;Connection Timeout=300",
|
||||
"FitmentDatabase": "Data Source=localhost;User ID=stageuser;Password=FXepK^cFYS|[H<;Connect Timeout=30;Encrypt=True;Trust Server Certificate=True;Application Intent=ReadWrite;Multi Subnet Failover=False",
|
||||
"PartSourceDatabase": "Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
|
||||
},
|
||||
"emailConfiguration": {
|
||||
|
||||
Reference in New Issue
Block a user