org.jlot.stats.client.StatsDumper Maven / Gradle / Ivy
package org.jlot.stats.client;
import java.util.Map;
import org.jlot.client.remote.StatsPushRestCommand;
import org.jlot.core.form.StatsForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.HttpServerErrorException;
public class StatsDumper implements Runnable
{
private StatsHarvester statsHarvester;
private StatsPushRestCommand statsPushRestCommand;
private String projectName;
private static final Logger logger = LoggerFactory.getLogger(StatsDumper.class);
public StatsDumper ( String projectName, StatsHarvester statsHarvester, StatsPushRestCommand statsPushRestCommand )
{
this.projectName = projectName;
this.statsHarvester = statsHarvester;
this.statsPushRestCommand = statsPushRestCommand;
}
public void dumpStats ( )
{
try
{
Map map = statsHarvester.getMap();
logger.info("Sending stats, size: {}", map.size());
StatsForm statsForm = new StatsForm();
statsForm.setProjectName(projectName);
statsForm.setTokenStats(map);
statsPushRestCommand.execute(statsForm);
statsHarvester.reset();
}
catch (HttpServerErrorException e)
{
logger.error("Response Body: {}, StatusCode {}", e.getResponseBodyAsString(), e.getStatusCode());
logger.error(e.getMessage(), e);
}
catch (Throwable e)
{
logger.error(e.getMessage(), e);
}
finally
{
logger.info("Finished sending stats");
}
}
@Override
public void run ( )
{
dumpStats();
}
}