<commit_before>/*
 *  CurrentCost_network.c
 *
 *  Created by <NAME>, 2013.
 *  This code is Public Domain.
 */

#include "CurrentCost.h"
#include <unistd.h>
#include <netdb.h>
#include "ctb-0.16/ctb.h"


#define MAX(a,b) ((a)>(b) ? (a) : (b))

#define INITIAL_BUFFER_SIZE        (8*1024)
#define BUFFER_SIZE_GROW_TRESHOLD  (1*1024)


ctb::SerialPort* g_serial_port;
int g_serial_fd = -1;
int g_socket_watts_fd = -1;
int g_socket_tmpr_fd = -1;
int g_max_fd = -1;
fd_set g_readfds;

size_t g_buf_size;
char* g_buf;


bool ExpandBuffer(char*& buf, size_t& buf_size, const size_t bytes_read)
{
	//Double the buffer size
	buf_size += buf_size;
	char* new_buf = new char[buf_size+1];
	if (!new_buf)
		return false;

	memcpy(new_buf, buf, bytes_read);
	delete[] buf;
	buf = new_buf;
	return true;
}

void SendBuffer(int socket_fd, const char* buf, int len)
{
	int offset = 0;
	do {
		offset += send(socket_fd, buf+offset, len-offset, 0);
	} while (offset<len);
}

void DumpMuninData(int socket_fd, bool is_watts)
{
	long start_of_munin_interval = time(NULL) - MUNIN_INTERVAL;
	double watts;
	double tmpr, total_tmpr=0.0;
	int count, total_tmpr_count=0;
	MsgInfo* msg_iter;
	int i;

	char buf[100];
	for (i=0; ENVIR_SENSOR_COUNT>i; i++)
	{
		watts = 0;
		tmpr = 0.0;
		count = 0;
		msg_iter = g_msg_info_list;
		while (msg_iter)
		{
			if (msg_iter->sensor==i && msg_iter->timestamp>=start_of_munin_interval)
			{
				count++;
				watts += msg_iter->watts;
				tmpr += msg_iter->tmpr;
			}
			msg_iter = msg_iter->next;
		}

		if (0 < count)
		{
			watts /= count;
			tmpr /= count;
		}

		if (is_watts)
		{
			sprintf(buf, "sensor%d.value %f\n", i, watts);
			SendBuffer(socket_fd, buf, strlen(buf));
		}
		else
		{
			total_tmpr += tmpr;
			total_tmpr_count++;
		}<commit_msg>Ignore null-readings<commit_after>/*
 *  CurrentCost_network.c
 *
 *  Created by <NAME>, 2013.
 *  This code is Public Domain.
 */

#include "CurrentCost.h"
#include <unistd.h>
#include <netdb.h>
#include "ctb-0.16/ctb.h"


#define MAX(a,b) ((a)>(b) ? (a) : (b))

#define INITIAL_BUFFER_SIZE        (8*1024)
#define BUFFER_SIZE_GROW_TRESHOLD  (1*1024)


ctb::SerialPort* g_serial_port;
int g_serial_fd = -1;
int g_socket_watts_fd = -1;
int g_socket_tmpr_fd = -1;
int g_max_fd = -1;
fd_set g_readfds;

size_t g_buf_size;
char* g_buf;


bool ExpandBuffer(char*& buf, size_t& buf_size, const size_t bytes_read)
{
	//Double the buffer size
	buf_size += buf_size;
	char* new_buf = new char[buf_size+1];
	if (!new_buf)
		return false;

	memcpy(new_buf, buf, bytes_read);
	delete[] buf;
	buf = new_buf;
	return true;
}

void SendBuffer(int socket_fd, const char* buf, int len)
{
	int offset = 0;
	do {
		offset += send(socket_fd, buf+offset, len-offset, 0);
	} while (offset<len);
}

void DumpMuninData(int socket_fd, bool is_watts)
{
	long start_of_munin_interval = time(NULL) - MUNIN_INTERVAL;
	double watts;
	double tmpr, total_tmpr=0.0;
	int count, total_tmpr_count=0;
	MsgInfo* msg_iter;
	int i;

	char buf[100];
	for (i=0; ENVIR_SENSOR_COUNT>i; i++)
	{
		watts = 0;
		tmpr = 0.0;
		count = 0;
		msg_iter = g_msg_info_list;
		while (msg_iter)
		{
			if (msg_iter->sensor==i && msg_iter->timestamp>=start_of_munin_interval)
			{
				count++;
				watts += msg_iter->watts;
				tmpr += msg_iter->tmpr;
			}
			msg_iter = msg_iter->next;
		}

		if (0 < count)
		{
			watts /= count;
			tmpr /= count;
		}

		if (is_watts)
		{
			sprintf(buf, "sensor%d.value %f\n", i, watts);
			SendBuffer(socket_fd, buf, strlen(buf));
		}
		else if (0.0 != tmpr)
		{
			total_tmpr += tmpr;
			total_tmpr_count++;
		}
{'fraction_non_alphanumeric': 0.14694335389792484, 'fraction_numerical': 0.02075154234436343, 'mean_word_length': 3.7058047493403694, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2718235', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1569, 'n_words': 340}