Skip to content

Commit

Permalink
Optimize CSV Handler
Browse files Browse the repository at this point in the history
Optimize CSV Handler and start implementing logging.

     + Optimize CSV handler by removing buy/sell data from csv as its not needed for trade.csv.
     + Update logging to use logging levels.
     + Remove trade.html as it will be revamped and it doesn't work with the new trade.csv.
     + Removed unused values in config.yml.
  • Loading branch information
noahbclarkson committed Jun 16, 2022
1 parent e0ccc7b commit 9b37064
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 223 deletions.
11 changes: 9 additions & 2 deletions src/main/java/unprotesting/com/github/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ public void onEnable() {
setupDataFiles();
mm = MiniMessage.miniMessage();
UtilFunctions.setDf(new DecimalFormat(Config.getConfig().getNumberFormat()));
getLogger().setLevel(Level.parse(Config.getConfig().getLogLevel().toUpperCase()));
Level logLevel = Level.parse(Config.getConfig().getLogLevel());
getLogger().setLevel(logLevel);
getLogger().info("Log level set to " + getLogger().getLevel().toString());
new Messages();

Bukkit.getScheduler().runTaskAsynchronously(this, () ->
Expand All @@ -112,9 +114,11 @@ public void onEnable() {
*/
public void updateTimePeriod() {

getLogger().config("Updating time period.");
new TimePeriod().addToMap();
cache = new LocalDataCache();
if (Config.getConfig().isWebServer()) {
if (Config.getConfig().isWebServerEnabled()) {
getLogger().fine("Writing CSV file");
CsvHandler.writeCsv();
}

Expand All @@ -128,6 +132,7 @@ private void checkEconomy() {

// Check if an economy plugin is installed and if it is enabled, if it is return.
if (EconomyFunctions.setupLocalEconomy(getServer())) {
getLogger().info("Found an economy plugin.");
return;
}

Expand Down Expand Up @@ -225,11 +230,13 @@ private void getEssentials() {
// If essentials is not installed, set essentialsEnabled to false and return.
if (plugin == null) {
setEssentialsEnabled(false);
getLogger().info("Essentials not found.");
return;
}

setEssentialsEnabled(true);
setEss((IEssentials) plugin);
getLogger().info("Found Essentials.");

}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/unprotesting/com/github/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Config {
@Getter
private static Config config;

private boolean webServer;
private boolean webServerEnabled;
private boolean sellPriceDifferenceVariationEnabled;
private boolean tutorial;
private boolean disableMaxBuysSells;
Expand Down Expand Up @@ -66,7 +66,7 @@ private void loadConfig() {
FileConfiguration configF = Main.getInstance().getDataFiles().getConfig();
setApiKey(configF.getString("apiKey", "xyz"));
setEmail(configF.getString("email", "[email protected]"));
setWebServer(configF.getBoolean("web-server-enabled", true));
setWebServerEnabled(configF.getBoolean("web-server-enabled", true));
setPort(configF.getInt("port", 8123));
setTimePeriod(configF.getInt("time-period", 30));
setMenuTitle(configF.getString("menu-title", "Shop"));
Expand Down
102 changes: 35 additions & 67 deletions src/main/java/unprotesting/com/github/data/csv/CsvHandler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package unprotesting.com.github.data.csv;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import unprotesting.com.github.Main;
Expand All @@ -21,109 +21,77 @@ public class CsvHandler {
public static void writeCsv() {

try {
write("trade", 0);
write("trade.csv", 0);
} catch (IOException e) {
Main.getInstance().getLogger().severe("Could not write to csv file.");
}

}

/**
* Creates a CSV file with all price, buy and sell data to be read by the webpage.
* @param csvName The name of the csv file.
* @param cutoffValue The cutoff value for the data.
* @throws IOException If the file could not be created.
* Write the latest x rows of price data.
* @param fileName The name of the file to write to.
* @param rows The number of rows to write.
*/
private static void write(String csvName, int cutoffValue) throws IOException {

int cutoff = cutoffValue;
FileWriter writer = new FileWriter("plugins/Auto-Tune/web/" + csvName + ".csv");
private static void write(String fileName, int rows) throws IOException {
FileWriter fileStream = new FileWriter(Config.getConfig().getDataLocation() + fileName);
BufferedWriter writer = new BufferedWriter(fileStream);
int size = Main.getInstance().getDatabase().getMap().size();
TimePeriod tp = Main.getInstance().getDatabase().getMap().get(size - 1);
List<String> items = Arrays.asList(tp.getItemTP().getItems());
List<String> enchantments = Arrays.asList(tp.getEnchantmentsTP().getItems());
writer.write("GDP,Balance,Debt,Loss,Inflation");

if (size < cutoff || cutoff < 3) {
cutoff = size;
if (rows == -1) {
rows = size;
}

Collections.sort(items);
Collections.sort(enchantments);


for (String item : items) {

writer.write("\n" + "%" + item + "\n");

for (int i = (size - cutoff); i < size; i++) {

ItemTimePeriod itp = Main.getInstance().getDatabase().getMap().get(i).getItemTP();
int pos = Arrays.asList(itp.getItems()).indexOf(item);

writer.append(i + "," + itp.getPrices()[pos] + ","
+ itp.getBuys()[pos] + "," + itp.getSells()[pos] + "\n");

}

writer.write(item + ",");
}

if (Config.getConfig().isEnableEnchantments()) {

for (String enchantment : enchantments) {
writer.write(enchantment.toUpperCase() + ",");
}
}

writer.write("\n" + "%" + enchantment + "\n");

for (int i = (size - cutoff); i < size; i++) {
writer.write("\n");

EnchantmentsTimePeriod etp = Main.getInstance().getDatabase()
.getMap().get(i).getEnchantmentsTP();
for (int i = (size - rows); i < size; i++) {
GdpTimePeriod gtp = Main.getInstance().getDatabase().getMap().get(i).getGdpTP();

int pos = Arrays.asList(etp.getItems()).indexOf(enchantment);
writer.write(gtp.getGdp() + ","
+ gtp.getBalance() + ","
+ gtp.getDebt() + ","
+ gtp.getLoss() + ","
+ gtp.getInflation());

writer.append(i + "," + etp.getPrices()[pos] + ","
+ etp.getBuys()[pos] + "," + etp.getSells()[pos] + "\n");
ItemTimePeriod itp = Main.getInstance().getDatabase().getMap().get(i).getItemTP();

}
EnchantmentsTimePeriod etp = Main.getInstance()
.getDatabase().getMap().get(i).getEnchantmentsTP();

if (itp == null) {
continue;
}

}

for (int k = 0; k < 5; k++) {

if (k == 0) {
writer.write("\n" + "%GDP" + "\n");
} else if (k == 1) {
writer.write("\n" + "%Balance" + "\n");
} else if (k == 2) {
writer.write("\n" + "%Debt" + "\n");
} else if (k == 3) {
writer.write("\n" + "%Loss" + "\n");
} else if (k == 4) {
writer.write("\n" + "%Inflation" + "\n");
for (int j = 0; j < itp.getItems().length; j++) {
writer.write("," + itp.getPrices()[j]);
}

for (int i = (size - cutoff); i < size; i++) {
if (Config.getConfig().isEnableEnchantments()) {

GdpTimePeriod gtp = Main.getInstance().getDatabase().getMap().get(i).getGdpTP();

if (k == 0) {
writer.write(i + "," + gtp.getGdp() + "\n");
} else if (k == 1) {
writer.write(i + "," + gtp.getBalance() + "\n");
} else if (k == 2) {
writer.write(i + "," + gtp.getDebt() + "\n");
} else if (k == 3) {
writer.write(i + "," + gtp.getLoss() + "\n");
} else if (k == 4) {
writer.write(i + "," + gtp.getInflation() + "\n");
for (int j = 0; j < etp.getItems().length; j++) {
writer.write("," + etp.getPrices()[j]);
}

}

}

writer.close();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ public PriceUpdateEvent(boolean isAsync) {
*/
private void calculateAndLoad() {

int playerCount = UtilFunctions.calculatePlayerCount();

// If the player count is less than "update-prices-threshold" then don't update the prices.
if (UtilFunctions.calculatePlayerCount() < Config.getConfig().getUpdatePricesThreshold()) {
if (playerCount < Config.getConfig().getUpdatePricesThreshold()) {

Main.getInstance().getLogger().info("Player count is less than "
+ Config.getConfig().getUpdatePricesThreshold()
+ " so not updating prices.");

return;
}

Main.getInstance().getLogger().info("Calculating prices as player count is " + playerCount);

Main.getInstance().updateTimePeriod();
updateItems();
Expand All @@ -65,13 +74,21 @@ private void updateItems() {
continue;

}

ItemData data = Main.getInstance().getCache().getItems().get(item);
double initialPrice = data.getPrice();
double[] sb = loadAverageBuySellValue(item, false);
double[] volatility = getMaxMinVolatility("shops", item);
data.setPrice(UtilFunctions.calculateNewPrice(data.getPrice(), volatility, sb[0], sb[1]));
Main.getInstance().getCache().getItems().put(item, data);

if (data.getPrice() != initialPrice) {

Main.getInstance().getLogger().info("Updated price of "
+ item + " from " + initialPrice + " to " + data.getPrice());

}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ private void updateSellPriceDifference() {

// If the new sell price difference is greater than the minimum then update it.
if (newSpd > Main.getInstance().getDataFiles().getConfig()
.getDouble("sell-price-difference", 10)) {
.getDouble("sell-price-difference", 15)) {

Main.getInstance().getCache().getEconomyInfo().updateSellPriceDifference(newSpd);

}

Main.getInstance().getLogger().finer("Sell price difference changed from "
+ spd + " to " + newSpd);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class LocalServer {
public LocalServer() {

// If "web-server-enabled" is false, don't start the server.
if (!Config.getConfig().isWebServer()) {
if (!Config.getConfig().isWebServerEnabled()) {
return;
}

Expand Down
6 changes: 0 additions & 6 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@

## -- General Settings -- ##

## API key given on sign-up
api-key: 'xyz'

## Email used on sign-up
email: '[email protected]'

## Enable/Disable integrated Web Server.
## Info: Use /trade to view the web-server
web-server-enabled: true
Expand Down
Loading

0 comments on commit 9b37064

Please sign in to comment.