Skip to content

Commit 0f6cd35

Browse files
fix code ql issues
1 parent 94fe09f commit 0f6cd35

10 files changed

Lines changed: 507 additions & 567 deletions

File tree

App/backend-api/Microsoft.GS.DPS.Host/API/KernelMemory/KernelMemory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ DPS.API.KernelMemory kernelMemory
200200

201201
if (status.RemainingSteps.Count == 0)
202202
{
203+
completeFlag = true;
203204
break;
204205
}
205206
var totalSteps = status.Steps.Count;

App/backend-api/Microsoft.GS.DPS.Host/API/UserInterface/UserInterface.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,7 @@ public static void AddAPIs(WebApplication app)
108108
{
109109
DPS.Storage.Document.Entities.Document result = await documents.GetDocument(DocumentId);
110110

111-
if (result == null)
112-
{
113-
return Results.NotFound();
114-
}
115-
else
116-
{
117-
return Results.Ok(result);
118-
}
111+
return result == null ? Results.NotFound() : Results.Ok(result);
119112
}
120113
)
121114
.DisableAntiforgery();
Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
1-
using System;
2-
using System.Threading.Tasks;
3-
using Azure.Core;
4-
using Azure.Identity;
5-
6-
namespace Microsoft.GS.DPSHost.Helpers
7-
{
8-
/// <summary>
9-
/// The Azure Credential Helper class
10-
/// </summary>
11-
public static class AzureCredentialHelper
12-
{
13-
/// <summary>
14-
/// Get the Azure Credentials based on the environment type
15-
/// </summary>
16-
/// <param name="clientId">The client Id in case of User assigned Managed identity</param>
17-
/// <returns>The Credential Object</returns>
18-
public static TokenCredential GetAzureCredential(string? clientId = null)
19-
{
20-
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
21-
22-
if (string.Equals(env, "Development", StringComparison.OrdinalIgnoreCase))
23-
{
24-
return new DefaultAzureCredential(); // CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
25-
}
26-
else
27-
{
28-
return clientId != null
29-
? new ManagedIdentityCredential(clientId)
30-
: new ManagedIdentityCredential();
31-
}
32-
}
33-
}
1+
using System;
2+
using System.Threading.Tasks;
3+
using Azure.Core;
4+
using Azure.Identity;
5+
6+
namespace Microsoft.GS.DPSHost.Helpers
7+
{
8+
/// <summary>
9+
/// The Azure Credential Helper class
10+
/// </summary>
11+
public static class AzureCredentialHelper
12+
{
13+
/// <summary>
14+
/// Get the Azure Credentials based on the environment type
15+
/// </summary>
16+
/// <param name="clientId">The client Id in case of User assigned Managed identity</param>
17+
/// <returns>The Credential Object</returns>
18+
public static TokenCredential GetAzureCredential(string? clientId = null)
19+
{
20+
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
21+
22+
// CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
23+
return string.Equals(env, "Development", StringComparison.OrdinalIgnoreCase)
24+
? new DefaultAzureCredential() // CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
25+
: (clientId != null ? new ManagedIdentityCredential(clientId) : new ManagedIdentityCredential());
26+
}
27+
}
3428
}

App/backend-api/Microsoft.GS.DPS/API/ChatHost/ChatHost.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,7 @@ Please feel free to ask me any questions related to those documents and contents
7474

7575
private async Task<ChatSession> makeNewSession(string? chatSessionId)
7676
{
77-
if(string.IsNullOrEmpty(chatSessionId))
78-
{
79-
this.sessionId = Guid.NewGuid().ToString();
80-
}
81-
else
82-
{
83-
this.sessionId = chatSessionId;
84-
}
77+
var sessionId = string.IsNullOrEmpty(chatSessionId) ? Guid.NewGuid().ToString() : chatSessionId;
8578

8679
//Create New Chat History
8780
this.chatHistory = new ChatHistory();
Lines changed: 83 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,83 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using System.Timers;
6-
using Microsoft.GS.DPS.Storage.Document;
7-
using Timers =System.Timers;
8-
9-
namespace Microsoft.GS.DPS.API.UserInterface
10-
{
11-
public class DataCacheManager
12-
{
13-
private readonly DocumentRepository _documentRepository;
14-
private Dictionary<string, List<string>> _keywordCache;
15-
private readonly Timers.Timer _cacheTimer;
16-
private readonly object _cacheLock = new object();
17-
18-
public DataCacheManager(DocumentRepository documentRepository)
19-
{
20-
_documentRepository = documentRepository;
21-
_keywordCache = new Dictionary<string, List<string>>();
22-
_cacheTimer = new Timers.Timer(5 * 60 * 1000); // 5 minutes
23-
_cacheTimer.Elapsed += async (sender, e) => await RefreshCacheAsync();
24-
_cacheTimer.Start();
25-
}
26-
27-
public async Task<Dictionary<string, List<string>>> GetConsolidatedKeywordsAsync()
28-
{
29-
if (_keywordCache.Count == 0)
30-
{
31-
await RefreshCacheAsync();
32-
}
33-
34-
lock (_cacheLock)
35-
{
36-
return new Dictionary<string, List<string>>(_keywordCache);
37-
}
38-
}
39-
40-
public async Task RefreshCacheAsync()
41-
{
42-
var consolidatedKeywords = new Dictionary<string, List<string>>();
43-
var documents = await _documentRepository.GetAllDocuments();
44-
45-
foreach (var document in documents)
46-
{
47-
if (document.Keywords != null)
48-
{
49-
foreach (var keywordDict in document.Keywords)
50-
{
51-
if (!consolidatedKeywords.ContainsKey(keywordDict.Key))
52-
{
53-
consolidatedKeywords[keywordDict.Key] = new List<string>();
54-
}
55-
56-
var values = keywordDict.Value.Split(',').Select(v => v.Trim()).ToArray();
57-
58-
foreach (var value in values)
59-
{
60-
if (!consolidatedKeywords[keywordDict.Key].Contains(value))
61-
{
62-
consolidatedKeywords[keywordDict.Key].Add(value);
63-
}
64-
}
65-
66-
consolidatedKeywords[keywordDict.Key] = consolidatedKeywords[keywordDict.Key].OrderBy(v => v).ToList();
67-
}
68-
}
69-
}
70-
71-
consolidatedKeywords = consolidatedKeywords.OrderBy(k => k.Key).ToDictionary(k => k.Key, v => v.Value);
72-
73-
lock (_cacheLock)
74-
{
75-
_keywordCache = consolidatedKeywords;
76-
}
77-
}
78-
79-
public void ManualRefresh()
80-
{
81-
_cacheTimer.Stop();
82-
_cacheTimer.Start();
83-
Task.Run(async () => await RefreshCacheAsync());
84-
}
85-
}
86-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using System.Timers;
6+
using Microsoft.GS.DPS.Storage.Document;
7+
using Timers =System.Timers;
8+
9+
namespace Microsoft.GS.DPS.API.UserInterface
10+
{
11+
public class DataCacheManager
12+
{
13+
private readonly DocumentRepository _documentRepository;
14+
private Dictionary<string, List<string>> _keywordCache;
15+
private readonly Timers.Timer _cacheTimer;
16+
private readonly object _cacheLock = new object();
17+
18+
public DataCacheManager(DocumentRepository documentRepository)
19+
{
20+
_documentRepository = documentRepository;
21+
_keywordCache = new Dictionary<string, List<string>>();
22+
_cacheTimer = new Timers.Timer(5 * 60 * 1000); // 5 minutes
23+
_cacheTimer.Elapsed += async (sender, e) => await RefreshCacheAsync();
24+
_cacheTimer.Start();
25+
}
26+
27+
public async Task<Dictionary<string, List<string>>> GetConsolidatedKeywordsAsync()
28+
{
29+
if (_keywordCache.Count == 0)
30+
{
31+
await RefreshCacheAsync();
32+
}
33+
34+
lock (_cacheLock)
35+
{
36+
return new Dictionary<string, List<string>>(_keywordCache);
37+
}
38+
}
39+
40+
public async Task RefreshCacheAsync()
41+
{
42+
var consolidatedKeywords = new Dictionary<string, List<string>>();
43+
var documents = await _documentRepository.GetAllDocuments();
44+
45+
foreach (var document in documents.Where(d => d.Keywords != null))
46+
{
47+
foreach (var keywordDict in document.Keywords)
48+
{
49+
if (!consolidatedKeywords.ContainsKey(keywordDict.Key))
50+
{
51+
consolidatedKeywords[keywordDict.Key] = new List<string>();
52+
}
53+
54+
var values = keywordDict.Value.Split(',').Select(v => v.Trim()).ToArray();
55+
56+
foreach (var value in values)
57+
{
58+
if (!consolidatedKeywords[keywordDict.Key].Contains(value))
59+
{
60+
consolidatedKeywords[keywordDict.Key].Add(value);
61+
}
62+
}
63+
64+
consolidatedKeywords[keywordDict.Key] = consolidatedKeywords[keywordDict.Key].OrderBy(v => v).ToList();
65+
}
66+
}
67+
68+
consolidatedKeywords = consolidatedKeywords.OrderBy(k => k.Key).ToDictionary(k => k.Key, v => v.Value);
69+
70+
lock (_cacheLock)
71+
{
72+
_keywordCache = consolidatedKeywords;
73+
}
74+
}
75+
76+
public void ManualRefresh()
77+
{
78+
_cacheTimer.Stop();
79+
_cacheTimer.Start();
80+
Task.Run(async () => await RefreshCacheAsync());
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)