-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update CosmosDB Samples #141
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.CosmosDB.Models | ||
{ | ||
public record CosmosDBListData( | ||
string id, | ||
List<string> value | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.CosmosDB.Models | ||
{ | ||
public record PubSubData( | ||
string id, | ||
string channel, | ||
string message, | ||
DateTime timestamp | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.CosmosDB.Models | ||
{ | ||
public record RedisData( | ||
string id, | ||
string key, | ||
string value, | ||
DateTime timestamp | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.Extensions.Logging; | ||
using StackExchange.Redis; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.CosmosDB.Models | ||
{ | ||
public class StreamData | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this not a |
||
{ | ||
public string id { get; set; } | ||
public Dictionary<string, string> values { get; set; } | ||
|
||
// Helper method to format stream message | ||
public static StreamData Format(StreamEntry entry, ILogger logger) | ||
{ | ||
logger.LogInformation("ID: {val}", entry.Id.ToString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we logging in a |
||
|
||
// Map each key value pair | ||
Dictionary<string, string> dict = entry.Values.ToDictionary(value => value.Name.ToString(), value => value.Value.ToString()); | ||
|
||
StreamData sampleItem = new StreamData { id = entry.Id, values = dict }; | ||
return sampleItem; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Microsoft.Extensions.Logging; | ||
using StackExchange.Redis; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.CosmosDB.Models | ||
{ | ||
public class StreamDataSingleDocument | ||
{ | ||
public string id { get; set; } | ||
public int maxlen { get; set; } | ||
public Dictionary<string, Dictionary<string, string>> messages { get; set; } | ||
|
||
public static StreamDataSingleDocument CreateNewEntry(StreamEntry entry, string streamName, ILogger logger) | ||
{ | ||
logger.LogInformation("Creating a new document for {val}. Inserting ID: {val} as the first entry", streamName, entry.Id.ToString()); | ||
|
||
// Map each key value pair | ||
Dictionary<string, string> dict = entry.Values.ToDictionary(value => value.Name.ToString(), value => value.Value.ToString()); | ||
|
||
// Create a new list of messages | ||
var list = new Dictionary<string, Dictionary<string, string>>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can simplify to |
||
list.Add(entry.Id.ToString(), dict); | ||
|
||
StreamDataSingleDocument data = new StreamDataSingleDocument { id = streamName, maxlen = 1000, messages = list }; | ||
return data; | ||
} | ||
|
||
public static StreamDataSingleDocument UpdateExistingEntry(StreamDataSingleDocument results, StreamEntry entry, ILogger logger) | ||
{ | ||
logger.LogInformation("Adding to {val} document. Inserting ID: {val} ", results.id, entry.Id.ToString()); | ||
|
||
// Map each key value pair | ||
Dictionary<string, string> dict = entry.Values.ToDictionary(value => value.Name.ToString(), value => value.Value.ToString()); | ||
|
||
// Update list of messages | ||
var list = results.messages; | ||
list.Add(entry.Id.ToString(), dict); | ||
|
||
if (list.Count > results.maxlen) | ||
{ | ||
string minKey = list.Keys.Min(); | ||
list.Remove(minKey); | ||
} | ||
|
||
StreamDataSingleDocument data = new StreamDataSingleDocument { id = results.id, maxlen = results.maxlen, messages = list }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can simplify to |
||
return data; | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should ideally start with capital letters by convention. This applies to all the models