Skip to content

Commit 2360cc3

Browse files
committed
add metrics scheduler
1 parent 9e36a0c commit 2360cc3

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package scaladex.server.service
2+
3+
import ch.epfl.scala.services.storage.sql.SqlRepo
4+
import com.typesafe.scalalogging.LazyLogging
5+
import java.time.Instant
6+
import scala.concurrent.ExecutionContext
7+
import scala.concurrent.Future
8+
import ch.epfl.scala.services.storage.sql.tables.ReleaseTable
9+
10+
class Metrics(db: SqlRepo)(implicit ec: ExecutionContext) extends LazyLogging {
11+
def run(): Future[Unit] = {
12+
logger.info(ReleaseTable.countProjects("3").sql)
13+
def global = for {
14+
artifacts <- db.countDistinctArtifacts()
15+
releases <- db.countReleases()
16+
projects <- db.countProjects()
17+
githubInfos <- db.getAllGithubInfos()
18+
scala210Projects <- db.countProjects("2.10")
19+
scala211Projects <- db.countProjects("2.11")
20+
scala212Projects <- db.countProjects("2.12")
21+
scala213Projects <- db.countProjects("2.13")
22+
scala3Projects <- db.countProjects("3")
23+
} yield {
24+
val contributors = githubInfos.flatMap(g => g.contributors).distinct.size
25+
logger.info(s"Total artifacts: $artifacts")
26+
logger.info(s"Total releases: $releases")
27+
logger.info(s"Total projects: $projects")
28+
logger.info(s"Total contributos: $contributors")
29+
logger.info(s"Scala 2.10 projects: $scala210Projects")
30+
logger.info(s"Scala 2.11 projects: $scala211Projects")
31+
logger.info(s"Scala 2.12 projects: $scala212Projects")
32+
logger.info(s"Scala 2.13 projects: $scala213Projects")
33+
logger.info(s"Scala 3 projects: $scala3Projects")
34+
}
35+
36+
def yearly = {
37+
def instant(year: Int) = Instant.parse(s"$year-01-01T00:00:00.00Z")
38+
(2012 to 2021).foldLeft(Future.successful(())) { (f, year) =>
39+
val from = instant(year)
40+
val to = instant(year + 1)
41+
for {
42+
_ <- f
43+
projects <- db.countProjects(from, to)
44+
} yield {
45+
logger.info(s"New projects in $year: $projects")
46+
}
47+
}
48+
}
49+
50+
def monthly = {
51+
def instant(month:Int, year: Int) = Instant.parse(s"$year-${String.format("%02d", month)}-01T00:00:00.00Z")
52+
val months = for {
53+
year <- 2012 to 2021
54+
month <- 1 to 12
55+
} yield {
56+
val (nextYear, nextMonth) = if (month + 1 == 13) (year + 1, 1) else (year, month + 1)
57+
() => db.countReleases(instant(month, year), instant(nextMonth, nextYear)).map { releases =>
58+
logger.info(s"Releases in $year-${String.format("%02d", month)}: $releases")
59+
}
60+
}
61+
months.foldLeft(Future.successful(())) { (f, month) => f.flatMap(_=> month()) }
62+
}
63+
64+
for {
65+
_ <- global
66+
_ <- yearly
67+
_ <- monthly
68+
} yield ()
69+
}
70+
}

0 commit comments

Comments
 (0)