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

Adding pwebdashboard module. #38

Merged
merged 1 commit into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions pwebdashboard/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import org.labkey.gradle.util.BuildUtils;

dependencies {
BuildUtils.addLabKeyDependency(project: project, config: "implementation", depProjectPath: ":server:modules:targetedms", depProjectConfig: "apiJarFile")
BuildUtils.addLabKeyDependency(project: project, config: "modules", depProjectPath: ":server:modules:targetedms", depProjectConfig: "published", depExtension: "module")
}
8 changes: 8 additions & 0 deletions pwebdashboard/module.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Name: PanoramaWeb Dashboard
ModuleClass: org.labkey.pwebdashboard.PwebDashboardModule
Label: PanoramaWeb Dashboard
Description: Queries and code to render PanoramaWeb usage charts.
URL: https://panoramaweb.org/wiki/home/page.view?name=Dashboard
License: Apache 2.0
LicenseURL: http://www.apache.org/licenses/LICENSE-2.0
SupportedDatabases: pgsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT FolderDocs.Project,
SUM(FolderDocs.Documents) AS TotalDocs,
SUM((CASE WHEN FolderDocs.TargetedMSFolderType='Experiment' OR FolderDocs.TargetedMSFolderType IS NULL THEN FolderDocs.Documents ELSE 0 END)) AS ExpDocs,
SUM((CASE WHEN FolderDocs.TargetedMSFolderType='Library' OR FolderDocs.TargetedMSFolderType='LibraryProtein' THEN FolderDocs.Documents ELSE 0 END)) AS LibDocs,
SUM((CASE WHEN FolderDocs.TargetedMSFolderType='QC' THEN FolderDocs.Documents ELSE 0 END)) AS QcDocs,
MAX(FolderDocs.LastUpload) AS LastUpload
FROM FolderDocs GROUP BY FolderDocs.Project
98 changes: 98 additions & 0 deletions pwebdashboard/src/org/labkey/pwebdashboard/FolderDocsTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.labkey.pwebdashboard;

import org.jetbrains.annotations.NotNull;
import org.labkey.api.data.BaseColumnInfo;
import org.labkey.api.data.ContainerFilter;
import org.labkey.api.data.ContainerForeignKey;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.ContainerTable;
import org.labkey.api.data.CoreSchema;
import org.labkey.api.data.JdbcType;
import org.labkey.api.data.SQLFragment;
import org.labkey.api.data.WrappedColumn;
import org.labkey.api.query.ExprColumn;
import org.labkey.api.query.FieldKey;
import org.labkey.api.targetedms.TargetedMSService;

import java.util.ArrayList;
import java.util.List;

