<commit_before>/** @file

  A brief file description

  @section license License

  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.
 */

/***************************************/
/****************************************************************************
 *
 *  StatProcessor.cc - Functions for computing node and cluster stat
 *                          aggregation
 *
 *
 ****************************************************************************/

#include "ink_config.h"
#include "StatProcessor.h"
#include "ink_unused.h"

#define STAT_CONFIG_FILE "stats.config.xml"

StatObjectList statObjectList;
StatXMLTag currentTag = INVALID_TAG;
StatObject *statObject = NULL;
char *exprContent = NULL;
static unsigned statCount = 0;  // global statistics object counter
bool nodeVar;
bool sumClusterVar;


void
startElement(void *userData, const char *name, const char **atts)
{
  NOWARN_UNUSED(userData);
  NOWARN_UNUSED(name);
  int i = 0;

  if (!strcmp(name, "ink:statistics"))
    currentTag = ROOT_TAG;
  else if (!strcmp(name, "statistics"))
    currentTag = STAT_TAG;
  else if (!strcmp(name, "destination"))
    currentTag = DST_TAG;
  else if (!strcmp(name, "expression"))
    currentTag = EXPR_TAG;
  else
    currentTag = INVALID_TAG;

  switch (currentTag) {
  case STAT_TAG:
    statObject = NEW(new StatObject(++statCount));
    Debug(MODULE_INIT, "\nStat #: ----------------------- %d -----------------------\n", statCount);

    for (i = 0; atts[i]; i += 2) {
      ink_debug_assert(atts[i + 1]);    // Attribute comes in pairs, hopefully.

      if (!strcmp(atts[i], "minimum")) {
        statObject->m_stats_min = (MgmtFloat) atof(atts[i + 1]);
      } else if (!strcmp(atts[i], "maximum")) {
        statObject->m_stats_max = (MgmtFloat) atof(atts[i + 1]);
      } else if (!strcmp(atts[i], "interval")) {
        statObject->m_update_interval = (ink_hrtime) atoi(atts[i + 1]);
      } else if (!strcmp(atts[i], "debug")) {
        statObject->m_debug = (atts[i + 1] && atts[i + 1][0] == '1');
      }

      Debug(MODULE_INIT, "\tDESTINTATION w/ attribute: %s -> %s\n", atts[i], atts[i + 1]);
    }
    break;

  case EXPR_TAG:
    exprContent = (char*)ats_malloc(BUFSIZ * 10);
    memset(exprContent, 0, BUFSIZ * 10);
    break;

  case DST_TAG:
    nodeVar = true;
    sumClusterVar = true;       // Should only be used with cluster variable

    for (i = 0; atts[i]; i += 2) {
      ink_debug_assert(atts[i + 1]);    // Attribute comes in pairs, hopefully.
      if (!strcmp(atts[i], "scope")) {
        nodeVar = (!strcmp(atts[i + 1], "node") ? true : false);
      } else if (!strcmp(atts[i], "operation")) {
        sumClusterVar = (!strcmp(atts[i + 1], "sum") ? true : false);
      }

      Debug(MODULE_INIT, "\tDESTINTATION w/ attribute: %s -> %s\n", atts[i], atts[i + 1]);
    }

    break;

  case INVALID_TAG:
    Debug(MODULE_INIT, "==========================================>%s<=\n", name);
    break;

  default:
    break;
  }
}


void
endElement(void *userData, const char *name)
{
  NOWARN_UNUSED(userData);
  NOWARN_UNUSED(name);
  switch (currentTag) {
  case STAT_TAG:
    statObjectList.enqueue(statObject);
    currentTag = ROOT_TAG;
    break;

  case EXPR_TAG:
    statObject->assignExpr(exprContent); // This hands over ownership of exprContent
    // fall through

  default:
    currentTag = STAT_TAG;
    break;
  }
}


void
charDataHandler(void *userData, const XML_Char * name, int len)
{
  NOWARN_UNUSED(userData);
  NOWARN_UNUSED(len);
  if (currentTag != EXPR_TAG && currentTag != DST_TAG) {
    return;
  }

  char content[BUFSIZ * 10];
  if (XML_extractContent(name, content, BUFSIZ * 10) == 0) {
    return;
  }

  if (currentTag == EXPR_TAG) {
    strncat(exprContent, content, ((BUFSIZ * 10) - strlen(exprContent) - 1));

  } else {
    statObject->assignDst(content, nodeVar, sumClusterVar);
  }
}


StatProcessor::StatProcessor():m_lmgmt(NULL), m_overviewGenerator(NULL)
{
  rereadConfig();
}


