Skip to content

Commit

Permalink
Merge pull request #291 from NDLANO/extended-stats
Browse files Browse the repository at this point in the history
Extend stats with grouped values
  • Loading branch information
gunnarvelle authored Sep 1, 2023
2 parents 1170208 + 241b935 commit 64adeee
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ case class Stats(
@(ApiModelProperty @field)(description = "The total number of favourited resources ") numberOfResources: Long,
@(ApiModelProperty @field)(description = "The total number of created tags") numberOfTags: Long,
@(ApiModelProperty @field)(description = "The total number of favourited subjects") numberOfSubjects: Long,
@(ApiModelProperty @field)(description = "The total number of shared folders") numberOfSharedFolders: Long
@(ApiModelProperty @field)(description = "The total number of shared folders") numberOfSharedFolders: Long,
@(ApiModelProperty @field)(description = "Stats for type resources") favouritedResources: List[ResourceStats]
) {}

case class ResourceStats(
@(ApiModelProperty @field)(description = "The type of favourited resouce") `type`: String,
@(ApiModelProperty @field)(description = "The number of favourited resource") number: Long
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -589,5 +589,10 @@ trait FolderRepository {
.single()
}

def numberOfResourcesGrouped()(implicit session: DBSession = ReadOnlyAutoSession): List[(Long, String)] = {
sql"select count(*) as antall, resource_type from ${DBResource.table} group by resource_type"
.map(rs => (rs.long("antall"), rs.string("resource_type")))
.list()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ trait ReadService {

def getStats: Option[Stats] = {
implicit val session: DBSession = folderRepository.getSession(true)
val groupedResources = folderRepository.numberOfResourcesGrouped()
for {
numberOfUsers <- userRepository.numberOfUsers()
numberOfFolders <- folderRepository.numberOfFolders()
Expand All @@ -461,7 +462,8 @@ trait ReadService {
numberOfResources,
numberOfTags,
numberOfSubjects,
numberOfSharedFolders
numberOfSharedFolders,
favouritedResources = groupedResources.map(gr => ResourceStats(gr._2, gr._1))
)
} yield stats
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class StatsControllerTest extends UnitSuite with TestEnvironment with ScalatraFu
}

test("That getting stats returns in fact stats") {
when(readService.getStats).thenReturn(Some(Stats(1, 2, 3, 4, 5, 6)))
when(readService.getStats).thenReturn(Some(Stats(1, 2, 3, 4, 5, 6, List.empty)))
get("/") {
status should be(200)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,8 @@ class ReadServiceTest extends UnitSuite with UnitTestEnvironment {
when(folderRepository.numberOfTags()(any)).thenReturn(Some(10))
when(userRepository.numberOfSubjects()(any)).thenReturn(Some(5))
when(folderRepository.numberOfSharedFolders()(any)).thenReturn(Some(5))
when(folderRepository.numberOfResourcesGrouped()(any)).thenReturn(List.empty)

service.getStats.get should be(Stats(5, 10, 20, 10, 5, 5))
service.getStats.get should be(Stats(5, 10, 20, 10, 5, 5, List.empty))
}
}

0 comments on commit 64adeee

Please sign in to comment.