Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Statistics per Organization #3663

Merged
merged 5 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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 @@ -286,9 +286,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
Copy link
Contributor

@jstriebel jstriebel Jan 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering why we can't use the scala slick syntax here. Or do you simply prefer SQL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my first draft, I was going to use the accessQueries to find all items in the visible scope of the requesting user, rather than filter by organization (that would have required sql syntax). However, it turned out that leads to inconsistencies e.g. with public datasets. So I changed course, but I didn’t change this code back to slick syntax.

However, I think the more complex the query, the easier to write and read is SQL compared to slick syntax. Since we introduced a join here, I’d keep this change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me 👍

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