-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(wip): postgres action listener
- Loading branch information
Showing
10 changed files
with
153 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...t/common/src/main/java/love/broccolai/tickets/minecraft/common/command/AdminCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package love.broccolai.tickets.minecraft.common.command; | ||
|
||
import com.google.inject.Inject; | ||
import java.time.Duration; | ||
import love.broccolai.tickets.api.service.StatisticService; | ||
import love.broccolai.tickets.minecraft.common.model.Commander; | ||
import love.broccolai.tickets.minecraft.common.utilities.DurationFormatter; | ||
import org.incendo.cloud.Command; | ||
import org.incendo.cloud.CommandManager; | ||
import org.incendo.cloud.context.CommandContext; | ||
import org.incendo.cloud.key.CloudKey; | ||
import org.incendo.cloud.parser.standard.DurationParser; | ||
import org.jspecify.annotations.NullMarked; | ||
|
||
import static net.kyori.adventure.text.Component.text; | ||
|
||
@NullMarked | ||
public final class AdminCommands extends AbstractCommand { | ||
|
||
private final static CloudKey<Duration> DURATION_KEY = CloudKey.cloudKey("duration", Duration.class); | ||
private final static Duration FOREVER_DURATION = Duration.ofSeconds(Long.MAX_VALUE); | ||
|
||
private final StatisticService statisticService; | ||
|
||
@Inject | ||
public AdminCommands(final StatisticService statisticService) { | ||
this.statisticService = statisticService; | ||
} | ||
|
||
@Override | ||
public void register(final CommandManager<Commander> commandManager) { | ||
Command.Builder<Commander> root = commandManager | ||
.commandBuilder("ticketsadmin"); | ||
|
||
commandManager.command( | ||
root.literal("stats") | ||
.literal("lifespan") | ||
.optional("duration", DurationParser.durationParser()) | ||
.handler(this::handleLifespan) | ||
); | ||
} | ||
|
||
public void handleLifespan(final CommandContext<Commander> context) { | ||
Commander commander = context.sender(); | ||
Duration search = context.optional(DURATION_KEY) | ||
.orElse(FOREVER_DURATION); | ||
|
||
Duration result = this.statisticService.averageTicketsLifespan(search); | ||
String formattedResult = DurationFormatter.formatDuration(result); | ||
|
||
commander.sendMessage( | ||
text("average ticket lifespan: " + formattedResult) | ||
); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...common/src/main/java/love/broccolai/tickets/minecraft/common/listener/ActionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package love.broccolai.tickets.minecraft.common.listener; | ||
|
||
import com.impossibl.postgres.api.jdbc.PGNotificationListener; | ||
|
||
public final class ActionListener implements PGNotificationListener { | ||
|
||
@Override | ||
public void notification(int processId, String channelName, String payload) { | ||
System.out.println(payload); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...on/src/main/java/love/broccolai/tickets/minecraft/common/utilities/DurationFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package love.broccolai.tickets.minecraft.common.utilities; | ||
|
||
import java.time.Duration; | ||
|
||
public final class DurationFormatter { | ||
|
||
private DurationFormatter() { | ||
} | ||
|
||
/** | ||
* Formats a Duration object into a string of format "Xh Ym". | ||
* | ||
* @param duration The Duration object to format. | ||
* @return A formatted string representing the duration in hours and minutes. | ||
*/ | ||
public static String formatDuration(final Duration duration) { | ||
if (duration.isZero()) { | ||
return "unknown"; | ||
} | ||
|
||
long hours = duration.toHours(); | ||
long minutes = duration.toMinutesPart(); | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
if (hours > 0) { | ||
sb.append(hours).append("h "); | ||
} | ||
if (minutes > 0) { | ||
sb.append(minutes).append("m"); | ||
} | ||
return sb.toString().trim(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters