Skip to content

Commit

Permalink
Merge pull request #3 from statisticssweden/Autopivot
Browse files Browse the repository at this point in the history
Autopivot
  • Loading branch information
likp authored Feb 19, 2021
2 parents 4ea8467 + 6e55c5c commit d9fb01a
Show file tree
Hide file tree
Showing 2 changed files with 406 additions and 23 deletions.
187 changes: 187 additions & 0 deletions PCAxis.Sql/Parser_23/PXSqlVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using PCAxis.Sql.QueryLib_23;
using PCAxis.Paxiom;
using PCAxis.Sql.Pxs;
using System.Configuration;
using log4net;

namespace PCAxis.Sql.Parser_23
Expand All @@ -21,6 +22,16 @@ public PXSqlVariables(PXSqlMeta_23 meta)
}


internal enum DefaultPivot
{
alg1,
alg2,
alg3,
alg4,
alg5,
alg6
}

/// <summary>
/// True if one or more variable has applied grouping and the grouping requires a sum in the dataextraction, i.e. not all data are stored .
/// </summary>
Expand Down Expand Up @@ -72,10 +83,186 @@ internal void setStubHeadPxs()

}
}
string shouldPivot = ConfigurationManager.AppSettings["autopivot"];
if (shouldPivot != null) {
if (shouldPivot.ToLower() == "yes")
{
setStubHeadOverridden();
}
}
}
internal void setStubHeadOverridden()
{
int numberOfVariables = this.Count;
DefaultPivot pivotAlg;
int selectedClassCount = 0;
int selectedTimeValuesCount = 0;
int selectedContentsValuesCount = 0;
int selectedTimeContentsValuesCount = 0;
int lowestSelectedClassValues = Int32.MaxValue;
string VariableWithLowestSelected="";

//int stubIndex = 1;// eller 0 ??????
foreach (PXSqlVariable tmpVar in this.Values)
{
if (tmpVar.isSelected)
{
if (tmpVar.IsTimevariable)
{
selectedTimeValuesCount = tmpVar.Values.Count;
}
else if (tmpVar.IsContentVariable)
{
selectedContentsValuesCount = tmpVar.Values.Count;
}
else
{
selectedClassCount += 1;
if (tmpVar.Values.Count < lowestSelectedClassValues)
{
lowestSelectedClassValues = tmpVar.Values.Count;
VariableWithLowestSelected = tmpVar.Name;
}
}
}

}
selectedTimeContentsValuesCount = selectedTimeValuesCount * selectedContentsValuesCount;
pivotAlg = DefaultPivot.alg1;
if (selectedTimeContentsValuesCount <= 12 && selectedClassCount > 0){
pivotAlg = DefaultPivot.alg1;
}
if (selectedTimeContentsValuesCount > 12 && selectedTimeValuesCount <= 24) {
pivotAlg = DefaultPivot.alg2;
}
if(selectedTimeValuesCount > 24){
pivotAlg = DefaultPivot.alg3;
}
if(selectedTimeContentsValuesCount <= 12 && selectedClassCount == 0){
pivotAlg = DefaultPivot.alg4;
}
if ((selectedTimeContentsValuesCount == 1 && selectedClassCount > 1) && (lowestSelectedClassValues <= 24) ){
pivotAlg = DefaultPivot.alg5;
}
switch(pivotAlg)
{
case DefaultPivot.alg1:
break;
case DefaultPivot.alg2:
foreach (PXSqlVariable tmpVar in this.Values)
{
if (tmpVar.isSelected)
{
if (tmpVar.IsTimevariable)
{
tmpVar.IsHeading = true;
tmpVar.IsStub = false;
tmpVar.Index = -2;
}
else if (tmpVar.IsContentVariable)
{
tmpVar.IsStub = true;
tmpVar.IsHeading = false;
tmpVar.Index = -2;
}
else
{
tmpVar.IsStub = true;
tmpVar.IsHeading = false;
}
}
}
break;
case DefaultPivot.alg3:
foreach (PXSqlVariable tmpVar in this.Values)
{
if (tmpVar.isSelected)
{
if (tmpVar.IsTimevariable)
{
tmpVar.IsStub = true;
tmpVar.IsHeading = false;
tmpVar.Index = 100;
}
else if (tmpVar.IsContentVariable)
{
tmpVar.IsHeading = true;
tmpVar.IsStub = false;
tmpVar.Index = -100;
}
else
{
tmpVar.IsStub = true;
tmpVar.IsHeading = false;
}
}
}
break;
case DefaultPivot.alg4:
foreach (PXSqlVariable tmpVar in this.Values)
{
if (tmpVar.isSelected)
{
if (tmpVar.IsTimevariable)
{
tmpVar.IsStub = false;
tmpVar.IsHeading = true;
tmpVar.Index = 1;
}
else if (tmpVar.IsContentVariable)
{
tmpVar.IsHeading = false;
tmpVar.IsStub = true;
tmpVar.Index = 1;
}
else
{
log.Warn("Variable " + tmpVar.Name + " isSelected, but neither time nor contents, something wring with algorithm . Setting it to stub");
tmpVar.IsStub = true;
tmpVar.IsHeading = false;
}
}
}
break;
case DefaultPivot.alg5:
foreach (PXSqlVariable tmpVar in this.Values)
{
if (tmpVar.isSelected)
{
if (tmpVar.IsContentVariable)
{
tmpVar.IsHeading = true;
tmpVar.IsStub = false;
tmpVar.Index = -300;
}
else if (tmpVar.IsTimevariable)
{
tmpVar.IsStub = false;
tmpVar.IsHeading = true;
tmpVar.Index = -200;
}

else if (tmpVar.Name == VariableWithLowestSelected)
{
tmpVar.IsHeading = true;
tmpVar.IsStub = false;
tmpVar.Index = -100;
}
else
{
tmpVar.IsHeading = false;
tmpVar.IsStub = true;
}
}
}
break;
default:
break;
}


}

internal void setStubHeadDefault()
{
//int stubIndex = 1;// eller 0 ??????
Expand Down
Loading

0 comments on commit d9fb01a

Please sign in to comment.