/*
** SellaNMS
** $Id: tidy.cxx,v 1.7 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

static const char rcsid[] = "$Id: tidy.cxx,v 1.7 2006/01/24 07:08:09 sella Exp $";

#include "tidy.h"
#include "daemon.h"
#include "storage/storagedata.h"
#include "storage/storage.h"
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <sys/stat.h>
#include <stl/slist>
#include <stl/string>
#include <stl/vector>

extern ConfigNode* config;
extern bool interrupt;

Tidy::Tidy() {
}

Tidy::~Tidy() {
}

RESULT Tidy::execute() {
	RESULT result = SUCCESS;

	time_t now, next_output = 0;

   TRACE(TRACE_INTERNAL, "Tidy::execute() starting");

	while (!interrupt) {
      now = time(NULL);

		if (now >= next_output) {
			next_output = now + interval;

			cleanOutput();
		}

		usleep(USLEEP_LONGLOOP_TIME);
	}

	TRACE(TRACE_THREAD, "Tidy::execute() exiting");

	return result;
}

RESULT Tidy::readConfig() {
	RESULT result = SUCCESS;

	ConfigNode node;

	if (!config->assignNode(getConfigPath(), node)) {
		LMSG("No '%s' section found within the config file.", getConfigPath());

		return FAILURE;
	}

	node.assignValue("interval", interval, INTERNAL_TIDY_INTERVAL_DEF, INTERNAL_TIDY_INTERVAL_MIN, INTERNAL_TIDY_INTERVAL_MAX);
	node.assignValue("output-age", outputAge, INTERNAL_TIDY_OUTPUTAGE_DEF, INTERNAL_TIDY_OUTPUTAGE_MIN, INTERNAL_TIDY_OUTPUTAGE_MAX);

	/* Configure Storage */
	if (storage.setConfig(*config) != SUCCESS) {
		result = FAILURE;
	}

	printConfig();

	return result;
}

void Tidy::printConfig() {
	string str;
	vector<string> v;
	vector<string>::iterator v_it;

	if (!TRACE_ISSET(TRACE_CONFIG)) {
		return;
	}

	TRACE(TRACE_CONFIG, "interval: %lu", interval);
	TRACE(TRACE_CONFIG, "output-age: %lu", outputAge);

#if 0
	if (config->assignValues("internal tidy policy input", v)) {
		str.erase();
		for (v_it = v.begin(); v_it != v.end(); ++v_it) {
			str.append(*v_it + " ");
		}
		TRACE(TRACE_CONFIG, "policy input: %s", str.c_str());
	} else {
		TRACE(TRACE_CONFIG, "policy input: none");
	}

	if (config->assignValues("internal tidy policy output", v)) {
		str.erase();
		for (v_it = v.begin(); v_it != v.end(); ++v_it) {
			str.append(*v_it + " ");
		}
		TRACE(TRACE_CONFIG, "policy output: %s", str.c_str());
	} else {
		TRACE(TRACE_CONFIG, "policy output: none");
	}
#endif
}

RESULT Tidy::cleanOutput() {
	RESULT result = SUCCESS;

	StorageData data;

	TRACE(TRACE_DEBUG, "deleting content older than %lu secs from output", outputAge);

	if ((result = storage.queryf(data, "DELETE FROM output WHERE UNIX_TIMESTAMP() - %d > UNIX_TIMESTAMP(timestamp) ", outputAge)) != SUCCESS) {
		LMSG("failed to delete from output");

		interrupt = true;
	} else {
		TRACE(TRACE_DEBUG, "deleted %lu rows from output", data.getNumRowAffected());
	}

	return result;
}

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim:
** vim: noet ts=3 sw=3
*/
