All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.swisscom.cloud.sb.broker.metrics.MetricsCache.groovy Maven / Gradle / Ivy

/*
 * Copyright (c) 2018 Swisscom (Switzerland) Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.swisscom.cloud.sb.broker.metrics

import com.swisscom.cloud.sb.broker.model.LastOperation
import com.swisscom.cloud.sb.broker.model.Plan
import com.swisscom.cloud.sb.broker.repository.LastOperationRepository
import com.swisscom.cloud.sb.broker.repository.PlanRepository
import com.swisscom.cloud.sb.broker.repository.ServiceBindingRepository
import com.swisscom.cloud.sb.broker.repository.ServiceInstanceRepository
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

import java.time.LocalDateTime

import static com.swisscom.cloud.sb.broker.model.LastOperation.Status.*

@Component
@Slf4j
class MetricsCache {
    @Autowired
    private ServiceInstanceRepository serviceInstanceRepository
    @Autowired
    private ServiceBindingRepository serviceBindingRepository
    @Autowired
    private PlanRepository planRepository
    @Autowired
    private LastOperationRepository lastOperationRepository
    @Autowired
    private ServiceBrokerMetricsConfig serviceBrokerMetricsConfig

    private static ServiceInstanceList _serviceInstanceList = new ServiceInstanceList()
    private static LocalDateTime serviceInstanceListLastModified = LocalDateTime.MIN

    private static Map _lastOperationMap = new HashMap()
    private static LocalDateTime lastOperationMapLastModified = LocalDateTime.MIN

    private static List _planList = new ArrayList<>()
    private static LocalDateTime planListLastModified = LocalDateTime.MIN

    private static Map _bindingCountByPlanGuid = new HashMap()
    private static LocalDateTime bindingCountByPlanGuidLastModified = LocalDateTime.MIN

    private static Map _failedLastOperationCountByPlanGuid = new HashMap()
    private static LocalDateTime failedLastOperationCountByPlanGuidLastModified = LocalDateTime.MIN

    ServiceInstanceList getServiceInstanceList() {
        synchronized (_serviceInstanceList) {
            if (hasExpired(serviceInstanceListLastModified)) {
                def allServiceInstances = serviceInstanceRepository.findAll();

                _serviceInstanceList.refresh(getLastOperationMap(), allServiceInstances)
                serviceInstanceListLastModified = LocalDateTime.now()
            }

            return _serviceInstanceList
        }
    }

    Map getLastOperationMap() {
        synchronized (_lastOperationMap) {
            if (hasExpired(lastOperationMapLastModified)) {
                def allLastOperations = lastOperationRepository.findAll()
                allLastOperations.each { lO -> _lastOperationMap.put(lO.guid, lO) }
                lastOperationMapLastModified = LocalDateTime.now()
            }

            return _lastOperationMap
        }
    }

    Map getBindingCountByPlanGuid() {
        synchronized (_bindingCountByPlanGuid) {
            if (hasExpired(bindingCountByPlanGuidLastModified))
                createBindingCountByPlanGuid().each { k, v -> _bindingCountByPlanGuid.put(k, v) }
            return _bindingCountByPlanGuid
        }
    }

    private Map createBindingCountByPlanGuid() {
        HashMap bindingCountByPlanGuid = new HashMap<>()

        Map planIdToPlanGuid = new HashMap<>()
        getListOfPlans().each { plan -> planIdToPlanGuid.put(plan.id, plan.guid) }

        Map serviceInstanceIdToPlanGuid = new HashMap<>()
        getServiceInstanceList().each { sI -> serviceInstanceIdToPlanGuid.put(sI.id, planIdToPlanGuid[sI.planId]) }

        def allBindings = serviceBindingRepository.findAll()
        allBindings.each { binding ->
            def planGuid = serviceInstanceIdToPlanGuid[binding.serviceInstanceId]
            bindingCountByPlanGuid.put(planGuid, bindingCountByPlanGuid.get(planGuid, 0.0D) + 1)
        }

        return bindingCountByPlanGuid
    }

    Double getFailedLastOperationCount(String planGuid) {
        synchronized (_failedLastOperationCountByPlanGuid) {
            if (hasExpired(failedLastOperationCountByPlanGuidLastModified))
                createFailedLastOperationMap().each { k, v -> _failedLastOperationCountByPlanGuid.put(k, v) }
            return _failedLastOperationCountByPlanGuid.get(planGuid, 0.0D)
        }
    }

    private Map createFailedLastOperationMap() {
        Map planIdToPlanGuid = new HashMap<>()
        getListOfPlans().each { plan -> planIdToPlanGuid.put(plan.id, plan.guid) }

        Map serviceInstanceGuidToPlanGuid = new HashMap<>()
        getServiceInstanceList().each { sI -> serviceInstanceGuidToPlanGuid.put(sI.guid, planIdToPlanGuid[sI.planId]) }

        Map failedCountByPlanId = new HashMap<>()
        lastOperationMap.values().each { lastOperation ->
            if (lastOperation.status == FAILED) {
                def planGuid = serviceInstanceGuidToPlanGuid.get(lastOperation.guid)
                failedCountByPlanId.put(planGuid, failedCountByPlanId.get(planGuid, 0.0D) + 1)
            }
        }

        failedCountByPlanId
    }

    private int getTimeoutInSeconds() {
        Integer.parseInt(serviceBrokerMetricsConfig.step.substring(0, serviceBrokerMetricsConfig.step.length()-1))
    }

    private boolean hasExpired(LocalDateTime dateOflastRefresh) {
        dateOflastRefresh.isBefore(LocalDateTime.now().minusSeconds(getTimeoutInSeconds()))
    }

    List getListOfPlans() {
        synchronized (_planList) {
            if (hasExpired(planListLastModified)) {
                _planList.clear()
                _planList.addAll(planRepository.findAll().findAll { p -> p.name && p.service != null && p.service.name })
                planListLastModified = LocalDateTime.now()
            }

            return _planList
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy