public async Task <ProgramByCSVOutputs> Handler(ProgramByCSVInputs args, ILambdaContext context) { if (this.store == null) { // Preload the dependencies (if they exist), // so that they are available during model deserialization. var asmLocation = this.GetType().Assembly.Location; var asmDir = Path.GetDirectoryName(asmLocation); var asmName = Path.GetFileNameWithoutExtension(asmLocation); var depPath = Path.Combine(asmDir, $"{asmName}.Dependencies.dll"); if (File.Exists(depPath)) { Console.WriteLine($"Loading dependencies from assembly: {depPath}..."); Assembly.LoadFrom(depPath); Console.WriteLine("Dependencies assembly loaded."); } this.store = new S3ModelStore <ProgramByCSVInputs>(RegionEndpoint.USWest1); } var l = new InvocationWrapper <ProgramByCSVInputs, ProgramByCSVOutputs>(store, ProgramByCSV.Execute); var output = await l.InvokeAsync(args); return(output); }
public async Task <ProgramByCSVOutputs> Handler(ProgramByCSVInputs args, ILambdaContext context) { // Preload dependencies (if they exist), // so that they are available during model deserialization. var sw = System.Diagnostics.Stopwatch.StartNew(); var asmLocation = this.GetType().Assembly.Location; var asmDir = Path.GetDirectoryName(asmLocation); // Explicitly load the dependencies project, it might have types // that aren't used in the function but are necessary for correct // deserialization. var asmName = Path.GetFileNameWithoutExtension(asmLocation); var depPath = Path.Combine(asmDir, $"{asmName}.Dependencies.dll"); if (File.Exists(depPath)) { Console.WriteLine($"Loading dependencies assembly from: {depPath}..."); Assembly.LoadFrom(depPath); Console.WriteLine("Dependencies assembly loaded."); } // Load all reference assemblies. Console.WriteLine($"Loading all referenced assemblies."); foreach (var asm in this.GetType().Assembly.GetReferencedAssemblies()) { try { Assembly.Load(asm); } catch (Exception e) { Console.WriteLine($"Failed to load {asm.FullName}"); Console.WriteLine(e.Message); } } sw.Stop(); Console.WriteLine($"Time to load assemblies: {sw.Elapsed.TotalSeconds})"); if (this.store == null) { this.store = new S3ModelStore <ProgramByCSVInputs>(RegionEndpoint.USWest1); } var l = new InvocationWrapper <ProgramByCSVInputs, ProgramByCSVOutputs>(store, ProgramByCSV.Execute); var output = await l.InvokeAsync(args); return(output); }
/// <summary> /// The ProgramByCSV function. /// </summary> /// <param name="model">The input model.</param> /// <param name="input">The arguments to the execution.</param> /// <returns>A ProgramByCSVOutputs instance containing computed results and the model with any new elements.</returns> public static ProgramByCSVOutputs Execute(Dictionary <string, Model> inputModels, ProgramByCSVInputs input) { var roomReader = new ProgramReader(input); var output = new ProgramByCSVOutputs(roomReader.RoomEntries.Count, roomReader.RoomQuantity, roomReader.TotalArea); foreach (var roomEntry in roomReader.RoomEntries) { output.Model.AddElement(new RoomDefinition(roomEntry.suiteName, roomEntry.suiteNumber, roomEntry.department, roomEntry.name, roomEntry.quantity, roomEntry.area, roomEntry.ratio, roomEntry.height, Guid.NewGuid(), roomEntry.name)); } return(output); }
public ProgramReader(ProgramByCSVInputs input) { RoomEntries = new List <RoomEntry>(); RoomQuantity = 0.0; TotalArea = 0.0; var lines = File.ReadAllText(input.Program.LocalFilePath).Split(new char[] { '\n', '\r' }); if (lines.Count() < 2) //Drop the process if only the header row or an empty file exists. { return; } var i = 1; foreach (var line in lines.Skip(1)) //skip assumed header row { var row = new List <string>(line.Trim().Split(',')); //Skip malformed rows or entries with quantities <= zero; if (row.Count() < 8 || !int.TryParse(row.ElementAt(4).ToString(), out int qVal) || qVal <= 0) { continue; } if (!double.TryParse(row.ElementAt(5).ToString(), out double aVal)) { throw new ArgumentException("Area in Row " + i.ToString() + " does not resolve to a double value."); } if (!double.TryParse(row.ElementAt(6).ToString(), out double rVal)) { throw new ArgumentException("Ratio in Row " + i.ToString() + " does not resolve to a double value."); } var height = 0.0; if (double.TryParse(row.ElementAt(7).ToString(), out double hVal)) { height = Math.Abs(hVal); } else { height = 4.0; } var roomEntry = new RoomEntry { suiteName = row.ElementAt(0).Trim(), suiteNumber = row.ElementAt(1).Trim(), department = row.ElementAt(2).Trim(), name = row.ElementAt(3).Trim() }; roomEntry.quantity = Math.Abs(qVal); roomEntry.area = Math.Abs(aVal); roomEntry.height = height; if (input.UseImperialUnits) //Convert from Imperial units if indicated. { roomEntry.area = Math.Round(roomEntry.area *= METRIC_FACTOR, 3); roomEntry.height = Math.Round(roomEntry.height *= METRIC_FACTOR, 3); } roomEntry.ratio = Math.Abs(rVal); RoomEntries.Add(roomEntry); RoomQuantity += roomEntry.quantity; TotalArea += roomEntry.area * roomEntry.quantity; i++; } RoomEntrySequence(input); }
private void RoomEntrySequence(ProgramByCSVInputs input) { var suiteIDs = new List <SuiteID>(); foreach (var roomEntry in RoomEntries) { suiteIDs.Add(new SuiteID { Name = roomEntry.suiteName, Number = roomEntry.suiteNumber }); } suiteIDs = suiteIDs.Distinct().ToList(); var suites = new List <SuiteEntry>(); foreach (var suiteID in suiteIDs) { var suiteRooms = new List <RoomEntry>(); var suiteArea = 0.0; foreach (var roomEntry in RoomEntries) { if (roomEntry.suiteName == suiteID.Name && roomEntry.suiteNumber == suiteID.Number) { suiteRooms.Add(roomEntry); suiteArea += roomEntry.area * roomEntry.quantity; } } if (input.RoomSequence == RoomSequence.Reverse) { suiteRooms.Reverse(); } if (input.RoomSequence == RoomSequence.AreaAscending) { suiteRooms = suiteRooms.OrderBy(r => r.area).ToList(); } if (input.RoomSequence == RoomSequence.AreaDescending) { suiteRooms = suiteRooms.OrderByDescending(r => r.area).ToList(); } suites.Add( new SuiteEntry { Name = suiteID.Name, Number = suiteID.Number, Rooms = suiteRooms, Area = suiteArea }); } if (input.SuiteSequence == SuiteSequence.Reverse) { suites.Reverse(); } if (input.SuiteSequence == SuiteSequence.AreaAscending) { suites = suites.OrderBy(s => s.Area).ToList(); } if (input.SuiteSequence == SuiteSequence.AreaDescending) { suites = suites.OrderByDescending(s => s.Area).ToList(); } if (input.SuiteSequence == SuiteSequence.NameAscending) { suites = suites.OrderBy(s => s.Name).ToList(); } if (input.SuiteSequence == SuiteSequence.NameDescending) { suites = suites.OrderByDescending(s => s.Name).ToList(); } if (input.SuiteSequence == SuiteSequence.NumberAscending) { suites = suites.OrderBy(s => s.Number).ToList(); } if (input.SuiteSequence == SuiteSequence.NumberDescending) { suites = suites.OrderByDescending(s => s.Number).ToList(); } RoomEntries.Clear(); foreach (var suite in suites) { foreach (var roomEntry in suite.Rooms) { RoomEntries.Add(roomEntry); } } }