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

#4369 - Display size of knowledge bases #4371

Merged
merged 1 commit into from
Dec 10, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ List<KBHandle> listInstances(KnowledgeBase kb, String aConceptIri, boolean aAll)
* The knowledge base to query
* @param aIdentifier
* The identifier of the entity
* @return All statements that match the specification
* @return All statements that match the specificat@Override ion
*/
List<Statement> listStatementsWithPredicateOrObjectReference(KnowledgeBase kb,
String aIdentifier);
Expand Down Expand Up @@ -629,4 +629,10 @@ Optional<KBConcept> readConcept(KnowledgeBase aKB, String aIdentifier, boolean a
List<KBHandle> listHandlesCaching(KnowledgeBase aKB, SPARQLQuery aQuery, boolean aAll);

Optional<KBHandle> fetchHandleCaching(KnowledgeBase aKB, SPARQLQuery aQuery, boolean aAll);

long getRepositorySize(KnowledgeBase aKB);

long getStatementCount(KnowledgeBase aKB);

long getIndexSize(KnowledgeBase aKB);
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,30 @@ public void destroy() throws Exception
repoManager.shutDown();
}

@Override
public long getRepositorySize(KnowledgeBase aKB)
{
var dataDir = new File(kbRepositoriesRoot, "repositories/" + aKB.getRepositoryId());
return FileUtils.sizeOfDirectory(dataDir);
}

@Override
public long getIndexSize(KnowledgeBase aKB)
{
var indexDir = new File(kbRepositoriesRoot, "indexes/" + aKB.getRepositoryId());
return FileUtils.sizeOfDirectory(indexDir);
}

@Override
public long getStatementCount(KnowledgeBase aKB)
{
// Load files into the repository
try (var conn = getConnection(aKB)) {
conn.setIsolationLevel(IsolationLevels.NONE);
return conn.size();
}
}

