Skip to content

Commit

Permalink
Separate Statistics per Organization (#3663)
Browse files Browse the repository at this point in the history
* [WIP] query statistics per orga

* separate statistics per organization

* scalafmt + changelog
  • Loading branch information
fm3 authored Jan 24, 2019
1 parent 8999fdd commit 60119c8
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ For upgrade instructions, please check the [migration guide](MIGRATIONS.md).

### Changed

-
- Statistics are now separated by organization, rather than showing the webKnossos instance’s totals. [#3663](https://github.com/scalableminds/webknossos/pull/3663)

### Fixed

Expand Down
11 changes: 6 additions & 5 deletions app/controllers/StatisticsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ class StatisticsController @Inject()(timeSpanService: TimeSpanService,
intervalHandler.get(interval) match {
case Some(handler) =>
for {
times <- timeSpanService.loggedTimePerInterval(handler, start, end)
numberOfUsers <- userDAO.countAll
numberOfDatasets <- dataSetDAO.countAll
numberOfAnnotations <- annotationDAO.countAll
numberOfAssignments <- taskDAO.countAllOpenInstances
organizationId <- Fox.successful(request.identity._organization)
times <- timeSpanService.loggedTimePerInterval(handler, start, end, organizationId)
numberOfUsers <- userDAO.countAllForOrganization(organizationId)
numberOfDatasets <- dataSetDAO.countAllForOrganization(organizationId)
numberOfAnnotations <- annotationDAO.countAllForOrganization(organizationId)
numberOfAssignments <- taskDAO.countAllOpenInstancesForOrganization(organizationId)
} yield {
Ok(
Json.obj(
Expand Down
8 changes: 8 additions & 0 deletions app/models/annotation/Annotation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ class AnnotationDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContex
count <- countList.headOption
} yield count

def countAllForOrganization(organizationId: ObjectId)(implicit ctx: DBAccessContext): Fox[Int] =
for {
countList <- run(
sql"select count(*) from (select a._id from #${existingCollectionName} a join webknossos.users_ u on a._user = u._id where u._organization = ${organizationId}) q"
.as[Int])
count <- countList.headOption
} yield count

// update operations

def insertOne(a: Annotation): Fox[Unit] =
Expand Down
7 changes: 7 additions & 0 deletions app/models/binary/DataSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ class DataSetDAO @Inject()(sqlClient: SQLClient,
parsed <- Fox.combined(r.toList.map(parse))
} yield parsed

def countAllForOrganization(organizationId: ObjectId)(implicit ctx: DBAccessContext): Fox[Int] =
for {
rList <- run(
sql"select count(_id) from #${existingCollectionName} where _organization = ${organizationId}".as[Int])
r <- rList.headOption
} yield r

def findOneByNameAndOrganizationName(name: String, organizationName: String)(
implicit ctx: DBAccessContext): Fox[DataSet] =
for {
Expand Down
6 changes: 4 additions & 2 deletions app/models/task/Task.scala
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,11 @@ class TaskDAO @Inject()(sqlClient: SQLClient, projectDAO: ProjectDAO)(implicit e
firstResult <- result.headOption.toFox
} yield firstResult

def countAllOpenInstances(implicit ctx: DBAccessContext): Fox[Int] =
def countAllOpenInstancesForOrganization(organizationId: ObjectId)(implicit ctx: DBAccessContext): Fox[Int] =
for {
result <- run(sql"select sum(openInstances) from webknossos.tasks_".as[Int])
result <- run(
sql"select sum(t.openInstances) from webknossos.tasks_ t join webknossos.projects_ p on t._project = p._id where ${organizationId} in (select _organization from webknossos.users_ where _id = p._owner)"
.as[Int])
firstResult <- result.headOption
} yield firstResult

Expand Down
7 changes: 7 additions & 0 deletions app/models/user/User.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ class UserDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
parsed <- Fox.combined(r.toList.map(parse))
} yield parsed

def countAllForOrganization(organizationId: ObjectId): Fox[Int] =
for {
resultList <- run(
sql"select count(_id) from #${existingCollectionName} where _organization = ${organizationId}".as[Int])
result <- resultList.headOption
} yield result

def insertOne(u: User)(implicit ctx: DBAccessContext): Fox[Unit] =
for {
_ <- run(
Expand Down
13 changes: 7 additions & 6 deletions app/models/user/time/TimeSpan.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,14 @@ class TimeSpanDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
parsed <- Fox.combined(r.toList.map(parse))
} yield parsed

def findAll(start: Option[Long], end: Option[Long]): Fox[List[TimeSpan]] =
def findAll(start: Option[Long], end: Option[Long], organizationId: ObjectId): Fox[List[TimeSpan]] =
for {
r <- run(Timespans
.filter(r =>
notdel(r) && (r.created >= new java.sql.Timestamp(start.getOrElse(0))) && r.created <= new java.sql.Timestamp(
end.getOrElse(MAX_TIMESTAMP)))
.result)
r <- run(sql"""select #${columnsWithPrefix("t.")} from #${existingCollectionName} t
join webknossos.users u on t._user = u._id
where t.created >= ${new java.sql.Timestamp(start.getOrElse(0))} and t.created <= ${new java.sql.Timestamp(
end.getOrElse(MAX_TIMESTAMP))}
and u._organization = ${organizationId}
""".as[TimespansRow])
parsed <- Fox.combined(r.toList.map(parse))
} yield parsed

Expand Down
5 changes: 3 additions & 2 deletions app/models/user/time/TimeSpanService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ class TimeSpanService @Inject()(annotationDAO: AnnotationDAO,

def loggedTimePerInterval[T](groupingF: TimeSpan => T,
start: Option[Long] = None,
end: Option[Long] = None): Fox[Map[T, Duration]] =
end: Option[Long] = None,
organizationId: ObjectId): Fox[Map[T, Duration]] =
for {
timeTrackingOpt <- timeSpanDAO.findAll(start, end).futureBox
timeTrackingOpt <- timeSpanDAO.findAll(start, end, organizationId).futureBox
} yield {
timeTrackingOpt match {
case Full(timeSpans) =>
Expand Down

0 comments on commit 60119c8

Please sign in to comment.