use of com.vmware.xenon.services.common.QueryTask.Builder 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.Builder 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);
});
}
Aggregations