public static DashboardEventParsed FromRawEvent(DashboardEventRaw rawEvent) { try { DashboardEventParsed parsed = new DashboardEventParsed(); parsed.Service = rawEvent.GetServiceShortName(); parsed.Region = rawEvent.GetRegion(); parsed.Timeline = EventTimelineUtilities.GetEventTimeline(rawEvent); if (Int32.TryParse(rawEvent.Status, out int status)) { parsed.Status = (DashboardEventStatus)status; } else { parsed.Status = 0; } if (parsed.Region.Equals("global")) { // If the returned region was global, it means the listed service name // didn't contain a region element, like ec2-us-east-1, but it may not // be an expected global service, like resourcegroups if (!Config.Instance.GlobalServices.Contains(parsed.Service)) { parsed.Region = parsed.Service; } } parsed.Description = parsed.GetDescriptionStringFromUpdates(); parsed.Summary = rawEvent.Summary; parsed.Date = Int64.Parse(rawEvent.Date); parsed.Start = parsed.Timeline.Start == default(DateTime) ? parsed.Date : ServiceUtilities.ConvertToUnixTimestamp(parsed.Timeline.Start); parsed.End = parsed.Timeline.End == default(DateTime) ? parsed.Date : ServiceUtilities.ConvertToUnixTimestamp(parsed.Timeline.End); return(parsed); } catch (Exception e) { throw new Exception($"Could not parse event:\r\n{JsonConvert.SerializeObject(rawEvent)}", e); } }
/// <summary> /// Gets the region of the event /// </summary> /// <returns></returns> public string GetRegion() { return(ServiceUtilities.GetRegion(this.Service)); }
/// <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", "*" } } }); } }
/// <summary> /// Gets the short name of the service like ec2, kms, s3, etc. /// </summary> /// <returns></returns> public string GetServiceShortName() { return(ServiceUtilities.GetServiceName(this.Service)); }