Skip to content

Commit 7bd0882

Browse files
authored
Merge pull request #17 from pme123/#15-rest-services
#15 rest services
2 parents 9bc7091 + cfa0f9c commit 7bd0882

File tree

8 files changed

+20
-26
lines changed

8 files changed

+20
-26
lines changed

client/src/main/scala/pme123/adapters/client/HttpServices.scala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package pme123.adapters.client
33
import com.thoughtworks.binding.{Binding, FutureBinding, dom}
44
import org.scalajs.dom.ext.{Ajax, AjaxException}
55
import org.scalajs.dom.raw.{HTMLElement, XMLHttpRequest}
6-
import play.api.libs.json.{JsError, JsResult, JsValue, Json}
6+
import play.api.libs.json._
77
import pme123.adapters.shared.Logger
88

99
import scala.concurrent.Future
@@ -13,13 +13,16 @@ import scala.util.{Failure, Success}
1313
trait HttpServices
1414
extends Logger {
1515

16-
def httpGet(apiPath: String, toEntity: JsValue => JsResult[Unit]): Binding[HTMLElement] =
17-
callService(apiPath, Ajax.get(apiPath), toEntity)
16+
def httpGet[A](apiPath: String
17+
, storeChange: A => Unit)
18+
(implicit reads: Reads[A]): Binding[HTMLElement] =
19+
callService[A](apiPath, Ajax.get(apiPath), storeChange)
1820

1921
@dom
20-
def callService(apiPath: String
21-
, ajaxCall: Future[XMLHttpRequest]
22-
, toEntity: JsValue => JsResult[Unit]): Binding[HTMLElement] = {
22+
def callService[A](apiPath: String
23+
, ajaxCall: Future[XMLHttpRequest]
24+
, storeChange: A => Unit)
25+
(implicit reads: Reads[A]): Binding[HTMLElement] = {
2326
FutureBinding(ajaxCall)
2427
.bind match {
2528
case None =>
@@ -30,7 +33,8 @@ trait HttpServices
3033
val json: JsValue = Json.parse(response.responseText)
3134
info(s"Json received from $apiPath: ${json.toString().take(20)}")
3235
<div>
33-
{toEntity(json)
36+
{json.validate[A]
37+
.map(storeChange)
3438
.map(_ => "")
3539
.recover { case JsError(errors) =>
3640
error(s"errors: $errors")

client/src/main/scala/pme123/adapters/client/ServerServices.scala

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package pme123.adapters.client
22

33
import com.thoughtworks.binding.Binding
44
import org.scalajs.dom.raw.HTMLElement
5-
import play.api.libs.json.JsValue
65
import pme123.adapters.shared.{ClientConfig, JobConfig}
76

87
/**
@@ -14,21 +13,13 @@ object ServerServices
1413
def jobConfigs(): Binding[HTMLElement] = {
1514
val apiPath = s"${UIStore.uiState.webContext.value}/jobConfigs"
1615

17-
def toObj(jsValue: JsValue) =
18-
jsValue.validate[Seq[JobConfig]]
19-
.map(results => UIStore.changeJobConfigs(results))
20-
21-
httpGet(apiPath, toObj)
16+
httpGet(apiPath, (results: Seq[JobConfig]) => UIStore.changeJobConfigs(results))
2217
}
2318

2419
def clientConfigs(): Binding[HTMLElement] = {
2520
val apiPath = s"${UIStore.uiState.webContext.value}/clientConfigs"
2621

27-
def toObj(jsValue: JsValue) =
28-
jsValue.validate[Seq[ClientConfig]]
29-
.map(results => UIStore.replaceAllClients(results))
30-
31-
httpGet(apiPath, toObj)
22+
httpGet(apiPath, (results: Seq[ClientConfig]) => UIStore.replaceAllClients(results))
3223
}
3324

3425

server/app/pme123/adapters/server/control/JobActorSchedulers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class JobActorSchedulers @Inject()()
5757

5858
def init(execDateTime: Instant): Instant = {
5959
if (execDateTime isBefore Instant.now) {
60-
init(execDateTime.plusSeconds(jobSchedule.intervalInMin * 60))
60+
init(execDateTime.plusSeconds((jobSchedule.intervalInMin * 60).asInstanceOf[Long]))
6161
} else {
6262
execDateTime
6363
}

server/app/pme123/adapters/server/entity/JobConfigCreator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ case class ScheduleConfigCreator(config: Config, timezone: ZoneId) {
3636
if (config.hasPath(firstTimeProp)) config.getString(firstTimeProp) else defaultFirstTime
3737

3838
private val intervalInMin =
39-
if (config.hasPath(intervalInMinProp)) config.getInt(intervalInMinProp) else defaultIntervalInMin
39+
if (config.hasPath(intervalInMinProp)) config.getDouble(intervalInMinProp) else defaultIntervalInMin
4040

4141
private val firstWeekday =
4242
if (config.hasPath(firstWeekdayProp)) Some(config.getString(firstWeekdayProp)) else defaultFirstWeekday

server/app/pme123/adapters/server/entity/JobSchedules.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ object JobSchedules {
2424

2525
case class JobSchedule(jobIdent: JobIdent, scheduleConfig: ScheduleConfig) {
2626

27-
private val minIntervalInMin = 1
28-
val intervalInMin: Long = Math.max(minIntervalInMin, scheduleConfig.intervalInMin).asInstanceOf[Long]
29-
27+
private val minIntervalInMin = 1.0/60 // min 1 second
28+
val intervalInMin: Double = Math.max(minIntervalInMin, scheduleConfig.intervalInMin)
3029
val intervalDuration: FiniteDuration = intervalInMin.minutes
3130

3231
// parameters for testing

server/conf/reference.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pme123.adapters {
138138
// Default is one day (1440 minutes) - be aware 1 minute is the smallest period possible
139139
// - and it must be greater than the time the import takes!
140140
// make also sure that the period is so that the import is at always the same time of day.
141-
// interval.minutes = 2
141+
// interval.minutes = 2.5
142142
}
143143
}, {
144144
ident = "demoJobWithDefaultScheduler"

shared/src/main/scala/pme123/adapters/shared/AdaptersContextProp.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ object AdaptersContextProp {
3131
case class SchedulerInfo(jobIdent: JobIdent
3232
, nextExecution: Instant
3333
, firstWeekday: String
34-
, periodInMin: Long)
34+
, periodInMin: Double)
3535

3636
object SchedulerInfo extends InstantHelper {
3737
implicit val jsonFormat: OFormat[SchedulerInfo] = derived.oformat[SchedulerInfo]()

shared/src/main/scala/pme123/adapters/shared/JobConfigs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object JobConfig {
1919

2020
}
2121

22-
case class ScheduleConfig(firstTime: String, intervalInMin: Int, firstWeekDay: Option[String] = None)
22+
case class ScheduleConfig(firstTime: String, intervalInMin: Double, firstWeekDay: Option[String] = None)
2323

2424
object ScheduleConfig {
2525
implicit val jsonFormat: OFormat[ScheduleConfig] = derived.oformat[ScheduleConfig]()

0 commit comments

Comments
 (0)