Now it displays the logs :D

This commit is contained in:
Nicolás Sánchez 2024-12-13 16:17:43 -03:00
parent ade62f87c6
commit 308fd87731
2 changed files with 137 additions and 49 deletions

View File

@ -45,6 +45,7 @@ public class InstanceInterface {
}
}
public static double getTraderTime(String exchange) throws IOException {
Request uptimeRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/trader_time")
@ -107,9 +108,9 @@ public class InstanceInterface {
}
}
public static List<String> getLastLogs() throws IOException {
public static String getLastLogs(String exchange) throws IOException {
Request logRequest = new Request.Builder()
.url(API_BASE_URL + "/get_log_lisd")
.url(API_BASE_URL + "/" + exchange + "/get_log_list")
.header("X-API-KEY", API_KEY)
.build();
@ -135,7 +136,8 @@ public class InstanceInterface {
logList.add(logsArray.get(i).getAsString());
}
return logList;
//return String.join("\n",logList);
return joinWithLineLimit("\n",globalSettings.amountOfLogLines,logList);
}
}
@ -143,22 +145,32 @@ public class InstanceInterface {
public static ExchangeStatsData getExchangeStatsData(String exchange) throws IOException {
//This needs to do A LOT of work
//All workers stats request
Request allWorkersStatsRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/get_all_worker_status")
.header("X-API-KEY", API_KEY)
.build();
//Instance global stats
Request instanceGlobalStatsRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/global_stats")
.header("X-API-KEY", API_KEY)
.build();
return null;
}
public static List<String> getAllWorkersStats(String exchange) throws IOException {
return null;
}
public static String joinWithLineLimit(String delimiter, int maxLines, Iterable<String> elements) {
StringBuilder result = new StringBuilder();
int currentLineCount = 0;
for (String element : elements) {
if (currentLineCount >= maxLines) {
break;
}
if (result.length() > 0) {
result.append(delimiter);
}
result.append(element);
currentLineCount++;
}
return result.toString();
}
private static double getTodaysProfit(String jsonResponse) {
try {
JsonElement jsonElement = JsonParser.parseString(jsonResponse);

View File

@ -19,8 +19,11 @@ import com.example.dcav2gui.MainActivity;
import com.example.dcav2gui.R;
import com.example.dcav2gui.TickerTracker;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -342,48 +345,121 @@ public class HomeFragment extends Fragment {
}
});
CompletableFuture<String> future5 = CompletableFuture.supplyAsync(() -> {
try {
return InstanceInterface.getLastLogs("binance");
} catch (IOException e) {
e.printStackTrace();
return null;
}
});
CompletableFuture<String> future6 = CompletableFuture.supplyAsync(() -> {
try {
return InstanceInterface.getLastLogs("gateio");
} catch (IOException e) {
e.printStackTrace();
return null;
}
});
CompletableFuture<String> future7 = CompletableFuture.supplyAsync(() -> {
try {
return InstanceInterface.getLastLogs("kucoin");
} catch (IOException e) {
e.printStackTrace();
return null;
}
});
CompletableFuture<String> future8 = CompletableFuture.supplyAsync(() -> {
try {
return InstanceInterface.getLastLogs("okex");
} catch (IOException e) {
e.printStackTrace();
return null;
}
});
// Wait for all futures to complete
CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2, future3, future4);
CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2, future3, future4, future5, future6, future7, future8);
// Update UI
try {
TickerTracker.PriceChangeData priceData = future1.get();
TickerTracker.PriceChangeData priceData2 = future2.get();
TickerTracker.PriceChangeData priceData3 = future3.get();;
allFutures.thenAccept(voidResult -> {
try {
requireActivity().runOnUiThread(() -> {
InstanceInterface.ProfitStatsData profitsData = future4.get();
TickerTracker.PriceChangeData priceData = null;
TickerTracker.PriceChangeData priceData2 = null;
TickerTracker.PriceChangeData priceData3 = null;
try {
priceData = future1.get();
priceData2 = future2.get();
priceData3 = future3.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
InstanceInterface.ProfitStatsData profitsData = null;
try {
profitsData = future4.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
String logs1 = null;
String logs2 = null;
String logs3 = null;
String logs4 = null;
try {
logs1 = future5.get();
logs2 = future6.get();
logs3 = future7.get();
logs4 = future8.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
pricePair1.setText(String.format(Locale.ROOT, "%.2f", priceData.getCurrentPrice()));
pricePair124hPercentage.setText(formatPercentage(priceData.getPriceChangePercent24h()));
pricePair17dPercentage.setText(formatPercentage(priceData.getPriceChangePercent7d()));
pricePair130dPercentage.setText(formatPercentage(priceData.getPriceChangePercent30d()));
setPercentageColor(pricePair124hPercentage, priceData.getPriceChangePercent24h());
setPercentageColor(pricePair17dPercentage, priceData.getPriceChangePercent7d());
setPercentageColor(pricePair130dPercentage, priceData.getPriceChangePercent30d());
pricePair2.setText(String.format(Locale.ROOT, "%.2f", priceData2.getCurrentPrice()));
pricePair224hPercentage.setText(formatPercentage(priceData2.getPriceChangePercent24h()));
pricePair27dPercentage.setText(formatPercentage(priceData2.getPriceChangePercent7d()));
pricePair230dPercentage.setText(formatPercentage(priceData2.getPriceChangePercent30d()));
setPercentageColor(pricePair224hPercentage, priceData2.getPriceChangePercent24h());
setPercentageColor(pricePair27dPercentage, priceData2.getPriceChangePercent7d());
setPercentageColor(pricePair230dPercentage, priceData2.getPriceChangePercent30d());
pricePair3.setText(String.format(Locale.ROOT, "%.2f", priceData3.getCurrentPrice()));
pricePair324hPercentage.setText(formatPercentage(priceData3.getPriceChangePercent24h()));
pricePair37dPercentage.setText(formatPercentage(priceData3.getPriceChangePercent7d()));
pricePair330dPercentage.setText(formatPercentage(priceData3.getPriceChangePercent30d()));
setPercentageColor(pricePair324hPercentage, priceData3.getPriceChangePercent24h());
setPercentageColor(pricePair37dPercentage, priceData3.getPriceChangePercent7d());
setPercentageColor(pricePair330dPercentage, priceData3.getPriceChangePercent30d());
profitsToday.setText(String.format(Locale.ROOT, "%.2f", profitsData.getProfitsToday()));
profitsThisMonth.setText(String.format(Locale.ROOT, "%.2f", profitsData.getProfitsThisMonth()));
pricePair1.setText(String.format(Locale.ROOT,"%.2f", priceData.getCurrentPrice()));
pricePair124hPercentage.setText(formatPercentage(priceData.getPriceChangePercent24h()));
pricePair17dPercentage.setText(formatPercentage(priceData.getPriceChangePercent7d()));
pricePair130dPercentage.setText(formatPercentage(priceData.getPriceChangePercent30d()));
setPercentageColor(pricePair124hPercentage,priceData.getPriceChangePercent24h());
setPercentageColor(pricePair17dPercentage,priceData.getPriceChangePercent7d());
setPercentageColor(pricePair130dPercentage,priceData.getPriceChangePercent30d());
//Populate logs
log1Content.setText(logs1);
log2Content.setText(logs2);
log3Content.setText(logs3);
log4Content.setText(logs4);
pricePair2.setText(String.format(Locale.ROOT,"%.2f", priceData2.getCurrentPrice()));
pricePair224hPercentage.setText(formatPercentage(priceData2.getPriceChangePercent24h()));
pricePair27dPercentage.setText(formatPercentage(priceData2.getPriceChangePercent7d()));
pricePair230dPercentage.setText(formatPercentage(priceData2.getPriceChangePercent30d()));
setPercentageColor(pricePair224hPercentage,priceData2.getPriceChangePercent24h());
setPercentageColor(pricePair27dPercentage,priceData2.getPriceChangePercent7d());
setPercentageColor(pricePair230dPercentage,priceData2.getPriceChangePercent30d());
});
pricePair3.setText(String.format(Locale.ROOT,"%.2f", priceData3.getCurrentPrice()));
pricePair324hPercentage.setText(formatPercentage(priceData3.getPriceChangePercent24h()));
pricePair37dPercentage.setText(formatPercentage(priceData3.getPriceChangePercent7d()));
pricePair330dPercentage.setText(formatPercentage(priceData3.getPriceChangePercent30d()));
setPercentageColor(pricePair324hPercentage,priceData3.getPriceChangePercent24h());
setPercentageColor(pricePair37dPercentage,priceData3.getPriceChangePercent7d());
setPercentageColor(pricePair330dPercentage,priceData3.getPriceChangePercent30d());
profitsToday.setText(String.format(Locale.ROOT,"%.2f", profitsData.getProfitsToday()));
profitsThisMonth.setText(String.format(Locale.ROOT,"%.2f", profitsData.getProfitsThisMonth()));
} catch (Exception e) {
Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
requireActivity().runOnUiThread(() ->
Toast.makeText(getContext(), "Failed to fetch price data. Check your connection.", Toast.LENGTH_SHORT).show()