use of com.vmware.xenon.common.ServiceStats.ServiceStat 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.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class AWSCostStatsService method createStatsForAwsService.
private Map<String, List<ServiceStat>> createStatsForAwsService(AwsServiceDetailDto serviceDetailDto) {
String currencyUnit = AWSStatsNormalizer.getNormalizedUnitValue(DIMENSION_CURRENCY_VALUE);
// remove any spaces in the service name.
String serviceCode = serviceDetailDto.id.replaceAll(" ", "");
Map<String, List<ServiceStat>> stats = new HashMap<>();
// Create stats for hourly resource cost
List<ServiceStat> serviceStats = new ArrayList<>();
String serviceResourceCostMetric = String.format(AWSConstants.SERVICE_RESOURCE_COST, serviceCode);
for (Entry<Long, Double> cost : serviceDetailDto.directCosts.entrySet()) {
if (cost.getValue() > 0) {
ServiceStat resourceCostStat = createStat(currencyUnit, serviceResourceCostMetric, cost.getKey(), cost.getValue());
serviceStats.add(resourceCostStat);
}
}
if (!serviceStats.isEmpty()) {
stats.put(serviceResourceCostMetric, serviceStats);
}
// Create stats for hourly other costs
serviceStats = new ArrayList<>();
String serviceOtherCostMetric = String.format(AWSConstants.SERVICE_OTHER_COST, serviceCode);
for (Entry<Long, Double> cost : serviceDetailDto.otherCosts.entrySet()) {
if (cost.getValue() > 0) {
ServiceStat otherCostStat = createStat(currencyUnit, serviceOtherCostMetric, cost.getKey(), cost.getValue());
serviceStats.add(otherCostStat);
}
}
if (!serviceStats.isEmpty()) {
stats.put(serviceOtherCostMetric, serviceStats);
}
// Create stats for monthly other costs
serviceStats = new ArrayList<>();
String serviceMonthlyOtherCostMetric = String.format(AWSConstants.SERVICE_MONTHLY_OTHER_COST, serviceCode);
for (Entry<Long, Double> cost : serviceDetailDto.remainingCosts.entrySet()) {
if (cost.getValue() > 0) {
ServiceStat monthlyOtherCostStat = createStat(currencyUnit, serviceMonthlyOtherCostMetric, cost.getKey(), cost.getValue());
serviceStats.add(monthlyOtherCostStat);
}
}
if (!serviceStats.isEmpty()) {
stats.put(serviceMonthlyOtherCostMetric, serviceStats);
}
// Create stats for monthly reserved recurring instance costs
serviceStats = new ArrayList<>();
String serviceReservedRecurringCostMetric = String.format(AWSConstants.SERVICE_RESERVED_RECURRING_COST, serviceCode);
for (Entry<Long, Double> cost : serviceDetailDto.reservedRecurringCosts.entrySet()) {
if (cost.getValue() > 0) {
ServiceStat recurringCostStat = createStat(currencyUnit, serviceReservedRecurringCostMetric, cost.getKey(), cost.getValue());
serviceStats.add(recurringCostStat);
}
}
if (!serviceStats.isEmpty()) {
stats.put(serviceReservedRecurringCostMetric, serviceStats);
}
return stats;
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class AWSMockStatsService method getMockStats.
/**
* Gets mock EC2 statistics.
*
* @param statsData The context object for mock stats.
* @param metricNames The metrics names to gather stats for.
*/
private void getMockStats(AWSMockStatsDataHolder statsData, String[] metricNames) {
for (String metricName : metricNames) {
// start feeding mock stats data
if (MOCK_STATS_MAP.get(metricName) != null) {
logFine(() -> String.format("Retrieving %s mock metric from AWS", metricName));
List<ServiceStat> statValue = MOCK_STATS_MAP.get(metricName);
statsData.statsResponse.statValues.put(AWSStatsNormalizer.getNormalizedStatKeyValue(metricName), statValue);
}
}
SingleResourceStatsCollectionTaskState respBody = new SingleResourceStatsCollectionTaskState();
statsData.statsResponse.computeLink = statsData.computeState.documentSelfLink;
respBody.statsAdapterReference = UriUtils.buildUri(getHost(), SELF_LINK);
respBody.taskStage = SingleResourceTaskCollectionStage.valueOf(statsData.statsRequest.nextStage);
respBody.statsList = new ArrayList<>();
respBody.statsList.add(statsData.statsResponse);
this.sendRequest(Operation.createPatch(statsData.statsRequest.taskReference).setBody(respBody));
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class LongRunEndToEndAzureStatsAggregation method checkInMemoryStatsPresent.
/**
* Perform check to verify that every compute resource has stats generated for all the metric
* keys using which stats aggregation was performed on resources.
*/
private void checkInMemoryStatsPresent(ServiceDocumentQueryResult res) {
for (Map.Entry<String, Object> resourceMap : res.documents.entrySet()) {
ServiceStats resStats = this.host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, null, resourceMap.getKey())));
int statCount = 0;
for (ServiceStat stat : resStats.entries.values()) {
for (String key : AzureTestUtil.getMetricNames()) {
if (stat.name.startsWith(key)) {
statCount++;
break;
}
}
}
// after stats aggregation all resources should have all metrics.
assertEquals("Did not find in-memory stats", AzureTestUtil.getMetricNames().size(), statCount);
}
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class TestAzureEnumerationTask method issueStatsRequest.
private void issueStatsRequest(String selfLink, boolean isComputeHost) throws Throwable {
// spin up a stateless service that acts as the parent link to patch back to
StatelessService parentService = new StatelessService() {
@Override
public void handleRequest(Operation op) {
if (op.getAction() == Action.PATCH) {
if (!TestAzureEnumerationTask.this.isMock) {
ComputeStatsResponse resp = op.getBody(ComputeStatsResponse.class);
if (resp == null) {
TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("response was null."));
return;
}
if (resp.statsList.size() != 1) {
TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("response size was incorrect."));
return;
}
if (resp.statsList.get(0).statValues.size() == 0) {
TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("incorrect number of metrics received."));
return;
}
if (!resp.statsList.get(0).computeLink.equals(selfLink)) {
TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("Incorrect resourceReference returned."));
return;
}
// Verify all the stats are obtained
verifyStats(resp, isComputeHost);
// Persist stats on Verification Host for testing the computeHost stats.
URI persistStatsUri = UriUtils.buildUri(getHost(), ResourceMetricsService.FACTORY_LINK);
ResourceMetricsService.ResourceMetrics resourceMetric = new ResourceMetricsService.ResourceMetrics();
resourceMetric.documentSelfLink = StatsUtil.getMetricKey(selfLink, Utils.getNowMicrosUtc());
resourceMetric.entries = new HashMap<>();
resourceMetric.timestampMicrosUtc = Utils.getNowMicrosUtc();
for (String key : resp.statsList.get(0).statValues.keySet()) {
List<ServiceStat> stats = resp.statsList.get(0).statValues.get(key);
for (ServiceStat stat : stats) {
if (stat == null) {
continue;
}
resourceMetric.entries.put(key, stat.latestValue);
}
}
TestAzureEnumerationTask.this.host.sendRequest(Operation.createPost(persistStatsUri).setReferer(TestAzureEnumerationTask.this.host.getUri()).setBodyNoCloning(resourceMetric));
}
TestAzureEnumerationTask.this.host.completeIteration();
}
}
};
String servicePath = UUID.randomUUID().toString();
Operation startOp = Operation.createPost(UriUtils.buildUri(this.host, servicePath));
this.host.startService(startOp, parentService);
ComputeStatsRequest statsRequest = new ComputeStatsRequest();
statsRequest.resourceReference = UriUtils.buildUri(this.host, selfLink);
statsRequest.nextStage = SingleResourceTaskCollectionStage.UPDATE_STATS.name();
statsRequest.isMockRequest = this.isMock;
statsRequest.taskReference = UriUtils.buildUri(this.host, servicePath);
this.host.sendAndWait(Operation.createPatch(UriUtils.buildUri(this.host, AzureUriPaths.AZURE_STATS_ADAPTER)).setBody(statsRequest).setReferer(this.host.getUri()));
}
Aggregations