Search in sources :

Example 16 with ComputeStats

use of com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats in project photon-model by vmware.

the class AzureCostStatsService method createMonthlyStats.

private void createMonthlyStats(Context context, Stages next) {
    context.billProcessedTimeMillis = context.billParsedMillis;
    if (AzureCostHelper.isCurrentMonth(new LocalDate(context.billParsedMillis, DateTimeZone.UTC))) {
        context.currentMonthBillParsedMillis = context.billParsedMillis;
    }
    context.allSubscriptionsCost.values().forEach(subscription -> createAzureSubscriptionStats(context, subscription));
    List<ComputeStats> eaAccountStats = AzureCostHelper.createEaAccountStats(context.eaAcCost, context.computeHostDesc, context.currentMonthBillParsedMillis, context.billParsedMillis, context.oldestBillProcessedMillis, context.auth.privateKey);
    if (eaAccountStats.size() > 0) {
        context.statsResponse.statsList.addAll(eaAccountStats);
        logInfo(() -> String.format("Finished collecting cost stats for the month of %s " + "for endpoint %s ", context.billMonthToDownload, context.computeHostDesc.endpointLink));
        postStats(context);
    }
    cleanUpMonthlyData(context);
    context.stage = next;
    handleRequest(context);
}
Also used : ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) LocalDate(org.joda.time.LocalDate)

Example 17 with ComputeStats

use of com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats in project photon-model by vmware.

the class AzureCostStatsService method createServiceStatsForSubscription.

private void createServiceStatsForSubscription(Context context, AzureSubscription subscription) {
    Consumer<List<ComputeState>> serviceStatsProcessor = (subscriptionComputeStates) -> subscriptionComputeStates.forEach(subscriptionComputeState -> {
        List<ComputeStats> resourceStatsList = new ArrayList<>();
        ComputeStats subscriptionStats = new ComputeStats();
        subscriptionStats.statValues = new ConcurrentHashMap<>();
        subscriptionStats.computeLink = subscriptionComputeState.documentSelfLink;
        subscriptionStats.addCustomProperty(PhotonModelConstants.DOES_CONTAIN_SERVICE_STATS, Boolean.TRUE.toString());
        for (AzureService service : subscription.getServices().values()) {
            List<ComputeStats> resourcesCostStats = new ArrayList<>();
            Map<String, List<ServiceStat>> statsForAzureService = createStatsForAzureService(context, service, resourcesCostStats);
            subscriptionStats.statValues.putAll(statsForAzureService);
            resourceStatsList.addAll(resourcesCostStats);
        }
        if (!subscriptionStats.statValues.isEmpty()) {
            context.statsResponse.statsList.add(subscriptionStats);
        }
        if (!resourceStatsList.isEmpty()) {
            context.statsResponse.statsList.addAll(resourceStatsList);
        }
    });
    processSubscriptionStats(context, subscription, serviceStatsProcessor);
}
Also used : AzureCostConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureCostConstants) AzureStatsNormalizer(com.vmware.photon.controller.model.adapters.azure.utils.AzureStatsNormalizer) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) StringUtils(org.apache.commons.lang3.StringUtils) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) EndpointAllocationTaskService(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) Utils(com.vmware.xenon.common.Utils) BufferedSink(okio.BufferedSink) Map(java.util.Map) AzureService(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureService) URI(java.net.URI) AzureSubscriptionsEnumerationRequest(com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService.AzureSubscriptionsEnumerationRequest) Builder(com.vmware.xenon.services.common.QueryTask.Builder) Path(java.nio.file.Path) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) Interceptor(okhttp3.Interceptor) ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) AzureDetailedBillHandler(com.vmware.photon.controller.model.adapters.azure.ea.utils.AzureDetailedBillHandler) Request(okhttp3.Request) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) OldEaSummarizedBillElement(com.vmware.photon.controller.model.adapters.azure.model.cost.OldEaSummarizedBillElement) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AzureSubscriptionsEnumerationService(com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService) AzureCostHelper(com.vmware.photon.controller.model.adapters.azure.ea.utils.AzureCostHelper) List(java.util.List) Stream(java.util.stream.Stream) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) INTERNAL_REQUEST_TIMEOUT_SECONDS(com.vmware.photon.controller.model.adapters.azure.constants.AzureCostConstants.INTERNAL_REQUEST_TIMEOUT_SECONDS) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) SingleResourceTaskCollectionStage(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceTaskCollectionStage) Okio(okio.Okio) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) OperationContext(com.vmware.xenon.common.OperationContext) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) HashMap(java.util.HashMap) Function(java.util.function.Function) AzureResource(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureResource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) CollectionUtils(org.apache.commons.collections.CollectionUtils) EaBillLinkElement(com.vmware.photon.controller.model.adapters.azure.model.cost.EaBillLinkElement) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AUTO_DISCOVERED_ENTITY(com.vmware.photon.controller.model.constants.PhotonModelConstants.AUTO_DISCOVERED_ENTITY) EaBillLinks(com.vmware.photon.controller.model.adapters.azure.model.cost.EaBillLinks) BiConsumer(java.util.function.BiConsumer) Response(okhttp3.Response) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) Call(okhttp3.Call) Callback(okhttp3.Callback) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) ExecutorService(java.util.concurrent.ExecutorService) OldApi(com.vmware.photon.controller.model.adapters.azure.model.cost.OldApi) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Files(java.nio.file.Files) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) TypeName(com.vmware.xenon.common.ServiceDocumentDescription.TypeName) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) File(java.io.File) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) BillParsingStatus(com.vmware.photon.controller.model.adapters.azure.model.cost.BillParsingStatus) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) LocalDate(org.joda.time.LocalDate) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) OkHttpClient(okhttp3.OkHttpClient) Paths(java.nio.file.Paths) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ServiceDocumentDescription(com.vmware.xenon.common.ServiceDocumentDescription) Comparator(java.util.Comparator) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) OperationJoin(com.vmware.xenon.common.OperationJoin) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) AzureService(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureService) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 18 with ComputeStats

