Skip to content

Commit 5de2dc7

Browse files
committed
Update metrics scheduler
1 parent 3a14b5b commit 5de2dc7

File tree

6 files changed

+48
-73
lines changed

6 files changed

+48
-73
lines changed

Diff for: modules/infra/src/main/scala/scaladex/infra/SqlDatabase.scala

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package scaladex.infra
22

33
import java.time.Instant
4+
import java.time.OffsetDateTime
5+
import java.time.ZoneOffset
46
import java.util.UUID
57

68
import scala.concurrent.ExecutionContext.Implicits.global
@@ -150,6 +152,11 @@ class SqlDatabase(datasource: HikariDataSource, xa: doobie.Transactor[IO]) exten
150152
def countProjects(): Future[Long] =
151153
run(ProjectTable.countProjects.unique)
152154

155+
def countProjects(year: Int): Future[Long] = {
156+
val instant = OffsetDateTime.of(year + 1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant
157+
run(ProjectTable.countProjectsUntil.unique(instant))
158+
}
159+
153160
override def countArtifacts(): Future[Long] =
154161
run(ArtifactTable.count.unique)
155162

Diff for: modules/infra/src/main/scala/scaladex/infra/sql/ProjectTable.scala

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ object ProjectTable {
4545
val countProjects: Query0[Long] =
4646
selectRequest(table, Seq("count(*)"))
4747

48+
val countProjectsUntil: Query[Instant, Long] =
49+
selectRequest1(
50+
table,
51+
Seq("count(*)"),
52+
where = Seq("creation_date < ?", "github_status!='Moved'", "github_status!='NotFound'")
53+
)
54+
4855
val selectByReference: Query[Project.Reference, Project] =
4956
selectRequest(fullTable, allFields, referenceFields.map(f => s"p.$f"))
5057

Diff for: modules/server/src/main/scala/scaladex/server/service/AdminService.scala

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import scaladex.core.model.Project
1212
import scaladex.core.model.Project.Settings
1313
import scaladex.core.model.UserState
1414
import scaladex.core.service.GithubClient
15-
import scaladex.core.service.SchedulerDatabase
1615
import scaladex.core.service.SearchEngine
1716
import scaladex.core.util.ScalaExtensions._
17+
import scaladex.infra.SqlDatabase
1818
import scaladex.view.Job
1919
import scaladex.view.Task
2020

2121
class AdminService(
2222
env: Env,
23-
database: SchedulerDatabase,
23+
database: SqlDatabase,
2424
searchEngine: SearchEngine,
2525
githubClientOpt: Option[GithubClient],
2626
sonatypeSynchronizer: SonatypeService
@@ -39,7 +39,8 @@ class AdminService(
3939
new JobScheduler(Job.projectDependencies, projectDependenciesUpdater.updateAll),
4040
new JobScheduler(Job.projectCreationDates, updateProjectCreationDate),
4141
new JobScheduler(Job.moveArtifacts, artifactsService.moveAll),
42-
new JobScheduler(Job.userSessions, userSessionService.updateAll)
42+
new JobScheduler(Job.userSessions, userSessionService.updateAll),
43+
new JobScheduler(Job.metrics, (new Metrics(database)).run)
4344
) ++
4445
githubClientOpt.map { client =>
4546
val githubUpdater = new GithubUpdater(database, client)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scaladex.server.service
2+
3+
import scala.concurrent.ExecutionContext
4+
import scala.concurrent.Future
5+
6+
import cats.implicits.toTraverseOps
7+
import com.typesafe.scalalogging.LazyLogging
8+
import scaladex.infra.SqlDatabase
9+
10+
class Metrics(db: SqlDatabase)(implicit ec: ExecutionContext) extends LazyLogging {
11+
def run(): Future[String] = {
12+
val years: Seq[Int] = Range.inclusive(2013, 2022)
13+
for {
14+
countByYear <- years.traverse(db.countProjects)
15+
projects <- db.getAllProjects()
16+
} yield {
17+
years.zip(countByYear).foreach { case (year, count) => logger.info(s"$year: $count") }
18+
val filteredProjects = projects.filter(p => !p.githubStatus.isMoved && !p.githubStatus.isNotFound)
19+
logger.info(s"total projects: ${filteredProjects.size}")
20+
val contributors = filteredProjects.flatMap(_.githubInfo).flatMap(_.contributors).map(_.login).distinct.size
21+
logger.info(s"total contributors: $contributors")
22+
"Success"
23+
}
24+
}
25+
}

Diff for: modules/template/src/main/scala/scaladex/view/Job.scala

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ object Job {
4444
"Find missing artifacts in Maven Central of the known group IDs.",
4545
24.hours
4646
)
47+
val metrics: Job = Job(
48+
"metrics",
49+
"Print regular metrics into the logs",
50+
5.minutes
51+
)
4752

4853
case class Status(state: State, results: Seq[Result], progress: Option[Progress]) {
4954
def isStarted: Boolean = state.isInstanceOf[Started]

Diff for: server/src/main/scala/scaladex/server/service/Metrics.scala

-70
This file was deleted.

0 commit comments

Comments
 (0)