/**
* Sanity check to test if a knowledge base is already registered with RDF4J.
*
Expand Down
4 changes: 4 additions & 0 deletions inception/inception-ui-kb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
<wicket:panel>
<form wicket:id="form" class="flex-content card">
<div class="card-header">
<wicket:container wicket:id="title"></wicket:container>
<wicket:container wicket:id="title"/>
</div>

<div class="scrolling card-body">
<wicket:container wicket:id="content"></wicket:container>
<wicket:container wicket:id="content"/>
</div>

<div class="card-footer">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public KnowledgeBase getKb()
return kb;
}

public boolean isKbSaved()
{
return kb != null && kb.getRepositoryId() != null;
}

public void setKb(KnowledgeBase aKB)
{
kb = aKB;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ kb.settings.schemaMapping = Schema Mapping
kb.settings.schemaMapping.classMapping = Class/Instance Mapping
kb.settings.schemaMapping.propertyMapping = Property Mapping
kb.settings.rootConcepts = Root Concepts
kb.settings.defineRootConcepts = Define Root Concepts
kb.settings.defineRootConcepts = Concepts
kb.settings.additionalMatchingProperties = Additional Matching Properties
kb.settings.defineAdditionalMatchingProperties = Define Additional Matching Properties
kb.settings.defineAdditionalMatchingProperties = Properties

kb.details.enabled=Enabled
kb.details.local.contents=Import contents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@

<body>
<wicket:panel>
<div class="row form-row" wicket:enclosure="repositorySize">
<div class="offset-sm-3 col-sm-9 d-flex flex-row text-muted">
<div class="me-5"><i class="fas fa-hdd me-1"></i> Repository: <span wicket:id="repositorySize"/> (<span wicket:id="statementCount"/> statements)</div>
<div><i class="fas fa-search me-1"></i> Full-text index: <span wicket:id="indexSize"/></div>
</div>
</div>

<!-- Import -->
<div wicket:enclosure="uploadForm">
<div class="row form-row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static de.tudarmstadt.ukp.inception.kb.RepositoryType.LOCAL;
import static de.tudarmstadt.ukp.inception.support.lambda.LambdaBehavior.enabledWhen;
import static de.tudarmstadt.ukp.inception.support.lambda.LambdaBehavior.visibleWhen;
import static de.tudarmstadt.ukp.inception.support.lambda.LambdaBehavior.visibleWhenNot;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.eclipse.rdf4j.rio.RDFFormat.NTRIPLES;
Expand All @@ -28,13 +29,15 @@

import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ClassAttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
Expand Down Expand Up @@ -111,28 +114,30 @@ private static final RDFFormat getRdfFormatForFileExt(String fileExt)
private @SpringBean KnowledgeBaseProperties kbproperties;

private final Map<String, KnowledgeBaseProfile> knowledgeBaseProfiles;
private final Map<String, KnowledgeBaseProfile> downloadedProfiles;
private final Map<String, File> uploadedFiles;
private final Map<String, KnowledgeBaseProfile> downloadedProfiles = new HashMap<>();
private final Map<String, File> uploadedFiles = new HashMap<>();

private final WebMarkupContainer listViewContainer;
private final WebMarkupContainer infoContainerLocal;
private final CompoundPropertyModel<KnowledgeBaseInfo> kbInfoModel;
private final CompoundPropertyModel<KnowledgeBaseInfo> kbInfoModel = CompoundPropertyModel
.of(Model.of());

private final Label repositorySize;
private final Label indexSize;
private final Label statementCount;

private FileUploadField fileUpload;

private KnowledgeBaseProfile selectedKnowledgeBaseProfile;

public LocalRepositorySettingsPanel(String aId, IModel<?> aModel,
public LocalRepositorySettingsPanel(String aId, IModel<KnowledgeBaseWrapper> aModel,
Map<String, KnowledgeBaseProfile> aKnowledgeBaseProfiles)
{
super(aId, aModel);

setOutputMarkupId(true);

knowledgeBaseProfiles = aKnowledgeBaseProfiles;
downloadedProfiles = new HashMap<>();
uploadedFiles = new HashMap<>();
kbInfoModel = CompoundPropertyModel.of(Model.of());

queue(uploadForm("uploadForm", "uploadField"));

Expand All @@ -142,17 +147,42 @@ public LocalRepositorySettingsPanel(String aId, IModel<?> aModel,

queue(fileExtensionsExportList("exportButtons"));

var kbModel = getModel().map(KnowledgeBaseWrapper::getKb);
var repoSizeModel = kbModel.map(kbService::getRepositorySize).orElse(0l);

repositorySize = new Label("repositorySize", LoadableDetachableModel
.of(() -> repoSizeModel.map(FileUtils::byteCountToDisplaySize).getObject()));
repositorySize.add(visibleWhen(getModel().map(KnowledgeBaseWrapper::isKbSaved)));
queue(repositorySize);

indexSize = new Label("indexSize",
LoadableDetachableModel.of(() -> kbModel.map(kbService::getIndexSize).orElse(0l)
.map(FileUtils::byteCountToDisplaySize).getObject()));
indexSize.add(visibleWhen(getModel().map(KnowledgeBaseWrapper::isKbSaved)));
queue(indexSize);

statementCount = new Label("statementCount", LoadableDetachableModel.of(() -> {
var repoSize = repoSizeModel.getObject();
if (repoSize > 25_000_000) {
var avgTripleSize = 140;
return "~"
+ NumberFormat.getCompactNumberInstance().format(repoSize / avgTripleSize);
// return "~" + (((repoSize / avgTripleSize) / 1000) * 1000);
}

return kbModel.map(kbService::getStatementCount).orElse(0l).getObject();
}));
statementCount.add(visibleWhen(getModel().map(KnowledgeBaseWrapper::isKbSaved)));
queue(statementCount);

var localKBs = knowledgeBaseProfiles.values().stream() //
.filter(kb -> LOCAL == kb.getType()) //
.collect(Collectors.toList());

listViewContainer = new WebMarkupContainer("listViewContainer");
ListView<KnowledgeBaseProfile> suggestions = localSuggestionsList("localKBs", localKBs);
listViewContainer.add(suggestions);
listViewContainer.add(localSuggestionsList("localKBs", localKBs));
listViewContainer.setOutputMarkupPlaceholderTag(true);
listViewContainer.add(visibleWhen(getModel().map(KnowledgeBaseWrapper::getKb) //
.map(kb -> kb.getRepositoryId() == null) //
.orElse(false)));
listViewContainer.add(visibleWhenNot(getModel().map(KnowledgeBaseWrapper::isKbSaved)));

var addKbButton = new LambdaAjaxLink("addKbButton", this::actionDownloadKbAndSetIRIs);
addKbButton.add(new Label("addKbLabel", new ResourceModel("kb.wizard.steps.local.addKb")));
Expand All @@ -175,7 +205,7 @@ private Form<Void> uploadForm(String aFormId, String aFieldId)
{
var form = new Form<Void>(aFormId);

FileInputConfig config = new FileInputConfig();
var config = new FileInputConfig();
config.initialCaption("Import knowledge base ...");
config.showPreview(false);
config.showUpload(false);
Expand All @@ -189,8 +219,8 @@ private Form<Void> uploadForm(String aFormId, String aFieldId)
public void handleUploadedFiles()
{
try {
for (FileUpload fu : fileUpload.getFileUploads()) {
File tmp = uploadFile(fu);
for (var fu : fileUpload.getFileUploads()) {
var tmp = uploadFile(fu);
getModel().getObject().putFile(fu.getClientFileName(), tmp);
}
}
Expand All @@ -215,7 +245,7 @@ private AjaxLink<Void> clearLink(String aId)

private ListView<String> fileExtensionsExportList(String aId)
{
ListView<String> fileExListView = new ListView<String>(aId, EXPORT_FORMAT_FILE_EXTENSIONS)
var fileExListView = new ListView<String>(aId, EXPORT_FORMAT_FILE_EXTENSIONS)
{
private static final long serialVersionUID = -1869762759620557362L;

Expand All @@ -242,23 +272,21 @@ protected void populateItem(ListItem<String> item)
private ListView<KnowledgeBaseProfile> localSuggestionsList(String aId,
List<KnowledgeBaseProfile> localKBs)
{
ListView<KnowledgeBaseProfile> suggestions = new ListView<KnowledgeBaseProfile>(aId,
localKBs)
var suggestions = new ListView<KnowledgeBaseProfile>(aId, localKBs)
{
private static final long serialVersionUID = 1L;

@Override
protected void populateItem(ListItem<KnowledgeBaseProfile> item)
{
LambdaAjaxLink link = new LambdaAjaxLink("suggestionLink",
var link = new LambdaAjaxLink("suggestionLink",
_target -> actionSelectPredefinedKB(_target, item.getModel()));

// Can not import the same KB more than once
boolean isImported = downloadedProfiles
.containsKey(item.getModelObject().getName());
var isImported = downloadedProfiles.containsKey(item.getModelObject().getName());
link.setEnabled(!isImported);

String itemLabel = item.getModelObject().getName();
var itemLabel = item.getModelObject().getName();
// Adjust label to indicate whether the KB has already been downloaded
if (isImported) {
// \u2714 is the checkmark symbol
Expand Down Expand Up @@ -319,11 +347,10 @@ private void actionSelectPredefinedKB(AjaxRequestTarget aTarget,

private File uploadFile(FileUpload fu) throws IOException
{
String fileName = fu.getClientFileName();
var fileName = fu.getClientFileName();
if (!uploadedFiles.containsKey(fileName)) {
FileUploadDownloadHelper fileUploadDownloadHelper = new FileUploadDownloadHelper(
getApplication());
File tmpFile = fileUploadDownloadHelper.writeFileUploadToTemporaryFile(fu, getModel());
var fileUploadDownloadHelper = new FileUploadDownloadHelper(getApplication());
var tmpFile = fileUploadDownloadHelper.writeFileUploadToTemporaryFile(fu, getModel());
uploadedFiles.put(fileName, tmpFile);
}
else {
Expand Down