public List<NullableFact> CalculateRoi(Request request) { var products = new Hierarchy(request.ProductHierarchy); var geographies = new Hierarchy(request.GeographyHierarchy); var causals = new Hierarchy(request.SalesComponentHierarchy); var periods = new Hierarchy(request.PeriodHierarchy); var adjuster = new Adjuster(products, geographies, causals, periods); var sales = adjuster.AdjustSales(request); var aggregatedDuetos = adjuster.AggregateSales(request.Spend, sales); var rois = request.Spend.Join(aggregatedDuetos, s => new { s.ProductId, s.GeographyId, CausalId = s.SalesComponentId, s.TimeId }, adt => new { adt.ProductId, adt.GeographyId, CausalId = adt.SalesComponentId, adt.TimeId }, (s, adt) => new NullableFact() { ProductId = s.ProductId, GeographyId = s.GeographyId, SalesComponentId = s.SalesComponentId, TimeId = s.TimeId, Value = adt.Value / s.Value }); return rois.ToList(); }
public void SingleProdGeog_Test() { short productId = 7; short geographyId = 11; short timeId = 13; var facts = new List<NullableFact>(); facts.Add(new NullableFact() { ProductId = productId, GeographyId = geographyId, SalesComponentId = 2, TimeId = timeId, Value = 100 }); facts.Add(new NullableFact() { ProductId = productId, GeographyId = geographyId, SalesComponentId = 3, TimeId = timeId, Value = 100 }); var relations = new List<Relation>(); relations.Add(new Relation() { Child = 2, Parent = 1 }); relations.Add(new Relation() { Child = 3, Parent = 1 }); var hierarchy = new Hierarchy(relations); var aggregator = new Aggregator(hierarchy); var result = aggregator.Aggregate(facts, 1); Assert.AreEqual(1, result.Count()); Assert.AreEqual(result[0].ProductId, productId); Assert.AreEqual(result[0].GeographyId, geographyId); Assert.AreEqual(result[0].TimeId, timeId); Assert.AreEqual(result[0].SalesComponentId, (short)1); Assert.AreEqual(result[0].Value, 200); }
public void TwoElementHierarchy_GetChildren() { short id1 = 1; short id2 = 2; short id3 = 3; var items = new List<Relation>(); items.Add(new Relation { Child = id1, Parent = id2 }); items.Add(new Relation { Child = id2, Parent = id3 }); var result = new Hierarchy(items); var children1 = result.GetChildren(id1); Assert.AreEqual(1, children1.Count()); Assert.IsTrue(children1.Contains(id1)); var children2 = result.GetChildren(id2); Assert.AreEqual(2, children2.Count()); Assert.IsTrue(children2.Contains(id1)); Assert.IsTrue(children2.Contains(id2)); var children3 = result.GetChildren(id3); Assert.AreEqual(3, children3.Count()); Assert.IsTrue(children3.Contains(id1)); Assert.IsTrue(children3.Contains(id2)); Assert.IsTrue(children3.Contains(id3)); }
public void NullParameterTest() { var hierarchy = new Hierarchy(null); Assert.IsNotNull(hierarchy); Assert.AreEqual(1, hierarchy.GetChildren(1).Single()); Assert.AreEqual(1, hierarchy.GetParents(1).Single()); }
public List<NullableFact> AggregateFactsBySalesComponent(List<NullableFact> facts, List<Relation> salesComponentRelations) { var hierarchy = new Hierarchy(salesComponentRelations); var aggregator = new Aggregator(hierarchy); var results = new List<NullableFact>(); foreach (var parent in salesComponentRelations.Select(s => s.Parent).Distinct()) { results.AddRange(aggregator.Aggregate(facts, parent)); } facts.AddRange(results); return facts; }
public void Empty_Test() { short productId = 7; short geographyId = 11; short timeId = 13; var facts = new List<NullableFact>(); facts.Add(new NullableFact() { ProductId = productId, GeographyId = geographyId, SalesComponentId = 2, TimeId = timeId, Value = 100 }); facts.Add(new NullableFact() { ProductId = productId, GeographyId = geographyId, SalesComponentId = 3, TimeId = timeId, Value = 100 }); var relations = new List<Relation>(); var hierarchy = new Hierarchy(relations); var aggregator = new Aggregator(hierarchy); var result = aggregator.Aggregate(facts, 1); Assert.AreEqual(0, result.Count()); }
public void SingleElement_GetParents() { short id1 = 1; short id2 = 2; var items = new List<Relation>(); items.Add(new Relation { Child = id1, Parent = id2 }); var result = new Hierarchy(items); var parents = result.GetParents(id1); Assert.AreEqual(2, parents.Count()); Assert.IsTrue(parents.Contains(id1)); Assert.IsTrue(parents.Contains(id2)); var parents0 = result.GetParents(id2); Assert.AreEqual(1, parents0.Count()); Assert.IsTrue(parents0.Contains(id2)); }
public void ComplexHierarchy_TwoLevelTree() { var items = new List<Relation>(); items.Add(new Relation{ Child = i2, Parent = i1}); items.Add(new Relation{ Child = i3, Parent = i2}); items.Add(new Relation{ Child = i4, Parent = i2}); items.Add(new Relation{ Child = i5, Parent = i1}); items.Add(new Relation{ Child = i6, Parent = i5}); items.Add(new Relation{ Child = i7, Parent = i5}); var result = new Hierarchy(items); this.AssertHelper(new List<short> { i1 }, result.GetParents(i1)); this.AssertHelper(new List<short> { i1, i2, i3, i4, i5, i6, i7 }, result.GetChildren(i1)); this.AssertHelper(new List<short> { i1, i2 }, result.GetParents(i2)); this.AssertHelper(new List<short> { i2, i3, i4 }, result.GetChildren(i2)); this.AssertHelper(new List<short> { i1, i2, i3 }, result.GetParents(i3)); this.AssertHelper(new List<short> { i3 }, result.GetChildren(i3)); }
public void AllItemsPoshortDown_GetParents() { var items = new List<Relation>(); items.Add(new Relation { Child = i1, Parent = i3 }); items.Add(new Relation { Child = i1, Parent = i4 }); items.Add(new Relation { Child = i2, Parent = i3 }); items.Add(new Relation { Child = i2, Parent = i4 }); items.Add(new Relation { Child = i1, Parent = i5 }); items.Add(new Relation { Child = i2, Parent = i5 }); items.Add(new Relation { Child = i3, Parent = i5 }); items.Add(new Relation { Child = i4, Parent = i5 }); var result = new Hierarchy(items); this.AssertHelper(new List<short> { i1, i3, i4, i5 }, result.GetParents(i1)); this.AssertHelper(new List<short> { i2, i3, i4, i5 }, result.GetParents(i2)); this.AssertHelper(new List<short> { i3, i5 }, result.GetParents(i3)); this.AssertHelper(new List<short> { i4, i5 }, result.GetParents(i4)); this.AssertHelper(new List<short> { i5 }, result.GetParents(i5)); }
public void TwiceSingleProdGeog_Test() { short productIdOne = 7; short geographyIdOne = 11; short productIdTwo = 19; short geographyIdTwo = 23; short timeId = 13; var facts = new List<NullableFact>(); facts.Add(new NullableFact() { ProductId = productIdOne, GeographyId = geographyIdOne, SalesComponentId = 2, TimeId = timeId, Value = 100 }); facts.Add(new NullableFact() { ProductId = productIdOne, GeographyId = geographyIdOne, SalesComponentId = 3, TimeId = timeId, Value = 100 }); facts.Add(new NullableFact() { ProductId = productIdTwo, GeographyId = geographyIdTwo, SalesComponentId = 2, TimeId = timeId, Value = 1000 }); facts.Add(new NullableFact() { ProductId = productIdTwo, GeographyId = geographyIdTwo, SalesComponentId = 3, TimeId = timeId, Value = 1000 }); var relations = new List<Relation>(); relations.Add(new Relation() { Child = 2, Parent = 1 }); relations.Add(new Relation() { Child = 3, Parent = 1 }); var hierarchy = new Hierarchy(relations); var aggregator = new Aggregator(hierarchy); var result = aggregator.Aggregate(facts, 1); Assert.AreEqual(2, result.Count()); var resultOne = result.Single(r => r.ProductId == productIdOne); Assert.AreEqual(resultOne.GeographyId, geographyIdOne); Assert.AreEqual(resultOne.TimeId, timeId); Assert.AreEqual(resultOne.SalesComponentId, (short)1); Assert.AreEqual(resultOne.Value, 200); var resultTwo = result.Single(r => r.ProductId == productIdTwo); Assert.AreEqual(resultTwo.GeographyId, geographyIdTwo); Assert.AreEqual(resultTwo.TimeId, timeId); Assert.AreEqual(resultTwo.SalesComponentId, (short)1); Assert.AreEqual(resultTwo.Value, 2000); }
internal Aggregator(Hierarchy hierarchy) { this.hierarchy = hierarchy; }
public void Setup() { this.prod = new Hierarchy(this.hierarchy); this.geog = new Hierarchy(this.hierarchy); this.causal = new Hierarchy(null); // no causal hierarchy, this is 'all causal' the same this.period = new Hierarchy(this.hierarchy); this.facts = new List<NullableFact>(); }