Skip to content

Commit

Permalink
Uprava formatovani zdrojoveho kodu
Browse files Browse the repository at this point in the history
  • Loading branch information
xjacka committed Feb 20, 2016
1 parent 5d6155b commit 25a0624
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions src/main/scala/xjacka/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ import java.io.FileNotFoundException
import java.net.{URL, URLConnection}
import java.text.SimpleDateFormat
import java.util.{Calendar, Date, TimeZone}

import spray.json._

import scala.util.Try

case class Assignee(val login: String) {

case class Assignee(login: String) {
override def toString = login
}

case class Issue(val title: String, val number: Int, val assignee : Option[Assignee])
case class Comment(val body: String, val createdAt: String, val user : Option[Assignee])
case class Repo(val name: String, val url : String)
case class Issue(title: String, number: Int, assignee: Option[Assignee])
case class Comment(body: String, createdAt: String, user: Option[Assignee])
case class Repo(name: String, url: String)

object MyJsonProtocol extends DefaultJsonProtocol {
implicit val assigneeFormat = jsonFormat(Assignee, "login")
Expand All @@ -42,7 +39,7 @@ object Main extends App {
}
}

def getTimeInterval (beforeDaysFrom: Int, beforeDaysTo: Int) : (Date, Date) = {
def getTimeInterval(beforeDaysFrom: Int, beforeDaysTo: Int): (Date, Date) = {
val date = new Date()
val cal = Calendar.getInstance()
cal.setTime(date)
Expand All @@ -51,10 +48,10 @@ object Main extends App {
cal.set(Calendar.SECOND, 0)
cal.set(Calendar.MILLISECOND, 0)
cal.add(Calendar.DAY_OF_MONTH, -beforeDaysFrom)
val zeroedDate = cal.getTime()
cal.add(Calendar.DAY_OF_MONTH, (beforeDaysFrom - beforeDaysTo))
val endOfTheDay = cal.getTime()
return (zeroedDate, endOfTheDay)
val zeroedDate = cal.getTime
cal.add(Calendar.DAY_OF_MONTH, beforeDaysFrom - beforeDaysTo)
val endOfTheDay = cal.getTime
(zeroedDate, endOfTheDay)
}

def showTodayLoggedTime(companyName: String, username: String, token: String, beforeDaysFrom: Int = 0, beforeDaysTo: Int = -1) = {
Expand All @@ -68,28 +65,27 @@ object Main extends App {

val repos = loadCompanyRepos(companyName, token)
val timeComments = repos.map((repo: Repo) =>
getTime(getCommentFromRepo(username, token, repo.url + "/issues/comments?since=" + nowAsISO)
.filter(t => isoDateFormat.parse(t.createdAt).before(endOfTheDay))
).filter(_ != "")).flatten
getTime(getCommentFromRepo(username, token, repo.url + "/issues/comments?since=" + nowAsISO)
.filter(t => isoDateFormat.parse(t.createdAt).before(endOfTheDay))).filter(_ != "")).flatten

if(timeComments.length == 0) {
println("Od " + readableDateFormat.format(zeroedDate) + " do " + readableDateFormat.format(endOfTheDay) + " žádné záznamy")
if (timeComments.length == 0) {
println(s"Od ${readableDateFormat.format(zeroedDate)} do ${readableDateFormat.format(endOfTheDay)} žádné záznamy")
} else {
println("Zalogovaný čas za " + readableDateFormat.format(zeroedDate) + " " + readableDateFormat.format(endOfTheDay))
println(s"Zalogovaný čas za ${readableDateFormat.format(zeroedDate)} ${readableDateFormat.format(endOfTheDay)}")
timeComments.foreach(println)
println("==============")
println("Celkem: " + timeComments.map(getMinutes(_)).reduceLeft(_ + _).toInt + " min")
println("Celkem: " + "%1.2f".format(timeComments.map(getMinutes(_)).reduceLeft(_ + _) / 60) + " h")
println(s"Celkem: ${timeComments.map(getMinutes).sum.toInt} min")
println(s"Celkem: " + "%1.2f".format(timeComments.map(getMinutes).sum / 60) + " h")
}
}

def loadCompanyRepos(company: String, token: String): List[Repo] = {
loadDataFromGithub[Repo](token, s"https://api.github.com/orgs/${company}/repos")
loadDataFromGithub[Repo](token, s"https://api.github.com/orgs/$company/repos")
}

def getMinutes(input: String): Float = {
val min = if ( input.contains("min") ) Try((input.split(":clock[0-9]:").last.trim.split("min").head.split("h").last.trim).toFloat).toOption else Some(0f)
val h = if (input.contains("h") ) Try((input.split(":clock[0-9]:").last.trim.split("h").head.split("min").last.trim).toFloat).toOption else Some(0f)
val min = if (input.contains("min")) Try(input.split(":clock[0-9]:").last.trim.split("min").head.split("h").last.trim.toFloat).toOption else Some(0f)
val h = if (input.contains("h")) Try(input.split(":clock[0-9]:").last.trim.split("h").head.split("min").last.trim.toFloat).toOption else Some(0f)
min.getOrElse(0f) + h.getOrElse(0f) * 60f
}

Expand All @@ -99,17 +95,17 @@ object Main extends App {
comments.filter(_.user.getOrElse(new Assignee("nobody")).login == username)
}

def loadDataFromGithub[T : JsonReader](token: String, apiUrl: String): List[T] = {
def loadDataFromGithub[T: JsonReader](token: String, apiUrl: String): List[T] = {
def makeRequest(apiURL: String): URLConnection = {
if(apiURL == "") return null
if (apiURL == "") return null
val url = new URL(apiURL)
val uc: URLConnection = url.openConnection()
uc.setRequestProperty("Authorization", "token " + token)
uc.setRequestProperty("Content-Type", "application/json")
uc
}

def getLinkToNext(urlConnection: URLConnection) : String = {
def getLinkToNext(urlConnection: URLConnection): String = {
if (!hasNext(urlConnection)) "" else {
val ln = urlConnection.getHeaderField("Link").split(",").filter(str => str.contains("rel=\"next\""))
val link = ln(0)
Expand All @@ -126,22 +122,22 @@ object Main extends App {
}
}

def parseData(uc: URLConnection) : List[T] = {
def parseData(uc: URLConnection): List[T] = {
try {
val in = uc.getInputStream()
val in = uc.getInputStream
val json = scala.io.Source.fromInputStream(in)("UTF-8").mkString.parseJson.asInstanceOf[JsArray]
in.close()
return json.elements.map(elem => elem.convertTo[T]).toList
} catch {
case e: FileNotFoundException =>
println("source not found")
println("Zdroj nebyl nalezen")
}
return List[T]()
List[T]()
}

def responses(resource : URLConnection) : Stream[URLConnection] = resource #:: responses(makeRequest(getLinkToNext(resource)))
def responses(resource: URLConnection): Stream[URLConnection] = resource #:: responses(makeRequest(getLinkToNext(resource)))

responses(makeRequest(apiUrl)).takeWhile(_ != null).toList.flatMap(parseData(_))
responses(makeRequest(apiUrl)).takeWhile(_ != null).toList.flatMap(parseData)
}

def getTime(comments: List[Comment]): List[String] = {
Expand All @@ -150,4 +146,3 @@ object Main extends App {

}


0 comments on commit 25a0624

Please sign in to comment.