Quintiq file version 2.0
#parent: #root
Method ApplyConfigurationQuery (
  KpiQuery kpiQuery
) id:Method_Kpi_frmKpiMatrix_ApplyConfigurationQuery
{
  #keys: '[146874.0.1463046408]'
  Body:
  [*
    // Applies the configuration query from a KpiQuery to radio button groups, List, and selector.
    selKpiCompareLabel.BoundValue( kpiQuery.SeriesSelection() );
    rbgTimeType.BoundValue( kpiQuery.Timetype() );
    
    // Expand all so the selection (in collapsed list) will be recorded in DataHolder
    lstKpiLabelFilter.ExpandAll();
    
    // Clear selection in list
    lstKpiLabelCompare.SelectByKey( Key::ZeroKey() );
    lstKpiLabelFilter.SelectByKey( Key::ZeroKey() );
    
    compareLabels := construct( KpiLabelValues );
    fixedLabels   := construct( KpiLabelValues );
    traverse( kpiQuery.LabelFilters().Tokenize( "," ), Elements, lv )
    {
      label := select( KpiTracker, Label.Value, labelvalue, labelvalue.Value() = lv );
      if ( not isnull( label ) )
      {
        if( label.Label() = selKpiCompareLabel.Data() )
        {
          lstKpiLabelCompare.ExtendSelectByKey( label.Key() );
          compareLabels.Add( label );
        }
        else
        {
          lstKpiLabelFilter.ExtendSelectByKey( label.Key() );
          fixedLabels.Add( label );
        }
      }
    }
    
    lstFilterKpis.SelectByKey( Key::ZeroKey() );
    
    traverse( kpiQuery.MetaDatas().Tokenize( "," ), Elements, md )
    {
      metadata := select( KpiTracker, MetaData, metadata, metadata.DisplayName() = md );
      if ( not isnull( metadata ) )
      {
        lstFilterKpis.ExtendSelectByKey( metadata.Key() );
      }
    }
    
    
    // Refresh data - Queries the KPI Tracker for the KPI compare data
    // Do this because the DataHolder has independency on drop down selector and is not reflect the changes immediately.
    compareLabel := selKpiCompareLabel.Data();
    
    data := KpiTracker.SnapshotRetrieval().GetLastSnapshotsForCompareLabelWithLabelFilterNvt( compareLabels, fixedLabels );
    
    set := Kpi_GlobalState.ConvertNvtToShadowSnapshotSetData( data );
    Kpi_GlobalState.PropagateLabel( set, compareLabel.Name() );
    Kpi_GlobalState.PropagateTimeStamp( set, rbgTimeType.BoundValue() );
                            
    // Set relations
    traverse( set, SnapshotData.Value, v ) 
    {
      v.UI_CompareLabel( relset, select( compareLabels, Elements, s, s.Value() = v.UI_LabelValue() ) );
      v.UI_Kpi( relset, select( KpiTracker, MetaData, k, k.Name() = v.Name() ) );
    }  
     
    dhSnapshotData.Data( &set );
  *]
}
