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

Adds ProgressDisplayGenerator. #1333

Merged
merged 2 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,125 @@
/*
* 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... [================= ] 72.5% complete > task 1 running > task 3 running
coollog marked this conversation as resolved.
Show resolved Hide resolved
*/
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
*/
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() {}
}
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)));
Copy link
Member

@chanseokoh chanseokoh Dec 10, 2018

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?

Copy link
Contributor Author

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 from AllocationCompletionTracker. I believe we could have AllocationCompletionTracker just give back the leaf allocations so that the set computation can be optimized away. I'll make a TODO for this optimization.

Copy link
Member

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.

}
}