W Sigma El Skibidi
W Sigma El Skibidi
CC#
Created by W Sigma El Skibidi on 3/9/2024 in #help
Does anyone have any suggestions to potentially improve and make this function work better?
I have a function that basically is supposed to look for gaps for a specified duration on a desired day. I'm looking for ways to improve this and I'm also open for suggestions if there's a completely better way to do this. Even if its some magical linq query.
List<TimeRange> FindAvailableGaps(List<TimeRange> existingBookings, DateTime desiredDate, TimeSpan desiredDuration)
{
// Define the full day range
var fullDayRange = new TimeRange(desiredDate, desiredDate.AddDays(1));

// Get the gaps between existing bookings
var bookedPeriods = existingBookings.OrderBy(b => b.Start).ToList();
var availableGaps = new List<TimeRange>();

if (bookedPeriods.Count > 0 && bookedPeriods[0].Start > fullDayRange.Start)
{
availableGaps.Add(new TimeRange(fullDayRange.Start, bookedPeriods[0].Start));
}

for (int i = 0; i < bookedPeriods.Count - 1; i++)
{
var gap = new TimeRange(bookedPeriods[i].End, bookedPeriods[i + 1].Start);
if (gap.Duration >= desiredDuration)
{
availableGaps.Add(gap);
}
}

if (bookedPeriods.Count > 0 && bookedPeriods.Last().End < fullDayRange.End)
{
availableGaps.Add(new TimeRange(bookedPeriods.Last().End, fullDayRange.End));
}

return availableGaps;
}
List<TimeRange> FindAvailableGaps(List<TimeRange> existingBookings, DateTime desiredDate, TimeSpan desiredDuration)
{
// Define the full day range
var fullDayRange = new TimeRange(desiredDate, desiredDate.AddDays(1));

// Get the gaps between existing bookings
var bookedPeriods = existingBookings.OrderBy(b => b.Start).ToList();
var availableGaps = new List<TimeRange>();

if (bookedPeriods.Count > 0 && bookedPeriods[0].Start > fullDayRange.Start)
{
availableGaps.Add(new TimeRange(fullDayRange.Start, bookedPeriods[0].Start));
}

for (int i = 0; i < bookedPeriods.Count - 1; i++)
{
var gap = new TimeRange(bookedPeriods[i].End, bookedPeriods[i + 1].Start);
if (gap.Duration >= desiredDuration)
{
availableGaps.Add(gap);
}
}

if (bookedPeriods.Count > 0 && bookedPeriods.Last().End < fullDayRange.End)
{
availableGaps.Add(new TimeRange(bookedPeriods.Last().End, fullDayRange.End));
}

return availableGaps;
}
I call the code using this and then also sort it by how close it is to the specified time that I am looking for.
// Find available gaps
var availableGaps = FindAvailableGaps(existingBookings, desiredDate, desiredDuration);

availableGaps = availableGaps.OrderBy(gap => Math.Abs(((decimal)(gap.Start.TimeOfDay.TotalMinutes + gap.End.TimeOfDay.TotalMinutes) / 2) -(decimal)desiredTime.TotalMinutes)).ToList();
// Find available gaps
var availableGaps = FindAvailableGaps(existingBookings, desiredDate, desiredDuration);

