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

Openrewrite code cleanup #57

Merged
merged 11 commits into from
Jan 24, 2023
117 changes: 59 additions & 58 deletions src/main/java/hudson/plugins/disk_usage/BuildDiskUsageAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Disk usage information for a single build
* @author dvrzalik
*/
//TODO really implementsProminentProjectAction???
// TODO really implementsProminentProjectAction???
@ExportedBean(defaultVisibility = 1)
public class BuildDiskUsageAction implements ProminentProjectAction, BuildBadgeAction, RunAction2 {

Expand All @@ -30,12 +30,12 @@ public class BuildDiskUsageAction implements ProminentProjectAction, BuildBadgeA
AbstractBuild build;
@Deprecated
DiskUsage diskUsage;

public BuildDiskUsageAction(AbstractBuild build) {
this.build = build;
}
}

public String getIconFileName() {
public String getIconFileName() {
return null;
}

Expand All @@ -46,127 +46,128 @@ public String getDisplayName() {
public String getUrlName() {
return Messages.UrlName();
}
public void setDiskUsage(Long size) throws IOException{

public void setDiskUsage(Long size) throws IOException {
AbstractProject project = build.getProject();
DiskUsageProperty property = (DiskUsageProperty) project.getProperty(DiskUsageProperty.class);
if(property==null){
if(property == null) {
DiskUsageUtil.addProperty(project);
property = (DiskUsageProperty) project.getProperty(DiskUsageProperty.class);
}
DiskUsageBuildInformation information = property.getDiskUsageBuildInformation(build.getId());
if(information!=null){
if(information != null) {
information.setSize(size);
}
else{
else {
property.getDiskUsage().addBuildInformation(new DiskUsageBuildInformation(build.getId(), build.getTimeInMillis(), build.getNumber(), size), build);
}
property.saveDiskUsage();
property.saveDiskUsage();
}

/**
* @return Disk usage of the build (included child builds)
*/
public Long getDiskUsage() {
AbstractProject project = build.getProject();
DiskUsageProperty property = (DiskUsageProperty) project.getProperty(DiskUsageProperty.class);
if(property==null){
if(property == null) {
DiskUsageUtil.addProperty(project);
property = (DiskUsageProperty) project.getProperty(DiskUsageProperty.class);
}
return property.getDiskUsageOfBuild(build.getId());
}
public Long getAllDiskUsage(){

public Long getAllDiskUsage() {
Long buildsDiskUsage = getDiskUsage();
AbstractProject project = build.getProject();
if(project instanceof ItemGroup){
buildsDiskUsage += getBuildsDiskUsageAllSubItems((ItemGroup)project);
}
if(project instanceof ItemGroup) {
buildsDiskUsage += getBuildsDiskUsageAllSubItems((ItemGroup) project);
}
return buildsDiskUsage;
}
public String getBuildUsageString(){

public String getBuildUsageString() {
return DiskUsageUtil.getSizeString(getAllDiskUsage());
}

private Long getBuildsDiskUsageAllSubItems(ItemGroup group){
private Long getBuildsDiskUsageAllSubItems(ItemGroup group) {
Long buildsDiskUsage = 0l;
for(Object item: group.getItems()){
if(item instanceof ItemGroup){
buildsDiskUsage += getBuildsDiskUsageAllSubItems((ItemGroup)item);
for(Object item: group.getItems()) {
if(item instanceof ItemGroup) {
buildsDiskUsage += getBuildsDiskUsageAllSubItems((ItemGroup) item);
}
else{
if(item instanceof AbstractProject){
else {
if(item instanceof AbstractProject) {
AbstractProject project = (AbstractProject) item;
DiskUsageProperty property = (DiskUsageProperty) project.getProperty(DiskUsageProperty.class);
if(property==null){
if(property == null) {
DiskUsageUtil.addProperty(project);
property = (DiskUsageProperty) project.getProperty(DiskUsageProperty.class);
}
Set<DiskUsageBuildInformation> informations = property.getDiskUsageOfBuilds();
for(DiskUsageBuildInformation information : informations){
if(information.getNumber() == build.getNumber()){
for(DiskUsageBuildInformation information: informations) {
if(information.getNumber() == build.getNumber()) {
buildsDiskUsage += information.getSize();
}
}
}
}
}
}
return buildsDiskUsage;
}

public Object readResolve() {
//for keeping backward compatibility
if(diskUsage!=null){
// for keeping backward compatibility
if(diskUsage != null) {
buildDiskUsage = diskUsage.buildUsage;
Node node = build.getBuiltOn();
if(node!=null && diskUsage.wsUsage!=null && diskUsage.wsUsage > 0){
if(node != null && diskUsage.wsUsage != null && diskUsage.wsUsage > 0) {
DiskUsageProperty property = (DiskUsageProperty) build.getProject().getProperty(DiskUsageProperty.class);
AbstractProject project = build.getProject().getRootProject();
if(property!=null && (project instanceof TopLevelItem))
property.putSlaveWorkspaceSize(node, node.getWorkspaceFor((TopLevelItem)project).getRemote(), diskUsage.wsUsage);
if(property != null && (project instanceof TopLevelItem)) {
property.putSlaveWorkspaceSize(node, node.getWorkspaceFor((TopLevelItem) project).getRemote(), diskUsage.wsUsage);
}
}
diskUsage=null;
diskUsage = null;
}
return this;
}

@Override
public void onAttached(Run<?, ?> r) {
//no action is needed
// no action is needed
}

@Override
public void onLoad(Run<?, ?> r) {
DiskUsageProperty property = (DiskUsageProperty) build.getProject().getProperty(DiskUsageProperty.class);
long size = 0L;
if(property==null){
if(property == null) {
return;
}
//backward compatibility
// backward compatibility
BuildDiskUsageAction action = null;
for(Action a : build.getActions()){
if(a instanceof BuildDiskUsageAction){
action = (BuildDiskUsageAction) a;
if(action.buildDiskUsage != null){
size=action.buildDiskUsage;
}
}
}
if(action!=null){
//remove old action, now it is added by transition action factory
build.getActions().remove(action);
try {
build.save();
} catch (IOException ex) {
Logger.getLogger(BuildDiskUsageAction.class.getName()).log(Level.SEVERE, null, ex);
for(Action a: build.getActions()) {
if(a instanceof BuildDiskUsageAction) {
action = (BuildDiskUsageAction) a;
if(action.buildDiskUsage != null) {
size = action.buildDiskUsage;
}
}
//Transient actions can be created even during deletion of job
if(property.getDiskUsageBuildInformation(build.getNumber())==null && build.getRootDir().exists()){
property.getDiskUsage().addBuildInformation(new DiskUsageBuildInformation(build.getId(),build.getTimeInMillis(), build.getNumber(), size), build);
}
if(action != null) {
// remove old action, now it is added by transition action factory
build.getActions().remove(action);
try {
build.save();
} catch (IOException ex) {
Logger.getLogger(BuildDiskUsageAction.class.getName()).log(Level.SEVERE, null, ex);
}
}
// Transient actions can be created even during deletion of job
if(property.getDiskUsageBuildInformation(build.getNumber()) == null && build.getRootDir().exists()) {
property.getDiskUsage().addBuildInformation(new DiskUsageBuildInformation(build.getId(), build.getTimeInMillis(), build.getNumber(), size), build);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,77 +24,77 @@
* @author dvrzalik
*/
@Extension
public class BuildDiskUsageCalculationThread extends DiskUsageCalculation {
//last scheduled task;
public class BuildDiskUsageCalculationThread extends DiskUsageCalculation {

// last scheduled task;
private static DiskUsageCalculation currentTask;
public BuildDiskUsageCalculationThread(){
super("Calculation of builds disk usage");
}

public BuildDiskUsageCalculationThread() {
super("Calculation of builds disk usage");
}

@Override
public void execute(TaskListener listener) throws IOException, InterruptedException {
if(!isCancelled() && startExecution()){
try{
if(!isCancelled() && startExecution()) {
try {
List<Item> items = new ArrayList<Item>();
ItemGroup<? extends Item> itemGroup = Jenkins.getInstance();
items.addAll(DiskUsageUtil.getAllProjects(itemGroup));
for (Object item : items) {
if (item instanceof AbstractProject) {

for(Object item: items) {
if(item instanceof AbstractProject) {
AbstractProject project = (AbstractProject) item;
// if (!project.isBuilding()) {
DiskUsageProperty property = (DiskUsageProperty) project.getProperty(DiskUsageProperty.class);
if(property==null){
property = new DiskUsageProperty();
project.addProperty(property);
// if (!project.isBuilding()) {
DiskUsageProperty property = (DiskUsageProperty) project.getProperty(DiskUsageProperty.class);
if(property == null) {
property = new DiskUsageProperty();
project.addProperty(property);
}
ProjectDiskUsage diskUsage = property.getProjectDiskUsage();
for(DiskUsageBuildInformation information: diskUsage.getBuildDiskUsage(true)) {
Map<Integer, AbstractBuild> loadedBuilds = project._getRuns().getLoadedBuilds();
AbstractBuild build = loadedBuilds.get(information.getNumber());
// do not calculat builds in progress
if(build != null && build.isBuilding()) {
continue;
}
try {
DiskUsageUtil.calculateDiskUsageForBuild(information.getId(), project);
}
ProjectDiskUsage diskUsage = property.getProjectDiskUsage();
for(DiskUsageBuildInformation information: diskUsage.getBuildDiskUsage(true)){
Map<Integer,AbstractBuild> loadedBuilds = project._getRuns().getLoadedBuilds();
AbstractBuild build = loadedBuilds.get(information.getNumber());
//do not calculat builds in progress
if(build!=null && build.isBuilding()){
continue;
}
try{
DiskUsageUtil.calculateDiskUsageForBuild(information.getId(), project);
}
catch(Exception e){
logger.log(Level.WARNING, "Error when recording disk usage for " + project.getName(), e);
}
catch (Exception e) {
logger.log(Level.WARNING, "Error when recording disk usage for " + project.getName(), e);
}
// }
}
// }
}
}
} catch (Exception ex) {
logger.log(Level.WARNING, "Error when recording disk usage for builds", ex);
}
}
else{
else {
DiskUsagePlugin plugin = Jenkins.getInstance().getPlugin(DiskUsagePlugin.class);
if(plugin.getConfiguration().isCalculationBuildsEnabled()){
if(plugin.getConfiguration().isCalculationBuildsEnabled()) {
logger.log(Level.FINER, "Calculation of builds is already in progress.");
}
else{
else {
logger.log(Level.FINER, "Calculation of builds is disabled.");
}
}
}
public CronTab getCronTab() throws ANTLRException{

public CronTab getCronTab() throws ANTLRException {
String cron = Jenkins.getInstance().getPlugin(DiskUsagePlugin.class).getConfiguration().getCountIntervalForBuilds();
CronTab tab = new CronTab(cron);
return tab;
}
}

@Override
public AperiodicWork getNewInstance() {
if(currentTask!=null){
public AperiodicWork getNewInstance() {
if(currentTask != null) {
currentTask.cancel();
}
else{
else {
cancel();
}
currentTask = new BuildDiskUsageCalculationThread();
Expand All @@ -105,12 +105,13 @@ public AperiodicWork getNewInstance() {
public DiskUsageCalculation getLastTask() {
return currentTask;
}
private boolean startExecution(){

private boolean startExecution() {
DiskUsagePlugin plugin = Jenkins.getInstance().getPlugin(DiskUsagePlugin.class);
if(!plugin.getConfiguration().isCalculationBuildsEnabled())
return false;
if(!plugin.getConfiguration().isCalculationBuildsEnabled()) {
return false;
}
return !isExecutingMoreThenOneTimes();
}

}
13 changes: 7 additions & 6 deletions src/main/java/hudson/plugins/disk_usage/DiskUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
*/
@Deprecated
public class DiskUsage {

public DiskUsage() {}

public DiskUsage(Long buildUsage, Long wsUsage){

public DiskUsage() {
}

public DiskUsage(Long buildUsage, Long wsUsage) {
this.buildUsage = buildUsage;
this.wsUsage = wsUsage;
}

Long buildUsage;
Long wsUsage;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author Lucie Votypkova
*/
@Extension
public class DiskUsageBuildActionFactory extends TransientActionFactory<AbstractBuild>{
public class DiskUsageBuildActionFactory extends TransientActionFactory<AbstractBuild> {

@Override
public Class type() {
Expand All @@ -28,6 +28,6 @@ public Class type() {

@Override
public Collection<? extends Action> createFor(AbstractBuild t) {
return new ArrayList<Action>(Collections.singleton(new BuildDiskUsageAction(t)));
return new ArrayList<Action>(Collections.singleton(new BuildDiskUsageAction(t)));
}
}
Loading