summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/scala/main/Main.scala20
-rw-r--r--src/main/scala/main/infrastructure/Http.scala33
-rw-r--r--src/main/scala/main/view/View.scala126
3 files changed, 91 insertions, 88 deletions
diff --git a/src/main/scala/main/Main.scala b/src/main/scala/main/Main.scala
index 388daa9..46c6bba 100644
--- a/src/main/scala/main/Main.scala
+++ b/src/main/scala/main/Main.scala
@@ -5,6 +5,11 @@
* | | ( <_> ) | \< <_| | | /\ ___/
* |__| \____/|__| \__ |____/ \___ >
* |__| \/
+ *
+ *
+ * author: Abdellah El Morabit
+ * year: 2025
+ * course: Java Advanced
*/
package main
@@ -18,20 +23,13 @@ import java.lang.foreign._
import zio._
-object Torque extends ZIOAppDefault with Runner {
-
-
- def run: ZIO[Any, Throwable, Unit] = {
-
- val v: View = new View
- v.serveView
- httpHandler
- }
-
+object Torque extends ZIOAppDefault with Runner {
/**
- * This method runs after the UI finishes which means that the stress tests also finished
+ * The http handler runs after the UI finishes which means that the stress tests also finished
* Which allows us to post the gathered data to the API
* */
+ def run: ZIO[Any, Throwable, Unit] = new View().serveView
+
}
diff --git a/src/main/scala/main/infrastructure/Http.scala b/src/main/scala/main/infrastructure/Http.scala
index 0317655..2f614dd 100644
--- a/src/main/scala/main/infrastructure/Http.scala
+++ b/src/main/scala/main/infrastructure/Http.scala
@@ -4,19 +4,32 @@ import zio._
import zio.http._
import main.domain._
-
/**
* HTTP Handler
* using ZIO
*
- *
- * source: https://ziohttp.com */
+ * This hander serves as the main HTTP server for the application.
+ * It shows the performance metrics at the time it gets run
+ * and exposes them via HTTP endpoints.
+ * You can check it out at http://localhost:8080/cpu or /ram or /platform
+ * source: https://ziohttp.com
+ */
+
+def httpHandler: ZIO[Any, Throwable, Unit] = {
+ for {
+ h <- Server.serve(routes).provide(Server.defaultWithPort(8080)).exitCode.fork
+ _ <- h.join
+ } yield ()
+}
+
+// was returning an exit code previously but that caused a type mismatch
+// [error] | Too many type arguments for zio.UIO[A]
+// [error] | expected: [A]
+// [error] | actual: [Any, Throwable]
+
+
+
+
+
- def httpHandler: ZIO[Any, Throwable, Unit] = {
- ZIO.attempt {
- Server.serve(routes)
- .provide(Server.default)
- .exitCode
- }
- }
diff --git a/src/main/scala/main/view/View.scala b/src/main/scala/main/view/View.scala
index ce57d0f..1eb0307 100644
--- a/src/main/scala/main/view/View.scala
+++ b/src/main/scala/main/view/View.scala
@@ -27,14 +27,12 @@ class View extends Runner {
* General utils that will be used a lot later
* */
- def clearScreen: Unit = print("\u001b[H\u001b[2J")
-
- def pressToContinue: Unit = {
- println("\nPress enter to continue")
- scala.io.StdIn.readLine()
- }
-
+ /**
+ * ASCII code that clears terminal screen
+ * source: https://stackoverflow.com/questions/56608263/how-to-clear-terminal-screen-in-scala
+ */
+ def pressToContinue: Task[Unit] = Console.printLine("\nPress enter to continue") *> Console.readLine.unit
/**
* Multi line println for a nice UI
@@ -42,9 +40,9 @@ class View extends Runner {
* source: https://docs.scala-lang.org/scala3/book/first-look-at-types.html#strings
* */
- def header: Unit = {
+ def header: Task[Unit] = {
- println(
+ Console.printLine(
"""
_/ |_ ___________ ________ __ ____
\ __\/ _ \_ __ \/ ____/ | \_/ __ \
@@ -58,8 +56,7 @@ class View extends Runner {
| Author: Abdellah El Morabit |
|=======================================|
"""
- )
- pressToContinue
+ ) *> pressToContinue
}
/**
@@ -70,8 +67,8 @@ class View extends Runner {
try {
stringInput.toInt
} catch {
- // return 3 to exit the program
- case e: Exception => 3
+ // return 6 to exit the program
+ case e: Exception => 6
}
}
@@ -83,70 +80,65 @@ class View extends Runner {
val cpu = hardware.getProcessor
ZIO.attempt {
+ Console.printLine("\u001b[2J")
println(s"""
- ===================================================
- || CPU Load: ${cpu.getSystemCpuLoad(1000) * 100}%
- || Logical Cores: ${cpu.getLogicalProcessorCount()}
- || Physical Cores: ${cpu.getPhysicalProcessorCount()}
- || Temperature: ${sensors.getCpuTemperature()}°C
- || Total RAM: ${memory.getTotal() / (1024.0 * 1024 * 1024)} GB
- || Available RAM: ${memory.getAvailable() / (1024.0 * 1024 * 1024)} GB
- ===================================================
- """.stripMargin)
- pressToContinue
- pressToContinue
- }
-
+ ===================================================
+ || CPU Load: ${cpu.getSystemCpuLoad(1000) * 100}%
+ || Logical Cores: ${cpu.getLogicalProcessorCount()}
+ || Physical Cores: ${cpu.getPhysicalProcessorCount()}
+ || Temperature: ${sensors.getCpuTemperature()}°C
+ || Total RAM: ${memory.getTotal() / (1024.0 * 1024 * 1024)} GB
+ || Available RAM: ${memory.getAvailable() / (1024.0 * 1024 * 1024)} GB
+ ===================================================
+ """.stripMargin)
+ } *> pressToContinue
}
+ def menuDesign: Task[Unit] = {
- def menu: ZIO[Any, Throwable, Unit] = {
-
- var continue = true
- var target = ""
-
- while (continue) {
-
- Thread.sleep(500)
- clearScreen
- println(
+ ZIO.attempt {
+ Console.printLine("\u001b[2J")
+ println(
- """
- _/ |_ ___________ ________ __ ____
- \ __\/ _ \_ __ \/ ____/ | \_/ __ \
- | | ( <_> ) | \< <_| | | /\ ___/
- |__| \____/|__| \__ |____/ \___ >
- |__| \/
+ """
+ _/ |_ ___________ ________ __ ____
+ \ __\/ _ \_ __ \/ ____/ | \_/ __ \
+ | | ( <_> ) | \< <_| | | /\ ___/
+ |__| \____/|__| \__ |____/ \___ >
+ |__| \/
- |======= Select the stress test ========|
- | 1: light Cpu |
- | 2: Heavy Cpu |
- | 3: Light Ram |
- | 4: Heavy Ram |
- | 5: Exit |
- |=======================================|
+ |======= Select the stress test ========|
+ | 1: light Cpu |
+ | 2: Heavy Cpu |
+ | 3: Light Ram |
+ | 4: Heavy Ram |
+ | 5: Http |
+ | 6: Exit |
+ |=======================================|
- """
- )
- target = scala.io.StdIn.readLine()
- continue = false
+ """
+ )
}
+ }
- clearScreen
- toInt(target) match {
+ def menu: ZIO[Any, Throwable, Unit] = {
+ for {
+ _ <- menuDesign
+ input <- Console.readLine("Enter your choice: ")
+ _ <- toInt(input) match {
+ case 1 => lightCpuRun *> resourcesView *> menu
+ case 2 => heavyCpuRun *> resourcesView *> menu
+ case 3 => lightRamRun *> resourcesView *> menu
+ case 4 => heavyRamRun *> resourcesView *> menu
+ case 5 => httpHandler *> resourcesView *> menu
+ case 6 => Console.printLine("Exiting...") *> ZIO.succeed(System.exit(0))
+ case _ => Console.printLine("Invalid choice") *> pressToContinue *> menu
+ }
+ } yield ()
+ }
- case 1 => lightCpuRun
- case 2 => heavyCpuRun
- case 3 => lightCpuRun
- case 4 => heavyCpuRun
+def serveView: ZIO[Any, Throwable, Unit] = header *> menu
- }
- resourcesView
- }
- def serveView: ZIO[Any, Throwable, Unit] = {
- clearScreen
- header
- menu
- }
}
+