diff --git a/README.md b/README.md index 71bdbb6..a1d3c61 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ It is available by [@luxmedbot](https://telegram.me/luxmedbot) - install postgres and create db **lbs** with login **lbs** and password **lsb123** - run using `./gradlew bootRun` -5. send `/start` to your bot +5. send `/start` command to your bot diff --git a/api/src/main/scala/com/lbs/api/http/package.scala b/api/src/main/scala/com/lbs/api/http/package.scala index e545d21..3238df2 100644 --- a/api/src/main/scala/com/lbs/api/http/package.scala +++ b/api/src/main/scala/com/lbs/api/http/package.scala @@ -64,9 +64,9 @@ package object http extends Logger { } def toTry: Try[HttpResponse[String]] = { - LOG.debug(s"Sending request:\n${hidePasswords(httpRequest)}") + debug(s"Sending request:\n${hidePasswords(httpRequest)}") val httpResponse = Try(httpRequest.asString) - LOG.debug(s"Received response:\n$httpResponse") + debug(s"Received response:\n$httpResponse") extractLuxmedError(httpResponse) match { case Some(error) => Try(throw error) case None => httpResponse.map(_.throwError) diff --git a/bot/src/main/scala/com/lbs/bot/telegram/TelegramClient.scala b/bot/src/main/scala/com/lbs/bot/telegram/TelegramClient.scala index 05ba3ef..5bdb3d5 100644 --- a/bot/src/main/scala/com/lbs/bot/telegram/TelegramClient.scala +++ b/bot/src/main/scala/com/lbs/bot/telegram/TelegramClient.scala @@ -49,12 +49,12 @@ class TelegramClient(onReceive: TelegramEvent => Unit, botToken: String) extends override def receiveMessage(msg: Message): Unit = { - LOG.debug(s"Received telegram message: $msg") + debug(s"Received telegram message: $msg") onReceive(TelegramEvent(msg, None)) } onCallbackWithTag(TagPrefix) { implicit cbq => - LOG.debug(s"Received telegram callback: $cbq") + debug(s"Received telegram callback: $cbq") ackCallback() for { data <- cbq.data.map(_.stripPrefix(TagPrefix)) diff --git a/common/src/main/scala/com/lbs/common/Logger.scala b/common/src/main/scala/com/lbs/common/Logger.scala index f825947..71f6888 100644 --- a/common/src/main/scala/com/lbs/common/Logger.scala +++ b/common/src/main/scala/com/lbs/common/Logger.scala @@ -1,26 +1,26 @@ /** - * MIT License - * - * Copyright (c) 2018 Yevhen Zadyra - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ + * MIT License + * + * Copyright (c) 2018 Yevhen Zadyra + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.lbs.common import org.slf4j @@ -29,38 +29,34 @@ import org.slf4j.LoggerFactory trait Logger { private val log: slf4j.Logger = LoggerFactory.getLogger(this.getClass) - protected val LOG = new LoggerWrapper + def debug(msg: => String): Unit = { + if (log.isDebugEnabled) + log.debug(msg) + } - class LoggerWrapper { - def debug(msg: => String): Unit = { - if (log.isDebugEnabled) - log.debug(msg) - } + def warn(msg: => String): Unit = { + if (log.isWarnEnabled) + log.warn(msg) + } - def warn(msg: => String): Unit = { - if (log.isWarnEnabled) - log.warn(msg) - } + def warn(msg: => String, throwable: Throwable): Unit = { + if (log.isWarnEnabled) + log.warn(msg, throwable) + } - def warn(msg: => String, throwable: Throwable): Unit = { - if (log.isWarnEnabled) - log.warn(msg, throwable) - } + def error(msg: => String): Unit = { + if (log.isErrorEnabled) + log.error(msg) + } - def error(msg: => String): Unit = { - if (log.isErrorEnabled) - log.error(msg) - } + def error(msg: => String, throwable: Throwable): Unit = { + if (log.isErrorEnabled) + log.error(msg, throwable) + } - def error(msg: => String, throwable: Throwable): Unit = { - if (log.isErrorEnabled) - log.error(msg, throwable) - } - - def info(msg: => String): Unit = { - if (log.isInfoEnabled) - log.info(msg) - } + def info(msg: => String): Unit = { + if (log.isInfoEnabled) + log.info(msg) } } diff --git a/common/src/main/scala/com/lbs/common/Scheduler.scala b/common/src/main/scala/com/lbs/common/Scheduler.scala index ffc8a22..ac5b80c 100644 --- a/common/src/main/scala/com/lbs/common/Scheduler.scala +++ b/common/src/main/scala/com/lbs/common/Scheduler.scala @@ -45,7 +45,7 @@ class Scheduler(poolSize: Int) extends Logger { fn } catch { case ex: Exception => - LOG.error(s"Unable to execute scheduler task", ex) + error(s"Unable to execute scheduler task", ex) } } } diff --git a/server/src/main/scala/com/lbs/server/actor/Account.scala b/server/src/main/scala/com/lbs/server/actor/Account.scala index 21ff56d..0bf851e 100644 --- a/server/src/main/scala/com/lbs/server/actor/Account.scala +++ b/server/src/main/scala/com/lbs/server/actor/Account.scala @@ -68,7 +68,7 @@ class Account(val userId: UserId, bot: Bot, dataService: DataService, val locali } goto(AskAction) using null case None => - LOG.error(s"This is not user [#${userId.userId}] account [#$accountId]") + error(s"This is not user [#${userId.userId}] account [#$accountId]") goto(AskAction) using null } } @@ -79,7 +79,7 @@ class Account(val userId: UserId, bot: Bot, dataService: DataService, val locali invokeNext() goto(AskAction) using null case e: Event => - LOG.error(s"Unhandled event in state:$stateName. Event: $e") + error(s"Unhandled event in state:$stateName. Event: $e") stay() } diff --git a/server/src/main/scala/com/lbs/server/actor/Book.scala b/server/src/main/scala/com/lbs/server/actor/Book.scala index b5877a9..3ee28b6 100644 --- a/server/src/main/scala/com/lbs/server/actor/Book.scala +++ b/server/src/main/scala/com/lbs/server/actor/Book.scala @@ -229,11 +229,11 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da whenSafe(CreateMonitoring) { case Event(Next, bookingData: BookingData) => - LOG.debug(s"Creating monitoring for $bookingData") + debug(s"Creating monitoring for $bookingData") Try(monitoringService.createMonitoring((userId -> bookingData).mapTo[Monitoring])) match { case Success(_) => bot.sendMessage(userId.source, lang.monitoringHasBeenCreated) case Failure(ex) => - LOG.error("Unable to create monitoring", ex) + error("Unable to create monitoring", ex) bot.sendMessage(userId.source, lang.unableToCreateMonitoring) } goto(RequestCity) using BookingData() @@ -243,7 +243,7 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da case Event(Init, _) => reinit() case e: Event => - LOG.error(s"Unhandled event in state:$stateName. Event: $e") + error(s"Unhandled event in state:$stateName. Event: $e") stay() } diff --git a/server/src/main/scala/com/lbs/server/actor/Chat.scala b/server/src/main/scala/com/lbs/server/actor/Chat.scala index a3bba33..c92bdf5 100644 --- a/server/src/main/scala/com/lbs/server/actor/Chat.scala +++ b/server/src/main/scala/com/lbs/server/actor/Chat.scala @@ -153,7 +153,7 @@ class Chat(val userId: UserId, dataService: DataService, monitoringService: Moni whenUnhandledSafe { case e: Event => - LOG.debug(s"Unhandled event in state:$stateName. Event: $e") + debug(s"Unhandled event in state:$stateName. Event: $e") stay() } diff --git a/server/src/main/scala/com/lbs/server/actor/DatePicker.scala b/server/src/main/scala/com/lbs/server/actor/DatePicker.scala index de1edf1..b7d4dbf 100644 --- a/server/src/main/scala/com/lbs/server/actor/DatePicker.scala +++ b/server/src/main/scala/com/lbs/server/actor/DatePicker.scala @@ -90,7 +90,7 @@ class DatePicker(val userId: UserId, val bot: Bot, val localization: Localizatio case Event(Init, _) => goto(AwaitMode) using null case e: Event => - LOG.error(s"Unhandled event in state:$stateName. Event: $e") + error(s"Unhandled event in state:$stateName. Event: $e") stay() } diff --git a/server/src/main/scala/com/lbs/server/actor/Login.scala b/server/src/main/scala/com/lbs/server/actor/Login.scala index e0c80d9..724ac62 100644 --- a/server/src/main/scala/com/lbs/server/actor/Login.scala +++ b/server/src/main/scala/com/lbs/server/actor/Login.scala @@ -91,7 +91,7 @@ class Login(source: MessageSource, bot: Bot, dataService: DataService, apiServic case Event(Init, _) => goto(LogIn) using LoginData() case e: Event => - LOG.error(s"Unhandled event in state:$stateName. Event: $e") + error(s"Unhandled event in state:$stateName. Event: $e") stay() } diff --git a/server/src/main/scala/com/lbs/server/actor/Pager.scala b/server/src/main/scala/com/lbs/server/actor/Pager.scala index d40815d..827f266 100644 --- a/server/src/main/scala/com/lbs/server/actor/Pager.scala +++ b/server/src/main/scala/com/lbs/server/actor/Pager.scala @@ -99,7 +99,7 @@ class Pager[Data](val userId: UserId, bot: Bot, makeMessage: (Data, Int, Int) => case Event(Init, _) => goto(PrepareData) using null case e: Event => - LOG.error(s"Unhandled event in state:$stateName. Event: $e") + error(s"Unhandled event in state:$stateName. Event: $e") stay() } diff --git a/server/src/main/scala/com/lbs/server/actor/Router.scala b/server/src/main/scala/com/lbs/server/actor/Router.scala index 813d46a..eb8932c 100644 --- a/server/src/main/scala/com/lbs/server/actor/Router.scala +++ b/server/src/main/scala/com/lbs/server/actor/Router.scala @@ -55,7 +55,7 @@ class Router(authActorFactory: ByMessageSourceActorFactory) extends Actor with L destroyChat(source) case SwitchUser(userId) => switchUser(userId) - case what => LOG.info(s"Unknown message: $what") + case what => info(s"Unknown message: $what") } private def addNewChatActor(source: MessageSource): ActorRef = { @@ -65,7 +65,7 @@ class Router(authActorFactory: ByMessageSourceActorFactory) extends Actor with L } private def destroyChat(source: MessageSource): Unit = { - LOG.info(s"Destroying chat for $source due to $idleTimeout inactivity") + info(s"Destroying chat for $source due to $idleTimeout of inactivity") timers.remove(source) removeChat(source) } diff --git a/server/src/main/scala/com/lbs/server/actor/SafeFSM.scala b/server/src/main/scala/com/lbs/server/actor/SafeFSM.scala index a69263f..440a465 100644 --- a/server/src/main/scala/com/lbs/server/actor/SafeFSM.scala +++ b/server/src/main/scala/com/lbs/server/actor/SafeFSM.scala @@ -30,7 +30,7 @@ trait SafeFSM[S, D] extends FSM[S, D] with Logger { protected val defaultEventHandler: StateFunction = { case e: Event => - LOG.warn(s"Unhandled event in state:$stateName. Event: $e") + warn(s"Unhandled event in state:$stateName. Event: $e") stay() } @@ -44,7 +44,7 @@ trait SafeFSM[S, D] extends FSM[S, D] with Logger { else eventHandler(event) } catch { case e: Exception => - LOG.error(s"Exception occurred while processing event $event", e) + error(s"Exception occurred while processing event $event", e) stay() } } diff --git a/server/src/main/scala/com/lbs/server/actor/StaticData.scala b/server/src/main/scala/com/lbs/server/actor/StaticData.scala index c926689..0ef9378 100644 --- a/server/src/main/scala/com/lbs/server/actor/StaticData.scala +++ b/server/src/main/scala/com/lbs/server/actor/StaticData.scala @@ -98,7 +98,7 @@ class StaticData(val userId: UserId, bot: Bot, val localization: Localization, o case Event(Init, _) => goto(AwaitConfig) using null case e: Event => - LOG.error(s"Unhandled event in state:$stateName. Event: $e") + error(s"Unhandled event in state:$stateName. Event: $e") stay() } diff --git a/server/src/main/scala/com/lbs/server/service/MonitoringService.scala b/server/src/main/scala/com/lbs/server/service/MonitoringService.scala index e393a12..e6b31b3 100644 --- a/server/src/main/scala/com/lbs/server/service/MonitoringService.scala +++ b/server/src/main/scala/com/lbs/server/service/MonitoringService.scala @@ -88,14 +88,14 @@ class MonitoringService extends Logger { } private def monitor(monitoring: Monitoring): Unit = { - LOG.debug(s"Looking for available terms. Monitoring [#${monitoring.recordId}]") + debug(s"Looking for available terms. Monitoring [#${monitoring.recordId}]") val dateFrom = optimizeDateFrom(monitoring.dateFrom) val termsEither = apiService.getAvailableTerms(monitoring.accountId, monitoring.cityId, monitoring.clinicId, monitoring.serviceId, monitoring.doctorId, dateFrom, Some(monitoring.dateTo)) termsEither match { case Right(terms) => if (terms.nonEmpty) { - LOG.debug(s"Found ${terms.length} terms by monitoring [#${monitoring.recordId}]") + debug(s"Found ${terms.length} terms by monitoring [#${monitoring.recordId}]") if (monitoring.autobook) { val term = terms.head bookAppointment(term, monitoring) @@ -103,16 +103,16 @@ class MonitoringService extends Logger { notifyUserAboutTerms(terms, monitoring) } } else { - LOG.debug(s"No new terms found for monitoring [#${monitoring.recordId}]") + debug(s"No new terms found for monitoring [#${monitoring.recordId}]") } case Left(ex: InvalidLoginOrPasswordException) => - LOG.error(s"User entered invalid name or password. Monitoring will be disabled", ex) + error(s"User entered invalid name or password. Monitoring will be disabled", ex) bot.sendMessage(monitoring.source, lang(monitoring.userId).invalidLoginOrPassword) val activeUserMonitorings = dataService.getActiveMonitorings(monitoring.accountId) activeUserMonitorings.foreach { m => deactivateMonitoring(m.recordId) } - case Left(ex) => LOG.error(s"Unable to receive terms by monitoring [#${monitoring.recordId}]", ex) + case Left(ex) => error(s"Unable to receive terms by monitoring [#${monitoring.recordId}]", ex) } } @@ -127,17 +127,17 @@ class MonitoringService extends Logger { val delaySnapshot = delay val periodSnapshot = period val future = monitoringExecutor.schedule(monitor(monitoring), delaySnapshot, periodSnapshot) - LOG.debug(s"Scheduled monitoring: [#${monitoring.recordId}] with delay: $delaySnapshot and period: $periodSnapshot") + debug(s"Scheduled monitoring: [#${monitoring.recordId}] with delay: $delaySnapshot and period: $periodSnapshot") activeMonitorings += (monitoring.recordId -> (monitoring -> future)) } } } private def initializeNewMonitorings(): Unit = { - LOG.debug(s"Looking for new monitorings created since $checkedOn") + debug(s"Looking for new monitorings created since $checkedOn") val currentTime = ZonedDateTime.now() val monitorings = dataService.getActiveMonitoringsSince(checkedOn) - LOG.debug(s"New active monitorings found: ${monitorings.length}") + debug(s"New active monitorings found: ${monitorings.length}") checkedOn = currentTime initializeMonitorings(monitorings) } @@ -153,7 +153,7 @@ class MonitoringService extends Logger { } toDisable.foreach { case (id, monitoring) => - LOG.debug(s"Monitoring [#$id] is going to be disable as outdated") + debug(s"Monitoring [#$id] is going to be disable as outdated") notifyChatAboutDisabledMonitoring(monitoring) deactivateMonitoring(id) } @@ -185,7 +185,7 @@ class MonitoringService extends Logger { bot.sendMessage(monitoring.source, lang(monitoring.userId).appointmentIsBooked(term, monitoring)) deactivateMonitoring(monitoring.recordId) case Left(ex) => - LOG.error(s"Unable to book appointment by monitoring [${monitoring.recordId}]", ex) + error(s"Unable to book appointment by monitoring [${monitoring.recordId}]", ex) } } @@ -193,7 +193,7 @@ class MonitoringService extends Logger { def deactivateMonitoring(monitoringId: JLong): Unit = { activeMonitorings.remove(monitoringId).foreach { case (monitoring, future) => - LOG.debug(s"Deactivating monitoring [#$monitoringId]") + debug(s"Deactivating monitoring [#$monitoringId]") if (!future.isCancelled) { future.cancel(true) monitoring.active = false @@ -230,12 +230,12 @@ class MonitoringService extends Logger { bot.sendMessage(monitoring.source, lang(monitoring.userId).termIsOutdated) } case Left(ex: InvalidLoginOrPasswordException) => - LOG.error(s"User entered invalid name or password. Monitoring will be disabled", ex) + error(s"User entered invalid name or password. Monitoring will be disabled", ex) bot.sendMessage(monitoring.source, lang(monitoring.userId).loginHasChangedOrWrong) - case Left(ex) => LOG.error(s"Error occurred during receiving terms for monitoring [#${monitoring.recordId}]", ex) + case Left(ex) => error(s"Error occurred during receiving terms for monitoring [#${monitoring.recordId}]", ex) } case None => - LOG.debug(s"Monitoring [#$monitoringId] not found in db") + debug(s"Monitoring [#$monitoringId] not found in db") } } @@ -252,7 +252,7 @@ class MonitoringService extends Logger { private def initialize(): Unit = { checkedOn = ZonedDateTime.now() val monitorings = dataService.getActiveMonitorings - LOG.debug(s"Active monitorings found: ${monitorings.length}") + debug(s"Active monitorings found: ${monitorings.length}") initializeMonitorings(monitorings) disableOutdated() initializeDbChecker() diff --git a/server/src/main/scala/com/lbs/server/service/SessionSupport.scala b/server/src/main/scala/com/lbs/server/service/SessionSupport.scala index 554bde7..82ccd25 100644 --- a/server/src/main/scala/com/lbs/server/service/SessionSupport.scala +++ b/server/src/main/scala/com/lbs/server/service/SessionSupport.scala @@ -24,15 +24,12 @@ package com.lbs.server.service import com.lbs.api.json.model.LoginResponse -import com.lbs.common.ParametrizedLock +import com.lbs.common.{Logger, ParametrizedLock} import com.lbs.server.exception.UserNotFoundException -import org.slf4j.LoggerFactory import scala.collection.mutable -trait SessionSupport { - - private val Log = LoggerFactory.getLogger(classOf[SessionSupport]) +trait SessionSupport extends Logger { case class Session(accessToken: String, tokenType: String) @@ -74,11 +71,11 @@ trait SessionSupport { case Right(s) => fn(s) match { case Left(ex) if ex.getMessage.contains("session has expired") => - Log.debug(s"The session for account [#$accountId] has expired. Try to relogin") + debug(s"The session for account [#$accountId] has expired. Try to relogin") sessions.remove(accountId) session.flatMap(fn) case another => - Log.debug(s"Call to remote api function has completed with result:\n$another") + debug(s"Call to remote api function has completed with result:\n$another") another } case Left(ex) => Left(ex)