You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@metron.apache.org by nickwallen <gi...@git.apache.org> on 2016/08/23 16:53:19 UTC

[GitHub] incubator-metron pull request #225: METRON-372 Enhance Statistical Operation...

GitHub user nickwallen opened a pull request:

    https://github.com/apache/incubator-metron/pull/225

    METRON-372 Enhance Statistical Operations Available for Use with the Profiler

    ### [METRON-372](https://issues.apache.org/jira/browse/METRON-372)
    
    This PR serves as a replacement for #214 .
    
    ### Changes
    
    Only basic math functions are currently available in Stellar for use with the Profiler.  This makes life difficult for users to create even basic profiles like a running average.  
    
    This can be seen in the following example where the average must be calculated manually in Stellar.  A variable `sum` and `cnt` must be maintained and then used to calculate the average.
    
    ```
    {
      "profile": "example3",
      "foreach": "ip_src_addr",
      "onlyif": "protocol == 'HTTP'",
      "init": {
        "sum": 0.0,
        "cnt": 0.0
      },
      "update": {
        "sum": "sum + resp_body_len",
        "cnt": "cnt + 1"
      },
      "result": "sum / cnt"
    }
    ```
    
    This change introduces a series of summary functions that make creating profiles much simpler for the user.  Instead of re-implementing the calculation of an average in Stellar, this leverages Commons Math to perform all the heavy lifting.
    
    The example above for calculating an average can be re-defined as follows.
    
    ```
    {
      "profile": "example3",
      "foreach": "ip_src_addr",
      "onlyif": "protocol == 'HTTP'",
      "init":   { "s": "STATS_INIT()" },
      "update": { "_": "STATS_ADD(length, s)" },
      "result": "STATS_MEAN(s)"
    }
    ```
    
    The following summary functions are supported.  These are all statistics that can be calculated in a single pass.  This means that none of the values being summarized are stored in memory.
    * count
    * mean
    * geometric mean
    * max
    * min
    * sum
    * population variance
    * variance
    * second moment
    * quadratic mean
    * standard deviation
    * sum of logs
    * sum of squares.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/nickwallen/incubator-metron METRON-372-NEW

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/incubator-metron/pull/225.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #225
    
