96: Extract Perps sample data into EvelynPerpsService and poll it from EMC
This commit is contained in:
@@ -431,3 +431,23 @@ minuts fast forsinkelse, og et punkt persisteres før publicering i et immutable
|
||||
hukommelsen og beholder navnereservationen, restart genindlæser filen, og `close()` frigiver navnet efter sikker
|
||||
terminering. `EvelynStatusIndexPoint` bliver i sin eksisterende package med alle seks indeksfelter; kun Evelyn
|
||||
Price Index beregnes og vises, mens de øvrige fem felter fortsat er nul. Legacy-rebalancering er fortsat inaktiv.
|
||||
|
||||
## 12. Perps-service og Mission Control
|
||||
|
||||
`com.r35157.evelyn.service.perps.EvelynPerpsService` ejer positionsdata og udleverer hele det aktuelle,
|
||||
immutable `PerpsSnapshot` gennem `getPositionsSnapshot()`. Snapshot'et indeholder wallets, deres venues og
|
||||
positioner med stabile ID'er blandt søskende, adskilt fra visningsnavne. Positioner indeholder instrument,
|
||||
`LONG`/`SHORT` og decimal USD-størrelse. Svaret indeholder ingen JavaFX-elementer, farver eller formatering.
|
||||
|
||||
`impl.ref.EvelynPerpsServiceImpl` implementerer desuden `NenjimProcess`. Hub'en registrerer og starter det samme
|
||||
objekt som `evelyn.perps.default` før EMC. Før `start()` afvises læsning; gentagen start afvises også.
|
||||
Service-API'et eksponerer kun læsning, ingen lifecycle- eller positionsændringer. Servicen ejer de otte
|
||||
uændrede eksempelpositioner fra den oprindelige Perps-fane og har ingen workers eller Jupiter/Phoenix-kald.
|
||||
|
||||
EMC læser snapshot'et straks ved åbning og hvert tredje sekund på en separat worker, uafhængigt af en eventuel
|
||||
senere integrations opdateringsinterval. Begge miljøfaner viser fortsat de samme tydeligt markerede eksempler;
|
||||
EMC ejer træstruktur, USD-formatering og grønne/røde rækker. Uændrede snapshots berører ikke eksisterende rækker,
|
||||
foldestatus eller markering. Ved ændringer genbruges træelementer efter deres ID'er inden for samme forælder.
|
||||
UI-opdateringer sker kun på JavaFX-tråden. Fejl bevarer visningen og stopper ikke næste pollingforsøg.
|
||||
Skjul/luk stopper polling; genåbning starter én ny løkke og afviser gamle resultater. Servicen fortsætter uafhængigt
|
||||
af vinduet. Der er ingen rigtig integration, simulerede ændringer, Refresh-knap eller handel i denne service.
|
||||
|
||||
@@ -271,6 +271,7 @@ jupiter-perps-alarm.default
|
||||
evelyn.status.prod
|
||||
evelyn.status.test
|
||||
evelyn.iou-burner.prod
|
||||
evelyn.perps.default
|
||||
nenjim.javafx-runtime.service
|
||||
evelyn.mission-control.default
|
||||
nenjim.process-manager.default
|
||||
@@ -283,6 +284,13 @@ navngivne `Production`- og `Test`-instanser gennem `EvelynStatusServiceImpl` og
|
||||
`EvelynStatusService`-views i Mission Control. Process Manager viser de nye ID'er fra Hub'ens startup-snapshot.
|
||||
Historik ligger fortsat i `data/evelyn/<instance-name>/status.log` med formatversion 1 og kræver ingen migration.
|
||||
|
||||
Mission Control kræver også `evelyn.perps.default` før `evelyn.mission-control.default` i den private
|
||||
`conf/NenjimHub.conf`. Hub'en konstruerer, registrerer og starter `EvelynPerpsServiceImpl` som en separat
|
||||
proces og injicerer samme objekts read-only `EvelynPerpsService`-view i EMC. Process Manager viser ID'et efter
|
||||
vellykket startup. Den lokale udviklingskonfiguration vælger nu servicen før EMC; andre deployments med EMC
|
||||
skal tilføje samme linje. Udeladt eller forkert rækkefølge giver en tydelig konstruktionsfejl uden automatisk
|
||||
indsættelse. Servicen bruger kun uændrede in-memory-eksempler og foretager ingen eksterne positionskald.
|
||||
|
||||
Hvis `evelyn.iou-burner.prod` vælges, skal både `assetaz.ticker.default` og
|
||||
`evelyn.status.prod` stå før den. Hub'en injicerer Production status-servicen til
|
||||
referenceprisopslag før nye buybacks; en manglende eller senere status-forgænger
|
||||
|
||||
@@ -28,10 +28,29 @@ The overview SHALL include hardcoded Jupiter and Phoenix venues with multiple sa
|
||||
- **AND** only position rows use direction-based backgrounds, with direction also conveyed by text
|
||||
|
||||
### Requirement: Prototype is clearly labeled and entirely local
|
||||
The Perps view SHALL explicitly label its data as sample data and explain that Phoenix rows are illustrative without an existing integration. It SHALL remain entirely within EMC, using no network calls, service changes, service communication, Refresh button, or simulated live updates for the prototype.
|
||||
The Perps view SHALL explicitly label its data as sample data and explain that Phoenix rows are illustrative without an existing integration. It SHALL obtain positions only from the Perps service's current in-memory snapshot, with no external network calls, Refresh button, or simulated position changes. EMC SHALL own all UI components, row colors and formatting.
|
||||
|
||||
#### Scenario: Inspect the prototype boundary
|
||||
- **WHEN** the Perps tab is displayed
|
||||
- **THEN** sample-data and illustrative-Phoenix labels are visible
|
||||
- **AND** there is no Refresh button or automated update of sample positions
|
||||
- **AND** displaying and interacting with the prototype uses only local EMC data
|
||||
- **AND** there is no Refresh button or simulated update of sample positions
|
||||
- **AND** displaying and interacting with the prototype uses only the service's local sample snapshot
|
||||
|
||||
### Requirement: Asynchronous polling follows window visibility
|
||||
EMC SHALL request the current Perps snapshot immediately on opening and every three seconds while visible, without blocking the UI thread. UI updates SHALL occur only on the JavaFX thread. Hiding the window SHALL stop polling; reopening SHALL create only one active polling loop and ignore results belonging to an earlier window session. Poll failures SHALL preserve the current view and allow subsequent attempts.
|
||||
|
||||
#### Scenario: Read takes time or fails
|
||||
- **WHEN** a snapshot read is delayed or fails
|
||||
- **THEN** the UI remains responsive and the existing view is retained
|
||||
- **AND** later polling remains possible without overlapping scheduled reads
|
||||
|
||||
#### Scenario: Hide and reopen
|
||||
- **WHEN** the owner hides and reopens EMC repeatedly
|
||||
- **THEN** each visible session has only one polling loop and obsolete results cannot update its views
|
||||
|
||||
### Requirement: Polling preserves Perps view state
|
||||
The Perps table SHALL retain matching wallet, venue and position identities across snapshots. Repeated unchanged snapshots SHALL leave existing rows, expansion, selection and formatting untouched without flicker. New groups SHALL be expanded by default, and future added or removed positions SHALL be reconciled by identity without resetting retained groups.
|
||||
|
||||
#### Scenario: Poll unchanged positions
|
||||
- **WHEN** a user collapses a group or selects a position and repeated snapshots are unchanged
|
||||
- **THEN** the same rows remain, with the same expansion and selection and no rebuild or refresh
|
||||
|
||||
@@ -89,9 +89,9 @@ After validating the entire plan, the Hub SHALL construct the configured Registr
|
||||
- **THEN** its deployment configuration, dedicated signer/wallet, Jupiter service and direct Raydium service are neither loaded nor constructed nor registered
|
||||
|
||||
### Requirement: Selected processes are constructed and started lazily in exact order
|
||||
The Hub SHALL support the modern process IDs `nenjim.registry.service`, `nenjim.journal.service`, `assetaz.ticker.default`, `jupiter-perps-alarm.default`, `evelyn.status.prod`, `evelyn.status.test`, `evelyn.iou-burner.prod`, `evelyn.liquidity.prod`, `nenjim.javafx-runtime.service`, `evelyn.mission-control.default`, and `nenjim.process-manager.default` through implementation-only construction capability that creates no process instance during validation. After passive composition, the Hub SHALL visit every configured entry after Registry in declared order, beginning with Journal. At each entry it SHALL construct only that process and any explicitly composed process-specific passive dependencies or administration view, register the process under the exact configured ID, resolve it through the canonical `NenjimProcess` contract where applicable, call `start()`, and record the ID only after successful return.
|
||||
The Hub SHALL support the modern process IDs `nenjim.registry.service`, `nenjim.journal.service`, `assetaz.ticker.default`, `jupiter-perps-alarm.default`, `evelyn.status.prod`, `evelyn.status.test`, `evelyn.iou-burner.prod`, `evelyn.liquidity.prod`, `evelyn.perps.default`, `nenjim.javafx-runtime.service`, `evelyn.mission-control.default`, and `nenjim.process-manager.default` through implementation-only construction capability that creates no process instance during validation. After passive composition, the Hub SHALL visit every configured entry after Registry in declared order, beginning with Journal. At each entry it SHALL construct only that process and any explicitly composed process-specific passive dependencies or administration view, register the process under the exact configured ID, resolve it through the canonical `NenjimProcess` contract where applicable, call `start()`, and record the ID only after successful return.
|
||||
|
||||
The production IOU burner SHALL receive the already constructed Production Evelyn status service, and SHALL require both Ticker and `evelyn.status.prod` to precede `evelyn.iou-burner.prod`. The configuration SHALL be the complete manually ordered process dependency closure. The Hub SHALL not discover, insert, recursively construct, reorder, or automatically start dependencies. A process omitted from `[AUTO_START]` SHALL not be constructed, registered, started, or recorded, even when its implementation is available. Legacy process prototypes outside the modern Hub graph SHALL not be selectable.
|
||||
The production IOU burner SHALL receive the already constructed Production Evelyn status service, and SHALL require both Ticker and `evelyn.status.prod` to precede `evelyn.iou-burner.prod`. EMC SHALL receive the already started `evelyn.perps.default` service, which SHALL precede EMC in the deployment autostart list. The configuration SHALL be the complete manually ordered process dependency closure. The Hub SHALL not discover, insert, recursively construct, reorder, or automatically start dependencies. A process omitted from `[AUTO_START]` SHALL not be constructed, registered, started, or recorded, even when its implementation is available. Legacy process prototypes outside the modern Hub graph SHALL not be selectable.
|
||||
|
||||
#### Scenario: Configured process starts successfully
|
||||
- **WHEN** a configured process entry is reached and its dependencies are already available
|
||||
@@ -133,6 +133,14 @@ The production IOU burner SHALL receive the already constructed Production Evely
|
||||
- **WHEN** the burner is reached without a previously constructed Production status service
|
||||
- **THEN** construction fails clearly without creating or starting the omitted dependency or the burner
|
||||
|
||||
#### Scenario: EMC has its Perps dependency
|
||||
- **WHEN** `evelyn.perps.default` appears before EMC in the deployment's ordered autostart list
|
||||
- **THEN** the Hub registers and starts that service and injects its service view into EMC
|
||||
|
||||
#### Scenario: EMC precedes Perps
|
||||
- **WHEN** EMC is selected without an earlier Perps process
|
||||
- **THEN** construction fails clearly without inserting or starting that dependency
|
||||
|
||||
### Requirement: Configured startup is fail-fast and reports actual milestones
|
||||
Registry and every selected process SHALL be registered before its `start()` call and appended to the Hub's immutable ordered startup snapshot only after `start()` returns successfully. Construction failure SHALL leave the selected process unregistered. Registration or startup failure SHALL stop the plan before any later entry is constructed or started; a startup failure MAY leave that process registered but SHALL not record it. Earlier successful processes SHALL remain in snapshots, no rollback or retry SHALL occur, and the Hub SHALL announce its existing online point and enter its blocking runtime wait only after every configured process succeeds.
|
||||
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package com.r35157.evelyn.emc.impl.ref;
|
||||
|
||||
import com.r35157.evelyn.service.status.EvelynStatusService;
|
||||
import com.r35157.evelyn.service.perps.EvelynPerpsService;
|
||||
import com.r35157.evelyn.service.perps.PerpsSnapshot;
|
||||
import com.fanitas.evelyn.core.EvelynStatusIndexPoint;
|
||||
import com.r35157.evelyn.emc.EvelynMissionControl;
|
||||
import com.r35157.nenjim.process.NenjimProcess;
|
||||
import com.r35157.service.javafx.JavaFxRuntimeService;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import javafx.animation.KeyFrame;
|
||||
import javafx.animation.Timeline;
|
||||
import javafx.application.Platform;
|
||||
@@ -26,6 +30,9 @@ import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
|
||||
public final class EvelynMissionControlImpl implements EvelynMissionControl, NenjimProcess {
|
||||
@@ -34,10 +41,12 @@ public final class EvelynMissionControlImpl implements EvelynMissionControl, Nen
|
||||
public EvelynMissionControlImpl(
|
||||
@NotNull EvelynStatusService evelynStatusServiceProd,
|
||||
@NotNull EvelynStatusService evelynStatusServiceTest,
|
||||
@NotNull EvelynPerpsService evelynPerpsService,
|
||||
@NotNull JavaFxRuntimeService javaFxRuntimeService
|
||||
) {
|
||||
this.evelynStatusProd = Objects.requireNonNull(evelynStatusServiceProd, "evelynStatusServiceProd");
|
||||
this.evelynStatusTest = Objects.requireNonNull(evelynStatusServiceTest, "evelynStatusServiceTest");
|
||||
this.evelynPerpsService = Objects.requireNonNull(evelynPerpsService, "evelynPerpsService");
|
||||
this.javaFxRuntimeService = Objects.requireNonNull(
|
||||
javaFxRuntimeService,
|
||||
"javaFxRuntimeService");
|
||||
@@ -145,23 +154,28 @@ public final class EvelynMissionControlImpl implements EvelynMissionControl, Nen
|
||||
if (window == null) {
|
||||
window = new Stage();
|
||||
window.setTitle("Evelyn Mission Control");
|
||||
window.setOnHidden(event -> refreshTimeline.stop());
|
||||
window.setOnHidden(event -> {
|
||||
refreshTimeline.stop();
|
||||
stopPerpsPolling();
|
||||
});
|
||||
window.setScene(new Scene(createRootTabs(), 600, 400));
|
||||
}
|
||||
window.setScene(new Scene(createRootTabs(), 600, 400));
|
||||
|
||||
refreshCharts();
|
||||
window.show();
|
||||
refreshTimeline.playFromStart();
|
||||
startPerpsPolling();
|
||||
}
|
||||
|
||||
private @NotNull TabPane createMissionControlTabs(
|
||||
@NotNull String color,
|
||||
@NotNull StatusIndexChart statusIndexChart
|
||||
@NotNull StatusIndexChart statusIndexChart,
|
||||
@NotNull PerpsTab perpsTab
|
||||
) {
|
||||
TabPane tabs = new TabPane(
|
||||
createOverviewTab(color, statusIndexChart),
|
||||
createMissionControlTab("Portfolio", color),
|
||||
PerpsTab.create(color),
|
||||
perpsTab.tab(),
|
||||
createMissionControlTab("Spot", color),
|
||||
createMissionControlTab("Liquidity Pools", color),
|
||||
createMissionControlTab("Alarms", color),
|
||||
@@ -239,16 +253,18 @@ public final class EvelynMissionControlImpl implements EvelynMissionControl, Nen
|
||||
private @NotNull TabPane createRootTabs() {
|
||||
productionChart = createStatusIndexChart();
|
||||
testChart = createStatusIndexChart();
|
||||
productionPerps = new PerpsTab(PRODUCTION_COLOR);
|
||||
testPerps = new PerpsTab(TEST_COLOR);
|
||||
|
||||
Tab productionTab = new Tab(
|
||||
"Production",
|
||||
createMissionControlTabs(PRODUCTION_COLOR, productionChart)
|
||||
createMissionControlTabs(PRODUCTION_COLOR, productionChart, productionPerps)
|
||||
);
|
||||
productionTab.setStyle("-fx-background-color: " + PRODUCTION_COLOR + ";");
|
||||
|
||||
Tab testTab = new Tab(
|
||||
"Test",
|
||||
createMissionControlTabs(TEST_COLOR, testChart)
|
||||
createMissionControlTabs(TEST_COLOR, testChart, testPerps)
|
||||
);
|
||||
testTab.setStyle("-fx-background-color: " + TEST_COLOR + ";");
|
||||
|
||||
@@ -262,6 +278,37 @@ public final class EvelynMissionControlImpl implements EvelynMissionControl, Nen
|
||||
refreshChart(productionChart, evelynStatusProd.getStatusIndexHistory());
|
||||
refreshChart(testChart, evelynStatusTest.getStatusIndexHistory());
|
||||
}
|
||||
|
||||
private void startPerpsPolling() {
|
||||
stopPerpsPolling();
|
||||
ScheduledExecutorService session = Executors.newSingleThreadScheduledExecutor(runnable -> {
|
||||
Thread thread = new Thread(runnable, "emc-perps-poll");
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
perpsPolling = session;
|
||||
session.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
PerpsSnapshot snapshot = evelynPerpsService.getPositionsSnapshot();
|
||||
javaFxRuntimeService.runLater(() -> {
|
||||
if (perpsPolling == session && window.isShowing()) {
|
||||
productionPerps.update(snapshot);
|
||||
testPerps.update(snapshot);
|
||||
}
|
||||
});
|
||||
} catch (RuntimeException exception) {
|
||||
log.warn("Unable to refresh Perps positions", exception);
|
||||
}
|
||||
}, 0, PERPS_REFRESH_SECONDS, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private void stopPerpsPolling() {
|
||||
ScheduledExecutorService session = perpsPolling;
|
||||
perpsPolling = null;
|
||||
if (session != null) {
|
||||
session.shutdownNow();
|
||||
}
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region Inner classes
|
||||
@@ -307,6 +354,8 @@ public final class EvelynMissionControlImpl implements EvelynMissionControl, Nen
|
||||
private static final String TEST_COLOR = "#d8f3dc";
|
||||
private static final double EMPTY_Y_BOUND = 1.0;
|
||||
private static final double CHART_REFRESH_SECONDS = 1.0;
|
||||
private static final long PERPS_REFRESH_SECONDS = 3;
|
||||
private static final Logger log = LoggerFactory.getLogger(EvelynMissionControlImpl.class);
|
||||
private static final long SINGLE_POINT_X_PADDING_MILLIS = 30_000;
|
||||
private static final DateTimeFormatter TIMESTAMP_TICK_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("HH:mm dd/MM/yy")
|
||||
@@ -333,6 +382,7 @@ public final class EvelynMissionControlImpl implements EvelynMissionControl, Nen
|
||||
// region Dependencies
|
||||
private final EvelynStatusService evelynStatusProd;
|
||||
private final EvelynStatusService evelynStatusTest;
|
||||
private final EvelynPerpsService evelynPerpsService;
|
||||
private final JavaFxRuntimeService javaFxRuntimeService;
|
||||
// endregion
|
||||
|
||||
@@ -340,5 +390,8 @@ public final class EvelynMissionControlImpl implements EvelynMissionControl, Nen
|
||||
private Stage window;
|
||||
private StatusIndexChart productionChart;
|
||||
private StatusIndexChart testChart;
|
||||
private PerpsTab productionPerps;
|
||||
private PerpsTab testPerps;
|
||||
private ScheduledExecutorService perpsPolling;
|
||||
// endregion
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.r35157.evelyn.emc.impl.ref;
|
||||
|
||||
import com.r35157.evelyn.service.perps.PerpsSnapshot;
|
||||
import com.r35157.evelyn.service.perps.PerpsSnapshot.Direction;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.ReadOnlyStringWrapper;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.control.Label;
|
||||
@@ -13,10 +16,15 @@ import javafx.scene.layout.VBox;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
final class PerpsTab {
|
||||
static Tab create(String environmentColor) {
|
||||
PerpsTab(String environmentColor) {
|
||||
NumberFormat usdFormat = NumberFormat.getCurrencyInstance(Locale.US);
|
||||
|
||||
TreeTableColumn<PositionRow, String> position = new TreeTableColumn<>("Position");
|
||||
@@ -28,7 +36,7 @@ final class PerpsTab {
|
||||
TreeTableColumn<PositionRow, String> direction = new TreeTableColumn<>("Direction");
|
||||
direction.setCellValueFactory(cell -> {
|
||||
Direction value = cell.getValue().getValue().direction();
|
||||
return new ReadOnlyStringWrapper(value == null ? "" : value.label);
|
||||
return new ReadOnlyStringWrapper(value == null ? "" : value == Direction.LONG ? "Long" : "Short");
|
||||
});
|
||||
direction.setPrefWidth(90);
|
||||
direction.setMinWidth(80);
|
||||
@@ -44,7 +52,7 @@ final class PerpsTab {
|
||||
size.setStyle("-fx-alignment: CENTER-RIGHT;");
|
||||
size.setSortable(false);
|
||||
|
||||
TreeTableView<PositionRow> table = new TreeTableView<>(createSampleRoot());
|
||||
table.getRoot().setExpanded(true);
|
||||
table.setShowRoot(false);
|
||||
table.setTreeColumn(position);
|
||||
table.getColumns().add(position);
|
||||
@@ -57,7 +65,7 @@ final class PerpsTab {
|
||||
super.updateItem(item, empty);
|
||||
setStyle(empty || item == null || item.direction() == null
|
||||
? ""
|
||||
: item.direction().rowStyle);
|
||||
: item.direction() == Direction.LONG ? LONG_ROW_STYLE : SHORT_ROW_STYLE);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -72,58 +80,93 @@ final class PerpsTab {
|
||||
content.setStyle("-fx-background-color: " + environmentColor + ";");
|
||||
VBox.setVgrow(table, Priority.ALWAYS);
|
||||
|
||||
Tab tab = new Tab("Perps", content);
|
||||
tab = new Tab("Perps", content);
|
||||
tab.setStyle("-fx-background-color: " + environmentColor + ";");
|
||||
}
|
||||
|
||||
Tab tab() {
|
||||
return tab;
|
||||
}
|
||||
|
||||
private static TreeItem<PositionRow> createSampleRoot() {
|
||||
return group("Sample positions",
|
||||
group("Sample wallet A",
|
||||
group("Jupiter",
|
||||
position("SOL-PERP", Direction.LONG, "12500.00"),
|
||||
position("BTC-PERP", Direction.SHORT, "8250.50")),
|
||||
group("Phoenix",
|
||||
position("SOL-PERP", Direction.SHORT, "3200.75"),
|
||||
position("ETH-PERP", Direction.LONG, "5400.00"))),
|
||||
group("Sample wallet B",
|
||||
group("Jupiter",
|
||||
position("ETH-PERP", Direction.LONG, "1875.25"),
|
||||
position("SOL-PERP", Direction.SHORT, "950.00")),
|
||||
group("Phoenix",
|
||||
position("BTC-PERP", Direction.LONG, "21000.00"),
|
||||
position("SOL-PERP", Direction.SHORT, "640.50"))));
|
||||
void update(PerpsSnapshot snapshot) {
|
||||
if (!Platform.isFxApplicationThread()) {
|
||||
throw new IllegalStateException("Perps views must be updated on the JavaFX Application Thread");
|
||||
}
|
||||
Objects.requireNonNull(snapshot, "snapshot");
|
||||
if (snapshot.equals(renderedSnapshot)) {
|
||||
return;
|
||||
}
|
||||
|
||||
TreeItem<PositionRow> selected = table.getSelectionModel().getSelectedItem();
|
||||
List<TreeItem<PositionRow>> wallets = new ArrayList<>();
|
||||
Map<String, TreeItem<PositionRow>> existingWallets = childrenById(table.getRoot());
|
||||
for (PerpsSnapshot.Wallet wallet : snapshot.wallets()) {
|
||||
TreeItem<PositionRow> walletItem = item(existingWallets, group(wallet.id(), wallet.name()));
|
||||
List<TreeItem<PositionRow>> venues = new ArrayList<>();
|
||||
Map<String, TreeItem<PositionRow>> existingVenues = childrenById(walletItem);
|
||||
for (PerpsSnapshot.Venue venue : wallet.venues()) {
|
||||
TreeItem<PositionRow> venueItem = item(existingVenues, group(venue.id(), venue.name()));
|
||||
List<TreeItem<PositionRow>> positions = new ArrayList<>();
|
||||
Map<String, TreeItem<PositionRow>> existingPositions = childrenById(venueItem);
|
||||
for (PerpsSnapshot.Position position : venue.positions()) {
|
||||
positions.add(item(existingPositions, new PositionRow(
|
||||
position.id(), position.instrument(), position.direction(), position.sizeUsd())));
|
||||
}
|
||||
updateChildren(venueItem, positions);
|
||||
venues.add(venueItem);
|
||||
}
|
||||
updateChildren(walletItem, venues);
|
||||
wallets.add(walletItem);
|
||||
}
|
||||
updateChildren(table.getRoot(), wallets);
|
||||
if (selected != null && table.getSelectionModel().getSelectedItem() != selected) {
|
||||
int selectedRow = table.getRow(selected);
|
||||
if (selectedRow >= 0) {
|
||||
table.getSelectionModel().select(selectedRow);
|
||||
}
|
||||
}
|
||||
renderedSnapshot = snapshot;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private static TreeItem<PositionRow> group(String label, TreeItem<PositionRow>... children) {
|
||||
TreeItem<PositionRow> item = new TreeItem<>(new PositionRow(label, null, null));
|
||||
item.getChildren().addAll(children);
|
||||
item.setExpanded(true);
|
||||
private static PositionRow group(String id, String label) {
|
||||
return new PositionRow(id, label, null, null);
|
||||
}
|
||||
|
||||
private static Map<String, TreeItem<PositionRow>> childrenById(TreeItem<PositionRow> parent) {
|
||||
Map<String, TreeItem<PositionRow>> children = new HashMap<>();
|
||||
for (TreeItem<PositionRow> child : parent.getChildren()) {
|
||||
children.put(child.getValue().id(), child);
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
||||
private static TreeItem<PositionRow> item(
|
||||
Map<String, TreeItem<PositionRow>> existing, PositionRow row) {
|
||||
TreeItem<PositionRow> item = existing.get(row.id());
|
||||
if (item == null) {
|
||||
item = new TreeItem<>(row);
|
||||
item.setExpanded(true);
|
||||
} else if (!row.equals(item.getValue())) {
|
||||
item.setValue(row);
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
private static TreeItem<PositionRow> position(String label, Direction direction, String sizeUsd) {
|
||||
return new TreeItem<>(new PositionRow(label, direction, new BigDecimal(sizeUsd)));
|
||||
}
|
||||
|
||||
private record PositionRow(String label, Direction direction, ΩAmountΩ sizeUsd) {
|
||||
}
|
||||
|
||||
private enum Direction {
|
||||
LONG("Long", "#e6f4ea"),
|
||||
SHORT("Short", "#fce8e6");
|
||||
|
||||
Direction(String label, String background) {
|
||||
this.label = label;
|
||||
this.rowStyle = "-fx-control-inner-background: " + background + ";"
|
||||
+ "-fx-control-inner-background-alt: " + background + ";";
|
||||
private static void updateChildren(TreeItem<PositionRow> parent, List<TreeItem<PositionRow>> children) {
|
||||
if (!parent.getChildren().equals(children)) {
|
||||
parent.getChildren().setAll(children);
|
||||
}
|
||||
|
||||
private final String label;
|
||||
private final String rowStyle;
|
||||
}
|
||||
|
||||
private PerpsTab() {
|
||||
private record PositionRow(String id, String label, Direction direction, ΩAmountΩ sizeUsd) {
|
||||
}
|
||||
|
||||
private static final String LONG_ROW_STYLE = "-fx-control-inner-background: #e6f4ea;"
|
||||
+ "-fx-control-inner-background-alt: #e6f4ea;";
|
||||
private static final String SHORT_ROW_STYLE = "-fx-control-inner-background: #fce8e6;"
|
||||
+ "-fx-control-inner-background-alt: #fce8e6;";
|
||||
private final Tab tab;
|
||||
private final TreeTableView<PositionRow> table = new TreeTableView<>(
|
||||
new TreeItem<>(group("root", "Sample positions")));
|
||||
private PerpsSnapshot renderedSnapshot;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.r35157.evelyn.service.perps;
|
||||
|
||||
import com.r35157.nenjim.component.NenjimComponent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/** Read-only current positions; startup belongs to the Hub, refresh timing to the caller. */
|
||||
public interface EvelynPerpsService extends NenjimComponent {
|
||||
/** Returns the complete immutable in-memory snapshot without making exchange requests. */
|
||||
@NotNull PerpsSnapshot getPositionsSnapshot();
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.r35157.evelyn.service.perps;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/** IDs are stable across reads and unique among siblings, independently of display names. */
|
||||
public record PerpsSnapshot(List<Wallet> wallets) {
|
||||
public PerpsSnapshot {
|
||||
wallets = List.copyOf(wallets);
|
||||
}
|
||||
|
||||
public record Wallet(String id, String name, List<Venue> venues) {
|
||||
public Wallet {
|
||||
Objects.requireNonNull(id, "id");
|
||||
Objects.requireNonNull(name, "name");
|
||||
venues = List.copyOf(venues);
|
||||
}
|
||||
}
|
||||
|
||||
public record Venue(String id, String name, List<Position> positions) {
|
||||
public Venue {
|
||||
Objects.requireNonNull(id, "id");
|
||||
Objects.requireNonNull(name, "name");
|
||||
positions = List.copyOf(positions);
|
||||
}
|
||||
}
|
||||
|
||||
public record Position(String id, String instrument, Direction direction, ΩAmountΩ sizeUsd) {
|
||||
public Position {
|
||||
Objects.requireNonNull(id, "id");
|
||||
Objects.requireNonNull(instrument, "instrument");
|
||||
Objects.requireNonNull(direction, "direction");
|
||||
Objects.requireNonNull(sizeUsd, "sizeUsd");
|
||||
}
|
||||
}
|
||||
|
||||
public enum Direction {
|
||||
LONG, SHORT
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.r35157.evelyn.service.perps.impl.ref;
|
||||
|
||||
import com.r35157.evelyn.service.perps.EvelynPerpsService;
|
||||
import com.r35157.evelyn.service.perps.PerpsSnapshot;
|
||||
import com.r35157.evelyn.service.perps.PerpsSnapshot.Position;
|
||||
import com.r35157.evelyn.service.perps.PerpsSnapshot.Venue;
|
||||
import com.r35157.evelyn.service.perps.PerpsSnapshot.Wallet;
|
||||
import com.r35157.evelyn.service.perps.PerpsSnapshot.Direction;
|
||||
import com.r35157.nenjim.process.NenjimProcess;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
/** Owns the fixed examples only. No exchange clients, position management or update worker. */
|
||||
public final class EvelynPerpsServiceImpl implements EvelynPerpsService, NenjimProcess {
|
||||
@Override
|
||||
public synchronized void start() {
|
||||
if (snapshot != null) {
|
||||
throw new IllegalStateException("Perps service has already started");
|
||||
}
|
||||
snapshot = new PerpsSnapshot(List.of(
|
||||
new Wallet("sample-wallet-a", "Sample wallet A", List.of(
|
||||
new Venue("jupiter", "Jupiter", List.of(
|
||||
position("sol-long", "SOL-PERP", Direction.LONG, "12500.00"),
|
||||
position("btc-short", "BTC-PERP", Direction.SHORT, "8250.50"))),
|
||||
new Venue("phoenix", "Phoenix", List.of(
|
||||
position("sol-short", "SOL-PERP", Direction.SHORT, "3200.75"),
|
||||
position("eth-long", "ETH-PERP", Direction.LONG, "5400.00"))))),
|
||||
new Wallet("sample-wallet-b", "Sample wallet B", List.of(
|
||||
new Venue("jupiter", "Jupiter", List.of(
|
||||
position("eth-long", "ETH-PERP", Direction.LONG, "1875.25"),
|
||||
position("sol-short", "SOL-PERP", Direction.SHORT, "950.00"))),
|
||||
new Venue("phoenix", "Phoenix", List.of(
|
||||
position("btc-long", "BTC-PERP", Direction.LONG, "21000.00"),
|
||||
position("sol-short", "SOL-PERP", Direction.SHORT, "640.50")))))
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull PerpsSnapshot getPositionsSnapshot() {
|
||||
PerpsSnapshot current = snapshot;
|
||||
if (current == null) {
|
||||
throw new IllegalStateException("Perps service has not started");
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
private static Position position(String id, String instrument, Direction direction, String sizeUsd) {
|
||||
return new Position(id, instrument, direction, new ΩAmountΩ(sizeUsd));
|
||||
}
|
||||
|
||||
private volatile PerpsSnapshot snapshot;
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import com.r35157.evelyn.service.liquidity.impl.ref.EvelynLiquidityConfiguration
|
||||
import com.r35157.evelyn.service.liquidity.impl.ref.EvelynLiquidityServiceImpl;
|
||||
import com.r35157.evelyn.service.status.EvelynStatusService;
|
||||
import com.r35157.evelyn.service.status.impl.ref.EvelynStatusServiceImpl;
|
||||
import com.r35157.evelyn.service.perps.EvelynPerpsService;
|
||||
import com.r35157.evelyn.service.perps.impl.ref.EvelynPerpsServiceImpl;
|
||||
import com.fanitas.evelyn.service.burner.EvelynIOUBurnerService;
|
||||
import com.fanitas.evelyn.service.burner.impl.ref.EvelynIOUBurnerServiceImpl;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -358,6 +360,12 @@ public final class NenjimHubImpl implements NenjimHub {
|
||||
solanaBlockChain, cis, liquidityConfiguration);
|
||||
}
|
||||
|
||||
private NenjimProcess constructEvelynPerpsProcess() {
|
||||
EvelynPerpsServiceImpl process = new EvelynPerpsServiceImpl();
|
||||
evelynPerpsService = process;
|
||||
return process;
|
||||
}
|
||||
|
||||
private NenjimProcess constructJavaFxRuntimeProcess() {
|
||||
JavaFxRuntimeServiceImpl process = new JavaFxRuntimeServiceImpl();
|
||||
javaFxRuntimeService = process;
|
||||
@@ -374,6 +382,10 @@ public final class NenjimHubImpl implements NenjimHub {
|
||||
evelynStatusServiceTest,
|
||||
EVELYN_MISSION_CONTROL_ID,
|
||||
EVELYN_STATUS_TEST_ID),
|
||||
requireProcessDependency(
|
||||
evelynPerpsService,
|
||||
EVELYN_MISSION_CONTROL_ID,
|
||||
EVELYN_PERPS_SERVICE_ID),
|
||||
requireProcessDependency(
|
||||
javaFxRuntimeService,
|
||||
EVELYN_MISSION_CONTROL_ID,
|
||||
@@ -421,6 +433,7 @@ public final class NenjimHubImpl implements NenjimHub {
|
||||
Map.entry(EVELYN_STATUS_TEST_ID, this::constructEvelynStatusTestProcess),
|
||||
Map.entry(EVELYN_LIQUIDITY_PROD_ID, this::constructEvelynLiquidityProdProcess),
|
||||
Map.entry(EVELYN_IOU_BURNER_PROD_ID, this::constructEvelynIOUBurnerProdProcess),
|
||||
Map.entry(EVELYN_PERPS_SERVICE_ID, this::constructEvelynPerpsProcess),
|
||||
Map.entry(JAVAFX_RUNTIME_SERVICE_ID, this::constructJavaFxRuntimeProcess),
|
||||
Map.entry(EVELYN_MISSION_CONTROL_ID, this::constructMissionControlProcess),
|
||||
Map.entry(PROCESS_MANAGER_ID, this::constructProcessManagerProcess)
|
||||
@@ -538,6 +551,8 @@ public final class NenjimHubImpl implements NenjimHub {
|
||||
new NenjimComponentId("evelyn.liquidity.prod");
|
||||
private static final NenjimComponentId EVELYN_MISSION_CONTROL_ID =
|
||||
new NenjimComponentId("evelyn.mission-control.default");
|
||||
private static final NenjimComponentId EVELYN_PERPS_SERVICE_ID =
|
||||
new NenjimComponentId("evelyn.perps.default");
|
||||
private static final NenjimComponentId JAVAFX_RUNTIME_SERVICE_ID =
|
||||
new NenjimComponentId("nenjim.javafx-runtime.service");
|
||||
private static final NenjimComponentId PROCESS_MANAGER_ID =
|
||||
@@ -576,6 +591,7 @@ public final class NenjimHubImpl implements NenjimHub {
|
||||
private JupiterPerpsAlarm jupiterPerpsAlarm;
|
||||
private EvelynStatusService evelynStatusServiceProd;
|
||||
private EvelynStatusService evelynStatusServiceTest;
|
||||
private EvelynPerpsService evelynPerpsService;
|
||||
private JupiterSwapService jupiterSwapService;
|
||||
private BoundNotificationService discordAssetAZTokenNotificationService;
|
||||
private EvelynIOUBurnerService evelynIOUBurnerServiceProd;
|
||||
|
||||
Reference in New Issue
Block a user