/// <summary> /// Calculates aggregated value for specific field /// </summary> /// <param name="field">field</param> /// <param name="func">aggregation name</param> /// <returns>Calculated aggregation</returns> private double CalcValue(FieldModel field, string func) { if (field.Type == ColumnType.stringType) { var validDataColumnIndexes = DataColumnIndexes.Where(index => Data.GetColumn <string>(field.UniqueName)[index] != null).DefaultIfEmpty(-1).ToArray(); if (validDataColumnIndexes[0] == -1) { return(0); } if (func == "count") { return(validDataColumnIndexes.Count()); } if (func == "distinctcount") { return(validDataColumnIndexes.Select(index => Data.GetColumn <string>(field.UniqueName)[index]).Distinct().ToList().Count); } } if (field.Type == ColumnType.doubleType || field.Type == ColumnType.dateType) { var validDataColumnIndexes = DataColumnIndexes.Where(index => Data.GetColumn <double?>(field.UniqueName)[index].HasValue).DefaultIfEmpty(-1).ToArray(); var column = Data.GetColumn <double?>(field.UniqueName); if (validDataColumnIndexes[0] == -1) { return(double.NaN); } if (func == "count") { return(validDataColumnIndexes.Count()); } if (func == "distinctcount") { return(validDataColumnIndexes.Select(index => column[index]).Distinct().ToList().Count); } if (func == "sum" || func == "none") { return(validDataColumnIndexes.Sum(index => column[index].Value)); } if (func == "average") { return(validDataColumnIndexes.Average(index => column[index].Value)); } if (func == "min") { return(validDataColumnIndexes.Min(index => column[index].Value)); } if (func == "max") { return(validDataColumnIndexes.Max(index => column[index].Value)); } } return(0); }
/// <summary> /// Filters data /// </summary> /// <param name="filters">filters to apply</param> public void FilterData(List <Filter> filters) { if (filters.Count == 0) { return; } foreach (var filter in filters) { filter.Field.Type = _columnNameTypes[filter.Field.UniqueName]; if (filter.Value == null) { ParallelQuery <int> indexes = DataColumnIndexes.AsParallel(); if (filter.Include != null) { if (filter.Field.Type == ColumnType.stringType) { CheckIncludeFilter(Data.GetColumn <string>(filter.Field.UniqueName), filter.Include, ref indexes); } else { CheckIncludeFilter(Data.GetColumn <double?>(filter.Field.UniqueName), filter.Include, ref indexes); } } else if (filter.Exclude != null) { if (filter.Field.Type == ColumnType.stringType) { CheckExcludeFilter(Data.GetColumn <string>(filter.Field.UniqueName), filter.Exclude, ref indexes); } else { CheckExcludeFilter(Data.GetColumn <double?>(filter.Field.UniqueName), filter.Exclude, ref indexes); } } else if (filter.Query != null) { if (filter.Field.Type == ColumnType.doubleType) { var column = Data.GetColumn <double?>(filter.Field.UniqueName); indexes = indexes.Where(index => CheckNumberFilterQuery(column[index], filter.Query)); } else if (filter.Field.Type == ColumnType.stringType) { var column = Data.GetColumn <string>(filter.Field.UniqueName); indexes = indexes.Where(index => CheckStringFilterQuery(column[index], filter.Query)); } else if (filter.Field.Type == ColumnType.dateType) { var column = Data.GetColumn <double?>(filter.Field.UniqueName); indexes = indexes.Where(index => CheckDateFilterQuery(column[index], filter.Query)); } } DataColumnIndexes = indexes.ToArray(); } if (filter.Value != null) { filter.Value.Field.Type = _columnNameTypes[filter.Value.Field.UniqueName]; var calculatedTotalsAggregation = new List <Aggregation>(); CalcByFields(new List <FieldModel> { filter.Field }, null, new List <FieldFuncValue> { filter.Value }, ref calculatedTotalsAggregation); var calculatedTotals = new Dictionary <object, double>(); foreach (var agg in calculatedTotalsAggregation) { calculatedTotals.Add(agg.Keys[filter.Field.UniqueName], agg.Values[filter.Value.Field.UniqueName][filter.Value.Func]); } CheckValueFilterQuery(ref calculatedTotals, filter); if (filter.Field.Type == ColumnType.stringType) { var column = Data.GetColumn <string>(filter.Field.UniqueName); DataColumnIndexes = DataColumnIndexes.Where(index => calculatedTotals.ContainsKey(column[index])).ToArray(); } else { var column = Data.GetColumn <double?>(filter.Field.UniqueName); DataColumnIndexes = DataColumnIndexes.Where(index => calculatedTotals.ContainsKey(column[index])).ToArray(); } } } }