use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class ResourceOperationUtilsTest method testIsAvailable_custProps_pos.
@Test
public void testIsAvailable_custProps_pos() {
ResourceOperationSpec spec = createResourceOperationSpec();
spec.targetCriteria = SCRIPT_CONTEXT_RESOURCE + ".customProperties.p1=='v1'";
ComputeState computeState = createComputeState("testIsAvailable_custProps_pos");
boolean ret = ResourceOperationUtils.isAvailable(computeState, spec);
Assert.assertTrue(ret);
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AWSCostStatsService method createQueryForComputeStatesByAccount.
/**
* Method creates a query operation to get the compute states corresponding to the specified account ID.
* The list of the resultant compute states is then passed to the specified handler for processing.
*
* @param context
* @param accountId
* @param queryResultConsumer
* @return operation object representing the query
*/
protected Operation createQueryForComputeStatesByAccount(AWSCostStatsCreationContext context, String accountId, Consumer<List<ComputeState>> queryResultConsumer) {
Query awsAccountsQuery = Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_TYPE, ComputeType.ENDPOINT_HOST).addCompositeFieldClause(ComputeState.FIELD_NAME_CUSTOM_PROPERTIES, EndpointAllocationTaskService.CUSTOM_PROP_ENPOINT_TYPE, PhotonModelConstants.EndpointType.aws.name()).addCompositeFieldClause(ComputeState.FIELD_NAME_CUSTOM_PROPERTIES, AWS_ACCOUNT_ID_KEY, accountId).addInCollectionItemClause(ComputeState.FIELD_NAME_TENANT_LINKS, context.computeDesc.tenantLinks).build();
QueryTask queryTask = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).setQuery(awsAccountsQuery).build();
queryTask.setDirect(true);
queryTask.tenantLinks = context.computeDesc.tenantLinks;
return QueryUtils.createQueryTaskOperation(this, queryTask, ServiceTypeCluster.INVENTORY_SERVICE).setCompletion((o, e) -> {
if (e != null) {
getFailureConsumer(context).accept(e);
return;
}
QueryTask responseTask = o.getBody(QueryTask.class);
List<ComputeState> accountComputeStates = responseTask.results.documents.values().stream().map(s -> Utils.fromJson(s, ComputeState.class)).filter(cs -> cs.parentLink == null && cs.endpointLink != null && !CollectionUtils.isEmpty(cs.endpointLinks)).collect(Collectors.toList());
queryResultConsumer.accept(accountComputeStates);
});
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AWSCostStatsService method createServiceStatsForAccount.
protected void createServiceStatsForAccount(AWSCostStatsCreationContext statsData, LocalDate billMonth, AwsAccountDetailDto awsAccountDetailDto) {
Consumer<ComputeState> serviceStatsProcessor = (accountComputeState) -> {
ComputeStats awsServiceStats = new ComputeStats();
awsServiceStats.statValues = new ConcurrentHashMap<>();
awsServiceStats.computeLink = accountComputeState.documentSelfLink;
awsServiceStats.addCustomProperty(PhotonModelConstants.DOES_CONTAIN_SERVICE_STATS, Boolean.TRUE.toString());
for (AwsServiceDetailDto serviceDetailDto : awsAccountDetailDto.serviceDetailsMap.values()) {
Map<String, List<ServiceStat>> statsForAwsService = createStatsForAwsService(serviceDetailDto);
awsServiceStats.statValues.putAll(statsForAwsService);
}
if (!awsServiceStats.statValues.isEmpty()) {
statsData.statsResponse.statsList.add(awsServiceStats);
}
};
insertEC2ServiceDetail(awsAccountDetailDto);
processAccountStats(statsData, billMonth, awsAccountDetailDto, serviceStatsProcessor);
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AWSCostStatsService method createAccountStats.
protected void createAccountStats(AWSCostStatsCreationContext statsData, LocalDate billMonth, AwsAccountDetailDto awsAccountDetailDto) {
Consumer<ComputeState> accountStatsProcessor = (accountComputeState) -> {
ComputeStats accountStats = new ComputeStats();
accountStats.statValues = new ConcurrentHashMap<>();
accountStats.computeLink = accountComputeState.documentSelfLink;
if (isBillUpdated(statsData, awsAccountDetailDto)) {
logWithContext(statsData, Level.INFO, () -> String.format("Persisting cost of Account: %s (%s) for month: %s", awsAccountDetailDto.id, accountComputeState.documentSelfLink, billMonth));
ServiceStat costStat = createStat(AWSStatsNormalizer.getNormalizedUnitValue(DIMENSION_CURRENCY_VALUE), AWSStatsNormalizer.getNormalizedStatKeyValue(AWSConstants.COST), awsAccountDetailDto.billProcessedTimeMillis, awsAccountDetailDto.cost);
accountStats.statValues.put(costStat.name, Collections.singletonList(costStat));
ServiceStat otherCostsStat = createStat(AWSStatsNormalizer.getNormalizedUnitValue(DIMENSION_CURRENCY_VALUE), AWSConstants.OTHER_CHARGES, awsAccountDetailDto.billProcessedTimeMillis, awsAccountDetailDto.otherCharges);
accountStats.statValues.put(otherCostsStat.name, Collections.singletonList(otherCostsStat));
ServiceStat oneTimeChargesStat = createStat(AWSStatsNormalizer.getNormalizedUnitValue(DIMENSION_CURRENCY_VALUE), PhotonModelConstants.ACCOUNT_ONE_TIME_CHARGES, awsAccountDetailDto.billProcessedTimeMillis, awsAccountDetailDto.accountOneTimeCharges);
accountStats.statValues.put(oneTimeChargesStat.name, Collections.singletonList(oneTimeChargesStat));
}
if (!accountStats.statValues.isEmpty()) {
statsData.statsResponse.statsList.add(accountStats);
}
};
processAccountStats(statsData, billMonth, awsAccountDetailDto, accountStatsProcessor);
ResourceMetrics prevMarkerMetrics = statsData.accountsMarkersMap.get(awsAccountDetailDto.id);
if (prevMarkerMetrics != null) {
prevMarkerMetrics.entries.putAll(transformMapDataTypes(awsAccountDetailDto.lineCountPerInterval));
}
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AWSCostStatsService method createMarkerMetrics.
private void createMarkerMetrics(AWSCostStatsCreationContext context, AwsAccountDetailDto accountDto) {
List<ComputeState> accountComputeStates = context.awsAccountIdToComputeStates.get(accountDto.id);
if ((accountComputeStates == null) || accountComputeStates.isEmpty()) {
logFine(() -> "AWS account with ID '%s' is not configured yet. Not creating marker metrics for the same.");
return;
}
// We use root compute state representing this account to save the account level stats
Map<String, ComputeState> rootComputesByEndpoint = findRootAccountComputeStateByEndpoint(accountComputeStates);
URI uri = UriUtils.buildUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.METRIC_SERVICE), ResourceMetricsService.FACTORY_LINK);
for (ComputeState compute : rootComputesByEndpoint.values()) {
ResourceMetrics markerMetrics = new ResourceMetrics();
markerMetrics.documentSelfLink = StatsUtil.getMetricKey(compute.documentSelfLink, Utils.getNowMicrosUtc());
markerMetrics.entries = new HashMap<>();
markerMetrics.entries.putAll(transformMapDataTypes(accountDto.lineCountPerInterval));
markerMetrics.entries.put(AWS_ACCOUNT_BILL_PROCESSED_TIME_MILLIS, accountDto.billProcessedTimeMillis.doubleValue());
markerMetrics.timestampMicrosUtc = getCurrentMonthStartTimeMicros();
markerMetrics.customProperties = new HashMap<>();
markerMetrics.customProperties.put(ResourceMetrics.PROPERTY_RESOURCE_LINK, compute.documentSelfLink);
markerMetrics.customProperties.put(PhotonModelConstants.CONTAINS_BILL_PROCESSED_TIME_STAT, Boolean.TRUE.toString());
markerMetrics.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(7);
sendRequest(Operation.createPost(uri).setBodyNoCloning(markerMetrics));
}
}
Aggregations