Natty
Natty
CC#
Created by Natty on 5/10/2024 in #help
Blazor HubConnectionBuilder Timeouts
Does this work for anyone?
hubConnection = new HubConnectionBuilder()
.WithUrl(Navigation.ToAbsoluteUri($"/chatroomhub"))
.WithServerTimeout(TimeSpan.FromSeconds(30))
.WithKeepAliveInterval(TimeSpan.FromSeconds(15))
.Build();
hubConnection = new HubConnectionBuilder()
.WithUrl(Navigation.ToAbsoluteUri($"/chatroomhub"))
.WithServerTimeout(TimeSpan.FromSeconds(30))
.WithKeepAliveInterval(TimeSpan.FromSeconds(15))
.Build();
I can navigate away from the page for minutes and nothing happens. Connection is never closed...
5 replies
CC#
Created by Natty on 2/17/2024 in #help
✅ 20/22 Test cases but getting TLE for last two.
https://leetcode.com/problems/lru-cache/description/?envType=list&envId=ep9peqp6
public class LRUCache {

private Dictionary<int, int> cache;
private LinkedList<int> lruTracker;
private readonly int capacity;

public LRUCache(int capacity) {
this.capacity = capacity;
cache = new Dictionary<int, int>(capacity);
lruTracker = new LinkedList<int>();
}

public int Get(int key) {
if (cache.TryGetValue(key, out int val)) {
lruTracker.Remove(key);
lruTracker.AddFirst(key);
return val;
}
else {
return -1;
}
}

public void Put(int key, int value) {

if (cache.ContainsKey(key)) {
cache[key] = value;
lruTracker.Remove(key);
lruTracker.AddFirst(key);
}
else {
if (cache.Count == capacity) {
int lruKey = lruTracker.Last.Value;
lruTracker.RemoveLast();
cache.Remove(lruKey);
}

cache.Add(key, value);
lruTracker.AddFirst(key);
}
}
}

/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.Get(key);
* obj.Put(key,value);
*/
public class LRUCache {

private Dictionary<int, int> cache;
private LinkedList<int> lruTracker;
private readonly int capacity;

public LRUCache(int capacity) {
this.capacity = capacity;
cache = new Dictionary<int, int>(capacity);
lruTracker = new LinkedList<int>();
}

public int Get(int key) {
if (cache.TryGetValue(key, out int val)) {
lruTracker.Remove(key);
lruTracker.AddFirst(key);
return val;
}
else {
return -1;
}
}

public void Put(int key, int value) {

if (cache.ContainsKey(key)) {
cache[key] = value;
lruTracker.Remove(key);
lruTracker.AddFirst(key);
}
else {
if (cache.Count == capacity) {
int lruKey = lruTracker.Last.Value;
lruTracker.RemoveLast();
cache.Remove(lruKey);
}

cache.Add(key, value);
lruTracker.AddFirst(key);
}
}
}

/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.Get(key);
* obj.Put(key,value);
*/
Not sure what else to optimize here. It's all O(1) calls to my knowledge...
2 replies
CC#
Created by Natty on 12/9/2023 in #help
HTTPS vs HTTP
No description
4 replies
CC#
Created by Natty on 11/25/2023 in #help
Code Review (Web Api)
Is this a proper set up for a web api project? If not, what should I change? https://pastebin.com/V4ky2YXX
36 replies
CC#
Created by Natty on 11/23/2023 in #help
How do I properly unit test a post request?
public interface IFacilityService
{
Task<Facility> CreateFacilityAsync(Facility facility);
}
public interface IFacilityService
{
Task<Facility> CreateFacilityAsync(Facility facility);
}
public async Task<Facility> CreateFacilityAsync(Facility facility)
{
_db.Facilities.Add(facility);
await _db.SaveChangesAsync();
return facility;
}
public async Task<Facility> CreateFacilityAsync(Facility facility)
{
_db.Facilities.Add(facility);
await _db.SaveChangesAsync();
return facility;
}
[HttpPost("facility")]
public async Task<IActionResult> CreateFacilityAsync([FromBody] Facility facilityRequest)
{
var createdFacility = await _facilityService.CreateFacilityAsync(facilityRequest);
return Ok(createdFacility);
}
[HttpPost("facility")]
public async Task<IActionResult> CreateFacilityAsync([FromBody] Facility facilityRequest)
{
var createdFacility = await _facilityService.CreateFacilityAsync(facilityRequest);
return Ok(createdFacility);
}
I am using Xunit and Nsubtitute, with EFC, and SQLite db.
14 replies
CC#
Created by Natty on 11/22/2023 in #help
Never seen before error message pop up.
No description
12 replies
CC#
Created by Natty on 8/8/2023 in #help
❔ swagger & performance profiler
I am trying to call an endpoint via swagger with performance profiler running, but it never gets any results. It looks like the app isn’t even running (yet the profiler does start). Is this an issue with Swagger?
3 replies
CC#
Created by Natty on 6/6/2023 in #help
❔ [FromBody] and async issues...
I have this code:
public DataFactoryController(AppIdealContext db)
{
_db = db;
_client = CreateClientAsync().GetAwaiter().GetResult();
}

[HttpPost("TriggerPipelineOne")]
public async Task<IActionResult> TriggerPipelineOneAsync([FromBody] PipelineOneUserSettingsRequest request)
{
var userSettings = ConvertUserSettingsObjectToDictionary(request);
string pipelineOne = Environment.GetEnvironmentVariable(PIPELINEONENAME);
return await TriggerPipelineAsync(pipelineOne, PipelineStatusOptions.StartedPipelineOne, userSettings);
}
public DataFactoryController(AppIdealContext db)
{
_db = db;
_client = CreateClientAsync().GetAwaiter().GetResult();
}

