Search in sources :

Example 1 with AsyncFunction

use of com.google.common.util.concurrent.AsyncFunction in project jersey by jersey.

the class ListenableFutureAgentResource method recommended.

private ListenableFuture<AgentResponse> recommended(final AgentResponse response) {
    destination.register(RxListenableFutureInvokerProvider.class);
    // Get a list of recommended destinations ...
    final ListenableFuture<List<Destination>> destinations = destination.path("recommended").request().header("Rx-User", "Guava").rx(RxListenableFutureInvoker.class).get(new GenericType<List<Destination>>() {
    });
    // ... transform them to Recommendation instances ...
    final ListenableFuture<List<Recommendation>> recommendations = Futures.transform(destinations, (AsyncFunction<List<Destination>, List<Recommendation>>) destinationList -> {
        final List<Recommendation> recommendationList = Lists.newArrayList(Lists.transform(destinationList, destination -> new Recommendation(destination.getDestination(), null, 0)));
        return Futures.immediateFuture(recommendationList);
    });
    // ... add forecasts and calculations ...
    final ListenableFuture<List<List<Recommendation>>> filledRecommendations = Futures.successfulAsList(Arrays.asList(// Add Forecasts to Recommendations.
    forecasts(recommendations), // Add Forecasts to Recommendations.
    calculations(recommendations)));
    // ... and transform the list into agent response with filled recommendations.
    return Futures.transform(filledRecommendations, (AsyncFunction<List<List<Recommendation>>, AgentResponse>) input -> {
        response.setRecommended(input.get(0));
        return Futures.immediateFuture(response);
    });
}
Also used : Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) GET(javax.ws.rs.GET) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) Suspended(javax.ws.rs.container.Suspended) Calculation(org.glassfish.jersey.examples.rx.domain.Calculation) Uri(org.glassfish.jersey.server.Uri) FutureCallback(com.google.common.util.concurrent.FutureCallback) Destination(org.glassfish.jersey.examples.rx.domain.Destination) GenericType(javax.ws.rs.core.GenericType) Forecast(org.glassfish.jersey.examples.rx.domain.Forecast) List(java.util.List) Futures(com.google.common.util.concurrent.Futures) Lists(com.google.common.collect.Lists) Recommendation(org.glassfish.jersey.examples.rx.domain.Recommendation) RxListenableFutureInvoker(org.glassfish.jersey.client.rx.guava.RxListenableFutureInvoker) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) WebTarget(javax.ws.rs.client.WebTarget) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) RxListenableFutureInvokerProvider(org.glassfish.jersey.client.rx.guava.RxListenableFutureInvokerProvider) ManagedAsync(org.glassfish.jersey.server.ManagedAsync) RxListenableFutureInvoker(org.glassfish.jersey.client.rx.guava.RxListenableFutureInvoker) List(java.util.List) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) Recommendation(org.glassfish.jersey.examples.rx.domain.Recommendation)

Example 2 with AsyncFunction

use of com.google.common.util.concurrent.AsyncFunction in project thingsboard by thingsboard.

the class JpaDashboardInfoDao method findDashboardsByTenantIdAndCustomerId.

@Override
public ListenableFuture<List<DashboardInfo>> findDashboardsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TimePageLink pageLink) {
    log.debug("Try to find dashboards by tenantId [{}], customerId[{}] and pageLink [{}]", tenantId, customerId, pageLink);
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(new CustomerId(customerId), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD, EntityType.DASHBOARD, pageLink);
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<DashboardInfo>>) input -> {
        List<ListenableFuture<DashboardInfo>> dashboardFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            dashboardFutures.add(findByIdAsync(relation.getTo().getId()));
        }
        return Futures.successfulAsList(dashboardFutures);
    });
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Time(java.sql.Time) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) Autowired(org.springframework.beans.factory.annotation.Autowired) CrudRepository(org.springframework.data.repository.CrudRepository) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) NULL_UUID_STR(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID_STR) EntityType(org.thingsboard.server.common.data.EntityType) SqlDao(org.thingsboard.server.dao.util.SqlDao) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) PageRequest(org.springframework.data.domain.PageRequest) UUID(java.util.UUID) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Objects(java.util.Objects) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) JpaAbstractSearchTextDao(org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao) List(java.util.List) DaoUtil(org.thingsboard.server.dao.DaoUtil) DashboardInfoDao(org.thingsboard.server.dao.dashboard.DashboardInfoDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) UUIDConverter(org.thingsboard.server.common.data.UUIDConverter) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) DashboardInfoEntity(org.thingsboard.server.dao.model.sql.DashboardInfoEntity) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) List(java.util.List) CustomerId(org.thingsboard.server.common.data.id.CustomerId) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo)

Example 3 with AsyncFunction

use of com.google.common.util.concurrent.AsyncFunction in project thingsboard by thingsboard.

the class CassandraAlarmDao method findAlarms.

