Skip to content

Commit 3a618e0

Browse files
committed
Update metrics scheduler
1 parent 2360cc3 commit 3a618e0

File tree

8 files changed

+53
-79
lines changed

8 files changed

+53
-79
lines changed

Diff for: modules/core/shared/src/main/scala/scaladex/core/model/GithubInfo.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ case class GithubInfo(
4141
codeOfConduct: Option[Url],
4242
chatroom: Option[Url],
4343
openIssues: Seq[GithubIssue], // right now it's all issues, not only beginners issues
44-
scalaPercentage: Option[Int]
44+
scalaPercentage: Option[Int],
45+
scalaLines: Option[Int]
4546
) {
4647
val contributorCount: Int = contributors.size
4748

@@ -80,6 +81,7 @@ object GithubInfo {
8081
codeOfConduct = None,
8182
chatroom = None,
8283
openIssues = Seq(),
83-
scalaPercentage = None
84+
scalaPercentage = None,
85+
scalaLines = None
8486
)
8587
}

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class GithubClient(token: Secret)(implicit val system: ActorSystem)
8585
contributors <- getContributors(repo.ref)
8686
openIssues <- getOpenIssues(repo.ref)
8787
chatroom <- getGitterChatRoom(repo.ref)
88-
scalaPercentage <- getPercentageOfLanguage(repo.ref, language = "Scala")
88+
(scalaLines, scalaPercentage) <- getPercentageOfLanguage(repo.ref, language = "Scala")
8989
} yield GithubInfo(
9090
homepage = repo.homepage.map(Url),
9191
description = repo.description,
@@ -103,7 +103,8 @@ class GithubClient(token: Secret)(implicit val system: ActorSystem)
103103
codeOfConduct = communityProfile.flatMap(_.codeOfConductFile).map(Url),
104104
chatroom = chatroom.map(Url),
105105
openIssues = openIssues.map(_.toGithubIssue).toList,
106-
scalaPercentage = Option(scalaPercentage)
106+
scalaPercentage = Option(scalaPercentage),
107+
scalaLines = scalaLines
107108
)
108109

109110
def getReadme(ref: Project.Reference): Future[Option[String]] = {
@@ -191,14 +192,15 @@ class GithubClient(token: Secret)(implicit val system: ActorSystem)
191192
.fallbackTo(Future.successful(Seq.empty))
192193
}
193194

194-
def getPercentageOfLanguage(ref: Project.Reference, language: String): Future[Int] = {
195+
def getPercentageOfLanguage(ref: Project.Reference, language: String): Future[(Option[Int], Int)] = {
195196
def toPercentage(portion: Int, total: Int): Int =
196197
((portion.toFloat / total) * 100).toInt
197198
val request =
198199
HttpRequest(uri = s"${repoUrl(ref)}/languages").addHeader(acceptJson).addCredentials(credentials)
199200
get[Map[String, Int]](request).map { response =>
200201
val totalNumBytes = response.values.sum
201-
response.get(language).fold(0)(toPercentage(_, totalNumBytes))
202+
val lines = response.get(language)
203+
(lines, lines.fold(0)(toPercentage(_, totalNumBytes)))
202204
}
203205
}
204206

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

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import scaladex.infra.sql.ProjectTable
3030
import scaladex.infra.sql.ReleaseDependenciesTable
3131
import scaladex.infra.sql.ReleaseTable
3232
import scaladex.infra.sql.UserSessionsTable
33+
import java.time.OffsetDateTime
34+
import java.time.ZoneOffset
3335

3436
class SqlDatabase(datasource: HikariDataSource, xa: doobie.Transactor[IO]) extends SchedulerDatabase with LazyLogging {
3537
private val flyway = DoobieUtils.flyway(datasource)
@@ -148,6 +150,11 @@ class SqlDatabase(datasource: HikariDataSource, xa: doobie.Transactor[IO]) exten
148150
def countProjects(): Future[Long] =
149151
run(ProjectTable.countProjects.unique)
150152

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ object GithubInfoTable {
2626
"code_of_conduct",
2727
"chatroom",
2828
"open_issues",
29-
"scala_percentage"
29+
"scala_percentage",
30+
"scala_lines"
3031
)
3132

3233
val insert: Update[(Project.Reference, GithubInfo)] =

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

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ object ProjectTable {
4444

4545
val countProjects: Query0[Long] =
4646
selectRequest(table, Seq("count(*)"))
47+
48+
val countProjectsUntil: Query[Instant, Long] =
49+
selectRequest1(table, "count(*)", where = Some("creation_date < ? AND github_status!='Moved' AND github_status!='NotFound'"))
4750

4851
val selectByReference: Query[Project.Reference, Project] =
4952
selectRequest(fullTable, allFields, referenceFields.map(f => s"p.$f"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package scaladex.server.service
2+
3+
import com.typesafe.scalalogging.LazyLogging
4+
import scala.concurrent.ExecutionContext
5+
import scala.concurrent.Future
6+
import scaladex.infra.SqlDatabase
7+
import cats.implicits.toTraverseOps
8+
9+
class Metrics(db: SqlDatabase)(implicit ec: ExecutionContext) extends LazyLogging {
10+
def run(): Future[Unit] = {
11+
val years: Seq[Int] = Range.inclusive(2013, 2022)
12+
for {
13+
countByYear <- years.traverse(db.countProjects)
14+
projects <- db.getAllProjects()
15+
} yield {
16+
years.zip(countByYear).foreach {
17+
case (year, count) => logger.info(s"$year: $count")
18+
}
19+
val filteredProjects = projects.filter(p => !p.githubStatus.moved && !p.githubStatus.notFound)
20+
logger.info(s"total projects: ${filteredProjects.size}")
21+
val linesOfCode = filteredProjects.flatMap(_.githubInfo).flatMap(_.scalaLines).map(_.toLong).sum
22+
logger.info(s"total lines of Scala code: $linesOfCode")
23+
val contributors = filteredProjects.flatMap(_.githubInfo).flatMap(_.contributors).map(_.login).distinct.size
24+
logger.info(s"total contributors: $contributors")
25+
}
26+
}
27+
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import scaladex.core.service.SearchEngine
1111
import scaladex.core.util.ScalaExtensions._
1212
import scaladex.infra.GithubClient
1313
import scaladex.view.SchedulerStatus
14+
import scaladex.infra.SqlDatabase
1415

1516
class SchedulerService(
1617
env: Env,
17-
database: SchedulerDatabase,
18+
database: SqlDatabase,
1819
searchEngine: SearchEngine,
1920
githubClientOpt: Option[GithubClient],
2021
sonatypeSynchronizer: SonatypeSynchronizer,
@@ -30,7 +31,8 @@ class SchedulerService(
3031
Scheduler("sync-search", searchSynchronizer.syncAll, 30.minutes),
3132
new MovedArtifactsSynchronizer(database),
3233
userSessionSynchronizer,
33-
new MovedArtifactsSynchronizer(database)
34+
new MovedArtifactsSynchronizer(database),
35+
Scheduler("metrics", (new Metrics(database)).run, 1.hour),
3436
) ++
3537
Option.when(!env.isLocal)(Scheduler("sync-sonatype-missing-releases", sonatypeSynchronizer.syncAll, 24.hours)) ++
3638
githubClientOpt.map(client => new GithubUpdater(database, client))

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

-70
This file was deleted.

0 commit comments

Comments
 (0)