<commit_before>package provider;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.TimeUnit;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.InfluxDB.ConsistencyLevel;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Pong;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import net.dv8tion.jda.JDA;
import net.dv8tion.jda.OnlineStatus;
import net.dv8tion.jda.entities.User;
import net.dv8tion.jda.events.message.guild.GuildMessageReceivedEvent;

public class Statistics extends Thread {
	private static Statistics instance;
	private static InfluxDB influxDB;
	private BatchPoints batchPoints;
	private static JDA jda;

	class InfluxInfo {
		String IP;
		String US;
		String PW;
	}

	private Statistics() {
		
	}
	
	static {
		instance = new Statistics();
	}
	
	public static Statistics getInstance() {
		return instance;
	}
	
	public void connect(JDA jda) {
		Gson gson = new Gson();
		try {
			JsonReader jReader = new JsonReader(new FileReader("./influxinfo.json"));
			InfluxInfo info = gson.fromJson(jReader, InfluxInfo.class);
			
			Statistics.influxDB = InfluxDBFactory.connect(info.IP, info.US, info.PW);
			Statistics.jda = jda;
			
			boolean connected = false;
			do {
				Pong response;
				try {
					response = influxDB.ping();
					if (!response.getVersion().equalsIgnoreCase("unknown")) {
						connected = true;
					}

					Thread.sleep(10L);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} while (!connected);
			System.out.println("[InfluxDB] connected. Version: " + influxDB.version());
			
			batchPoints = BatchPoints
					.database("kok_monitor")
					.tag("async", "true")
					.retentionPolicy("default")
					.consistency(ConsistencyLevel.ALL)
					.build();
			
			this.start();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void run() {
		while (true) {
			try {
				//update statistics
				int onlineUser = 0;
				for(User user : jda.getUsers()) {
					if (!user.getOnlineStatus().equals(OnlineStatus.OFFLINE))
						onlineUser++;
				}
				Point users = Point.measurement("users")
						.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						.addField("online", onlineUser)
						.addField("total", jda.getUsers().size())
						.build();
				
				Point system = Point.measurement("system")
						.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						.addField("used_ram", (double)(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024)
						.addField("total_ram", (double) Runtime.getRuntime().maxMemory() / 1024 / 1024)
						.addField("no_threads", Thread.getAllStackTraces().keySet().size())
						.build();
				
				batchPoints.point(system);
				batchPoints.point(users);
				influxDB.write(batchPoints);
				
				Thread.sleep(1000);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void logMessage(GuildMessageReceivedEvent event) {
		int noOfMentions = event.getMessage().getMentionedUsers().size();
		if (event.getMessage().mentionsEveryone())
			noOfMentions = event.getGuild().getUsers().size();
		
		Point messages = Point.measurement("messages")
				.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
				.addField("content_length", event.getMessage().getContent().length())
				.addField("channel", event.getChannel().getName())
				.addField("no_mentions", noOfMentions)
				.build();
		
		batchPoints.point(messages);
	}
	
	public void logCommandReceived(String commandName) {
		Point commands = Point.measurement("commands")
				.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
				.addField("name", commandName)
				.build();
		
		batchPoints.point(commands);
	}

	
	public void logKarma(String action) {
		Point karma = Point.measurement("karma")
				.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
				.addField("action", action)
				.build();
		
		batchPoints.point(karma);
	}
	
}<commit_msg>Fixed concurrency errors for the statistics db updates<commit_after>package provider;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.TimeUnit;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Pong;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import net.dv8tion.jda.JDA;
import net.dv8tion.jda.OnlineStatus;
import net.dv8tion.jda.entities.User;
import net.dv8tion.jda.events.message.guild.GuildMessageReceivedEvent;

public class Statistics extends Thread {
	private static Statistics instance;
	private static InfluxDB influxDB;
	private static String dbName = "kok_monitor";
	private static JDA jda;

	class InfluxInfo {
		String IP;
		String US;
		String PW;
	}

	private Statistics() {
		
	}
	
	static {
		instance = new Statistics();
	}
	
	public static Statistics getInstance() {
		return instance;
	}
	
	public void connect(JDA jda) {
		Gson gson = new Gson();
		try {
			JsonReader jReader = new JsonReader(new FileReader("./influxinfo.json"));
			InfluxInfo info = gson.fromJson(jReader, InfluxInfo.class);
			
			Statistics.influxDB = InfluxDBFactory.connect(info.IP, info.US, info.PW);
			Statistics.jda = jda;
			
			boolean connected = false;
			do {
				Pong response;
				try {
					response = influxDB.ping();
					if (!response.getVersion().equalsIgnoreCase("unknown")) {
						connected = true;
					}

					Thread.sleep(10L);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} while (!connected);
			influxDB.enableBatch(2000, 1000, TimeUnit.MILLISECONDS);
			System.out.println("[InfluxDB] connected. Version: " + influxDB.version());
			
			this.start();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void run() {
		while (true) {
			try {
				//update statistics
				int onlineUser = 0;
				for(User user : jda.getUsers()) {
					if (!user.getOnlineStatus().equals(OnlineStatus.OFFLINE))
						onlineUser++;
				}
				Point users = Point.measurement("users")
						.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						.addField("online", onlineUser)
						.addField("total", jda.getUsers().size())
						.build();
				influxDB.write(dbName, "default", users);
				
				Point system = Point.measurement("system")
						.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
						.addField("used_ram", (double)(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024)
						.addField("total_ram", (double) Runtime.getRuntime().maxMemory() / 1024 / 1024)
						.addField("no_threads", Thread.getAllStackTraces().keySet().size())
						.build();
				influxDB.write(dbName, "default", system);
				
				Thread.sleep(1000);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void logMessage(GuildMessageReceivedEvent event) {
		int noOfMentions = event.getMessage().getMentionedUsers().size();
		if (event.getMessage().mentionsEveryone())
			noOfMentions = event.getGuild().getUsers().size();
		
		Point messages = Point.measurement("messages")
				.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
				.addField("content_length", event.getMessage().getContent().length())
				.addField("channel", event.getChannel().getName())
				.addField("no_mentions", noOfMentions)
				.build();
		influxDB.write(dbName, "default", messages);
	}
	
	public void logCommandReceived(String commandName) {
		Point commands = Point.measurement("commands")
				.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
				.addField("name", commandName)
				.build();
		influxDB.write(dbName, "default", commands);
	}

	
	public void logKarma(String action) {
		Point karma = Point.measurement("karma")
				.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
				.addField("action", action)
				.build();
		influxDB.write(dbName, "default", karma);
	}
	
}
{'fraction_non_alphanumeric': 0.13723524251159294, 'fraction_numerical': 0.007770397292893846, 'mean_word_length': 3.7841726618705036, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1204258', 'n_tokens_mistral': 3163, 'n_tokens_neox': 2553, 'n_words': 481}