Skip to content

Commit

Permalink
old data compress. minor bug fix. etc
Browse files Browse the repository at this point in the history
  • Loading branch information
7sat committed Feb 18, 2023
1 parent 57b0cc7 commit b3f381a
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>xyz.jpenilla</groupId>
<artifactId>DynamicShop-Graph</artifactId>
<version>0.2.6-custom</version>
<version>0.2.7</version>
<packaging>jar</packaging>

<name>DynamicShop Graph</name>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xyz/jpenilla/dsgraph/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void stopQueueUpdatesTask() {
public void startCleanOldDataTask() {
stopCleanOldDataTask();
cleanOldDataTask = new CleanOldDataTask();
cleanOldDataTask.runTaskTimerAsynchronously(plugin, 20L * 10L, 20L * 60L * 60L * 3L);
cleanOldDataTask.runTaskTimerAsynchronously(plugin, 20L * 10L, 20L * 60L * 60L); // 1시간
}

public void stopCleanOldDataTask() {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/xyz/jpenilla/dsgraph/command/CommandDSGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import co.aikar.commands.BaseCommand;
import co.aikar.commands.CommandHelp;
import co.aikar.commands.annotation.*;
import org.bukkit.Bukkit;
import xyz.jpenilla.dsgraph.DSGraph;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import xyz.jpenilla.dsgraph.config.StockConfig;

@CommandAlias("dsgraph|dsg")
public class CommandDSGraph extends BaseCommand {
Expand All @@ -30,4 +32,18 @@ public void onReload(CommandSender sender) {
plugin.getTaskManager().restart();
sender.sendMessage("Done reloading plugin");
}

@Subcommand("compress")
@CommandPermission("dsgraph.compress")
public void onCompress(CommandSender sender) {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () ->{
sender.sendMessage("compress start");

DSGraph.getInstance().getTaskManager().stopRecordDataTask();
DSGraph.getInstance().getCfg().getFiles().forEach(StockConfig::compress);
DSGraph.getInstance().getTaskManager().startRecordDataTask();

sender.sendMessage("compress end");
});
}
}
3 changes: 3 additions & 0 deletions src/main/java/xyz/jpenilla/dsgraph/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class Config {
@Getter
private int deleteAfterDays;
@Getter
private int compressAfterHours;
@Getter
private int port;
@Getter
private boolean webServer;
Expand Down Expand Up @@ -49,6 +51,7 @@ public void load() {

saveUnchangedData = config.getBoolean(Fields.saveUnchangedData);
deleteAfterDays = config.getInt(Fields.deleteAfterDays);
compressAfterHours = config.getInt(Fields.compressAfterHours);
port = config.getInt(Fields.port);
webServer = config.getBoolean(Fields.webServer);
customHTML = config.getBoolean(Fields.customHTML);
Expand Down
73 changes: 66 additions & 7 deletions src/main/java/xyz/jpenilla/dsgraph/config/StockConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import me.sat7.dynamicshop.DynaShopAPI;
import org.bukkit.inventory.meta.ItemMeta;
import xyz.jpenilla.dsgraph.DSGraph;
import xyz.jpenilla.dsgraph.StockEntry;
Expand Down Expand Up @@ -90,6 +91,9 @@ public void update() {
ItemStack is = new ItemStack(material);
is.setItemMeta(itemMeta);

if(!DynaShopAPI.validateShopName(shopName))
return;

StockEntry newEntry = new StockEntry(shopName, is);
if (!newEntry.equals(lastEntry) || DSGraph.getInstance().getCfg().isSaveUnchangedData()) {
try {
Expand All @@ -106,17 +110,24 @@ public void update() {
}
}

public void clean() {
try {
public void clean()
{
try
{
ArrayList<String[]> al = new ArrayList<>();
CSVReader reader2 = new CSVReader(new FileReader(path));
reader2.readAll().forEach(line -> {
if (!line[0].equals(StockEntry.Fields.Time)) {
reader2.readAll().forEach(line ->
{
if (!line[0].equals(StockEntry.Fields.Time))
{
StockEntry u = new StockEntry(line);
if (u.getLocalDateTime().isAfter(LocalDateTime.now().minusDays(DSGraph.getInstance().getCfg().getDeleteAfterDays()))) {
if (u.getLocalDateTime().isAfter(LocalDateTime.now().minusDays(DSGraph.getInstance().getCfg().getDeleteAfterDays())))
{
al.add(u.getRecord());
}
} else {
}
else
{
al.add(StockEntry.getHeader());
}
});
Expand All @@ -125,7 +136,55 @@ public void clean() {
CSVWriter writer = new CSVWriter(sw);
writer.writeAll(al);
writer.close();
} catch (Exception e) {
} catch (Exception e)
{
e.printStackTrace();
}
}

public void compress()
{
try
{
ArrayList<String[]> al = new ArrayList<>();
CSVReader reader2 = new CSVReader(new FileReader(path));

LocalDateTime lastTime = null;

for (String[] line : reader2.readAll())
{
if (!line[0].equals(StockEntry.Fields.Time))
{
StockEntry u = new StockEntry(line);

if (lastTime == null)
lastTime = u.getLocalDateTime();

int trimVal = 0;

if (u.getLocalDateTime().isBefore(LocalDateTime.now().minusHours(DSGraph.getInstance().getCfg().getCompressAfterHours())))
{
trimVal = 10;
}

if (trimVal == 0 || u.getLocalDateTime().isAfter(lastTime.plusMinutes(trimVal)))
{
al.add(u.getRecord());
lastTime = u.getLocalDateTime();
}

} else
{
al.add(StockEntry.getHeader());
}
}

FileWriter sw = new FileWriter(path);
CSVWriter writer = new CSVWriter(sw);
writer.writeAll(al);
writer.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/xyz/jpenilla/dsgraph/task/CleanOldDataTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import org.bukkit.scheduler.BukkitRunnable;

public class CleanOldDataTask extends BukkitRunnable {

@Override
public void run() {
DSGraph.getInstance().getTaskManager().stopRecordDataTask();
DSGraph.getInstance().getCfg().getFiles().forEach(StockConfig::compress);
DSGraph.getInstance().getCfg().getFiles().forEach(StockConfig::clean);
DSGraph.getInstance().getTaskManager().startRecordDataTask();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void run() {
new Gson().toJson(datas, writer);
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
//ex.printStackTrace();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
saveUnchangedData: true
deleteAfterDays: 120
compressAfterHours: 3
webServer: true
port: 8180
customHTML: false
Expand Down
11 changes: 5 additions & 6 deletions src/main/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0" />
<title>DynamicShop-Graph</title>

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" type="text/css"
rel="stylesheet" media="screen,projection"/>
rel="stylesheet" media="screen,projection" />
<style>
body {
display: flex;
Expand All @@ -19,8 +19,7 @@
main {
flex: 1 0 auto;
}

</style>
</style>
</head>

<body>
Expand Down Expand Up @@ -107,7 +106,7 @@
var $newOpt = $("<option>").attr("value", newValue).text(a[i]);
$("#data-select").append($newOpt);
}
$('select').formSelect();
$("#data-select").formSelect()
});

$('#data-select').bind('change', function () {
Expand Down

0 comments on commit b3f381a

Please sign in to comment.