[HttpPost("TriggerPipelineOne")]
public async Task<IActionResult> TriggerPipelineOneAsync([FromBody] PipelineOneUserSettingsRequest request)
{
var userSettings = ConvertUserSettingsObjectToDictionary(request);
string pipelineOne = Environment.GetEnvironmentVariable(PIPELINEONENAME);
return await TriggerPipelineAsync(pipelineOne, PipelineStatusOptions.StartedPipelineOne, userSettings);
}
When I remove all the async stuff and make it void etc. then my FromBody works when deployed app in Azure environment. However, with the current code, I keep getting different JSON error messages in the console (even when using the same exact data each time). It seems like it's not getting the full data or something, like it's cut off at different parts, hence the different json error messages. Any ideas?
3 replies
CC#
Created by Natty on 1/20/2023 in #help
Multiple Users Concurrency Issue? EF6
49 replies
CC#
Created by Natty on 12/29/2022 in #help
Updating a property and saving it to DB (EF Core)
I have a List of objects that I queried from my db context. I’m trying to change one property on it, such as: responses.ForEach(r => r.Status = “Done”); Then I’m calling _db.SaveChanges(); I’m not seeing the status updated in the database, am I doing something wrong?
22 replies
CC#
Created by Natty on 12/16/2022 in #help
Clean up LINQ statements?
var cashFlowDates = db.CashFlows.Include(cash => cash.Cells).OrderByDescending(cash => cash.Id).FirstOrDefault().Cells.Where(cell => cell.Column == 0).Select(cell => cell.Value).Distinct();
var balanceDates = db.Balances.Include(bal => bal.Cells).OrderByDescending(bal => bal.Id).FirstOrDefault().Cells.Where(cell => cell.Column == 0).Select(cell => cell.Value).Distinct();
var performanceDates = db.Benchmarks.Include(bench => bench.Cells).OrderByDescending(bench => bench.Id).FirstOrDefault().Cells.Where(cell => cell.Column == 0).Select(cell => cell.Value).Distinct();
var allDates = cashFlowDates.Concat(balanceDates).Concat(performanceDates).Distinct();
var cashFlowDates = db.CashFlows.Include(cash => cash.Cells).OrderByDescending(cash => cash.Id).FirstOrDefault().Cells.Where(cell => cell.Column == 0).Select(cell => cell.Value).Distinct();
var balanceDates = db.Balances.Include(bal => bal.Cells).OrderByDescending(bal => bal.Id).FirstOrDefault().Cells.Where(cell => cell.Column == 0).Select(cell => cell.Value).Distinct();
var performanceDates = db.Benchmarks.Include(bench => bench.Cells).OrderByDescending(bench => bench.Id).FirstOrDefault().Cells.Where(cell => cell.Column == 0).Select(cell => cell.Value).Distinct();
var allDates = cashFlowDates.Concat(balanceDates).Concat(performanceDates).Distinct();
In an ideal situation I would just do the query on the Cells table, but that table gets updated with ALL the cells, each time a user uploads a file, and there wouldn't be a way to track which data they want to use then (couldn't remove the old data if it was wrong, etc). What I have here works great, but is there a way to clean up these queries? I.E. Anyway to shorten this? Do I need all the calls here? Does calling Distinct() on each query even make sense (is that slower/faster)? etc.
5 replies
CC#
Created by Natty on 12/15/2022 in #help
❔ LINQ Question - Thought I solved it...
I have a list of dates in month/day/year format, and I've created a hashset that holds ending quarter months:
var allDates = List(); // "1/1/2017, 5/23/2018, etc"
var quarterEndMonths = new HashSet<string> { "3", "6", "9", "12" };
var allDates = List(); // "1/1/2017, 5/23/2018, etc"
var quarterEndMonths = new HashSet<string> { "3", "6", "9", "12" };
My goal is to have a bool that checks if the allDates only contains Quarter months (3,6,9,12). Here's what I have so far:
bool onlyContainsQuarters = allDates.Any(x => quarterEndMonths.Any(prefix => x.StartsWith(prefix)));
bool onlyContainsQuarters = allDates.Any(x => quarterEndMonths.Any(prefix => x.StartsWith(prefix)));
36 replies
CC#
Created by Natty on 12/15/2022 in #help
Ez LINQ Noob Question
How can I make this return a true/false? Is there a way to clean this up without adding a .Count() > 0 call?
bool onlyContainsQuarters = allDates.Where(x => quarterEndMonths.Any(prefix => x.StartsWith(prefix)));
bool onlyContainsQuarters = allDates.Where(x => quarterEndMonths.Any(prefix => x.StartsWith(prefix)));
9 replies
CC#
Created by Natty on 12/14/2022 in #help
Ez LINQ Help
I am trying to get a list of Values from the Cells where the Columns table Number is equal to 0. I think I am close, but not quite there:
var cashFlowCells = db.CashFlows.Include(x => x.Cells).Where(x => x.Columns.FirstOrDefault().Number == 0).OrderByDescending(x => x.Id).FirstOrDefault().Cells.Select(x => x.Value).ToList();
var cashFlowCells = db.CashFlows.Include(x => x.Cells).Where(x => x.Columns.FirstOrDefault().Number == 0).OrderByDescending(x => x.Id).FirstOrDefault().Cells.Select(x => x.Value).ToList();
10 replies