<commit_before>/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2019      The Fluent Bit Authors
 *  Copyright (C) 2015-2018 Treasure Data Inc.
 *
 *  Licensed 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.
 */

#ifndef FLB_SP_TIMESERIES_H
#define FLB_SP_TIMESERIES_H

#include <math.h>
#include <float.h>
#include <fluent-bit/stream_processor/flb_sp_parser.h>

#define TIMESERIES_FUNCTIONS_SIZE 2

typedef struct timeseries *(*timeseries_function_alloc_typ)(int);
typedef struct timeseries *(*timeseries_function_clone_typ)(struct timeseries *);

typedef void (*timeseries_function_add_typ)(struct timeseries *,
                                            struct flb_time *);

typedef void (*timeseries_function_rem_typ) (struct timeseries *,
                                             struct timeseries *,
                                             struct flb_time *);

typedef void (*timeseries_function_calc_typ) (struct timeseries *,
                                              struct flb_sp_cmd_key *,
                                              msgpack_packer *,
                                              int, struct flb_time *);

typedef void (*timeseries_function_destroy_typ)(struct timeseries *);

/* forecast functions */
struct timeseries *cb_forecast_alloc()
{
    struct timeseries *ts;

    ts = (struct timeseries *) flb_calloc(1, sizeof(struct timeseries_forecast));

    return ts;
}

struct timeseries *cb_forecast_clone(struct timeseries *ts)
{
    /*
       This function doean't actually clone all parameters. It only copies the
       values required for hopping window calculations
     */
    struct timeseries_forecast *forecast;
    struct timeseries_forecast *forecast_c;

    forecast = (struct timeseries_forecast *) ts;

    forecast_c = (struct timeseries_forecast *)
                 flb_calloc(1, sizeof(struct timeseries_forecast));

    forecast_c->sigma_x = forecast->sigma_x;
    forecast_c->sigma_y = forecast->sigma_y;
    forecast_c->sigma_xy = forecast->sigma_xy;
    forecast_c->sigma_x2 = forecast->sigma_x2;

    return (struct timeseries *) forecast_c;
}

void cb_forecast_add(struct timeseries *ts, struct flb_time *tm)
{
    double x;
    double y;
    struct timeseries_forecast *forecast;
    struct aggr_num *val;

    forecast = (struct timeseries_forecast *) ts;

    val = ts->nums;

    switch (val->type) {
    /* Forecast values are always floating points */
    case FLB_SP_NUM_I64:
        if (!forecast->offset)
        {
            forecast->offset = flb_calloc(1, sizeof(double));
            *forecast->offset = (double) val->i64;
        }

        x = ((double) val->i64 - *forecast->offset);
        break;
    case FLB_SP_NUM_F64:
        if (!forecast->offset)
        {
            forecast->offset = flb_calloc(1, sizeof(double));
            *forecast->offset = val->f64;
        }

        x = val->f64 - *forecast->offset;
        break;
    default:
        return;
        break;
    }

    if (!forecast->latest_x)
    {
        forecast->latest_x = flb_malloc(sizeof(double));
    }

    *forecast->latest_x = x;

    val++;
    switch (val->type) {
    /* Forecast values are always floating points */
    case FLB_SP_NUM_I64:
        y = (double) val->i64;
        break;
    case FLB_SP_NUM_F64:
        y = val->f64;
        break;
    default:
        return;
        break;
    }

    forecast->sigma_x += x;
    forecast->sigma_y += y;

    forecast->sigma_xy += x * y;
    forecast->sigma_x2 += x * x;
}<commit_msg>sp: forecast: validate memory allocations (CID 253518 253519)<commit_after>/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2019      The Fluent Bit Authors
 *  Copyright (C) 2015-2018 Treasure Data Inc.
 *
 *  Licensed 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.
 */

#ifndef FLB_SP_TIMESERIES_H
#define FLB_SP_TIMESERIES_H

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/stream_processor/flb_sp_parser.h>

#include <math.h>
#include <float.h>

#define TIMESERIES_FUNCTIONS_SIZE 2

typedef struct timeseries *(*timeseries_function_alloc_typ)(int);
typedef struct timeseries *(*timeseries_function_clone_typ)(struct timeseries *);

typedef void (*timeseries_function_add_typ)(struct timeseries *,
                                            struct flb_time *);

typedef void (*timeseries_function_rem_typ) (struct timeseries *,
                                             struct timeseries *,
                                             struct flb_time *);

typedef void (*timeseries_function_calc_typ) (struct timeseries *,
                                              struct flb_sp_cmd_key *,
                                              msgpack_packer *,
                                              int, struct flb_time *);

typedef void (*timeseries_function_destroy_typ)(struct timeseries *);

/* forecast functions */
struct timeseries *cb_forecast_alloc()
{
    struct timeseries *ts;

    ts = (struct timeseries *) flb_calloc(1, sizeof(struct timeseries_forecast));

    return ts;
}

struct timeseries *cb_forecast_clone(struct timeseries *ts)
{
    /*
       This function doean't actually clone all parameters. It only copies the
       values required for hopping window calculations
     */
    struct timeseries_forecast *forecast;
    struct timeseries_forecast *forecast_c;

    forecast = (struct timeseries_forecast *) ts;

    forecast_c = (struct timeseries_forecast *)
                 flb_calloc(1, sizeof(struct timeseries_forecast));
    if (!forecast_c) {
        flb_errno();
        return NULL;
    }

    forecast_c->sigma_x = forecast->sigma_x;
    forecast_c->sigma_y = forecast->sigma_y;
    forecast_c->sigma_xy = forecast->sigma_xy;
    forecast_c->sigma_x2 = forecast->sigma_x2;

    return (struct timeseries *) forecast_c;
}

void cb_forecast_add(struct timeseries *ts, struct flb_time *tm)
{
    double x;
    double y;
    struct timeseries_forecast *forecast;
    struct aggr_num *val;

    forecast = (struct timeseries_forecast *) ts;

    val = ts->nums;

    switch (val->type) {
    /* Forecast values are always floating points */
    case FLB_SP_NUM_I64:
        if (!forecast->offset) {
            forecast->offset = flb_calloc(1, sizeof(double));
            if (!forecast->offset) {
                flb_errno();
                return;
            }
            *forecast->offset = (double) val->i64;
        }

        x = ((double) val->i64 - *forecast->offset);
        break;
    case FLB_SP_NUM_F64:
        if (!forecast->offset) {
            forecast->offset = flb_calloc(1, sizeof(double));
            if (!forecast->offset) {
                flb_errno();
                return;
            }
            *forecast->offset = val->f64;
        }
        x = val->f64 - *forecast->offset;
        break;
    default:
        return;
        break;
    }

    if (!forecast->latest_x) {
        forecast->latest_x = flb_malloc(sizeof(double));
        if (!forecast->latest_x) {
            flb_errno();
            return;
        }
    }

    *forecast->latest_x = x;

    val++;
    switch (val->type) {
    /* Forecast values are always floating points */
    case FLB_SP_NUM_I64:
        y = (double) val->i64;
        break;
    case FLB_SP_NUM_F64:
        y = val->f64;
        break;
    default:
        return;
        break;
    }

    forecast->sigma_x += x;
    forecast->sigma_y += y;

    forecast->sigma_xy += x * y;
    forecast->sigma_x2 += x * x;
}
{'fraction_non_alphanumeric': 0.10330483013635314, 'fraction_numerical': 0.012017564132193206, 'mean_word_length': 2.2808946171341926, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '976453', 'n_tokens_mistral': 2653, 'n_tokens_neox': 2551, 'n_words': 670}