Skip to content

Commit

Permalink
s/Env/Log/ (#176)
Browse files Browse the repository at this point in the history
* s/Env/Log/

* Fix examples
  • Loading branch information
purrgrammer authored and raulraja committed Feb 5, 2019
1 parent 02c3738 commit 4eb9abd
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 236 deletions.
22 changes: 11 additions & 11 deletions debug/shared/src/main/scala/debug.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ object debug {
})
} yield lastR

def showEnv(env: Env): Document = env.rounds match {
def showLog(log: Log): Document = log.rounds match {
case Nil => Document.empty
case _ => {
val duration: Option[Long] = for {
firstRound <- env.rounds.headOption
firstRound <- log.rounds.headOption
firstRequestStart <- firstRequest(firstRound)
lastRound <- env.rounds.lastOption
lastRound <- log.rounds.lastOption
lastRequestEnd <- lastRequest(lastRound)
} yield lastRequestEnd - firstRequestStart
val durationDoc =
duration.fold(Document.empty: Document)((d: Long) =>
Document.text("Fetch execution") :: showDuration(d))

durationDoc :/: Document.nest(2, pile(env.rounds.mapWithIndex((r, i) => showRound(r, i + 1))))
durationDoc :/: Document.nest(2, pile(log.rounds.mapWithIndex((r, i) => showRound(r, i + 1))))
}
}

Expand Down Expand Up @@ -91,25 +91,25 @@ object debug {
Document.text(s"`${d.name}` missing identities ${ids}")

def showRoundCount(err: FetchException): Document =
Document.text(s", fetch interrupted after ${err.environment.rounds.size} rounds")
Document.text(s", fetch interrupted after ${err.log.rounds.size} rounds")

def showException(err: FetchException): Document = err match {
case MissingIdentity(id, q, env) =>
case MissingIdentity(id, q, log) =>
Document.text(s"[ERROR] Identity with id `${id}` for data source `${q.data.name}` not found") :: showRoundCount(err)
case UnhandledException(exc, env) =>
case UnhandledException(exc, log) =>
Document
.text(s"[ERROR] Unhandled `${exc.getClass.getName}`: '${exc.getMessage}'") :: showRoundCount(err)
}

/* Given a [[fetch.env.Env]], describe it with a human-readable string. */
def describe(env: Env): String =
string(showEnv(env))
/* Given a [[fetch.env.Log]], describe it with a human-readable string. */
def describe(log: Log): String =
string(showLog(log))

/* Given a [[Throwable]], describe it with a human-readable string. */
def describe(err: Throwable): String = err match {
case fe: FetchException => string(
showException(fe) :/:
Document.nest(2, showEnv(fe.environment))
Document.nest(2, showLog(fe.log))
)
case _ => string(Document.text("Unexpected exception"))
}
Expand Down
24 changes: 12 additions & 12 deletions docs/src/main/tut/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -725,15 +725,15 @@ def failingFetch[F[_] : ConcurrentEffect]: Fetch[F, String] = for {
c <- fetchException
} yield s"${a.username} loves ${b.username}"
val result: IO[Either[Throwable, (Env, String)]] = Fetch.runEnv[IO](failingFetch).attempt
val result: IO[Either[Throwable, (Log, String)]] = Fetch.runLog[IO](failingFetch).attempt
```

Now let's use the `fetch.debug.describe` function for describing the error if we find one:

```tut:book
import fetch.debug.describe
val value: Either[Throwable, (Env, String)] = result.unsafeRunSync
val value: Either[Throwable, (Log, String)] = result.unsafeRunSync
println(value.fold(describe, _.toString))
```
Expand All @@ -750,13 +750,13 @@ identity was not found. Whenever an identity cannot be found, the fetch executio
def missingUser[F[_] : ConcurrentEffect] =
getUser(5)
val result: IO[Either[Throwable, (Env, User)]] = Fetch.runEnv[IO](missingUser).attempt
val result: IO[Either[Throwable, (Log, User)]] = Fetch.runLog[IO](missingUser).attempt
```

And now we can execute the fetch and describe its execution:

```tut:book
val value: Either[Throwable, (Env, User)] = result.unsafeRunSync
val value: Either[Throwable, (Log, User)] = result.unsafeRunSync
println(value.fold(describe, _.toString))
```
Expand All @@ -766,11 +766,11 @@ As you can see in the output, the identity `5` for the user source was not found

```tut:book
value match {
case Left(mi @ MissingIdentity(id, q, e)) => {
case Left(mi @ MissingIdentity(id, q, log)) => {
println("Data: " + q.data.name)
println("Identity: " + id)
println(describe(mi.environment))
println(describe(log))
}
case _ =>
}
Expand Down Expand Up @@ -862,7 +862,7 @@ Fetch.run[IO](fetchFriends).unsafeRunTimed(5.seconds)
# Debugging

We have introduced the handy `fetch.debug.describe` function for debugging errors, but it can do more than that. It can also give you a detailed description of
a fetch execution given an environment.
a fetch execution given an execution log.

Add the following line to your dependencies for including Fetch's debugging facilities:

Expand All @@ -873,7 +873,7 @@ Add the following line to your dependencies for including Fetch's debugging faci
## Fetch execution

We are going to create an interesting fetch that applies all the optimizations available (caching, batching and concurrent request) for ilustrating how we can
visualize fetch executions using the environment.
visualize fetch executions using the execution log.

```tut:silent
def batched[F[_] : ConcurrentEffect]: Fetch[F, List[User]] =
Expand All @@ -892,15 +892,15 @@ def interestingFetch[F[_] : ConcurrentEffect]: Fetch[F, String] =
batched >> cached >> notCached >> concurrent >> Fetch.pure("done")
```

Now that we have the fetch let's run it, get the environment and visualize its execution using the `describe` function:
Now that we have the fetch let's run it, get the log and visualize its execution using the `describe` function:

```tut:book
val io = Fetch.runEnv[IO](interestingFetch)
val io = Fetch.runLog[IO](interestingFetch)
val (env, result) = io.unsafeRunSync
val (log, result) = io.unsafeRunSync
io.unsafeRunTimed(5.seconds) match {
case Some((env, result)) => println(describe(env))
case Some((log, result)) => println(describe(log))
case None => println("Unable to run fetch")
}
```
Expand Down
18 changes: 9 additions & 9 deletions examples/src/test/scala/DoobieExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,24 @@ class DoobieExample extends WordSpec with Matchers {
implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)

"We can fetch one author from the DB" in {
val io: IO[(Env, Author)] = Fetch.runEnv[IO](Authors.fetchAuthor(1))
val io: IO[(Log, Author)] = Fetch.runLog[IO](Authors.fetchAuthor(1))

val (env, result) = io.unsafeRunSync
val (log, result) = io.unsafeRunSync

result shouldEqual Author(1, "William Shakespeare")
env.rounds.size shouldEqual 1
log.rounds.size shouldEqual 1
}

"We can fetch multiple authors from the DB in parallel" in {
def fetch[F[_]: ConcurrentEffect]: Fetch[F, List[Author]] =
List(1, 2).traverse(Authors.fetchAuthor[F])

val io: IO[(Env, List[Author])] = Fetch.runEnv[IO](fetch)
val io: IO[(Log, List[Author])] = Fetch.runLog[IO](fetch)

val (env, result) = io.unsafeRunSync
val (log, result) = io.unsafeRunSync

result shouldEqual Author(1, "William Shakespeare") :: Author(2, "Charles Dickens") :: Nil
env.rounds.size shouldEqual 1
log.rounds.size shouldEqual 1
}

"We can fetch multiple authors from the DB using a for comprehension" in {
Expand All @@ -135,12 +135,12 @@ class DoobieExample extends WordSpec with Matchers {
b <- Authors.fetchAuthor(a.id + 1)
} yield List(a, b)

val io: IO[(Env, List[Author])] = Fetch.runEnv[IO](fetch)
val io: IO[(Log, List[Author])] = Fetch.runLog[IO](fetch)

val (env, result) = io.unsafeRunSync
val (log, result) = io.unsafeRunSync

result shouldEqual Author(1, "William Shakespeare") :: Author(2, "Charles Dickens") :: Nil
env.rounds.size shouldEqual 2
log.rounds.size shouldEqual 2
}

}
18 changes: 9 additions & 9 deletions examples/src/test/scala/Http4sExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,21 @@ class Http4sExample extends WordSpec with Matchers {
implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)

"We can fetch one user" in {
val io: IO[(Env, User)] = Fetch.runEnv[IO](fetchUser(1))
val io: IO[(Log, User)] = Fetch.runLog[IO](fetchUser(1))

val (env, result) = io.unsafeRunSync
val (log, result) = io.unsafeRunSync

println(result)
env.rounds.size shouldEqual 1
log.rounds.size shouldEqual 1
}

"We can fetch multiple users in parallel" in {
val io = Fetch.runEnv[IO](fetchManyUsers(List(1, 2, 3)))
val io = Fetch.runLog[IO](fetchManyUsers(List(1, 2, 3)))

val (env, result) = io.unsafeRunSync
val (log, result) = io.unsafeRunSync

result.foreach(println)
env.rounds.size shouldEqual 1
log.rounds.size shouldEqual 1
}

"We can fetch multiple users with their posts" in {
Expand All @@ -152,17 +152,17 @@ class Http4sExample extends WordSpec with Matchers {
usersWithPosts <- users.traverse(fetchPosts[F])
} yield usersWithPosts

val io = Fetch.runEnv[IO](fetch)
val io = Fetch.runLog[IO](fetch)

val (env, results) = io.unsafeRunSync
val (log, results) = io.unsafeRunSync

results
.map {
case (user, posts) =>
s"${user.username} has ${posts.size} posts"
}
.foreach(println)
env.rounds.size shouldEqual 2
log.rounds.size shouldEqual 2
}

}
12 changes: 6 additions & 6 deletions examples/src/test/scala/JedisExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ class JedisExample extends WordSpec with Matchers {

"We can use a Redis cache" in {
val cache = RedisCache[IO]("localhost")
val io: IO[(Env, HttpExample.User)] = Fetch.runEnv[IO](fetch, cache)
val io: IO[(Log, HttpExample.User)] = Fetch.runLog[IO](fetch, cache)

val (env, result) = io.unsafeRunSync
val (log, result) = io.unsafeRunSync

println(result)
env.rounds.size shouldEqual 2
log.rounds.size shouldEqual 2

val io2: IO[(Env, HttpExample.User)] = Fetch.runEnv[IO](fetch, cache)
val (env2, result2) = io2.unsafeRunSync
val io2: IO[(Log, HttpExample.User)] = Fetch.runLog[IO](fetch, cache)
val (log2, result2) = io2.unsafeRunSync

println(result2)
env2.rounds.size shouldEqual 0
log2.rounds.size shouldEqual 0
}
}
Loading

0 comments on commit 4eb9abd

Please sign in to comment.