Browse Source

Job Manufacturing Summary improvements and some template analysis performance improvements

Kenric Nugteren 2 years ago
parent
commit
ce768816dd

+ 24 - 20
prs.desktop/Dashboards/Manufacturing/ManufacturingTemplateAnalysis.xaml.cs

@@ -37,7 +37,7 @@ namespace PRSDesktop
         private readonly Dictionary<string, string> SectionDisplayNames = new() { { "Total", "Total" } };
 
         private ManufacturingSection[] sections;
-        private ManufacturingTemplateStage[] stages;
+        private Dictionary<Guid, ManufacturingTemplateStage[]> stages;
         private ManufacturingTemplate[] templates;
 
         public ManufacturingTemplateAnalysis()
@@ -77,10 +77,13 @@ namespace PRSDesktop
 
                 data.Rows.Clear();
 
-                templates = new Client<ManufacturingTemplate>().Load(
+                templates = new Client<ManufacturingTemplate>().Query(
                     new Filter<ManufacturingTemplate>(x => x.ID).IsNotEqualTo(Guid.Empty).TextSearch(_search, x => x.Code, x => x.Name),
+                    new Columns<ManufacturingTemplate>(x => x.ID)
+                        .Add(x => x.Name)
+                        .Add(x => x.Code),
                     new SortOrder<ManufacturingTemplate>(x => x.Code)
-                );
+                ).ToObjects<ManufacturingTemplate>().ToArray();
 
                 var packets = new Client<ManufacturingPacket>().Query(
                     new Filter<ManufacturingPacket>(x => x.Completed).IsGreaterThanOrEqualTo(_from).And(x => x.Completed).IsLessThan(_to.AddDays(1)),
@@ -90,7 +93,7 @@ namespace PRSDesktop
                         x => x.ManufacturingTemplateLink.ID,
                         x => x.Quantity
                     )
-                );
+                ).ToObjects<ManufacturingPacket>();
 
                 var history = new Client<ManufacturingHistory>().Query(
                     new Filter<ManufacturingHistory>(x => x.Packet.Completed).IsGreaterThanOrEqualTo(_from).And(x => x.Packet.Completed)
@@ -102,12 +105,17 @@ namespace PRSDesktop
                         x => x.QADuration,
                         x => x.WorkDuration
                     )
-                );
+                ).ToObjects<ManufacturingHistory>()
+                    .GroupBy(x => x.Packet.ID)
+                    .ToDictionary(x => x.Key, x => x.ToList());
 
                 foreach (var template in templates)
                 {
                     Progress.SetMessage(string.Format("Processing {0}: {1}", template.Code, template.Name));
 
+                    var templateStageSections = (stages.GetValueOrDefault(template.ID) ?? Enumerable.Empty<ManufacturingTemplateStage>())
+                        .Select(x => x.Section.ID).Where(x => sections.Any(y => x == y.ID));
+
                     var values = new List<object>();
                     values.Add(template.Code);
                     values.Add(template.Name);
@@ -117,24 +125,20 @@ namespace PRSDesktop
                         times[section.ID] = 0L;
 
                     var qty = 0;
-                    var pktrows = packets.Rows.Where(r => r.Get<ManufacturingPacket, Guid>(c => c.ManufacturingTemplateLink.ID).Equals(template.ID));
-                    foreach (var pktrow in pktrows)
+                    var pkts = packets.Where(p => p.ManufacturingTemplateLink.ID.Equals(template.ID));
+                    foreach (var p in pkts)
                     {
-                        var thisqty = pktrow.Get<ManufacturingPacket, int>(x => x.Quantity);
+                        var thisqty = p.Quantity;
                         if (thisqty > 0)
                         {
                             qty += thisqty;
-                            foreach (var section in sections)
-                                if (stages.Any(x => x.Template.ID.Equals(template.ID) && x.Section.ID.Equals(section.ID)))
-                                {
-                                    var pktid = pktrow.Get<ManufacturingPacket, Guid>(c => c.ID);
-                                    var histrows = history.Rows.Where(r =>
-                                        r.Get<ManufacturingHistory, Guid>(c => c.Section.ID).Equals(section.ID) &&
-                                        r.Get<ManufacturingHistory, Guid>(c => c.Packet.ID).Equals(pktid));
-                                    foreach (var histrow in histrows)
-                                        times[section.ID] += histrow.Get<ManufacturingHistory, TimeSpan>(c => c.QADuration).Ticks +
-                                                             histrow.Get<ManufacturingHistory, TimeSpan>(c => c.WorkDuration).Ticks;
-                                }
+
+                            if (history.TryGetValue(p.ID, out var histories))
+                            {
+                                foreach (var section in templateStageSections)
+                                    foreach (var hist in histories.Where(x => x.Section.ID == section))
+                                        times[section] += hist.QADuration.Ticks + hist.WorkDuration.Ticks;
+                            }
                         }
                     }
 
@@ -183,7 +187,7 @@ namespace PRSDesktop
                 new SortOrder<ManufacturingSection>(x => x.Factory.Sequence).ThenBy(x => x.Sequence)
             );
 
-            stages = new Client<ManufacturingTemplateStage>().Load();
+            stages = new Client<ManufacturingTemplateStage>().Load().GroupBy(x => x.Template.ID).ToDictionary(x => x.Key, x => x.ToArray());
 
             data = new DataTable();
             data.Columns.Add("Code", typeof(string));