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

Job for calculating the size and the assset count of each hosted repository #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,28 @@
/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2008-present Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.repository.sizeassetcount;

import org.sonatype.nexus.repository.Facet;

/**
* Facet used for calculating the size and the blobcount of repositories
* @since 3.7.0
*/
@Facet.Exposed
public interface SizeAssetCountAttributesFacet extends Facet{

/**
* Calculate the size and the asset count of a repository
*/
void calculateSizeAssetCount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2008-present Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.repository.sizeassetcount;

import com.google.common.collect.Streams;
import org.sonatype.nexus.repository.FacetSupport;
import org.sonatype.nexus.repository.config.Configuration;
import org.sonatype.nexus.repository.manager.RepositoryManager;
import org.sonatype.nexus.repository.storage.*;
import org.sonatype.nexus.repository.transaction.TransactionalStoreMetadata;
import org.sonatype.nexus.transaction.UnitOfWork;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @since 3.7.0
*/
@Named
public class SizeAssetCountAttributesFacetImpl extends FacetSupport implements SizeAssetCountAttributesFacet {

@Inject
private RepositoryManager repositoryManager;



// Key for the attributes for Size and Blob count
public static final String SIZE_ASSET_COUNT_KEY_ATTRIBUTES = "sizeAssetCount";
// Key for the data assetcount
public static final String ASSET_COUNT_KEY = "assetCount";
// Key for the data size
public static final String SIZE_KEY = "size";


public void calculateSizeAssetCount() {
String repositoryName = getRepository().getName();
log.debug("Repository name {} ", repositoryName);
Map<String, Object> attributesMap = new HashMap<>();
attributesMap.put(ASSET_COUNT_KEY,0L);
attributesMap.put(SIZE_KEY,0L);
if (optionalFacet(StorageFacet.class).isPresent()) {
TransactionalStoreMetadata.operation.withDb(facet(StorageFacet.class).txSupplier()).call(() -> {
StorageTx storageTx = UnitOfWork.currentTx();

//First Get the bucket
Bucket bucket = storageTx.findBucket(getRepository());

//Assets of the bucket
Iterable<Asset> assets = storageTx.browseAssets(bucket);
if (assets != null) {
attributesMap.put(ASSET_COUNT_KEY, storageTx.countAssets(Query.builder().where("1").eq(1).build(), Collections.singletonList(getRepository())));
attributesMap.put(SIZE_KEY, Streams.stream(assets).mapToLong(Asset::size).sum());
}

return null;
});
}
Configuration configuration = getRepository().getConfiguration();
Objects.requireNonNull(configuration.getAttributes())
.put(SIZE_ASSET_COUNT_KEY_ATTRIBUTES, attributesMap);
try {
repositoryManager.update(configuration);
log.debug("The attributes sizeAssetCount of the repository {} have been updated", repositoryName);
} catch (Exception e) {
log.error("Error occuring during the update of rhe repository {} ", repositoryName, e);
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2008-present Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.repository.sizeassetcount.internal;


import org.sonatype.nexus.logging.task.TaskLogging;
import org.sonatype.nexus.repository.Repository;
import org.sonatype.nexus.repository.RepositoryTaskSupport;
import org.sonatype.nexus.repository.Type;
import org.sonatype.nexus.repository.sizeassetcount.SizeAssetCountAttributesFacet;
import org.sonatype.nexus.repository.types.HostedType;
import org.sonatype.nexus.scheduling.Cancelable;

import javax.inject.Inject;
import javax.inject.Named;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.sonatype.nexus.logging.task.TaskLogType.NEXUS_LOG_ONLY;

/**
* Task that calculates the size and the asset count of a repository
*
* @since 3.7.0
*/
@Named
@TaskLogging(NEXUS_LOG_ONLY)
public class SizeAssetCountAttributesCalculatingTask extends RepositoryTaskSupport
implements Cancelable
{

public static final String PREFIX_MESSAGE = "Calculate the size and the asset count of the repository ";
private final Type hostedType;

@Inject
public SizeAssetCountAttributesCalculatingTask(@Named(HostedType.NAME) final Type hostedType) {
this.hostedType = checkNotNull(hostedType);
}


@Override
public String getMessage() {
return PREFIX_MESSAGE + getRepositoryField();
}


@Override
protected void execute(Repository repository) {
repository.facet(SizeAssetCountAttributesFacet.class).calculateSizeAssetCount();

}

@Override
protected boolean appliesTo(Repository repository) {
return hostedType.equals(repository.getType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2008-present Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.repository.sizeassetcount.internal;

import org.sonatype.nexus.formfields.FormField;
import org.sonatype.nexus.formfields.RepositoryCombobox;
import org.sonatype.nexus.repository.sizeassetcount.SizeAssetCountAttributesFacet;
import org.sonatype.nexus.repository.types.HostedType;
import org.sonatype.nexus.scheduling.TaskDescriptorSupport;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import static org.sonatype.nexus.repository.RepositoryTaskSupport.REPOSITORY_NAME_FIELD_ID;

/**
* @since 3.7.0
*/
@Named
@Singleton
public class SizeAssetCountAttributesCalculatingTaskDescriptor extends TaskDescriptorSupport
{
public static final String TYPE_ID = "repository.calculate-size-assetcount";

@Inject
public SizeAssetCountAttributesCalculatingTaskDescriptor() {
super(TYPE_ID,
SizeAssetCountAttributesCalculatingTask.class,
" Calculate the size and the asset count of a repository",
VISIBLE,
EXPOSED,
new RepositoryCombobox(
REPOSITORY_NAME_FIELD_ID,
"Repository",
"Select the repository which you will calculate the size and the asset count",
FormField.MANDATORY
).includingAnyOfFacets(SizeAssetCountAttributesFacet.class).includingAnyOfTypes(HostedType.NAME).includeAnEntryForAllRepositories()
);
}
}
Loading