Esempio n. 1
0
        public static Option <Message> FromMessageStreamRecord(DynamoDBEvent.DynamodbStreamRecord streamRecord)
        {
            Message MapNewMessage(Dictionary <string, AttributeValue> attributes)
            {
                var message = new Message();

                if (attributes.TryGetValue(nameof(Content), out var content))
                {
                    message.Content = content.S;
                }

                if (attributes.TryGetValue(nameof(AuthorName), out var authorName))
                {
                    message.AuthorName = authorName.S;
                }

                return(message);
            }

            if (streamRecord.EventName == OperationType.INSERT)
            {
                return(MapNewMessage(streamRecord.Dynamodb.NewImage));
            }

            return(Option <Message> .None);
        }
        public static async Task Songlist_index__insert_songs__none_found()
        {
            // Arrange
            var record = new DynamoDBEvent.DynamodbStreamRecord {
                Dynamodb = new StreamRecord {
                    Keys = new Dictionary <string, AttributeValue> {
                        {
                            "word", new AttributeValue {
                                S = "stay"
                            }
                        }
                    },
                    NewImage = new Dictionary <string, AttributeValue> {
                        {
                            "search_title", new AttributeValue {
                                S = "stay"
                            }
                        },
                        {
                            "artist", new AttributeValue {
                                S = "ABC123"
                            }
                        },
                        {
                            "search_artist", new AttributeValue {
                                S = "abc123"
                            }
                        },
                        {
                            "title", new AttributeValue {
                                S = "Stay"
                            }
                        },
                        {
                            "song_number", new AttributeValue {
                                S = "123"
                            }
                        }
                    }
                },
                EventName = "INSERT"
            };
            Mock <IDynamodbDependencyProvider> dynamodbProvider = new Mock <IDynamodbDependencyProvider>(MockBehavior.Strict);

            dynamodbProvider.Setup(x => x.DynamodbGetItemAsync(It.Is <Dictionary <string, AttributeValue> >(y =>
                                                                                                            y.GetValueOrDefault("word").S == "stay"
                                                                                                            ))).Returns(Task.FromResult(new GetItemResponse()));

            dynamodbProvider.Setup(x => x.DynamodbUpdateItemAsync(
                                       It.IsAny <Dictionary <string, AttributeValue> >(),
                                       "SET songs = :n",
                                       It.IsAny <Dictionary <string, AttributeValue> >()
                                       )).Returns(Task.FromResult(new UpdateItemResponse()));

            var songListIndex = new Logic(dynamodbProvider.Object);

            // Act
            // Assert
            await songListIndex.HandleRequest(record);
        }
        public async Task <List <CompaniesMerging> > FunctionHandler(DynamoDBEvent input, ILambdaContext context)
        {
            Table table = Table.LoadTable(client, "RatingsByType");
            List <CompaniesMerging> books = new List <CompaniesMerging>();
            List <DynamoDBEvent.DynamodbStreamRecord> records = (List <DynamoDBEvent.DynamodbStreamRecord>)input.Records;

            #region Single Record
            if (records.Count > 0)
            {
                DynamoDBEvent.DynamodbStreamRecord record = records[0];
                if (record.EventName.Equals("INSERT"))
                {
                    Document         myDoc  = Document.FromAttributeMap(record.Dynamodb.NewImage);
                    CompaniesMerging myBook = JsonConvert.DeserializeObject <CompaniesMerging>(myDoc.ToJson());
                    var request             = new UpdateItemRequest
                    {
                        TableName = "RatingsByType",
                        Key       = new Dictionary <string, AttributeValue>
                        {
                            { "type", new AttributeValue {
                                  S = myBook.type
                              } }
                        },
                        AttributeUpdates = new Dictionary <string, AttributeValueUpdate>()
                        {
                            {
                                "count",
                                new AttributeValueUpdate {
                                    Action = "ADD", Value = new AttributeValue {
                                        N = "1"
                                    }
                                }
                            },
                            {
                                "totalRating",
                                new AttributeValueUpdate {
                                    Action = "ADD", Value = new AttributeValue {
                                        N = myBook.rating.ToString()
                                    }
                                }
                            },/*
                               * {
                               * "averageRating",
                               * new AttributeValueUpdate{ Action = "ADD", Value = new AttributeValue{ N = ""} }
                               * },  */
                        },
                    };
                    await client.UpdateItemAsync(request);
                }
            }
            #endregion
            return(books);
        }
Esempio n. 4
0
        private Task <SendMessageResponse> ProcessRecord(DynamoDBEvent.DynamodbStreamRecord record)
        {
            string json = Document.FromAttributeMap(record.Dynamodb.NewImage).ToJson();
            List <AttributeValue> events;

            // Be prepared for anomalous records
            try
            {
                events = record.Dynamodb.NewImage["events"].L;
            }
            catch (Exception)
            {
                Console.WriteLine($"Invalid record schema: {json}");
                return(default);
Esempio n. 5
0
        public async Task <List <Item> > FunctionHandler(DynamoDBEvent input, ILambdaContext context)
        {
            Table       table = Table.LoadTable(client, "Assignment5A");
            List <Item> items = new List <Item>();

            List <DynamoDBEvent.DynamodbStreamRecord> records = (List <DynamoDBEvent.DynamodbStreamRecord>)input.Records;

            if (records.Count > 0)
            {
                DynamoDBEvent.DynamodbStreamRecord record = records[0];
                if (record.EventName.Equals("INSERT"))
                {
                    Document myDoc   = Document.FromAttributeMap(record.Dynamodb.NewImage);
                    Item     myItem  = JsonConvert.DeserializeObject <Item>(myDoc.ToJson());
                    var      request = new UpdateItemRequest
                    {
                        TableName = "RatingsByType",
                        Key       = new Dictionary <string, AttributeValue>
                        {
                            { "type", new AttributeValue {
                                  S = myItem.type
                              } }
                        },
                        AttributeUpdates = new Dictionary <string, AttributeValueUpdate>()
                        {
                            {
                                "numRatings",
                                new AttributeValueUpdate {
                                    Action = "ADD", Value = new AttributeValue {
                                        N = "1"
                                    }
                                }
                            },
                            {
                                "averageRating",
                                new AttributeValueUpdate {
                                    Action = "ADD", Value = new AttributeValue {
                                        N = "1"
                                    }
                                }
                            },
                        },
                    };
                    await client.UpdateItemAsync(request);
                }
            }

            return(items);
        }
Esempio n. 6
0
        private MessageEvent CreateMessageEvent(DynamoDBEvent.DynamodbStreamRecord record)
        {
            MessageOutput message  = null;
            var           newImage = record.Dynamodb.NewImage;

            if (newImage.Count > 0)
            {
                message = new MessageOutput
                {
                    Author = newImage.ContainsKey("Author") ? newImage["Author"].S : "",
                    Id     = int.Parse(newImage["Id"].N),
                    Text   = newImage.ContainsKey("Text") ? newImage["Text"].S : "",
                    Time   = DateTime.Parse(newImage["Time"].S)
                };
            }

            var messageEvent = new MessageEvent
            {
                Keys = new MessageKeys
                {
                    Id   = int.Parse(record.Dynamodb.Keys["Id"].N),
                    Time = DateTime.Parse(record.Dynamodb.Keys["Time"].S)
                },
                Message = message
            };

            if (record.EventName == OperationType.INSERT)
            {
                messageEvent.Type = MessageEventType.INSERT;
            }

            if (record.EventName == OperationType.REMOVE)
            {
                messageEvent.Type = MessageEventType.REMOVE;
            }

            if (record.EventName == OperationType.MODIFY)
            {
                messageEvent.Type = MessageEventType.MODIFY;
            }

            return(messageEvent);
        }
Esempio n. 7
0
        public static Option <Message> FromUserStreamRecord(DynamoDBEvent.DynamodbStreamRecord streamRecord)
        {
            Message MapNewUserMessage(Dictionary <string, AttributeValue> attributes)
            {
                var message = new Message();

                if (attributes.TryGetValue(nameof(User.UserName), out var userName))
                {
                    message.Content = $"{userName.S} has joined.";
                }

                return(message);
            }

            Message MapDeletedUserMessage(Dictionary <string, AttributeValue> attributes)
            {
                var message = new Message();

                if (attributes.TryGetValue(nameof(User.UserName), out var userName))
                {
                    message.Content = $"{userName.S} has left.";
                }

                return(message);
            }

            if (streamRecord.EventName == OperationType.INSERT)
            {
                return(MapNewUserMessage(streamRecord.Dynamodb.NewImage));
            }

            if (streamRecord.EventName == OperationType.REMOVE)
            {
                return(MapDeletedUserMessage(streamRecord.Dynamodb.OldImage));
            }

            return(Option <Message> .None);
        }
Esempio n. 8
0
        public async Task <List <Item> > FunctionHandler(DynamoDBEvent input, ILambdaContext context)
        {
            Table       table = Table.LoadTable(client, "RatingsByType");
            List <Item> items = new List <Item>();
            List <DynamoDBEvent.DynamodbStreamRecord> records = (List <DynamoDBEvent.DynamodbStreamRecord>)input.Records;

            int    count      = 0;
            double currentAvg = 0;
            double total      = 0;


            if (records.Count > 0)
            {
                DynamoDBEvent.DynamodbStreamRecord record = records[0];
                if (record.EventName.Equals("INSERT"))
                {
                    Document myDoc  = Document.FromAttributeMap(record.Dynamodb.NewImage);
                    Item     myItem = JsonConvert.DeserializeObject <Item>(myDoc.ToJson());

                    string          type = myItem.type;
                    GetItemResponse res  = await client.GetItemAsync(tableName, new Dictionary <string, AttributeValue>
                    {
                        { "type", new AttributeValue {
                              S = type
                          } }
                    }
                                                                     );

                    Document myDoc1 = Document.FromAttributeMap(res.Item);
                    Stat     myStat = JsonConvert.DeserializeObject <Stat>(myDoc1.ToJson());
                    count      = myStat.count;
                    currentAvg = myStat.averageRating;
                    if (count == 0)
                    {
                        total += myItem.rating;
                    }
                    else
                    {
                        if (count >= 2)
                        {
                            total = (myItem.rating + (currentAvg * count)) / (count + 1);
                        }
                        else
                        {
                            total = (myItem.rating + currentAvg) / (count + 1);
                        }
                    }

                    var request = new UpdateItemRequest
                    {
                        TableName = "RatingsByType",
                        Key       = new Dictionary <string, AttributeValue>
                        {
                            { "type", new AttributeValue {
                                  S = myItem.type
                              } }
                        },
                        AttributeUpdates = new Dictionary <string, AttributeValueUpdate>()
                        {
                            {
                                "count",
                                new AttributeValueUpdate {
                                    Action = "ADD", Value = new AttributeValue {
                                        N = "1"
                                    }
                                }
                            },
                            {
                                "averageRating",
                                new AttributeValueUpdate {
                                    Action = "PUT", Value = new AttributeValue {
                                        N = total.ToString()
                                    }
                                }
                            },
                        },
                    };
                    await client.UpdateItemAsync(request);
                }
            }

            return(items);
        }
Esempio n. 9
0
        public void Setup()
        {
            // APIGatewayProxy
            _baseAPIGatewayProxyRequest = new APIGatewayProxyRequest
            {
                HttpMethod = "POST",
                Path       = "/test/path",
            };

            _baseAPIGatewayProxyResponse = new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
            };

            // ApplicationLoadBalancer
            var albRequestContext = new ApplicationLoadBalancerRequest.ALBRequestContext
            {
                Elb = new ApplicationLoadBalancerRequest.ElbInfo()
            };

            albRequestContext.Elb.TargetGroupArn = TestArn;
            _baseApplicationLoadBalancerRequest  = new ApplicationLoadBalancerRequest
            {
                HttpMethod     = "POST",
                Path           = "/test/path",
                RequestContext = albRequestContext,
            };

            _baseApplicationLoadBalancerResponse = new ApplicationLoadBalancerResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
            };

            // SQSEvent
            var sqsRecord = new SQSEvent.SQSMessage
            {
                EventSourceArn = TestArn
            };

            _baseSQSEvent = new SQSEvent
            {
                Records = new List <SQSEvent.SQSMessage> {
                    sqsRecord
                },
            };

            // SNSEvent
            var snsMessaage = new SNSEvent.SNSMessage()
            {
                Message = "Test Message",
            };
            var snsRecord = new SNSEvent.SNSRecord
            {
                EventSubscriptionArn = TestArn,
                Sns = snsMessaage
            };

            _baseSNSEvent = new SNSEvent
            {
                Records = new List <SNSEvent.SNSRecord> {
                    snsRecord
                },
            };

            // KinesisEvent
            var kinesisRecord = new KinesisEvent.KinesisEventRecord
            {
                EventSourceARN = TestArn
            };

            _baseKinesisEvent = new KinesisEvent
            {
                Records = new List <KinesisEvent.KinesisEventRecord> {
                    kinesisRecord
                },
            };

            // S3Event
            var s3Record = new Amazon.S3.Util.S3EventNotification.S3EventNotificationRecord
            {
                S3 = new Amazon.S3.Util.S3EventNotification.S3Entity
                {
                    Bucket = new Amazon.S3.Util.S3EventNotification.S3BucketEntity
                    {
                        Arn = TestArn
                    }
                }
            };

            _baseS3Event = new S3Event
            {
                Records = new List <Amazon.S3.Util.S3EventNotification.S3EventNotificationRecord> {
                    s3Record
                },
            };

            // DynamoDBEvent
            var dynamoDBRecord = new DynamoDBEvent.DynamodbStreamRecord
            {
                EventSourceArn = TestArn
            };

            _baseDynamoDBEvent = new DynamoDBEvent
            {
                Records = new List <DynamoDBEvent.DynamodbStreamRecord> {
                    dynamoDBRecord
                },
            };

            // KinesisFirehoseEvent
            _baseKinesisFirehoseEvent = new KinesisFirehoseEvent
            {
                DeliveryStreamArn = TestArn,
            };
        }
Esempio n. 10
0
        public async Task <List <CompanyItem> > FunctionHandler(DynamoDBEvent input, ILambdaContext context)
        {
            Table table = Table.LoadTable(client, "Assignment5");
            List <CompanyItem> items = new List <CompanyItem>();
            List <DynamoDBEvent.DynamodbStreamRecord> records = (List <DynamoDBEvent.DynamodbStreamRecord>)input.Records;

            if (records.Count > 0)
            {
                DynamoDBEvent.DynamodbStreamRecord record = records[0];
                if (record.EventName.Equals("INSERT"))
                {
                    Document    myDoc  = Document.FromAttributeMap(record.Dynamodb.NewImage);
                    CompanyItem myItem = JsonConvert.DeserializeObject <CompanyItem>(myDoc.ToJson());

                    //one of attempts at grabbing the count and rating to divide and get the average but had a hard time
                    //pulling the data out and working with it tried scans and getItemRequest

                    //var tableName = "RatingsByType";


                    //var request2 = new GetItemRequest
                    //{
                    //    TableName = tableName,
                    //    Key = new Dictionary<string, AttributeValue>() { { "type", new AttributeValue { S = myItem.type } } },
                    //    // Optional parameters.
                    //    ProjectionExpression = "count, rating",
                    //    ConsistentRead = true
                    //};
                    //var response = client.GetItemAsync(request2);

                    //var num = myItem.rating / Convert.ToInt32(response);

                    var request = new UpdateItemRequest
                    {
                        TableName = "RatingsByType",
                        Key       = new Dictionary <string, AttributeValue>
                        {
                            { "type", new AttributeValue {
                                  S = myItem.type
                              } }
                        },
                        AttributeUpdates = new Dictionary <string, AttributeValueUpdate>()
                        {
                            {
                                "count",
                                new AttributeValueUpdate {
                                    Action = "ADD", Value = new AttributeValue {
                                        N = "1"
                                    }
                                }
                            },

                            {
                                "rating",
                                new AttributeValueUpdate {
                                    Action = "ADD", Value = new AttributeValue {
                                        N = myItem.rating.ToString()
                                    }
                                }
                            },
                        },
                    };
                    await client.UpdateItemAsync(request);
                }
            }
            return(items);
        }
Esempio n. 11
0
        public async Task <List <Assignment5> > FunctionHandler(DynamoDBEvent input, ILambdaContext context)
        {
            Table table  = Table.LoadTable(client, "Assignment5");
            Table table2 = Table.LoadTable(client, "RatingsByType");
            List <Assignment5>   ratings  = new List <Assignment5>();
            List <RatingsByType> ratings2 = new List <RatingsByType>();
            List <DynamoDBEvent.DynamodbStreamRecord> records = (List <DynamoDBEvent.DynamodbStreamRecord>)input.Records;

            if (records.Count > 0)
            {
                DynamoDBEvent.DynamodbStreamRecord record = records[0];

                if (record.EventName.Equals("INSERT"))
                {
                    Document    doc         = Document.FromAttributeMap(record.Dynamodb.NewImage);
                    Assignment5 givenRating = JsonConvert.DeserializeObject <Assignment5>(doc.ToJson());

                    double average = givenRating.rating;

                    GetItemResponse res = await client.GetItemAsync("RatingsByType", new Dictionary <string, AttributeValue>
                    {
                        {
                            "type", new AttributeValue {
                                S = givenRating.type
                            }
                        }
                    }
                                                                    );

                    Document      getDoc = Document.FromAttributeMap(res.Item);
                    RatingsByType myItem = JsonConvert.DeserializeObject <RatingsByType>(getDoc.ToJson());



                    average = ((myItem.count * myItem.average) + givenRating.rating / (myItem.count + 1));

                    var request = new UpdateItemRequest
                    {
                        TableName = "RatingsByType",
                        Key       = new Dictionary <string, AttributeValue>
                        {
                            { "type", new AttributeValue {
                                  S = givenRating.type
                              } }
                        },

                        AttributeUpdates = new Dictionary <string, AttributeValueUpdate>()
                        {
                            {
                                "count",
                                new AttributeValueUpdate {
                                    Action = "ADD", Value = new AttributeValue {
                                        N = "1"
                                    }
                                }
                            },
                            {
                                "total",
                                new AttributeValueUpdate {
                                    Action = "ADD", Value = new AttributeValue {
                                        N = givenRating.rating.ToString()
                                    }
                                }
                            },
                            {
                                "average",
                                new AttributeValueUpdate {
                                    Action = "PUT", Value = new AttributeValue {
                                        N = average.ToString("0.#")
                                    }
                                }
                            }
                        },
                    };



                    await client.UpdateItemAsync(request);
                }
            }

            return(ratings);
        }
Esempio n. 12
0
        public async Task <List <Item> > FunctionHandler(DynamoDBEvent input, ILambdaContext context)
        {
            Table       table = Table.LoadTable(client, "RatingsByTypes");
            List <Item> items = new List <Item>();
            List <DynamoDBEvent.DynamodbStreamRecord> records = (List <DynamoDBEvent.DynamodbStreamRecord>)input.Records;

            if (records.Count > 0)
            {
                DynamoDBEvent.DynamodbStreamRecord record = records[0];
                if (record.EventName.Equals("INSERT"))
                {
                    Document myDoc  = Document.FromAttributeMap(record.Dynamodb.NewImage); //.Keys
                    Item     myItem = JsonConvert.DeserializeObject <Item>(myDoc.ToJson());


                    var request1 = new GetItemRequest
                    {
                        TableName = "RatingsByTypes",
                        Key       = new Dictionary <string, AttributeValue>()
                        {
                            { "types", new AttributeValue {
                                  S = myItem.type
                              } }
                        },
                        ProjectionExpression = "types, counts, avgRating",
                        ConsistentRead       = true
                    };
                    var response1 = await client.GetItemAsync(request1);

                    Document   doc    = Document.FromAttributeMap(response1.Item);
                    typeRating myType = JsonConvert.DeserializeObject <typeRating>(doc.ToJson());

                    //var attributeList = response1.Item;

                    double newRating = 0;
                    if (myItem.company == "B")
                    {
                        newRating = myItem.rating / 2;
                    }
                    else
                    {
                        newRating = myItem.rating;
                    }

                    double myRating = 0;
                    if (myType.avgRating != null)
                    {
                        myRating = Double.Parse(myType.avgRating);
                    }
                    double myCounts = 0;
                    if (myType.avgRating != null)
                    {
                        myCounts = Double.Parse(myType.counts);
                    }

                    double averageRating = ((myRating * myCounts) + newRating) / (myCounts + 1);

                    Console.WriteLine("type: " + myType.types);
                    Console.WriteLine("count: " + myType.counts);
                    Console.WriteLine("avgRating: " + myType.avgRating);
                    Console.WriteLine("new avgRating: " + averageRating.ToString());

                    var request = new UpdateItemRequest
                    {
                        TableName = "RatingsByTypes",
                        Key       = new Dictionary <string, AttributeValue>
                        {
                            { "types", new AttributeValue {
                                  S = myItem.type
                              } }
                        },
                        AttributeUpdates = new Dictionary <string, AttributeValueUpdate>()
                        {
                            {
                                "counts",
                                new AttributeValueUpdate {
                                    Action = "ADD", Value = new AttributeValue {
                                        N = "1"
                                    }
                                }
                            },
                            {
                                "avgRating",
                                new AttributeValueUpdate {
                                    Action = "PUT", Value = new AttributeValue {
                                        N = Math.Round(averageRating, 2).ToString()
                                    }
                                }
                            },
                        },
                    };
                    await client.UpdateItemAsync(request);
                }
            }
            return(items);
        }
        public async Task ProcessStreamRecordAsync(DynamoDBEvent.DynamodbStreamRecord record, ILambdaContext context)
        {
            context.Logger.LogLine($"Process Stream Match result keys:{Document.FromAttributeMap(record.Dynamodb.Keys).ToJson()}.");

            if (record.EventName != OperationType.MODIFY)
            {
                context.Logger.LogLine($"Process Stream {record.EventName}.");
                return;
            }

            context.Logger.LogLine($"Process Stream Match old:{Document.FromAttributeMap(record.Dynamodb.OldImage).ToJson()}.");
            context.Logger.LogLine($"Process Stream Match new:{Document.FromAttributeMap(record.Dynamodb.NewImage).ToJson()}.");

            var oldResult = JsonConvert.DeserializeObject <DataRepository.DataEntities.Match>(Document.FromAttributeMap(record.Dynamodb.OldImage).ToJson());
            var newResult = JsonConvert.DeserializeObject <DataRepository.DataEntities.Match>(Document.FromAttributeMap(record.Dynamodb.NewImage).ToJson());


            var homeStats = await _teamsService.GetTeamSeasonStatsAsync(oldResult.HomeTeamId, oldResult.Year, oldResult.Season);

            var awayStats = await _teamsService.GetTeamSeasonStatsAsync(oldResult.AwayTeamId, oldResult.Year, oldResult.Season);

            if (homeStats == null)
            {
                context.Logger.LogLine($"Team stats not found for team {oldResult.HomeTeamId} processing {newResult.Year}#{newResult.Season}#{newResult.Round}#{newResult.MatchId}. \n Stats will be generated.");
                homeStats = await _teamsService.AddTeamStatsAsync(oldResult.HomeTeamId,
                                                                  oldResult.Year, oldResult.Season, oldResult.HomeTeamName);
            }

            if (awayStats == null)
            {
                context.Logger.LogLine($"Team stats not found for team {oldResult.AwayTeamId} processing {newResult.Year}#{newResult.Season}#{newResult.Round}#{newResult.MatchId}. \n Stats will be generated.");
                awayStats = await _teamsService.AddTeamStatsAsync(oldResult.AwayTeamId,
                                                                  oldResult.Year, oldResult.Season, oldResult.AwayTeamName);
            }

            if (!(await ValidateMatchAsync(context.Logger, oldResult, newResult, homeStats, awayStats)))
            {
                throw new DataException("Unable to process record, invalid operation for match.");
            }

            SetYearSeason(newResult.Year, newResult.Season);


            if (!oldResult.WasPlayed && newResult.WasPlayed) //just add points
            {
                context.Logger.LogLine($"Set new result {newResult.MatchId}.");
                SetNewResult(newResult, homeStats, awayStats);
                await _teamsService.UpdateTeamStatsAsync(newResult.HomeTeamId, newResult.Year, newResult.Season, homeStats);

                await _teamsService.UpdateTeamStatsAsync(newResult.AwayTeamId, newResult.Year, newResult.Season, awayStats);
            }
            else if (oldResult.WasPlayed && newResult.WasPlayed)//first remove points then add
            {
                context.Logger.LogLine($"Replace result {newResult.MatchId}.");
                RemoveOldResult(oldResult, homeStats, awayStats);
                SetNewResult(newResult, homeStats, awayStats);
                await _teamsService.UpdateTeamStatsAsync(newResult.HomeTeamId, newResult.Year, newResult.Season, homeStats);

                await _teamsService.UpdateTeamStatsAsync(newResult.AwayTeamId, newResult.Year, newResult.Season, awayStats);
            }
            else if (oldResult.WasPlayed && !newResult.WasPlayed) //remove points
            {
                context.Logger.LogLine($"Remove result {newResult.MatchId}.");
                RemoveOldResult(oldResult, homeStats, awayStats);
                await _teamsService.UpdateTeamStatsAsync(newResult.HomeTeamId, newResult.Year, newResult.Season, homeStats);

                await _teamsService.UpdateTeamStatsAsync(newResult.AwayTeamId, newResult.Year, newResult.Season, awayStats);
            }
            else //do nothing, no change
            {
                context.Logger.LogLine($"No change in result {newResult.MatchId}.");
            }
        }
