Quintiq file version 2.0
#parent: #root
Method FilterDataOnSelectedLabels (
  Boolean isLabelFilteringEnabled,
  structured[KpiLabelValue] selectedLabels
) as String id:Method_Kpi_GlobalState_FilterDataOnSelectedLabels
{
  #keys: '[146874.0.1589145116]'
  Body:
  [*
    // Sets/unsets the filter on the snapshots based on the selection in the label filter.
    filter := "";
    
    if( isLabelFilteringEnabled )
    {
      filters := construct( structured[String] );
    
      traverse( selectedLabels, Elements.Label, label )
      {
        values := construct( structured[String] );
        traverse( label, Value, value,
                  exists( selectedLabels, Elements, selected, selected = value) )
        {
          values.Add( 'exists( object, Label, l, l.Name() = "'
                       + label.Name()
                       + '" and l.Value() = "'
                       + value.Value()
                       + '") ' );
    
        }
        filters.Add( '(' + values.ToString( ' or ' ) + ')' );
      }
      filter := filters.ToString( ' and ' );
      debuginfo( ' >>> Generated Label filter = ', filter );
    }
    
    return filter;
  *]
}
