use of org.bf2.srs.fleetmanager.rest.service.model.RegistryInstanceTypeValueDto in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class UsageMetrics method init.
public synchronized void init() {
expirationPeriod = Duration.ofSeconds(expirationPeriodSeconds);
int stagger = 0;
// Only stagger if the expiration period is at least 1 minute (testing support).
if (expirationPeriod.compareTo(Duration.ofMinutes(1)) >= 0) {
stagger = new Random().nextInt(expirationPeriodSeconds) + 1;
log.debug("Staggering usage metrics cache expiration by {} seconds", stagger);
}
nextExpiration = Instant.now().plus(Duration.ofSeconds(stagger));
for (RegistryStatusValueDto status : RegistryStatusValueDto.values()) {
Gauge.builder(USAGE_STATISTICS_REGISTRIES_STATUS, () -> {
Arc.initialize();
var ctx = Arc.container().requestContext();
ctx.activate();
try {
return getUsageStatisticsCached().getRegistryCountPerStatus().get(status);
} finally {
ctx.deactivate();
}
}).tags(Tags.of(TAG_USAGE_STATISTICS_STATUS, status.value())).register(metrics);
}
for (RegistryInstanceTypeValueDto type : RegistryInstanceTypeValueDto.values()) {
Gauge.builder(USAGE_STATISTICS_REGISTRIES_TYPE, () -> {
Arc.initialize();
var ctx = Arc.container().requestContext();
ctx.activate();
try {
return getUsageStatisticsCached().getRegistryCountPerType().get(type);
} finally {
ctx.deactivate();
}
}).tags(Tags.of(TAG_USAGE_STATISTICS_TYPE, type.value())).register(metrics);
}
Gauge.builder(USAGE_STATISTICS_ACTIVE_USERS, () -> {
Arc.initialize();
var ctx = Arc.container().requestContext();
ctx.activate();
try {
return getUsageStatisticsCached().getActiveUserCount();
} finally {
ctx.deactivate();
}
}).register(metrics);
Gauge.builder(USAGE_STATISTICS_ACTIVE_ORGANISATIONS, () -> {
Arc.initialize();
var ctx = Arc.container().requestContext();
ctx.activate();
try {
return getUsageStatisticsCached().getActiveOrganisationCount();
} finally {
ctx.deactivate();
}
}).register(metrics);
}
use of org.bf2.srs.fleetmanager.rest.service.model.RegistryInstanceTypeValueDto in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class RegistryServiceImpl method createRegistry.
@Audited
@Override
public RegistryDto createRegistry(RegistryCreateDto registryCreate) throws RegistryStorageConflictException, TermsRequiredException, ResourceLimitReachedException, EvalInstancesNotAllowedException, TooManyEvalInstancesForUserException, TooManyInstancesException, AccountManagementServiceException {
final AccountInfo accountInfo = authService.extractAccountInfo();
// Make sure we have more instances available (max capacity not yet reached).
long instanceCount = storage.getRegistryCountTotal();
if (instanceCount >= maxInstances) {
throw new TooManyInstancesException();
}
// Figure out if we are going to create a standard or eval instance.
ResourceType resourceType = evalInstancesOnlyEnabled ? ResourceType.REGISTRY_INSTANCE_EVAL : accountManagementService.determineAllowedResourceType(accountInfo);
if (resourceType == ResourceType.REGISTRY_INSTANCE_EVAL) {
// Are eval instances allowed?
if (!evalInstancesEnabled) {
throw new EvalInstancesNotAllowedException();
}
// Limit the # of eval instances per user. Need to check storage for list of eval registry instances.
List<RegistryData> registriesByOwner = storage.getRegistriesByOwner(accountInfo.getAccountUsername());
int evalInstanceCount = 0;
for (RegistryData registryData : registriesByOwner) {
// TODO Perform a dedicated query
if (RegistryInstanceTypeValueDto.EVAL.value().equals(registryData.getInstanceType())) {
evalInstanceCount++;
}
}
if (evalInstanceCount >= maxEvalInstancesPerUser) {
throw new TooManyEvalInstancesForUserException();
}
}
// Try to consume some quota from AMS for the appropriate resource type (standard or eval). If successful
// we'll get back a subscriptionId - if not we'll throw an exception.
String subscriptionId = accountManagementService.createResource(accountInfo, resourceType);
// Convert to registry data and persist it in the DB.
RegistryInstanceTypeValueDto instanceType = resourceTypeToInstanceType(resourceType);
RegistryData registryData = convertRegistry.convert(registryCreate, subscriptionId, accountInfo.getAccountUsername(), accountInfo.getOrganizationId(), accountInfo.getAccountId(), instanceType);
// Generate the ID
registryData.setId(UUID.randomUUID().toString());
storage.createOrUpdateRegistry(registryData);
tasks.submit(ScheduleRegistryTask.builder().registryId(registryData.getId()).build());
return convertRegistry.convert(registryData);
}
Aggregations