availableGaps = availableGaps.OrderBy(gap => Math.Abs(((decimal)(gap.Start.TimeOfDay.TotalMinutes + gap.End.TimeOfDay.TotalMinutes) / 2) -(decimal)desiredTime.TotalMinutes)).ToList();
I know for a fact there is probably a better way to do this and im open for suggestions. Thanks 🙂 I am using the TimePeriodLibrary.NET currently.
6 replies
CC#
Created by W Sigma El Skibidi on 3/6/2024 in #help
Im having an issue checking for collisions when comparing DateTime objects.
I have this function that's supposed to check for collisions with bookings. I am using TimeSpans and DateTime objects. I found a post on Stackoverflow that this...
private bool CheckBooking(List<Booking> sameDayBookings, DateTime newStart, DateTime newEnd)
{
Span<Booking> itemSpan = CollectionsMarshal.AsSpan(sameDayBookings);
ref var searchSpace = ref MemoryMarshal.GetReference(itemSpan);

for (var i = 0; i < itemSpan.Length; i++)
{
var item = Unsafe.Add(ref searchSpace, i);
var existingStart = item.DateTime;
var existingEnd = item.DateTime.Add(item.Duration);
bool overlap = newStart < existingEnd && existingStart < newEnd;

if (overlap)
{
return false;
}
}
return true;
}
private bool CheckBooking(List<Booking> sameDayBookings, DateTime newStart, DateTime newEnd)
{
Span<Booking> itemSpan = CollectionsMarshal.AsSpan(sameDayBookings);
ref var searchSpace = ref MemoryMarshal.GetReference(itemSpan);

for (var i = 0; i < itemSpan.Length; i++)
{
var item = Unsafe.Add(ref searchSpace, i);
var existingStart = item.DateTime;
var existingEnd = item.DateTime.Add(item.Duration);
bool overlap = newStart < existingEnd && existingStart < newEnd;

if (overlap)
{
return false;
}
}
return true;
}
When using this code during normal time periods from 12am to 11pm it works perfectly fine but when I do a check for a time thats supposed to go into a new day it doesnt take that into account. Im not sure what else I could add to this function to take that into account. For example if i have a booking that's at 11:30:00pm and the timespan is for an hour, it should in theory be booked until 12:30:00am. But for some reason when I submit a booking for 12am it returns true.
10 replies
CC#
Created by W Sigma El Skibidi on 3/3/2024 in #help
Hangfire retries jobs at any server
I have ran into an issue with hangfire that causes my retry jobs to file due to them running on a server that doesnt support it. Currently, I have 2 servers with 2 different queus specified. I dont use "default" queues since my system is separated. My issue is caused during chaos testing when jobs are supposed to fail and then ultimately retried. My issue is that when a job is supposed to retry, it doesnt go to the "queue" that I have specified. I'm just wondering if there's any way to specifiy in the annotations where to retry the failed batch jobs etc. Also, if there isn't an option, does Hangfire attempt to run it on a different server eventually?
11 replies
CC#
Created by W Sigma El Skibidi on 2/27/2024 in #help
Configuring LocalStack with AWS S3 dotnet web api
So im trying to setup AmazonS3 with one of my APIs, the endpoint essentially is supposed to upload an image to localstack thats inside a docker container. My issue is with the configuration. I was searching for a resolution and apparently AWS SDK automatically appends the bucket name alongside the url of the endpoint that its trying to interact with. The resolution I found online on Github Issues is to configure AmazonS3Config and set ForcePathStyle to true. The issue im having is that it's not available anymore and I'm not entirely sure how else to configure it in Program.cs. Im using dotnet 7, this is what i currently have in my startup.
// AWS S3 configuration
builder.Services.AddDefaultAWSOptions(builder.Configuration.GetAWSOptions());
builder.Services.AddAWSService<IAmazonS3>();
// AWS S3 configuration
builder.Services.AddDefaultAWSOptions(builder.Configuration.GetAWSOptions());
builder.Services.AddAWSService<IAmazonS3>();
The error im having is:
System.Net.Http.HttpRequestException: Name or service not known (prlnk.cdn.localstack-aws:4566)
System.Net.Http.HttpRequestException: Name or service not known (prlnk.cdn.localstack-aws:4566)
Essentailly the issue im having is with this controller that basically craps some files and is supposed to upload them to s3
public async Task<ParkingImage> UploadToParklinkS3(List<IFormFile> files)
{
const string bucketName = "prlnk.cdn";
var bucketExists = await Amazon.S3.Util.AmazonS3Util.DoesS3BucketExistV2Async(_amazonS3, bucketName);

if(!bucketExists)
{
try
{
var request = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = true,
};

await _amazonS3.PutBucketAsync(request);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
}
}
// more code....

}
public async Task<ParkingImage> UploadToParklinkS3(List<IFormFile> files)
{
const string bucketName = "prlnk.cdn";
var bucketExists = await Amazon.S3.Util.AmazonS3Util.DoesS3BucketExistV2Async(_amazonS3, bucketName);

if(!bucketExists)
{
try
{
var request = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = true,
};

await _amazonS3.PutBucketAsync(request);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
}
}
// more code....

}
2 replies