Search in sources :

Example 1 with SECONDS

use of java.time.temporal.ChronoUnit.SECONDS in project keywhiz by square.

the class ClientDAO method sawClient.

public void sawClient(Client client) {
    Instant now = Instant.now();
    Instant lastSeen = Optional.ofNullable(client.getLastSeen()).map(ls -> Instant.ofEpochSecond(ls.toEpochSecond())).orElse(null);
    // this way we can have less granularity on lastSeen and save db writes
    if (lastSeen == null || now.isAfter(lastSeen.plus(lastSeenThreshold, SECONDS))) {
        dslContext.transaction(configuration -> {
            Param<Long> val = DSL.val(now.getEpochSecond(), CLIENTS.LASTSEEN);
            DSL.using(configuration).update(CLIENTS).set(CLIENTS.LASTSEEN, DSL.when(CLIENTS.LASTSEEN.isNull(), val).otherwise(DSL.greatest(CLIENTS.LASTSEEN, val))).where(CLIENTS.ID.eq(client.getId())).execute();
        });
    }
}
Also used : SECONDS(java.time.temporal.ChronoUnit.SECONDS) MEMBERSHIPS(keywhiz.jooq.tables.Memberships.MEMBERSHIPS) ImmutableSet(com.google.common.collect.ImmutableSet) DSL(org.jooq.impl.DSL) ClientsRecord(keywhiz.jooq.tables.records.ClientsRecord) ApiDate(keywhiz.api.ApiDate) Readonly(keywhiz.service.config.Readonly) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Instant(java.time.Instant) Inject(javax.inject.Inject) Param(org.jooq.Param) Configuration(org.jooq.Configuration) List(java.util.List) CLIENTS(keywhiz.jooq.tables.Clients.CLIENTS) OffsetDateTime(java.time.OffsetDateTime) ChronoUnit(java.time.temporal.ChronoUnit) Optional(java.util.Optional) DSLContext(org.jooq.DSLContext) Client(keywhiz.api.model.Client) Instant(java.time.Instant)

Aggregations

Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Instant (java.time.Instant)1 OffsetDateTime (java.time.OffsetDateTime)1 ChronoUnit (java.time.temporal.ChronoUnit)1 SECONDS (java.time.temporal.ChronoUnit.SECONDS)1 List (java.util.List)1 Optional (java.util.Optional)1 Inject (javax.inject.Inject)1 ApiDate (keywhiz.api.ApiDate)1 Client (keywhiz.api.model.Client)1 CLIENTS (keywhiz.jooq.tables.Clients.CLIENTS)1 MEMBERSHIPS (keywhiz.jooq.tables.Memberships.MEMBERSHIPS)1 ClientsRecord (keywhiz.jooq.tables.records.ClientsRecord)1 Readonly (keywhiz.service.config.Readonly)1 Configuration (org.jooq.Configuration)1 DSLContext (org.jooq.DSLContext)1 Param (org.jooq.Param)1 DSL (org.jooq.impl.DSL)1