@Override
public ListenableFuture<List<AlarmInfo>> findAlarms(AlarmQuery query) {
    log.trace("Try to find alarms by entity [{}], searchStatus [{}], status [{}] and pageLink [{}]", query.getAffectedEntityId(), query.getSearchStatus(), query.getStatus(), query.getPageLink());
    EntityId affectedEntity = query.getAffectedEntityId();
    String searchStatusName;
    if (query.getSearchStatus() == null && query.getStatus() == null) {
        searchStatusName = AlarmSearchStatus.ANY.name();
    } else if (query.getSearchStatus() != null) {
        searchStatusName = query.getSearchStatus().name();
    } else {
        searchStatusName = query.getStatus().name();
    }
    String relationType = BaseAlarmService.ALARM_RELATION_PREFIX + searchStatusName;
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(affectedEntity, relationType, RelationTypeGroup.ALARM, EntityType.ALARM, query.getPageLink());
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<AlarmInfo>>) input -> {
        List<ListenableFuture<AlarmInfo>> alarmFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            alarmFutures.add(Futures.transform(findAlarmByIdAsync(relation.getTo().getId()), (Function<Alarm, AlarmInfo>) AlarmInfo::new));
        }
        return Futures.successfulAsList(alarmFutures);
    });
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Autowired(org.springframework.beans.factory.annotation.Autowired) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) Function(com.google.common.base.Function) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) UUID(java.util.UUID) Alarm(org.thingsboard.server.common.data.alarm.Alarm) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) CassandraAbstractModelDao(org.thingsboard.server.dao.nosql.CassandraAbstractModelDao) QueryBuilder.select(com.datastax.driver.core.querybuilder.QueryBuilder.select) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Select(com.datastax.driver.core.querybuilder.Select) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) AlarmEntity(org.thingsboard.server.dao.model.nosql.AlarmEntity) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with AsyncFunction

use of com.google.common.util.concurrent.AsyncFunction in project thingsboard by thingsboard.

the class DeviceServiceImpl method findDevicesByQuery.

@Override
public ListenableFuture<List<Device>> findDevicesByQuery(DeviceSearchQuery query) {
    ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(query.toEntitySearchQuery());
    ListenableFuture<List<Device>> devices = Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<Device>>) relations1 -> {
        EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
        List<ListenableFuture<Device>> futures = new ArrayList<>();
        for (EntityRelation relation : relations1) {
            EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
            if (entityId.getEntityType() == EntityType.DEVICE) {
                futures.add(findDeviceByIdAsync(new DeviceId(entityId.getId())));
            }
        }
        return Futures.successfulAsList(futures);
    });
    devices = Futures.transform(devices, new Function<List<Device>, List<Device>>() {

        @Nullable
        @Override
        public List<Device> apply(@Nullable List<Device> deviceList) {
            return deviceList == null ? Collections.emptyList() : deviceList.stream().filter(device -> query.getDeviceTypes().contains(device.getType())).collect(Collectors.toList());
        }
    });
    return devices;
}
Also used : java.util(java.util) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Cache(org.springframework.cache.Cache) Cacheable(org.springframework.cache.annotation.Cacheable) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheEvict(org.springframework.cache.annotation.CacheEvict) TextPageData(org.thingsboard.server.common.data.page.TextPageData) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) org.thingsboard.server.common.data(org.thingsboard.server.common.data) CacheManager(org.springframework.cache.CacheManager) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) Nullable(javax.annotation.Nullable) DEVICE_CACHE(org.thingsboard.server.common.data.CacheConstants.DEVICE_CACHE) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Function(com.google.common.base.Function) DeviceCredentialsType(org.thingsboard.server.common.data.security.DeviceCredentialsType) DaoUtil.toUUIDs(org.thingsboard.server.dao.DaoUtil.toUUIDs) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Validator(org.thingsboard.server.dao.service.Validator) Collectors(java.util.stream.Collectors) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) CustomerDao(org.thingsboard.server.dao.customer.CustomerDao) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) TenantDao(org.thingsboard.server.dao.tenant.TenantDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) DeviceSearchQuery(org.thingsboard.server.common.data.device.DeviceSearchQuery) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) StringUtils(org.springframework.util.StringUtils) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Function(com.google.common.base.Function) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Nullable(javax.annotation.Nullable)

Example 5 with AsyncFunction

use of com.google.common.util.concurrent.AsyncFunction in project thingsboard by thingsboard.

the class CassandraDashboardInfoDao method findDashboardsByTenantIdAndCustomerId.

@Override
public ListenableFuture<List<DashboardInfo>> findDashboardsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TimePageLink pageLink) {
    log.debug("Try to find dashboards by tenantId [{}], customerId[{}] and pageLink [{}]", tenantId, customerId, pageLink);
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(new CustomerId(customerId), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD, EntityType.DASHBOARD, pageLink);
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<DashboardInfo>>) input -> {
        List<ListenableFuture<DashboardInfo>> dashboardFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            dashboardFutures.add(findByIdAsync(relation.getTo().getId()));
        }
        return Futures.successfulAsList(dashboardFutures);
    });
}
Also used : DashboardInfoEntity(org.thingsboard.server.dao.model.nosql.DashboardInfoEntity) CassandraAbstractSearchTextDao(org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Autowired(org.springframework.beans.factory.annotation.Autowired) UUID(java.util.UUID) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) EntityType(org.thingsboard.server.common.data.EntityType) DaoUtil(org.thingsboard.server.dao.DaoUtil) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Collections(java.util.Collections) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) List(java.util.List) CustomerId(org.thingsboard.server.common.data.id.CustomerId) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo)

Aggregations

AsyncFunction (com.google.common.util.concurrent.AsyncFunction)17 Futures (com.google.common.util.concurrent.Futures)14 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)14 List (java.util.List)12 ArrayList (java.util.ArrayList)11 Function (com.google.common.base.Function)7 FutureCallback (com.google.common.util.concurrent.FutureCallback)7 Slf4j (lombok.extern.slf4j.Slf4j)7 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)6 Collections (java.util.Collections)6 Nonnull (javax.annotation.Nonnull)6 Future (java.util.concurrent.Future)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)5 Optional (com.google.common.base.Optional)4 JdkFutureAdapters (com.google.common.util.concurrent.JdkFutureAdapters)4 BigInteger (java.math.BigInteger)4 UUID (java.util.UUID)4 Nullable (javax.annotation.Nullable)4 Inject (javax.inject.Inject)4