<commit_before>#include "stats.h"

gboolean test_stats_enabled = FALSE;
GList *test_stats_queue = NULL;

RaucStats *r_stats_new(const gchar *label)
{
	RaucStats *stats = g_new0(RaucStats, 1);

	stats->label = g_strdup(label);
	stats->min = G_MAXDOUBLE;
	stats->max = G_MINDOUBLE;

	return stats;
}

void r_stats_add(RaucStats *stats, gdouble value)
{
	g_return_if_fail(stats);

	stats->values[stats->next] = value;
	stats->next = (stats->next + 1) % 64;
	stats->count++;

	stats->sum += value;

	if (value < stats->min)
		stats->min = value;
	if (value > stats->max)
		stats->max = value;
}

gdouble r_stats_get_avg(const RaucStats *stats)
{
	g_return_val_if_fail(stats, 0.0);

	if (stats->count)
		return stats->sum / stats->count;
	else
		return 0.0;
}

gdouble r_stats_get_recent_avg(const RaucStats *stats)
{
	gdouble sum = 0.0;
	guint64 count = stats->count;

	g_return_val_if_fail(stats, 0.0);

	if (count > 64)
		count = 64;

	for (unsigned int i = 0; i < count; i++)
		sum += stats->values[i];

	if (count)
		return sum / count;
	else
		return 0.0;
}

void r_stats_show(const RaucStats *stats, const gchar *prefix)
{
	g_autoptr(GString) msg = g_string_sized_new(128);

	g_return_if_fail(stats);
	g_return_if_fail(prefix);

	g_string_append_printf(msg, "%s: count=%"G_GUINT64_FORMAT, prefix, stats->count);
	if (!stats->count)<commit_msg>src/stats: combine prefix and label in r_stats_show<commit_after>#include "stats.h"

gboolean test_stats_enabled = FALSE;
GList *test_stats_queue = NULL;

RaucStats *r_stats_new(const gchar *label)
{
	RaucStats *stats = g_new0(RaucStats, 1);

	stats->label = g_strdup(label);
	stats->min = G_MAXDOUBLE;
	stats->max = G_MINDOUBLE;

	return stats;
}

void r_stats_add(RaucStats *stats, gdouble value)
{
	g_return_if_fail(stats);

	stats->values[stats->next] = value;
	stats->next = (stats->next + 1) % 64;
	stats->count++;

	stats->sum += value;

	if (value < stats->min)
		stats->min = value;
	if (value > stats->max)
		stats->max = value;
}

gdouble r_stats_get_avg(const RaucStats *stats)
{
	g_return_val_if_fail(stats, 0.0);

	if (stats->count)
		return stats->sum / stats->count;
	else
		return 0.0;
}

gdouble r_stats_get_recent_avg(const RaucStats *stats)
{
	gdouble sum = 0.0;
	guint64 count = stats->count;

	g_return_val_if_fail(stats, 0.0);

	if (count > 64)
		count = 64;

	for (unsigned int i = 0; i < count; i++)
		sum += stats->values[i];

	if (count)
		return sum / count;
	else
		return 0.0;
}

void r_stats_show(const RaucStats *stats, const gchar *prefix)
{
	g_autofree gchar *prefix_label = NULL;
	g_autoptr(GString) msg = g_string_sized_new(128);

	g_return_if_fail(stats);

	if (prefix) {
		prefix_label = g_strdup_printf("%s %s", prefix, stats->label);
	} else {
		prefix_label = g_strdup(stats->label);
	}

	g_string_append_printf(msg, "%s: count=%"G_GUINT64_FORMAT, prefix_label, stats->count);
	if (!stats->count)
{'fraction_non_alphanumeric': 0.1450381679389313, 'fraction_numerical': 0.018736988202637056, 'mean_word_length': 4.587209302325581, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, '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': '2297598', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1159, 'n_words': 252}