----

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #225: METRON-372 Enhance Statistical Operation...

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/225#discussion_r75933510
  
    --- Diff: metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarStatisticalFunctionsTest.java ---
    @@ -0,0 +1,146 @@
    +/*
    + *
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing, software
    + *  distributed under the License is distributed on an "AS IS" BASIS,
    + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + *  See the License for the specific language governing permissions and
    + *  limitations under the License.
    + *
    + */
    +
    +package org.apache.metron.common.stellar;
    +
    +import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
    +import org.apache.metron.common.dsl.Context;
    +import org.apache.metron.common.dsl.StellarFunctions;
    +import org.junit.Before;
    +import org.junit.Test;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +
    +import static org.junit.Assert.assertEquals;
    +import static org.junit.Assert.assertNotNull;
    +
    +/**
    + * Tests the statistical summary functions of Stellar.
    + */
    +public class StellarStatisticalFunctionsTest {
    +
    +  private List<Double> values;
    +  private Map<String, Object> variables;
    +  private SummaryStatistics stats;
    +
    +  private static Object run(String rule, Map<String, Object> variables) {
    +    StellarProcessor processor = new StellarProcessor();
    +    return processor.parse(rule, x -> variables.get(x), StellarFunctions.FUNCTION_RESOLVER(), Context.EMPTY_CONTEXT());
    --- End diff --
    
    Can we check that stellar's`validate()` works here too?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron issue #225: METRON-372 Enhance Statistical Operations Avail...

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on the issue:

    https://github.com/apache/incubator-metron/pull/225
  
    +1


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #225: METRON-372 Enhance Statistical Operation...

Posted by nickwallen <gi...@git.apache.org>.
Github user nickwallen commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/225#discussion_r75949574
  
    --- Diff: metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarStatisticalFunctionsTest.java ---
    @@ -0,0 +1,146 @@
    +/*
    + *
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing, software
    + *  distributed under the License is distributed on an "AS IS" BASIS,
    + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + *  See the License for the specific language governing permissions and
    + *  limitations under the License.
    + *
    + */
    +
    +package org.apache.metron.common.stellar;
    +
    +import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
    +import org.apache.metron.common.dsl.Context;
    +import org.apache.metron.common.dsl.StellarFunctions;
    +import org.junit.Before;
    +import org.junit.Test;
    +
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +
    +import static org.junit.Assert.assertEquals;
    +import static org.junit.Assert.assertNotNull;
    +
    +/**
    + * Tests the statistical summary functions of Stellar.
    + */
    +public class StellarStatisticalFunctionsTest {
    +
    +  private List<Double> values;
    +  private Map<String, Object> variables;
    +  private SummaryStatistics stats;
    +
    +  private static Object run(String rule, Map<String, Object> variables) {
    +    StellarProcessor processor = new StellarProcessor();
    +    return processor.parse(rule, x -> variables.get(x), StellarFunctions.FUNCTION_RESOLVER(), Context.EMPTY_CONTEXT());
    --- End diff --
    
    A call to validate has been added.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #225: METRON-372 Enhance Statistical Operation...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/incubator-metron/pull/225


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #225: METRON-372 Enhance Statistical Operation...

Posted by nickwallen <gi...@git.apache.org>.
Github user nickwallen commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/225#discussion_r75934713
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/SummaryStatisticsFunctions.java ---
    @@ -0,0 +1,198 @@
    +/*
    + *
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing, software
    + *  distributed under the License is distributed on an "AS IS" BASIS,
    + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + *  See the License for the specific language governing permissions and
    + *  limitations under the License.
    + *
    + */
    +
    +package org.apache.metron.common.dsl.functions;
    +
    +import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
    +import org.apache.metron.common.dsl.BaseStellarFunction;
    +
    +import java.util.List;
    +
    +import static org.apache.metron.common.utils.ConversionUtils.convert;
    +
    +/**
    + * Provides Stellar functions that can calculate summary statistics on
    + * streams of data.
    + *
    + * These functions are limited to those that can be calculated in a
    + * single-pass so that the values are not stored in memory.  This leverages
    + * the commons-math SummaryStatistics class.
    + */
    +public class SummaryStatisticsFunctions {
    +
    +  /**
    +   * Initialize the summary statistics.
    +   *
    +   *  STATS_INIT ()
    +   */
    +  public static class Init extends BaseStellarFunction {
    +    @Override
    +    public Object apply(List<Object> args) {
    +      return new SummaryStatistics();
    +    }
    +  }
    +
    +  /**
    +   * Add an input value to those that are used to calculate the summary statistics.
    +   *
    +   *  STATS_ADD (value, stats)
    +   */
    +  public static class Add extends BaseStellarFunction {
    +    @Override
    +    public Object apply(List<Object> args) {
    +      double value = convert(args.get(0), Double.class);
    +      SummaryStatistics stats = convert(args.get(1), SummaryStatistics.class);
    +      stats.addValue(value);
    +      return null;
    --- End diff --
    
    
    How would a user use this?  Is there a 'prettier' way that I am not thinking about?  This looks absolutely nasty.
    
    ```
    STATS_ADD(STATS_ADD(STATS_ADD(s, 1), 2), 3)
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #225: METRON-372 Enhance Statistical Operation...

Posted by nickwallen <gi...@git.apache.org>.
Github user nickwallen commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/225#discussion_r75949744
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/SummaryStatisticsFunctions.java ---
    @@ -0,0 +1,198 @@
    +/*
    + *
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing, software
    + *  distributed under the License is distributed on an "AS IS" BASIS,
    + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + *  See the License for the specific language governing permissions and
    + *  limitations under the License.
    + *
    + */
    +
    +package org.apache.metron.common.dsl.functions;
    +
    +import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
    +import org.apache.metron.common.dsl.BaseStellarFunction;
    +
    +import java.util.List;
    +
    +import static org.apache.metron.common.utils.ConversionUtils.convert;
    +
    +/**
    + * Provides Stellar functions that can calculate summary statistics on
    + * streams of data.
    + *
    + * These functions are limited to those that can be calculated in a
    + * single-pass so that the values are not stored in memory.  This leverages
    + * the commons-math SummaryStatistics class.
    + */
    +public class SummaryStatisticsFunctions {
    +
    +  /**
    +   * Initialize the summary statistics.
    +   *
    +   *  STATS_INIT ()
    +   */
    +  public static class Init extends BaseStellarFunction {
    +    @Override
    +    public Object apply(List<Object> args) {
    +      return new SummaryStatistics();
    +    }
    +  }
    +
    +  /**
    +   * Add an input value to those that are used to calculate the summary statistics.
    +   *
    +   *  STATS_ADD (value, stats)
    +   */
    +  public static class Add extends BaseStellarFunction {
    +    @Override
    +    public Object apply(List<Object> args) {
    +      double value = convert(args.get(0), Double.class);
    +      SummaryStatistics stats = convert(args.get(1), SummaryStatistics.class);
    +      stats.addValue(value);
    +      return null;
    --- End diff --
    
    This is not supported in the latest patch.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #225: METRON-372 Enhance Statistical Operation...

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/225#discussion_r75933182
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/SummaryStatisticsFunctions.java ---
    @@ -0,0 +1,198 @@
    +/*
    + *
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing, software
    + *  distributed under the License is distributed on an "AS IS" BASIS,
    + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + *  See the License for the specific language governing permissions and
    + *  limitations under the License.
    + *
    + */
    +
    +package org.apache.metron.common.dsl.functions;
    +
    +import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
    +import org.apache.metron.common.dsl.BaseStellarFunction;
    +
    +import java.util.List;
    +
    +import static org.apache.metron.common.utils.ConversionUtils.convert;
    +
    +/**
    + * Provides Stellar functions that can calculate summary statistics on
    + * streams of data.
    + *
    + * These functions are limited to those that can be calculated in a
    + * single-pass so that the values are not stored in memory.  This leverages
    + * the commons-math SummaryStatistics class.
    + */
    +public class SummaryStatisticsFunctions {
    +
    +  /**
    +   * Initialize the summary statistics.
    +   *
    +   *  STATS_INIT ()
    +   */
    +  public static class Init extends BaseStellarFunction {
    +    @Override
    +    public Object apply(List<Object> args) {
    +      return new SummaryStatistics();
    +    }
    +  }
    +
    +  /**
    +   * Add an input value to those that are used to calculate the summary statistics.
    +   *
    +   *  STATS_ADD (value, stats)
    +   */
    +  public static class Add extends BaseStellarFunction {
    +    @Override
    +    public Object apply(List<Object> args) {
    +      double value = convert(args.get(0), Double.class);
    --- End diff --
    
    Should we swap the args so the SummaryStatistics arg comes first and allow users to pass as many numbers as they want to STATS_ADD(...)?  Such as `STATS_ADD(stats, 1, 2, 3, 4)`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #225: METRON-372 Enhance Statistical Operation...

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/225#discussion_r75933341
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/SummaryStatisticsFunctions.java ---
    @@ -0,0 +1,198 @@
    +/*
    + *
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing, software
    + *  distributed under the License is distributed on an "AS IS" BASIS,
    + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + *  See the License for the specific language governing permissions and
    + *  limitations under the License.
    + *
    + */
    +
    +package org.apache.metron.common.dsl.functions;
    +
    +import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
    +import org.apache.metron.common.dsl.BaseStellarFunction;
    +
    +import java.util.List;
    +
    +import static org.apache.metron.common.utils.ConversionUtils.convert;
    +
    +/**
    + * Provides Stellar functions that can calculate summary statistics on
    + * streams of data.
    + *
    + * These functions are limited to those that can be calculated in a
    + * single-pass so that the values are not stored in memory.  This leverages
    + * the commons-math SummaryStatistics class.
    + */
    +public class SummaryStatisticsFunctions {
    +
    +  /**
    +   * Initialize the summary statistics.
    +   *
    +   *  STATS_INIT ()
    +   */
    +  public static class Init extends BaseStellarFunction {
    +    @Override
    +    public Object apply(List<Object> args) {
    +      return new SummaryStatistics();
    +    }
    +  }
    +
    +  /**
    +   * Add an input value to those that are used to calculate the summary statistics.
    +   *
    +   *  STATS_ADD (value, stats)
    +   */
    +  public static class Add extends BaseStellarFunction {
    +    @Override
    +    public Object apply(List<Object> args) {
    +      double value = convert(args.get(0), Double.class);
    +      SummaryStatistics stats = convert(args.get(1), SummaryStatistics.class);
    +      stats.addValue(value);
    +      return null;
    --- End diff --
    
    Should we return the SummaryStatistics class here?  That way we can chain them?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #225: METRON-372 Enhance Statistical Operation...

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/225#discussion_r75934951
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/SummaryStatisticsFunctions.java ---
    @@ -0,0 +1,198 @@
    +/*
    + *
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing, software
    + *  distributed under the License is distributed on an "AS IS" BASIS,
    + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + *  See the License for the specific language governing permissions and
    + *  limitations under the License.
    + *
    + */
    +
    +package org.apache.metron.common.dsl.functions;
    +
    +import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
    +import org.apache.metron.common.dsl.BaseStellarFunction;
    +
    +import java.util.List;
    +
    +import static org.apache.metron.common.utils.ConversionUtils.convert;
    +
    +/**
    + * Provides Stellar functions that can calculate summary statistics on
    + * streams of data.
    + *
    + * These functions are limited to those that can be calculated in a
    + * single-pass so that the values are not stored in memory.  This leverages
    + * the commons-math SummaryStatistics class.
    + */
    +public class SummaryStatisticsFunctions {
    +
    +  /**
    +   * Initialize the summary statistics.
    +   *
    +   *  STATS_INIT ()
    +   */
    +  public static class Init extends BaseStellarFunction {
    +    @Override
    +    public Object apply(List<Object> args) {
    +      return new SummaryStatistics();
    +    }
    +  }
    +
    +  /**
    +   * Add an input value to those that are used to calculate the summary statistics.
    +   *
    +   *  STATS_ADD (value, stats)
    +   */
    +  public static class Add extends BaseStellarFunction {
    +    @Override
    +    public Object apply(List<Object> args) {
    +      double value = convert(args.get(0), Double.class);
    +      SummaryStatistics stats = convert(args.get(1), SummaryStatistics.class);
    +      stats.addValue(value);
    +      return null;
    --- End diff --
    
    Yeah, let's not do that.  Instead, let's make it so that the stats are first and the user can pass as many nums as they like, so that would be `STATS_ADD(s, 1, 2, 3)`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---