Skip to content

Update CosmosDB Samples#141

Open
mapalan wants to merge 3 commits intomainfrom
mapalan/UpdateCosmosSamples
Open

Update CosmosDB Samples#141
mapalan wants to merge 3 commits intomainfrom
mapalan/UpdateCosmosSamples

Conversation

@mapalan
Copy link
Copy Markdown
Contributor

@mapalan mapalan commented Jan 17, 2024

Updating the samples to be more in line with the rest of the samples.


namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.CosmosDB.Models
{
public class StreamData
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not a record like PubSubData and RedisData?

// Helper method to format stream message
public static StreamData Format(StreamEntry entry, ILogger logger)
{
logger.LogInformation("ID: {val}", entry.Id.ToString());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we logging in a Format method? Is this best practice?

namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.CosmosDB.Models
{
public record CosmosDBListData(
string id,
Copy link
Copy Markdown

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

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>>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can simplify to var list = new Dictionary<string, Dictionary<string, string>> { { entry.Id.ToString(), dict } };

list.Remove(minKey);
}

StreamDataSingleDocument data = new StreamDataSingleDocument { id = results.id, maxlen = results.maxlen, messages = list };
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can simplify to new()

// Format the key/value pairs
foreach (KeyValuePair<string, string> entry in document.values)
{
values[i++] = new NameValueEntry(entry.Key, entry.Value);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be a better way to do this, no?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe create a list first then convert to array?

if (cosmosData == null || cosmosData.Count <= 0) return;

IDatabaseAsync redisDb = s_redisConnection.Value.GetDatabase();
//for each item upladed to cosmos, write it to Redis
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spelling again


resultsHolder.Add(listEntry);
CosmosDBListData newEntry = new CosmosDBListData(id: ListKey, value: resultsHolder);
await db.UpsertItemAsync<CosmosDBListData>(newEntry);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove <CosmosDBListData>

timestamp: DateTime.UtcNow
);

logger.LogInformation($"Key: \"{newKey}\", Value: \"{cosmosDBOut.value}\" addedd to Cosmos DB container: \"{Environment.GetEnvironmentVariable(ContainerSetting.Replace("%",""))}\" at id: \"{cosmosDBOut.id}\"");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spelling


// Map each key value pair
Dictionary<string, string> dict = RedisUtilities.StreamEntryToDictionary(entry);
Dictionary<string, string> dict = entry.Values.ToDictionary(value => value.Name.ToString(), value => value.Value.ToString());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we not using this method anymore?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants