Exemplo n.º 1
0
        /// <summary>
        /// Filters the provided data based on the request
        /// </summary>
        /// <param name="data">The retrieved data to filter</param>
        /// <param name="request">The filter settings</param>
        /// <returns>Whether the item should be filtered out</returns>
        private bool ShouldInclude(DashboardEventParsed data, GetDashboardEventsRequest request)
        {
            bool include = true;

            if (request != null && data != null)
            {
                // If an end time was specified, but if before the start,
                // it is possible for End to be 0 and a start time specified, so
                // don't throw for that case
                if (request.End > 0 && request.End < request.Start)
                {
                    throw new ArgumentException("The end date must be greater than or equal to the start.");
                }

                if (request.Services != null && request.Services.Any())
                {
                    include = request.Services.Contains(data.Service);

                    if (include == false)
                    {
                        return(false);
                    }
                }

                if (request.Regions != null && request.Regions.Any())
                {
                    include = request.Regions.Contains(data.Region);

                    if (include == false)
                    {
                        return(false);
                    }
                }

                // This comparison is against the posted Date property of the service health dashboard event, it is not comparing it to the beginning or
                // end dates,
                if (request.Start > 0)
                {
                    include = data.Start >= request.Start;

                    if (include == false)
                    {
                        return(false);
                    }
                }

                if (request.End > 0)
                {
                    include = data.End <= request.End;

                    if (include == false)
                    {
                        return(false);
                    }
                }
            }

            return(include);
        }
Exemplo n.º 2
0
        private APIGatewayProxyResponse CreateResponse(IEnumerable <DashboardEventParsed> data, GetDashboardEventsRequest request)
        {
            try
            {
                string body               = String.Empty;
                string contentType        = String.Empty;
                string contentDisposition = String.Empty;

                if (request.Output == "json")
                {
                    body        = JsonConvert.SerializeObject(data);
                    contentType = "application/json";
                }
                else //Otherwise it's csv
                {
                    StringBuilder buffer = new StringBuilder();

                    buffer.AppendLine(String.Join(",", typeof(DashboardEventParsed).GetTypeInfo().GetProperties().Select(x => "\"" + x.Name + "\"")));

                    foreach (DashboardEventParsed item in data)
                    {
                        buffer.AppendLine(String.Join(",", item.GetType().GetProperties().Select(x => "\"" + (x.Name == "MonthlyOutageDurations" ? JsonConvert.SerializeObject((Dictionary <string, long>)x.GetValue(item)).Replace("\"", "\"\"") : x.GetValue(item).ToString().Replace("\"", "\"\"")) + "\"")));
                    }

                    //Move back 2 to remove the \r\n from the last AppendLine
                    buffer.Length += -2;
                    body           = buffer.ToString();

                    contentType        = "application/octet-stream";
                    contentDisposition = "attachment; filename='serviceavailability.csv'";
                }

                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.OK,
                    Body = body,
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", contentType }, { "Content-Disposition", contentDisposition }, { "Access-Control-Allow-Origin", "*" }
                    }
                });
            }
            catch (Exception e)
            {
                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Body = JsonConvert.SerializeObject(e, new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    }),
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "application/json" }, { "Access-Control-Allow-Origin", "*" }
                    }
                });
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Method to respond to an API request and retrieve the data from DynamoDB with
        /// possible filters included
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <APIGatewayProxyResponse> GetData(APIGatewayProxyRequest request, ILambdaContext context)
        {
            this._context = context;
            context.LogInfo($"Get data request\r\n{JsonConvert.SerializeObject(request)}");

            try
            {
                GetDashboardEventsRequest req        = new GetDashboardEventsRequest(request.QueryStringParameters);
                List <ScanCondition>      conditions = new List <ScanCondition>();

                if (req.Start > 0)
                {
                    conditions.Add(new ScanCondition("Date", ScanOperator.GreaterThanOrEqual, ServiceUtilities.ConvertFromUnixTimestamp(req.Start)));
                }

                if (req.End > 0)
                {
                    conditions.Add(new ScanCondition("Date", ScanOperator.LessThanOrEqual, ServiceUtilities.ConvertFromUnixTimestamp(req.End)));
                }

                if (req.Regions != null && req.Regions.Any())
                {
                    conditions.Add(new ScanCondition("Region", ScanOperator.In, req.Regions.ToArray())); // Casting to Array is important
                }

                if (req.Services != null && req.Services.Any())
                {
                    conditions.Add(new ScanCondition("Service", ScanOperator.In, req.Services.ToArray())); // Casting to Array is important
                }

                AsyncSearch <DashboardEventParsed> search = ddbContext.ScanAsync <DashboardEventParsed>(conditions);
                IEnumerable <DashboardEventParsed> data   = await search.GetRemainingAsync();

                return(CreateResponse(data, req));
            }
            catch (AggregateException e)
            {
                this._context.LogError(e);

                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Body = FlattenToJsonString(e),
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "application/json" }, { "Access-Control-Allow-Origin", "*" }
                    }
                });
            }
            catch (Exception e)
            {
                this._context.LogError(e);

                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Body = JsonConvert.SerializeObject(e, new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                    }),
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "application/json" }, { "Access-Control-Allow-Origin", "*" }
                    }
                });
            }
        }