use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AzureCostStatsService method getServiceMetadata.
private void getServiceMetadata(Context context, Stages next) {
Query.Builder builder = Query.Builder.create();
builder.addKindFieldClause(ResourceMetrics.class);
builder.addCompositeFieldClause(ResourceMetrics.FIELD_NAME_CUSTOM_PROPERTIES, ResourceMetrics.PROPERTY_RESOURCE_LINK, context.computeHostDesc.documentSelfLink);
builder.addCompositeFieldClause(ResourceMetrics.FIELD_NAME_CUSTOM_PROPERTIES, PhotonModelConstants.CONTAINS_BILL_PROCESSED_TIME_STAT, Boolean.TRUE.toString());
QueryTask.Builder queryTaskBuilder = QueryTask.Builder.createDirectTask().addOption(QueryOption.SORT).addOption(QueryOption.TOP_RESULTS).addOption(QueryOption.INCLUDE_ALL_VERSIONS).addOption(QueryOption.EXPAND_CONTENT).orderDescending(ServiceDocument.FIELD_NAME_SELF_LINK, ServiceDocumentDescription.TypeName.STRING).setResultLimit(1).setQuery(builder.build());
if (!AzureCostConstants.SHOULD_REFRESH_INDEX) {
queryTaskBuilder.addOption(QueryOption.DO_NOT_REFRESH);
}
QueryTask queryTask = queryTaskBuilder.build();
queryTask.tenantLinks = context.computeHostDesc.tenantLinks;
Operation queryTaskOp = QueryUtils.createQueryTaskOperation(this, queryTask, ServiceTypeCluster.METRIC_SERVICE).setExpiration(Utils.fromNowMicrosUtc(TimeUnit.SECONDS.toMicros(INTERNAL_REQUEST_TIMEOUT_SECONDS)));
this.sendWithDeferredResult(queryTaskOp, QueryTask.class).whenComplete((response, exception) -> {
if (exception != null) {
handleError(context, Stages.GET_HISTORICAL_COSTS, exception, true);
return;
}
if (response == null || response.results == null || response.results.documents == null) {
context.billProcessedTimeMillis = 0;
return;
}
Collection<Object> values = response.results.documents.values();
if (!values.isEmpty()) {
if (values.iterator().next() == null) {
context.billProcessedTimeMillis = 0;
return;
}
ResourceMetrics rawResourceMetrics = Utils.fromJson(values.iterator().next(), ResourceMetrics.class);
context.billProcessedTimeMillis = rawResourceMetrics.entries.getOrDefault(PhotonModelConstants.CLOUD_ACCOUNT_COST_SYNC_MARKER_MILLIS, 0d).longValue();
context.oldestBillProcessedMillis = rawResourceMetrics.entries.getOrDefault(AzureCostConstants.OLDEST_BILL_PROCESSED_MILLIS, 0d).longValue();
}
context.stage = next;
handleRequest(context);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AzureCostStatsService method getStoredEaUsageCost.
private void getStoredEaUsageCost(Context context, Stages next) {
Query.Builder builder = Query.Builder.create();
builder.addKindFieldClause(ResourceMetrics.class);
builder.addCompositeFieldClause(ResourceMetrics.FIELD_NAME_CUSTOM_PROPERTIES, ResourceMetrics.PROPERTY_RESOURCE_LINK, context.computeHostDesc.documentSelfLink);
builder.addRangeClause(QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, AzureCostConstants.USAGE_COST), QueryTask.NumericRange.createDoubleRange(0d, Double.MAX_VALUE, true, true));
Builder queryTaskBuilder = Builder.createDirectTask().addOption(QueryOption.SORT).addOption(QueryOption.TOP_RESULTS).addOption(QueryOption.INCLUDE_ALL_VERSIONS).addOption(QueryOption.EXPAND_CONTENT).orderDescending(ResourceMetrics.FIELD_NAME_TIMESTAMP, TypeName.LONG).setResultLimit(1).setQuery(builder.build());
if (!AzureCostConstants.SHOULD_REFRESH_INDEX) {
queryTaskBuilder.addOption(QueryOption.DO_NOT_REFRESH);
}
QueryTask queryTask = queryTaskBuilder.build();
QueryUtils.startQueryTask(this, queryTask, ServiceTypeCluster.METRIC_SERVICE).whenComplete((response, exception) -> {
if (exception != null) {
handleError(context, Stages.DOWNLOAD_DETAILED_BILL, exception, true);
return;
}
if (response == null || response.results == null || response.results.documents == null) {
context.storedCurrentMonthEaUsageCost = null;
return;
}
Collection<Object> values = response.results.documents.values();
if (!values.isEmpty()) {
if (values.iterator().next() == null) {
context.storedCurrentMonthEaUsageCost = null;
return;
}
ResourceMetrics rawResourceMetrics = Utils.fromJson(values.iterator().next(), ResourceMetrics.class);
context.storedCurrentMonthEaUsageCost = rawResourceMetrics.entries.get(AzureCostConstants.USAGE_COST);
}
context.stage = next;
handleRequest(context);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AzureEndpointAdapterService method checkIfAccountExistsAndGetExistingDocuments.
private void checkIfAccountExistsAndGetExistingDocuments(EndpointConfigRequest req, Operation op) {
String accountId = req.endpointProperties.get(USER_LINK_KEY);
if (accountId != null && !accountId.isEmpty() && req.tenantLinks != null && !req.tenantLinks.isEmpty()) {
QueryTask queryTask = QueryUtils.createAccountQuery(accountId, PhotonModelConstants.EndpointType.azure.name(), req.tenantLinks);
queryTask.tenantLinks = req.tenantLinks;
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((qrt, e) -> {
if (e != null) {
logSevere(() -> String.format("Failure retrieving query results for azure compute host corresponding to" + "the account ID: %s", e.toString()));
op.fail(e);
return;
}
if (qrt.results.documentCount > 0) {
req.accountAlreadyExists = true;
Object state = qrt.results.documents.values().iterator().next();
ComputeState computeHost = Utils.fromJson(state, ComputeState.class);
req.existingComputeState = computeHost;
getComputeDescription(req, computeHost.descriptionLink, op);
} else {
req.accountAlreadyExists = false;
op.setBody(req);
op.complete();
return;
}
});
} else {
req.accountAlreadyExists = false;
op.setBody(req);
op.complete();
}
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class TestVSphereOvfProvisionTaskBase method findFirstOvfDescriptionLink.
protected String findFirstOvfDescriptionLink() throws Exception {
QueryTask.Query q = QueryTask.Query.Builder.create().addFieldClause(ComputeService.ComputeState.FIELD_NAME_ID, "ovf-", QueryTask.QueryTerm.MatchType.PREFIX).build();
QueryTask qt = QueryTask.Builder.createDirectTask().setQuery(q).build();
Operation op = QueryUtils.createQueryTaskOperation(this.host, qt, ServiceTypeCluster.INVENTORY_SERVICE);
QueryTask result = this.host.waitForResponse(op).getBody(QueryTask.class);
return result.results.documentLinks.get(0);
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class TestVSphereProvisionWithStaticIpTask method findTemplate.
private ComputeState findTemplate() throws InterruptedException, ExecutionException, TimeoutException {
String templateVmName = System.getProperty("vc.templateVmName");
QuerySpecification qs = new QuerySpecification();
qs.options.add(QueryOption.EXPAND_CONTENT);
qs.query.addBooleanClause(Query.Builder.create().addFieldClause(ComputeState.FIELD_NAME_NAME, templateVmName).addFieldClause(ServiceDocument.FIELD_NAME_KIND, Utils.buildKind(ComputeState.class)).build());
QueryTask qt = QueryTask.create(qs).setDirect(true);
Operation op = QueryUtils.createQueryTaskOperation(this.host, qt, ServiceTypeCluster.INVENTORY_SERVICE);
QueryTask result = this.host.sendWithFuture(op).thenApply(o -> o.getBody(QueryTask.class)).get(10, TimeUnit.SECONDS);
return Utils.fromJson(result.results.documents.values().iterator().next(), ComputeState.class);
}
Aggregations