/**
* Created by vsharma on 12/8/2016.
*/
public class FolderDocsTable extends ContainerTable
{
public static final String NAME = "FolderDocs";

public FolderDocsTable(@NotNull PwebDashboardSchema userSchema, ContainerFilter cf)
{
super(userSchema, cf);

setName(NAME);

BaseColumnInfo folderCol = new WrappedColumn(getColumn("EntityId"), "Folder");
ContainerForeignKey.initColumn(folderCol, userSchema);
addColumn(folderCol);

/*
WITH RECURSIVE project(parent, entityId, name) AS (
SELECT parent, entityId, name FROM core.containers WHERE name='QC Subfolder 2a'
UNION
SELECT a.parent AS parent, p.parent AS entityId, a.name AS name FROM project p, core.containers a
WHERE a.entityId = p.parent
) SELECT * FROM project WHERE parent = (SELECT entityId FROM core.containers WHERE parent IS NULL)
*/
// Get the project for the folder. Traverse up the folder tree.
SQLFragment projectColSql = new SQLFragment("(WITH RECURSIVE project(parent, entityId, name) AS ( ");
projectColSql.append(" SELECT parent, entityId, name FROM ").append(CoreSchema.getInstance().getTableInfoContainers(), "c");
projectColSql.append(" WHERE entityId=").append(ExprColumn.STR_TABLE_ALIAS).append(".EntityId");
projectColSql.append(" UNION ");
projectColSql.append(" SELECT a.parent AS parent, p.parent AS entityId, a.name AS name FROM project p, core.containers a WHERE a.entityId = p.parent ");
projectColSql.append(" ) SELECT entityId FROM project WHERE parent = ?)");
projectColSql.add(ContainerManager.getRoot().getEntityId());
ExprColumn projectCol = new ExprColumn(this, "Project", projectColSql, JdbcType.VARCHAR);
projectCol.setReadOnly(true);
ContainerForeignKey.initColumn(projectCol, userSchema);
addColumn(projectCol);

TargetedMSService tmsService = TargetedMSService.get();

SQLFragment docCountSql = new SQLFragment(" (SELECT COUNT(*) FROM ");
docCountSql.append(tmsService.getTableInfoRuns(), "r");
docCountSql.append(" WHERE container=").append(ExprColumn.STR_TABLE_ALIAS).append(".EntityId");
docCountSql.append(" AND deleted = FALSE AND statusId=1) ");
ExprColumn documentCountCol = new ExprColumn(this, "Documents", docCountSql, JdbcType.INTEGER);
addColumn(documentCountCol);

SQLFragment lastUploadSql = new SQLFragment(" (SELECT MAX(Created) FROM ");
lastUploadSql.append(tmsService.getTableInfoRuns(), "r");
lastUploadSql.append(" WHERE container=").append(ExprColumn.STR_TABLE_ALIAS).append(".EntityId");
lastUploadSql.append(" AND deleted = FALSE AND statusId=1) ");
ExprColumn lastUploadCol = new ExprColumn(this, "LastUpload", lastUploadSql, JdbcType.DATE);
addColumn(lastUploadCol);

SQLFragment folderTypeColSql = new SQLFragment(" (SELECT p.value FROM prop.properties AS p INNER JOIN prop.propertySets AS ps ON p.set = ps.set ");
folderTypeColSql.append(" WHERE p.Name=?");
folderTypeColSql.add(tmsService.FOLDER_TYPE_PROP_NAME);
folderTypeColSql.append(" AND ps.category=?");
folderTypeColSql.add("moduleProperties." + tmsService.MODULE_NAME);
folderTypeColSql.append(" AND ps.ObjectId=").append(ExprColumn.STR_TABLE_ALIAS).append(".EntityId");
folderTypeColSql.append(") ");
ExprColumn folderTypeCol = new ExprColumn(this, "TargetedMSFolderType", folderTypeColSql, JdbcType.VARCHAR);
addColumn(folderTypeCol);


List<FieldKey> visibleColumns = new ArrayList<>();
visibleColumns.add(FieldKey.fromParts("Project"));
visibleColumns.add(FieldKey.fromParts("Created"));
visibleColumns.add(FieldKey.fromParts("CreatedBy"));
visibleColumns.add(FieldKey.fromParts("Folder"));
visibleColumns.add(FieldKey.fromParts("TargetedMSFolderType"));
visibleColumns.add(FieldKey.fromParts("Documents"));
visibleColumns.add(FieldKey.fromParts("LastUpload"));
setDefaultVisibleColumns(visibleColumns);

SQLFragment sql = new SQLFragment("(CreatedBy IS NOT NULL)");
addCondition(sql);
}
}
244 changes: 244 additions & 0 deletions pwebdashboard/src/org/labkey/pwebdashboard/ProjectAdminsTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
package org.labkey.pwebdashboard;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.data.BaseColumnInfo;
import org.labkey.api.data.ColumnInfo;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerFilter;
import org.labkey.api.data.ContainerForeignKey;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.ContainerTable;
import org.labkey.api.data.CoreSchema;
import org.labkey.api.data.DataColumn;
import org.labkey.api.data.JdbcType;
import org.labkey.api.data.RenderContext;
import org.labkey.api.data.SQLFragment;
import org.labkey.api.data.WrappedColumn;
import org.labkey.api.query.ExprColumn;
import org.labkey.api.query.FieldKey;
import org.labkey.api.security.Group;
import org.labkey.api.security.MemberType;
import org.labkey.api.security.PrincipalType;
import org.labkey.api.security.SecurityManager;
import org.labkey.api.security.SecurityPolicy;
import org.labkey.api.security.SecurityPolicyManager;
import org.labkey.api.security.User;
import org.labkey.api.security.UserPrincipal;
import org.labkey.api.security.UserUrls;
import org.labkey.api.security.roles.ProjectAdminRole;
import org.labkey.api.security.roles.Role;
import org.labkey.api.security.roles.RoleManager;
import org.labkey.api.security.roles.SiteAdminRole;
import org.labkey.api.util.HtmlString;
import org.labkey.api.util.Link;
import org.labkey.api.util.PageFlowUtil;
import org.labkey.api.view.ActionURL;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Created by vsharma on 12/8/2016.
*/
public class ProjectAdminsTable extends ContainerTable
{
public static final String NAME = "ProjectAdmins";

public ProjectAdminsTable(@NotNull PwebDashboardSchema userSchema, ContainerFilter cf)
{
super(userSchema, cf);

setName(NAME);

BaseColumnInfo projectCol = new WrappedColumn(getMutableColumn("EntityId"), "Project");
ContainerForeignKey.initColumn(projectCol, userSchema);
addColumn(projectCol);


SQLFragment projectAdminsSql = new SQLFragment("(SELECT ");
projectAdminsSql.append(getSqlDialect().getGroupConcat(new SQLFragment("p.Name"), true, false))
.append(" AS Admins FROM ").append(CoreSchema.getInstance().getTableInfoRoleAssignments(), "ra")
.append(" INNER JOIN ").append(CoreSchema.getInstance().getTableInfoPrincipals(), "p")
.append(" ON (ra.UserId = p.UserId)")
.append(" WHERE ra.ResourceId = ").append(ExprColumn.STR_TABLE_ALIAS).append(".EntityId AND ra.Role = ?)");
projectAdminsSql.add(RoleManager.getRole(ProjectAdminRole.class).getUniqueName());
ExprColumn projectAdminsCol = new ExprColumn(this, "ProjectAdminsSearchable", projectAdminsSql, JdbcType.VARCHAR);
projectAdminsCol.setReadOnly(true);
addColumn(projectAdminsCol);


BaseColumnInfo adminCol = new WrappedColumn(getMutableColumn("EntityId"), "ProjectAdmins");
addColumn(adminCol);
adminCol.setDisplayColumnFactory(colInfo -> new AdminsColumn(colInfo));

List<FieldKey> visibleColumns = new ArrayList<>();
visibleColumns.add(FieldKey.fromParts("Created"));
visibleColumns.add(FieldKey.fromParts("CreatedBy"));
visibleColumns.add(FieldKey.fromParts("Project"));
visibleColumns.add(FieldKey.fromParts("ProjectAdmins"));
setDefaultVisibleColumns(visibleColumns);

// Only 'project' containers.
Container root = ContainerManager.getRoot();
SQLFragment sql = new SQLFragment("(Parent = ? AND CreatedBy IS NOT NULL)");
sql.add(root.getEntityId());
addCondition(sql);
}

class AdminsColumn extends DataColumn
{
public AdminsColumn(ColumnInfo col)
{
super(col);
}

@Override
public Object getValue(RenderContext ctx)
{
String entityId = ctx.get(getColumnInfo().getFieldKey(), String.class);
Container container = ContainerManager.getForId(entityId);

if (container != null)
{
return getCommaSeparatedAdmins(container);
}

return null;
}

@Override
public Object getDisplayValue(RenderContext ctx)
{
return getValue(ctx);
}

@NotNull
@Override
public String getFormattedValue(RenderContext ctx)
{
String value = h(getValue(ctx));
if(value != null) value = value.replaceAll(",", "<br />");
return value != null ? value : "";
}

@Nullable
private String getCommaSeparatedAdmins(Container container)
{
List<UserPrincipal> allAdmins = getAdmins(container);

StringBuilder display = new StringBuilder();

String space = "";

if (allAdmins.size() > 0)
{
for(UserPrincipal user: allAdmins)
{
display.append(space);
display.append(user.getName());
space = ", ";

}

return display.toString();
}
return null;
}

@Override
public void renderGridCellContents(RenderContext ctx, Writer out) throws IOException
{
String entityId = ctx.get(getColumnInfo().getFieldKey(), String.class);
Container container = ContainerManager.getForId(entityId);

if (container != null)
{
List<UserPrincipal> allAdmins = getAdmins(container);

StringBuilder display = new StringBuilder();

String space = "";

if (allAdmins.size() > 0)
{
for(UserPrincipal user: allAdmins)
{
display.append(space);
display.append(getUserDetails(container, user));
space = ",&nbsp;";

}

out.write(display.toString());
return;
}
}
else
{
out.write("");
}
}

@NotNull
private List<UserPrincipal> getAdmins(Container container)
{
List<UserPrincipal> allAdmins = new ArrayList<>();

SecurityPolicy policy = SecurityPolicyManager.getPolicy(container);
Role projectAdminRole = RoleManager.getRole(ProjectAdminRole.class);
Role siteAdminRole = RoleManager.getRole(SiteAdminRole.class);
Map<String, Map<PrincipalType, List<UserPrincipal>>> roleMap = policy.getAssignmentsAsMap();

for (String roleName : roleMap.keySet())
{
Role role = RoleManager.getRole(roleName);
if (role == null || (!(role.equals(projectAdminRole) || role.equals(siteAdminRole))))
{
continue;
}
Map<PrincipalType, List<UserPrincipal>> projAdmins = roleMap.get(roleName);
for (PrincipalType pType : projAdmins.keySet())
{
if (pType.equals(PrincipalType.USER))
{
allAdmins.addAll(projAdmins.get(pType));
}
else if (pType.equals(PrincipalType.GROUP))
{
List<UserPrincipal> adminGroups = projAdmins.get(pType);
for(UserPrincipal group: adminGroups)
{
Group grp = SecurityManager.getGroup(group.getUserId());
Set<User> grpMembers = SecurityManager.getAllGroupMembers(grp, MemberType.ACTIVE_USERS);
allAdmins.addAll(grpMembers);
}
}
}
}
return allAdmins;
}

private HtmlString getUserDetails(Container container, UserPrincipal member)
{
ActionURL url = PageFlowUtil.urlProvider(UserUrls.class).getUserDetailsURL(container, member.getUserId(), null);
return new Link.LinkBuilder(member.getName()).href(url).clearClasses().build().getHtmlString();
}

@Override
public boolean isFilterable()
{
return false;
}

@Override
public boolean isSortable()
{
return false;
}
}
}
Loading