Esempio n. 14
0
        private async Task ProcessRecordAsync(DynamoDBEvent.DynamodbStreamRecord record, ILambdaContext context)
        {
            if (record.EventName == OperationType.MODIFY)
            {
                var newImage          = record.Dynamodb.NewImage;
                var messagesProcessed = int.Parse(newImage["MessageCount"].N);
                if (!newImage.ContainsKey("ElapsedTime") && newImage.ContainsKey("MessagesSent") && messagesProcessed >= int.Parse(newImage["MessagesSent"].N))
                {
                    // This means we're done processing all messages. Calculate the elapsed time.
                    var endTime           = DateTimeOffset.Parse(newImage["EndTime"].S);
                    var startTime         = DateTimeOffset.Parse(newImage["StartTime"].S);
                    var elapsed           = endTime - startTime;
                    var secondsPerMessage = elapsed.TotalSeconds / messagesProcessed;
                    await amazonDynamoDB.UpdateItemAsync(new UpdateItemRequest
                    {
                        TableName = "message-processor",
                        Key       = record.Dynamodb.Keys,
                        ExpressionAttributeValues = new Dictionary <string, AttributeValue>
                        {
                            { ":elapsed", new AttributeValue {
                                  N = elapsed.TotalMilliseconds.ToString()
                              } },
                            { ":rate", new AttributeValue {
                                  N = secondsPerMessage.ToString()
                              } },
                        },
                        UpdateExpression = "SET ElapsedTime = :elapsed, SecondsPerMessage = :rate"
                    });

                    context.Logger.LogLine($"Processing finished. Waiting to get CloudWatch stats.");
                    await Task.Delay(1000 * 60 * 2); // Wait a couple minutes for CloudWatch stats to be aggregated

                    var functionName = newImage["pk"].S.Contains("Sqs") ? "MessageProcessor-SqsProcessor" : "MessageProcessor-SnsProcessor";
                    var stats        = await GetStatisticsAsync(functionName, startTime, DateTimeOffset.UtcNow);

                    context.Logger.LogLine($"Stats: {JsonSerializer.Serialize(stats)}");

                    await amazonDynamoDB.UpdateItemAsync(new UpdateItemRequest
                    {
                        TableName = "message-processor",
                        Key       = record.Dynamodb.Keys,
                        ExpressionAttributeNames = new Dictionary <string, string>
                        {
                            { "#duration", "Duration" }
                        },
                        ExpressionAttributeValues = new Dictionary <string, AttributeValue>
                        {
                            { ":concurrent", new AttributeValue {
                                  N = stats["ConcurrentExecutionsMax"].ToString()
                              } },
                            { ":duration", new AttributeValue {
                                  N = stats["DurationAverage"].ToString()
                              } },
                            { ":invocations", new AttributeValue {
                                  N = stats["InvocationsSum"].ToString()
                              } },
                        },
                        UpdateExpression = "SET ConcurrentExecutions = :concurrent, #duration = :duration, Invocations = :invocations"
                    });
                }
            }
        }
Esempio n. 15
0
        //--- Methods ---
        public async Task HandleRequest(DynamoDBEvent.DynamodbStreamRecord dynamodbStreamRecord)
        {
            var action      = dynamodbStreamRecord.EventName.Value;
            var requestItem = new Dictionary <string, AttributeValue>();

            LambdaLogger.Log($"Action: {action}");
            switch (action)
            {
            case "INSERT": {
                requestItem = dynamodbStreamRecord.Dynamodb.NewImage;
                break;
            }

            case "REMOVE": {
                requestItem = dynamodbStreamRecord.Dynamodb.OldImage;
                break;
            }
            }

            LambdaLogger.Log($"Request Item: {JsonConvert.SerializeObject(requestItem)}");
            requestItem.TryGetValue("song_number", out var number);
            requestItem.TryGetValue("artist", out var artist);
            requestItem.TryGetValue("title", out var title);

            // song to remove from index
            var songItem = new SongModel.Song {
                Artist     = artist.S,
                Title      = title.S,
                SongNumber = number.S
            };

            LambdaLogger.Log($"INDEXED SONG ITEM: {JsonConvert.SerializeObject(songItem)}");

            var splitSongTitle = title.S.Split(" ");

            foreach (var wordFromList in splitSongTitle)
            {
                var word = wordFromList.ToLowerInvariant();

                // key
                var recordKey = new Dictionary <string, AttributeValue> {
                    {
                        "word", new AttributeValue {
                            S = word
                        }
                    }
                };

                // Find existing songs in database for that word
                var existingSongsInDb = await GetExistingSongs(recordKey).ConfigureAwait(false);

                LambdaLogger.Log($"EXISTING SONGS IN DATABASE: {JsonConvert.SerializeObject(existingSongsInDb)}");

                var songIndexInExistingSongs = GetIndexOfThisSong(existingSongsInDb, songItem);
                LambdaLogger.Log($"songIndexInExistingSongs: {songIndexInExistingSongs}");

                switch (action)
                {
                case "INSERT": {
                    LambdaLogger.Log($"INDEXING WORD: {word}");
                    await InsertSong(existingSongsInDb, songIndexInExistingSongs, recordKey, songItem).ConfigureAwait(false);
                }
                break;

                case "REMOVE": {
                    LambdaLogger.Log($"INDEXING WORD: {word}");
                    await DeleteSong(existingSongsInDb, songIndexInExistingSongs, recordKey).ConfigureAwait(false);
                }
                break;
                }
            }
        }