Esempio n. 1
0
 public static void WriteArray(this Utf8JsonWriter writer, TLObject[][] value)
 {
     writer.WriteStartArray();
     for (int i = 0; i < value.Length; i++)
     {
         writer.WriteArray(value[i]);
     }
     writer.WriteEndArray();
 }
        public override void Write(Utf8JsonWriter writer, IAnalyzer <IMetric> analyzer, JsonSerializerOptions options)
        {
            writer.WriteStartObject();

            if (analyzer is Size size)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Size");
                writer.WriteString(SerdeExt.WHERE_FIELD, size.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is Completeness completeness)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Completeness");
                writer.WriteString(SerdeExt.COLUMN_FIELD, completeness.Column.GetOrElse(string.Empty));
                writer.WriteString(SerdeExt.WHERE_FIELD, completeness.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is Compliance compliance)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Compliance");
                writer.WriteString(SerdeExt.WHERE_FIELD, compliance.Where.GetOrElse(string.Empty));
                writer.WriteString("instance", compliance.Instance);
                writer.WriteString("predicate", compliance.Predicate.ToString());
                writer.WriteEndObject();
                return;
            }

            if (analyzer is PatternMatch patternMatch)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "PatternMatch");
                writer.WriteString(SerdeExt.COLUMN_FIELD, patternMatch.Column.GetOrElse(string.Empty));
                writer.WriteString(SerdeExt.WHERE_FIELD, patternMatch.Where.GetOrElse(string.Empty));
                writer.WriteString("pattern", patternMatch.Regex.ToString());
                writer.WriteEndObject();
                return;
            }

            if (analyzer is Sum sum)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Sum");
                writer.WriteString(SerdeExt.COLUMN_FIELD, sum.Column.GetOrElse(string.Empty));
                writer.WriteString(SerdeExt.WHERE_FIELD, sum.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is Mean mean)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Mean");
                writer.WriteString(SerdeExt.COLUMN_FIELD, mean.Column.GetOrElse(string.Empty));
                writer.WriteString(SerdeExt.WHERE_FIELD, mean.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is Minimum min)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Minimum");
                writer.WriteString(SerdeExt.COLUMN_FIELD, min.Column.GetOrElse(string.Empty));
                writer.WriteString(SerdeExt.WHERE_FIELD, min.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }


            if (analyzer is Maximum max)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Maximum");
                writer.WriteString(SerdeExt.COLUMN_FIELD, max.Column.GetOrElse(string.Empty));
                writer.WriteString(SerdeExt.WHERE_FIELD, max.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }


            //TODO: missing if (analyzer is CountDistinct max)

            if (analyzer is Distinctness distinctness)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Distinctness");
                writer.WriteArray(SerdeExt.COLUMN_FIELD, distinctness.Columns.Select(col => col.ToString()));
                writer.WriteString(SerdeExt.WHERE_FIELD, distinctness.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is Entropy entropy)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Entropy");
                writer.WriteString(SerdeExt.COLUMN_FIELD, entropy.Columns.First());
                writer.WriteEndObject();
                return;
            }

            if (analyzer is MutualInformation mutual)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "MutualInformation");
                writer.WriteArray(SerdeExt.COLUMN_FIELD, mutual.Columns.Select(col => col.ToString()));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is UniqueValueRatio uniqueValueRatio)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "UniqueValueRatio");
                writer.WriteArray(SerdeExt.COLUMN_FIELD, uniqueValueRatio.Columns.Select(col => col.ToString()));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is Uniqueness uniqueness)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Uniqueness");
                writer.WriteArray(SerdeExt.COLUMN_FIELD, uniqueness.Columns.Select(col => col.ToString()));
                writer.WriteEndObject();
                return;
            }


            if (analyzer is Histogram histogram)
            {
                if (histogram.BinningUdf.HasValue)
                {
                    throw new ArgumentException("Unable to serialize Histogram with binningUdf!");
                }

                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Histogram");
                writer.WriteString(SerdeExt.COLUMN_FIELD, histogram.Column);
                writer.WriteNumber("maxDetailBins", histogram.MaxDetailBins);
                writer.WriteEndObject();
                return;
            }

            if (analyzer is DataType dataType)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "DataType");
                writer.WriteString(SerdeExt.COLUMN_FIELD, dataType.Column.GetOrElse(string.Empty));
                writer.WriteString(SerdeExt.WHERE_FIELD, dataType.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is ApproxCountDistinct approxCountDistinct)
            {
                throw new NotImplementedException();
            }

            if (analyzer is Correlation correlation)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "Correlation");
                writer.WriteString("firstColumn", correlation.ColumnA);
                writer.WriteString("secondColumn", correlation.ColumnB);
                writer.WriteString(SerdeExt.WHERE_FIELD, correlation.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is StandardDeviation stDev)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "StandardDeviation");
                writer.WriteString(SerdeExt.COLUMN_FIELD, stDev.Column.GetOrElse(string.Empty));
                writer.WriteString(SerdeExt.WHERE_FIELD, stDev.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is ApproxQuantile approxQuantile)
            {
                throw new NotImplementedException();
            }

            if (analyzer is MinLength minLength)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "MinLength");
                writer.WriteString(SerdeExt.COLUMN_FIELD, minLength.Column.GetOrElse(string.Empty));
                writer.WriteString(SerdeExt.WHERE_FIELD, minLength.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }

            if (analyzer is MaxLength maxLength)
            {
                writer.WriteString(SerdeExt.ANALYZER_NAME_FIELD, "MaxLength");
                writer.WriteString(SerdeExt.COLUMN_FIELD, maxLength.Column.GetOrElse(string.Empty));
                writer.WriteString(SerdeExt.WHERE_FIELD, maxLength.Where.GetOrElse(string.Empty));
                writer.WriteEndObject();
                return;
            }

            throw new ArgumentException($"Unable to serialize analyzer {analyzer}");
        }