3
0
mirror of https://github.com/Qortal/qortal.git synced 2025-02-11 17:55:50 +00:00

added new '/addresses/levels' API call that will pull an array of accounts with their levels, based on an input number. Accounts and levels at or above the input number will be pulled and displayed along with their level. Thanks to @kennycud!

This commit is contained in:
crowetic 2024-09-27 18:00:32 -07:00
parent 5a691762ed
commit aba4c6000f
4 changed files with 110 additions and 7 deletions

View File

@ -20,10 +20,7 @@ import org.qortal.asset.Asset;
import org.qortal.controller.LiteNode;
import org.qortal.controller.OnlineAccountsManager;
import org.qortal.crypto.Crypto;
import org.qortal.data.account.AccountData;
import org.qortal.data.account.AccountPenaltyData;
import org.qortal.data.account.RewardShareData;
import org.qortal.data.account.SponsorshipReport;
import org.qortal.data.account.*;
import org.qortal.data.network.OnlineAccountData;
import org.qortal.data.network.OnlineAccountLevel;
import org.qortal.data.transaction.PublicizeTransactionData;
@ -668,7 +665,7 @@ public class AddressesResource {
description = "Returns sponsorship statistics for an account's sponsor",
responses = {
@ApiResponse(
description = "the statistics",
description = "statistics",
content = @Content(mediaType = MediaType.APPLICATION_JSON, array = @ArraySchema(schema = @Schema(implementation = SponsorshipReport.class)))
)
}
@ -698,4 +695,31 @@ public class AddressesResource {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
}
}
}
@GET
@Path("/levels/{minLevel}")
@Operation(
summary = "Return accounts with levels greater than or equal to input",
responses = {
@ApiResponse(
description = "online accounts",
content = @Content(mediaType = MediaType.APPLICATION_JSON, array = @ArraySchema(schema = @Schema(implementation = AddressLevelPairing.class)))
)
}
)
@ApiErrors({ApiError.REPOSITORY_ISSUE})
public List<AddressLevelPairing> getAddressLevelPairings(@PathParam("minLevel") int minLevel) {
try (final Repository repository = RepositoryManager.getRepository()) {
// get the level address pairings
List<AddressLevelPairing> pairings = repository.getAccountRepository().getAddressLevelPairings(minLevel);
return pairings;
} catch (DataException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
}
}
}

View File

@ -0,0 +1,43 @@
package org.qortal.data.account;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import java.util.Arrays;
// All properties to be converted to JSON via JAXB
@XmlAccessorType(XmlAccessType.FIELD)
public class AddressLevelPairing {
private String address;
private int level;
// Constructors
// For JAXB
protected AddressLevelPairing() {
}
public AddressLevelPairing(String address, int level) {
this.address = address;
this.level = level;
}
// Getters / setters
public String getAddress() {
return address;
}
public int getLevel() {
return level;
}
@Override
public String toString() {
return "SponsorshipReport{" +
"address='" + address + '\'' +
", level=" + level +
'}';
}
}

View File

@ -165,7 +165,9 @@ public interface AccountRepository {
*/
public Optional<String> getSponsor(String address) throws DataException;
/** How to order results when fetching asset balances. */
public List<AddressLevelPairing> getAddressLevelPairings(int minLevel) throws DataException;
/** How to order results when fetching asset balances. */
public enum BalanceOrdering {
/** assetID first, then balance, then account address */
ASSET_BALANCE_ACCOUNT,

View File

@ -1,5 +1,6 @@
package org.qortal.repository.hsqldb;
import cash.z.wallet.sdk.rpc.Service;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.qortal.asset.Asset;
@ -1241,6 +1242,39 @@ public class HSQLDBAccountRepository implements AccountRepository {
}
}
@Override
public List<AddressLevelPairing> getAddressLevelPairings(int minLevel) throws DataException {
StringBuffer accLevelSql = new StringBuffer(51);
accLevelSql.append( "SELECT account,level FROM ACCOUNTS WHERE level >= ?" );
try {
ResultSet accountLevelResultSet = this.repository.checkedExecute(accLevelSql.toString(),minLevel);
List<AddressLevelPairing> addressLevelPairings;
if( accountLevelResultSet == null ) {
addressLevelPairings = new ArrayList<>(0);
}
else {
addressLevelPairings = new ArrayList<>();
do {
AddressLevelPairing pairing
= new AddressLevelPairing(
accountLevelResultSet.getString(1),
accountLevelResultSet.getInt(2)
);
addressLevelPairings.add(pairing);
} while (accountLevelResultSet.next());
}
return addressLevelPairings;
} catch (SQLException e) {
throw new DataException("Can't get addresses for this level from blockchain data", e);
}
}
/**
* Produce Sponsorship Report
*