void
StatProcessor::rereadConfig()
{
  textBuffer *fileContent = NULL;
  Rollback *fileRB = NULL;
  char *fileBuffer = NULL;<commit_msg>TS-953: consolidate string copy/concat for the rest of mgmt/stats/.<commit_after>/** @file

  A brief file description

  @section license License

  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.
 */

/***************************************/
/****************************************************************************
 *
 *  StatProcessor.cc - Functions for computing node and cluster stat
 *                          aggregation
 *
 *
 ****************************************************************************/

#include "ink_config.h"
#include "StatProcessor.h"
#include "ink_unused.h"

#define STAT_CONFIG_FILE "stats.config.xml"

StatObjectList statObjectList;
StatXMLTag currentTag = INVALID_TAG;
StatObject *statObject = NULL;
char *exprContent = NULL;
static unsigned statCount = 0;  // global statistics object counter
bool nodeVar;
bool sumClusterVar;


void
startElement(void *userData, const char *name, const char **atts)
{
  NOWARN_UNUSED(userData);
  NOWARN_UNUSED(name);
  int i = 0;

  if (!strcmp(name, "ink:statistics"))
    currentTag = ROOT_TAG;
  else if (!strcmp(name, "statistics"))
    currentTag = STAT_TAG;
  else if (!strcmp(name, "destination"))
    currentTag = DST_TAG;
  else if (!strcmp(name, "expression"))
    currentTag = EXPR_TAG;
  else
    currentTag = INVALID_TAG;

  switch (currentTag) {
  case STAT_TAG:
    statObject = NEW(new StatObject(++statCount));
    Debug(MODULE_INIT, "\nStat #: ----------------------- %d -----------------------\n", statCount);

    for (i = 0; atts[i]; i += 2) {
      ink_debug_assert(atts[i + 1]);    // Attribute comes in pairs, hopefully.

      if (!strcmp(atts[i], "minimum")) {
        statObject->m_stats_min = (MgmtFloat) atof(atts[i + 1]);
      } else if (!strcmp(atts[i], "maximum")) {
        statObject->m_stats_max = (MgmtFloat) atof(atts[i + 1]);
      } else if (!strcmp(atts[i], "interval")) {
        statObject->m_update_interval = (ink_hrtime) atoi(atts[i + 1]);
      } else if (!strcmp(atts[i], "debug")) {
        statObject->m_debug = (atts[i + 1] && atts[i + 1][0] == '1');
      }

      Debug(MODULE_INIT, "\tDESTINTATION w/ attribute: %s -> %s\n", atts[i], atts[i + 1]);
    }
    break;

  case EXPR_TAG:
    exprContent = (char*)ats_malloc(BUFSIZ * 10);
    memset(exprContent, 0, BUFSIZ * 10);
    break;

  case DST_TAG:
    nodeVar = true;
    sumClusterVar = true;       // Should only be used with cluster variable

    for (i = 0; atts[i]; i += 2) {
      ink_debug_assert(atts[i + 1]);    // Attribute comes in pairs, hopefully.
      if (!strcmp(atts[i], "scope")) {
        nodeVar = (!strcmp(atts[i + 1], "node") ? true : false);
      } else if (!strcmp(atts[i], "operation")) {
        sumClusterVar = (!strcmp(atts[i + 1], "sum") ? true : false);
      }

      Debug(MODULE_INIT, "\tDESTINTATION w/ attribute: %s -> %s\n", atts[i], atts[i + 1]);
    }

    break;

  case INVALID_TAG:
    Debug(MODULE_INIT, "==========================================>%s<=\n", name);
    break;

  default:
    break;
  }
}


void
endElement(void *userData, const char *name)
{
  NOWARN_UNUSED(userData);
  NOWARN_UNUSED(name);
  switch (currentTag) {
  case STAT_TAG:
    statObjectList.enqueue(statObject);
    currentTag = ROOT_TAG;
    break;

  case EXPR_TAG:
    statObject->assignExpr(exprContent); // This hands over ownership of exprContent
    // fall through

  default:
    currentTag = STAT_TAG;
    break;
  }
}


void
charDataHandler(void *userData, const XML_Char * name, int len)
{
  NOWARN_UNUSED(userData);
  NOWARN_UNUSED(len);
  if (currentTag != EXPR_TAG && currentTag != DST_TAG) {
    return;
  }

  char content[BUFSIZ * 10];
  if (XML_extractContent(name, content, BUFSIZ * 10) == 0) {
    return;
  }

  if (currentTag == EXPR_TAG) {
    ink_strlcat(exprContent, content, BUFSIZ * 10); // see above for the size

  } else {
    statObject->assignDst(content, nodeVar, sumClusterVar);
  }
}


StatProcessor::StatProcessor():m_lmgmt(NULL), m_overviewGenerator(NULL)
{
  rereadConfig();
}


void
StatProcessor::rereadConfig()
{
  textBuffer *fileContent = NULL;
  Rollback *fileRB = NULL;
  char *fileBuffer = NULL;
{'fraction_non_alphanumeric': 0.17807089859851608, 'fraction_numerical': 0.007625721352019786, 'mean_word_length': 3.4274635036496353, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4010243', 'n_tokens_mistral': 3149, 'n_tokens_neox': 3042, 'n_words': 864}