use of com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats in project photon-model by vmware.

the class AzureCostHelper method createBillProcessedTimeStat.

static ComputeStats createBillProcessedTimeStat(ComputeStateWithDescription computeHostDesc, long billProcessedTimeMillis) {
    // Create bill processed time stat: will be linked with the EA account's compute state
    // since the bill is obtained and processed at the EA account level.
    ComputeStats accountStats = new ComputeStats();
    accountStats.computeLink = computeHostDesc.documentSelfLink;
    accountStats.statValues = new ConcurrentSkipListMap<>();
    accountStats.addCustomProperty(PhotonModelConstants.CONTAINS_BILL_PROCESSED_TIME_STAT, Boolean.TRUE.toString());
    ServiceStat billProcessedTimeStat = AzureCostHelper.createServiceStat(PhotonModelConstants.CLOUD_ACCOUNT_COST_SYNC_MARKER_MILLIS, billProcessedTimeMillis, PhotonModelConstants.UNIT_MILLISECONDS, billProcessedTimeMillis);
    accountStats.statValues.put(billProcessedTimeStat.name, Collections.singletonList(billProcessedTimeStat));
    return accountStats;
}
Also used : ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)

Example 19 with ComputeStats

use of com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats in project photon-model by vmware.

the class TestAWSCostAdapterService method issueStatsRequest.

private void issueStatsRequest(ComputeState account) throws Throwable {
    List<ComputeStats> allStats = new ArrayList<>();
    // 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 (TestAWSCostAdapterService.this.isMock) {
                    SingleResourceStatsCollectionTaskState resp = op.getBody(SingleResourceStatsCollectionTaskState.class);
                    allStats.addAll(resp.statsList);
                    if (resp.isFinalBatch) {
                        verifyCollectedStats(allStats);
                        TestAWSCostAdapterService.this.host.completeIteration();
                    }
                } else {
                    TestAWSCostAdapterService.this.host.completeIteration();
                }
            }
        }
    };
    sendStatsRequest(account, parentService);
}
Also used : ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) StatelessService(com.vmware.xenon.common.StatelessService) ArrayList(java.util.ArrayList) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) Operation(com.vmware.xenon.common.Operation)

Example 20 with ComputeStats

use of com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats in project photon-model by vmware.

the class SingleResourceStatsCollectionTaskService method updateAndPersistStats.

private void updateAndPersistStats(SingleResourceStatsCollectionTaskState currentState) {
    if (currentState.statsAdapterReference == null) {
        throw new IllegalStateException("stats adapter reference should not be null");
    }
    if (currentState.statsList.size() == 0) {
        // If there are no stats reported and if it's a final batch, just finish the task.
        if (currentState.isFinalBatch) {
            SingleResourceStatsCollectionTaskState nextStatePatch = new SingleResourceStatsCollectionTaskState();
            nextStatePatch.taskInfo = TaskUtils.createTaskState(TaskStage.FINISHED);
            TaskUtils.sendPatch(this, nextStatePatch);
        }
        return;
    }
    long expirationTime = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(EXPIRATION_INTERVAL);
    List<Operation> operations = new ArrayList<>();
    List<ResourceMetrics> metricsList = new ArrayList<>();
    List<InMemoryResourceMetric> inMemoryMetricsList = new ArrayList<>();
    // Push the last collection metric to the in memory stats available at the
    // compute-link/stats URI.
    ServiceStats.ServiceStat minuteStats = new ServiceStats.ServiceStat();
    String statsLink = getAdapterLinkFromURI(currentState.statsAdapterReference);
    minuteStats.name = getLastCollectionMetricKeyForAdapterLink(statsLink, true);
    minuteStats.latestValue = Utils.getNowMicrosUtc();
    minuteStats.sourceTimeMicrosUtc = Utils.getNowMicrosUtc();
    minuteStats.unit = PhotonModelConstants.UNIT_MICROSECONDS;
    URI inMemoryStatsUri = UriUtils.buildStatsUri(UriUtils.extendUri(UriUtils.buildUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.INVENTORY_SERVICE)), currentState.computeLink));
    operations.add(Operation.createPost(inMemoryStatsUri).setBody(minuteStats));
    populateResourceMetrics(metricsList, getLastCollectionMetricKeyForAdapterLink(statsLink, false), minuteStats, currentState.computeLink, expirationTime, null);
    for (ComputeStats stats : currentState.statsList) {
        // TODO: https://jira-hzn.eng.vmware.com/browse/VSYM-330
        InMemoryResourceMetric hourlyMemoryState = new InMemoryResourceMetric();
        if (currentState.publishInMemory) {
            String computeId = UriUtils.getLastPathSegment(stats.computeLink);
            hourlyMemoryState.timeSeriesStats = new HashMap<>();
            hourlyMemoryState.documentSelfLink = computeId + StatsConstants.HOUR_SUFFIX;
            inMemoryMetricsList.add(hourlyMemoryState);
        }
        for (Entry<String, List<ServiceStat>> entries : stats.statValues.entrySet()) {
            // sort stats by source time
            entries.getValue().sort(Comparator.comparing(o -> o.sourceTimeMicrosUtc));
            // Persist every data point
            for (ServiceStat serviceStat : entries.getValue()) {
                String computeLink = stats.computeLink;
                if (computeLink == null) {
                    computeLink = currentState.computeLink;
                }
                if (currentState.publishInMemory) {
                    // update in-memory stats
                    updateInMemoryStats(hourlyMemoryState, entries.getKey(), serviceStat, StatsConstants.BUCKET_SIZE_HOURS_IN_MILLIS);
                }
                populateResourceMetrics(metricsList, entries.getKey(), serviceStat, computeLink, expirationTime, stats.getCustomProperties());
            }
        }
    }
    for (ResourceMetrics metrics : metricsList) {
        operations.add(Operation.createPost(UriUtils.buildUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.METRIC_SERVICE), ResourceMetricsService.FACTORY_LINK)).setBodyNoCloning(metrics));
    }
    if (currentState.publishInMemory) {
        for (InMemoryResourceMetric metric : inMemoryMetricsList) {
            operations.add(Operation.createPost(getHost(), InMemoryResourceMetricService.FACTORY_LINK).setBodyNoCloning(metric));
        }
    }
    // Save each data point sequentially to create time based monotonically increasing sequence.
    batchPersistStats(operations, 0, currentState.isFinalBatch);
}
Also used : Service(com.vmware.xenon.common.Service) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) AggregationType(com.vmware.xenon.common.ServiceStats.TimeSeriesStats.AggregationType) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) Utils(com.vmware.xenon.common.Utils) TaskFactoryService(com.vmware.xenon.services.common.TaskFactoryService) Map(java.util.Map) URI(java.net.URI) EnumSet(java.util.EnumSet) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) ServiceStats(com.vmware.xenon.common.ServiceStats) List(java.util.List) TimeSeriesStats(com.vmware.xenon.common.ServiceStats.TimeSeriesStats) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) TaskState(com.vmware.xenon.common.TaskState) TaskOption(com.vmware.photon.controller.model.tasks.TaskOption) FactoryService(com.vmware.xenon.common.FactoryService) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) TaskService(com.vmware.xenon.services.common.TaskService) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) TaskUtils(com.vmware.photon.controller.model.tasks.TaskUtils) InMemoryResourceMetric(com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService.InMemoryResourceMetric) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ResourceMetricsService(com.vmware.photon.controller.model.monitoring.ResourceMetricsService) ArrayList(java.util.ArrayList) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) OperationSequence(com.vmware.xenon.common.OperationSequence) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) TypeName(com.vmware.xenon.common.ServiceDocumentDescription.TypeName) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) InMemoryResourceMetricService(com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) Comparator(java.util.Comparator) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) InMemoryResourceMetric(com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService.InMemoryResourceMetric) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ServiceStats(com.vmware.xenon.common.ServiceStats) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ComputeStats (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)20 ServiceStat (com.vmware.xenon.common.ServiceStats.ServiceStat)15 ArrayList (java.util.ArrayList)11 SingleResourceStatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)8 ResourceMetrics (com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)7 Operation (com.vmware.xenon.common.Operation)7 QueryTask (com.vmware.xenon.services.common.QueryTask)7 HashMap (java.util.HashMap)7 List (java.util.List)7 Map (java.util.Map)7 LocalDate (org.joda.time.LocalDate)7 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)6 PhotonModelConstants (com.vmware.photon.controller.model.constants.PhotonModelConstants)6 ServiceDocument (com.vmware.xenon.common.ServiceDocument)6 StatelessService (com.vmware.xenon.common.StatelessService)6 UriUtils (com.vmware.xenon.common.UriUtils)6 Utils (com.vmware.xenon.common.Utils)6 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)5 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)5 ClusterUtil (com.vmware.photon.controller.model.util.ClusterUtil)5