Skip to content

Commit

Permalink
Update sequence on ExecutionEventAdded events
Browse files Browse the repository at this point in the history
  • Loading branch information
toddburnside committed Feb 6, 2025
1 parent 3a37b2b commit dd849f6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
12 changes: 12 additions & 0 deletions explore/src/clue/scala/queries/common/VisitQueriesGQL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,15 @@ object VisitQueriesGQL:
}
"""
// ${ExecutionVisitsSubquery.Fragments}

@GraphQL
trait ExecutionEventAddedSubscription extends GraphQLOperation[ObservationDB]:
val document = s"""
subscription($$input: ExecutionEventAddedInput!) {
executionEventAdded(input: $$input) {
value {
id
}
}
}
"""
38 changes: 27 additions & 11 deletions explore/src/main/scala/explore/config/sequence/SequenceTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import cats.Eq
import cats.derived.*
import cats.effect.IO
import cats.syntax.all.*
import clue.StreamingClient
import clue.data.syntax.*
import crystal.Pot
import crystal.react.*
import crystal.react.View
Expand Down Expand Up @@ -35,13 +37,16 @@ import lucuma.core.util.TimeSpan
import lucuma.react.common.ReactFnProps
import lucuma.react.primereact.Message
import lucuma.refined.*
import lucuma.schemas.ObservationDB
import lucuma.schemas.ObservationDB.Types.ExecutionEventAddedInput
import lucuma.schemas.model.ExecutionVisits
import lucuma.schemas.odb.SequenceQueriesGQL.*
import lucuma.schemas.odb.input.*
import lucuma.ui.syntax.all.*
import lucuma.ui.syntax.all.given
import queries.common.ObsQueriesGQL
import queries.common.TargetQueriesGQL
import queries.common.VisitQueriesGQL
import queries.common.VisitQueriesGQL.*

object SequenceTile:
Expand Down Expand Up @@ -96,6 +101,23 @@ object SequenceTile:

given Reusability[SequenceData] = Reusability.byEq

def obsEditSubscription(obsId: Observation.Id)(using StreamingClient[IO, ObservationDB]) =
ObsQueriesGQL.ObservationEditSubscription
.subscribe[IO]:
obsId.toObservationEditInput

def executionEventAddedSubscription(obsId: Observation.Id)(using
StreamingClient[IO, ObservationDB]
) = VisitQueriesGQL.ExecutionEventAddedSubscription.subscribe[IO](
ExecutionEventAddedInput(observationId = obsId.assign)
)

def queryTriggers(obsId: Observation.Id)(using StreamingClient[IO, ObservationDB]) =
for {
o <- obsEditSubscription(obsId)
e <- executionEventAddedSubscription(obsId)
} yield o.merge(e)

private val component =
ScalaFnComponent
.withHooks[Props]
Expand All @@ -108,14 +130,13 @@ object SequenceTile:
.raiseGraphQLErrors
.map(_.observation.flatMap(_.execution))
.attemptPot
.resetOnResourceSignals:
ObsQueriesGQL.ObservationEditSubscription
.subscribe[IO]:
props.obsId.toObservationEditInput
.reRunOnResourceSignals:
queryTriggers(props.obsId)
.useStreamResourceOnMountBy: (props, ctx, _) => // Query Sequence
import ctx.given

val targetChanges = props.targetIds.traverse: targetId =>
// sequence also triggers on target changes
val targetChanges = props.targetIds.map: targetId =>
TargetQueriesGQL.TargetEditSubscription
.subscribe[IO](targetId)

Expand All @@ -124,12 +145,7 @@ object SequenceTile:
.raiseGraphQLErrors
.map(SequenceData.fromOdbResponse)
.attemptPot
.resetOnResourceSignals:
for
o <- ObsQueriesGQL.ObservationEditSubscription
.subscribe[IO](props.obsId.toObservationEditInput)
t <- targetChanges
yield o.merge(t.sequence)
.reRunOnResourceSignals(queryTriggers(props.obsId), targetChanges*)
.useEffectWithDepsBy((_, _, visits, sequenceData) =>
(visits.toPot.flatten, sequenceData.toPot.flatten).tupled
): (props, _, _, _) =>
Expand Down

0 comments on commit dd849f6

Please sign in to comment.