<commit_before>/*
* Copyright 2015-2017 <NAME>.
* See LICENSE file for license details.
*/

#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#include "status_bar.h"
#include "base_monitor.h"
#include "nginx_monitor.h"
#include "http_download.h"
#include "configs.h"

void* nginx_init(GArray* arguments) {
  monitor_arg_check("nginx", arguments, "(icon, status_uri)");

  struct nginx_monitor* m = malloc(sizeof(struct nginx_monitor));

  m->base = base_monitor_init(nginx_sleep_time, nginx_update_text, nginx_free);

  char* icon = g_array_index(arguments, GString*, 0)->str;
  m->icon = g_string_new(icon);

  char* status_uri = g_array_index(arguments, GString*, 1)->str;
  m->request_str = g_string_new(status_uri);

  m->res = g_string_new(NULL);
  m->curl = curl_easy_init();

  m->err = malloc((strlen(m->icon->str) + 2)*sizeof(char));
  sprintf(m->err, "%s!", m->icon->str);

  return m;
}

gboolean nginx_update_text(void* ptr) {
  struct nginx_monitor* m = (struct nginx_monitor*)ptr;
  monitor_null_check(m, "nginx_monitor", "update");

  CURLcode code = download_data(m->curl, m->request_str->str, m->res);
  char* output;

  if (code == CURLE_OK && format_nginx_status(m->res, m->icon) != -1) {
    output = m->res->str;
  } else {
    output = m->err;
  }

  g_mutex_lock(m->base->mutex);
  m->base->text = g_string_assign(m->base->text, output);
  g_mutex_unlock(m->base->mutex);

  return TRUE;
}

int nginx_sleep_time(void* ptr) {
  return 5;
}

void nginx_free(void* ptr) {
  struct nginx_monitor* m = (struct nginx_monitor*)ptr;
  monitor_null_check(m, "nginx_monitor", "free");

  g_string_free(m->icon, TRUE);
  g_string_free(m->request_str, TRUE);

  free(m->err);
  g_string_free(m->res, TRUE);
  curl_easy_cleanup(m->curl);

  base_monitor_free(m->base);

  free(m);
}

int format_nginx_status(GString* res, GString* icon) {
  int code = 0;

  int active;
  GString* output = g_string_new(icon->str);

  char** words = g_strsplit(res->str, "\n", -1);

  char** word = words;
  while (*word != 0) {
    g_strstrip(*word);

    if (strstr(*word, "Active connections: ") != NULL) {
      int nread = sscanf(*word, "Active connections: %d", &active);
      if (nread == 1) {
        g_string_append_printf(output, "%d", active);
      } else {
        code = -1;
      }
    }

    word = word + 1;
  }

  g_string_assign(res, output->str);
  g_string_free(output, TRUE);
  g_strfreev(words);
  return code;
}<commit_msg>Make nginx monitor return error if nothing read<commit_after>/*
* Copyright 2015-2017 <NAME>.
* See LICENSE file for license details.
*/

#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#include "status_bar.h"
#include "base_monitor.h"
#include "nginx_monitor.h"
#include "http_download.h"
#include "configs.h"

void* nginx_init(GArray* arguments) {
  monitor_arg_check("nginx", arguments, "(icon, status_uri)");

  struct nginx_monitor* m = malloc(sizeof(struct nginx_monitor));

  m->base = base_monitor_init(nginx_sleep_time, nginx_update_text, nginx_free);

  char* icon = g_array_index(arguments, GString*, 0)->str;
  m->icon = g_string_new(icon);

  char* status_uri = g_array_index(arguments, GString*, 1)->str;
  m->request_str = g_string_new(status_uri);

  m->res = g_string_new(NULL);
  m->curl = curl_easy_init();

  m->err = malloc((strlen(m->icon->str) + 2)*sizeof(char));
  sprintf(m->err, "%s!", m->icon->str);

  return m;
}

gboolean nginx_update_text(void* ptr) {
  struct nginx_monitor* m = (struct nginx_monitor*)ptr;
  monitor_null_check(m, "nginx_monitor", "update");

  CURLcode code = download_data(m->curl, m->request_str->str, m->res);
  char* output;

  if (code == CURLE_OK && format_nginx_status(m->res, m->icon) != -1) {
    output = m->res->str;
  } else {
    output = m->err;
  }

  g_mutex_lock(m->base->mutex);
  m->base->text = g_string_assign(m->base->text, output);
  g_mutex_unlock(m->base->mutex);

  return TRUE;
}

int nginx_sleep_time(void* ptr) {
  return 5;
}

void nginx_free(void* ptr) {
  struct nginx_monitor* m = (struct nginx_monitor*)ptr;
  monitor_null_check(m, "nginx_monitor", "free");

  g_string_free(m->icon, TRUE);
  g_string_free(m->request_str, TRUE);

  free(m->err);
  g_string_free(m->res, TRUE);
  curl_easy_cleanup(m->curl);

  base_monitor_free(m->base);

  free(m);
}

int format_nginx_status(GString* res, GString* icon) {
  int code = -1;

  int active;
  GString* output = g_string_new(icon->str);

  char** words = g_strsplit(res->str, "\n", -1);

  char** word = words;
  while (*word != 0) {
    g_strstrip(*word);

    if (strstr(*word, "Active connections: ") != NULL) {
      int nread = sscanf(*word, "Active connections: %d", &active);
      if (nread == 1) {
        g_string_append_printf(output, "%d", active);
        code = 0;
      }
    }

    word = word + 1;
  }

  g_string_assign(res, output->str);
  g_string_free(output, TRUE);
  g_strfreev(words);
  return code;
}
{'fraction_non_alphanumeric': 0.17079556898288017, 'fraction_numerical': 0.007653575025176234, 'mean_word_length': 4.1675338189386055, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7016148', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1900, 'n_words': 382}