-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Adds ProgressDisplayGenerator. #1333
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
...mon/src/main/java/com/google/cloud/tools/jib/plugins/common/ProgressDisplayGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright 2018 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.cloud.tools.jib.plugins.common; | ||
|
||
import com.google.cloud.tools.jib.event.progress.Allocation; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* Generates a display of progress and unfinished tasks. | ||
* | ||
* <p>Example: | ||
* | ||
* <p>Executing tasks...<br> | ||
* [================= ] 72.5% complete<br> | ||
* > task 1 running<br> | ||
* > task 3 running | ||
*/ | ||
class ProgressDisplayGenerator { | ||
|
||
/** Line above progress bar. */ | ||
private static final String HEADER = "Executing tasks:"; | ||
|
||
/** Maximum number of bars in the progress display. */ | ||
private static final int PROGRESS_BAR_COUNT = 50; | ||
|
||
/** | ||
* Generates a progress display. | ||
* | ||
* @param progress the overall progress, with {@code 1.0} meaning fully complete | ||
* @param unfinishedAllocations the unfinished {@link Allocation}s | ||
* @return the progress display as a list of lines | ||
*/ | ||
static List<String> generateProgressDisplay( | ||
double progress, List<Allocation> unfinishedAllocations) { | ||
List<String> lines = new ArrayList<>(); | ||
|
||
lines.add(HEADER); | ||
lines.add(generateProgressBar(progress)); | ||
lines.addAll(generateUnfinishedTasks(unfinishedAllocations)); | ||
|
||
return lines; | ||
} | ||
|
||
/** | ||
* Generates the progress bar line. | ||
* | ||
* @param progress the overall progress, with {@code 1.0} meaning fully complete | ||
* @return the progress bar line | ||
*/ | ||
private static String generateProgressBar(double progress) { | ||
StringBuilder progressBar = new StringBuilder(); | ||
progressBar.append('['); | ||
|
||
int barsToDisplay = (int) Math.round(PROGRESS_BAR_COUNT * progress); | ||
for (int barIndex = 0; barIndex < PROGRESS_BAR_COUNT; barIndex++) { | ||
progressBar.append(barIndex < barsToDisplay ? '=' : ' '); | ||
} | ||
|
||
return progressBar | ||
.append(']') | ||
.append(String.format(" %.1f", progress * 100)) | ||
.append("% complete") | ||
.toString(); | ||
} | ||
|
||
/** | ||
* Generates the display of the unfinished tasks from a list of unfinished {@link Allocation}s | ||
* | ||
* @param unfinishedAllocations the list of unfinished {@link Allocation}s | ||
* @return the display of the unfinished {@link Allocation}s | ||
*/ | ||
private static List<String> generateUnfinishedTasks(List<Allocation> unfinishedAllocations) { | ||
List<String> lines = new ArrayList<>(); | ||
for (Allocation unfinishedAllocation : getLeafAllocations(unfinishedAllocations)) { | ||
lines.add("> " + unfinishedAllocation.getDescription()); | ||
} | ||
return lines; | ||
} | ||
|
||
/** | ||
* Gets a list of just the leaf {@link Allocation}s in {@code unfinishedAllocations} in the same | ||
* order as they appear in {@code unfinishedAllocations}. | ||
* | ||
* @param unfinishedAllocations the list of unfinished {@link Allocation}s | ||
* @return the list of unfinished {@link Allocation}s | ||
*/ | ||
// TODO: Optimization: Change AllocationCompletionTracker#getUnfinishedAllocations to only return | ||
// leaf Allocations so that this computation is unnecessary. | ||
private static List<Allocation> getLeafAllocations(List<Allocation> unfinishedAllocations) { | ||
// Prunes the set of all unfinished Allocations to leave just the leaves. | ||
Set<Allocation> leafAllocationSet = new HashSet<>(unfinishedAllocations); | ||
for (Allocation allocation : unfinishedAllocations) { | ||
Optional<Allocation> parent = allocation.getParent(); | ||
|
||
while (parent.isPresent()) { | ||
leafAllocationSet.remove(parent.get()); | ||
parent = parent.get().getParent(); | ||
} | ||
} | ||
|
||
// Makes a list of leaf allocations in the same order as the unfinishedAllocations. | ||
List<Allocation> leafAllocations = new ArrayList<>(); | ||
for (Allocation allocation : unfinishedAllocations) { | ||
if (leafAllocationSet.contains(allocation)) { | ||
leafAllocations.add(allocation); | ||
} | ||
} | ||
return leafAllocations; | ||
} | ||
|
||
private ProgressDisplayGenerator() {} | ||
} |
71 changes: 71 additions & 0 deletions
71
...src/test/java/com/google/cloud/tools/jib/plugins/common/ProgressDisplayGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2018 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.cloud.tools.jib.plugins.common; | ||
|
||
import com.google.cloud.tools.jib.event.progress.Allocation; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** Tests for {@link ProgressDisplayGenerator}. */ | ||
public class ProgressDisplayGeneratorTest { | ||
|
||
@Test | ||
public void testGenerateProgressDisplay_progressBar_0() { | ||
Assert.assertEquals( | ||
Arrays.asList( | ||
"Executing tasks:", | ||
"[ ] 0.0% complete"), | ||
ProgressDisplayGenerator.generateProgressDisplay(0, Collections.emptyList())); | ||
} | ||
|
||
@Test | ||
public void testGenerateProgressDisplay_progressBar_50() { | ||
Assert.assertEquals( | ||
Arrays.asList( | ||
"Executing tasks:", | ||
"[========================= ] 50.0% complete"), | ||
ProgressDisplayGenerator.generateProgressDisplay(0.5, Collections.emptyList())); | ||
} | ||
|
||
@Test | ||
public void testGenerateProgressDisplay_progressBar_100() { | ||
Assert.assertEquals( | ||
Arrays.asList( | ||
"Executing tasks:", | ||
"[==================================================] 100.0% complete"), | ||
ProgressDisplayGenerator.generateProgressDisplay(1, Collections.emptyList())); | ||
} | ||
|
||
@Test | ||
public void testGenerateProgressDisplay_unfinishedTasks() { | ||
Allocation root = Allocation.newRoot("does not display", 2); | ||
Allocation childLeft = root.newChild("does not display", 2); | ||
Allocation childLeftDown = childLeft.newChild("childLeftDown", 2); | ||
Allocation childRight = root.newChild("childRight", 2); | ||
|
||
Assert.assertEquals( | ||
Arrays.asList( | ||
"Executing tasks:", | ||
"[========================= ] 50.0% complete", | ||
"> childLeftDown", | ||
"> childRight"), | ||
ProgressDisplayGenerator.generateProgressDisplay( | ||
0.5, Arrays.asList(root, childLeft, childLeftDown, childRight))); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... looks like a caller should enumerate all unfinished allocations. So I think the caller will go over every node in the tree and identify which node is unfinished. Knowing that, is it not easy for the caller to identify leaves and pass only leaves?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The caller would be the one that gets the unfinished allocations from
ProgressEventHandler#getUnfinishedAllocations
, which gets it fromAllocationCompletionTracker
. I believe we could haveAllocationCompletionTracker
just give back the leaf allocations so that the set computation can be optimized away. I'll make a TODO for this optimization.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that'd be nice.