Skip to content

Commit

Permalink
Caching update information in reports to increase performance
Browse files Browse the repository at this point in the history
  • Loading branch information
sultan committed Oct 16, 2022
1 parent 608c527 commit 2e1b614
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ protected void renderTableHeaderCells( String... keys )

protected String getLabel( ArtifactVersion version, AbstractVersionDetails details )
{

if ( equals( version, newestUpdateCache.get( details, of( SUBINCREMENTAL ) ) ) )
{
return getText( "report.latestSubIncremental" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class OverviewStats
* @param updates collection of all version updates, typically from
* {@linkplain org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel#getAllUpdates()}
* @param cache if not null, cache to retrieve the version information, initialised with
* the {@link ArtifactVersions#getOldestUpdate(Optional)} update information
* the {@link ArtifactVersions#getNewestUpdate(Optional)} update information
* @param <T> subclass of {@linkplain OverviewStats}
* @param <V> subclass of {@linkplain ArtifactVersions}
* @return instance of the {@linkplain OverviewStats}
Expand All @@ -69,19 +69,19 @@ public static <T extends OverviewStats, V extends ArtifactVersions> T fromUpdate
OverviewStats stats = new OverviewStats();
updates.forEach( details ->
{
if ( getOldestUpdate( cache, details, of( SUBINCREMENTAL ) ) != null )
if ( getNewestUpdate( cache, details, of( SUBINCREMENTAL ) ) != null )
{
stats.incrementAny();
}
else if ( getOldestUpdate( cache, details, of( INCREMENTAL ) ) != null )
else if ( getNewestUpdate( cache, details, of( INCREMENTAL ) ) != null )
{
stats.incrementIncremental();
}
else if ( getOldestUpdate( cache, details, of( MINOR ) ) != null )
else if ( getNewestUpdate( cache, details, of( MINOR ) ) != null )
{
stats.incrementMinor();
}
else if ( getOldestUpdate( cache, details, of( MAJOR ) ) != null )
else if ( getNewestUpdate( cache, details, of( MAJOR ) ) != null )
{
stats.incrementMajor();
}
Expand All @@ -93,11 +93,11 @@ else if ( getOldestUpdate( cache, details, of( MAJOR ) ) != null )
return (T) stats;
}

protected static <V extends ArtifactVersions> ArtifactVersion getOldestUpdate( ArtifactVersionsCache cache,
protected static <V extends ArtifactVersions> ArtifactVersion getNewestUpdate( ArtifactVersionsCache cache,
V details,
Optional<Segment> segment )
Optional<Segment> segment )
{
return cache != null ? cache.get( details, segment ) : details.getOldestUpdate( segment );
return cache != null ? cache.get( details, segment ) : details.getNewestUpdate( segment );
}

public int getMajor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void incrementDependencies()
*
* @param updates collection of all version updates, typically from {@linkplain PluginUpdatesModel#getAllUpdates()}
* @param cache if not null, cache to retrieve the version information, initialised with
* the {@link ArtifactVersions#getOldestUpdate(Optional)} update information
* the {@link ArtifactVersions#getNewestUpdate(Optional)} update information
* @param <T> always equal to {@linkplain PluginOverviewStats}
* @param <V> always equal to {@linkplain org.codehaus.mojo.versions.PluginUpdatesDetails}
* @return instance of the {@linkplain PluginOverviewStats}, initialised with the update information
Expand All @@ -66,19 +66,19 @@ public static <T extends OverviewStats, V extends ArtifactVersions> T fromUpdate
PluginOverviewStats stats = new PluginOverviewStats();
updates.forEach( details ->
{
if ( getOldestUpdate( cache, details, of( SUBINCREMENTAL ) ) != null )
if ( getNewestUpdate( cache, details, of( SUBINCREMENTAL ) ) != null )
{
stats.incrementAny();
}
else if ( getOldestUpdate( cache, details, of( INCREMENTAL ) ) != null )
else if ( getNewestUpdate( cache, details, of( INCREMENTAL ) ) != null )
{
stats.incrementIncremental();
}
else if ( getOldestUpdate( cache, details, of( MINOR ) ) != null )
else if ( getNewestUpdate( cache, details, of( MINOR ) ) != null )
{
stats.incrementMinor();
}
else if ( getOldestUpdate( cache, details, of( MAJOR ) ) != null )
else if ( getNewestUpdate( cache, details, of( MAJOR ) ) != null )
{
stats.incrementMajor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
import org.apache.maven.model.Dependency;
import org.codehaus.mojo.versions.PluginUpdatesDetails;
import org.codehaus.mojo.versions.api.AbstractVersionDetails;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.ArtifactVersionsCache;
import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel;
import org.codehaus.plexus.i18n.I18N;

Expand All @@ -48,8 +46,6 @@
*/
public class PluginUpdatesReportRenderer extends AbstractVersionsReportRenderer<PluginUpdatesModel>
{
protected ArtifactVersionsCache newestUpdateCache
= new ArtifactVersionsCache( AbstractVersionDetails::getNewestUpdate );

public PluginUpdatesReportRenderer( I18N i18n, Sink sink, Locale locale, String bundleName,
PluginUpdatesModel model )
Expand Down Expand Up @@ -157,9 +153,11 @@ protected <T extends OverviewStats> void renderOverviewTableRow( T stats )

protected void renderSummaryTableRow( Dependency artifact, PluginUpdatesDetails details )
{
boolean upToDate = !details.isUpdateAvailable();

sink.tableRow();
sink.tableCell();
if ( !details.isUpdateAvailable() )
if ( upToDate )
{
renderSuccessIcon();
}
Expand Down Expand Up @@ -237,66 +235,69 @@ protected void renderSummaryTableRow( Dependency artifact, PluginUpdatesDetails
}

@SuppressWarnings( "checkstyle:MethodLength" )
private void renderPluginDetail( Dependency artifact, PluginUpdatesDetails plugin )
private void renderPluginDetail( Dependency artifact, PluginUpdatesDetails details )
{
sink.section2();
sink.sectionTitle2();
sink.text( MessageFormat.format( getText( "report.plugin" ),
ArtifactUtils.versionlessKey( plugin.getGroupId(), plugin.getArtifactId() ) ) );
ArtifactUtils.versionlessKey( details.getGroupId(), details.getArtifactId() ) ) );
sink.sectionTitle2_();

renderPluginDetailTable( plugin );
renderPluginDetailTable( details );

if ( !plugin.getDependencyVersions().isEmpty() )
if ( !details.getDependencyVersions().isEmpty() )
{
sink.section3();
sink.sectionTitle3();
sink.text( MessageFormat.format( getText( "report.pluginDependencies" ),
ArtifactUtils.versionlessKey( plugin.getGroupId(), plugin.getArtifactId() ) ) );
ArtifactUtils.versionlessKey( details.getGroupId(), details.getArtifactId() ) ) );
sink.sectionTitle3_();

renderSummaryTable( plugin.getDependencyVersions(), false );
renderSummaryTable( details.getDependencyVersions(), false );

sink.section3_();

plugin.getDependencyVersions().forEach( this::renderDependencyDetail );
details.getDependencyVersions().forEach( this::renderDependencyDetail );
}
sink.section2_();
}

private void renderPluginDetailTable( PluginUpdatesDetails plugin )
private void renderPluginDetailTable( PluginUpdatesDetails details )
{
// warning: using caches here may break plugin report
ArtifactVersion[] allUpdates = details.getAllUpdates( empty() );
boolean upToDate = allUpdates == null || allUpdates.length == 0;

final SinkEventAttributes headerAttributes = new SinkEventAttributeSet();
headerAttributes.addAttribute( SinkEventAttributes.WIDTH, "20%" );
headerAttributes.addAttribute( SinkEventAttributes.WIDTH, "70%" );
final SinkEventAttributes cellAttributes = new SinkEventAttributeSet();
headerAttributes.addAttribute( SinkEventAttributes.WIDTH, "80%" );
headerAttributes.addAttribute( SinkEventAttributes.WIDTH, "30%" );
sink.table();
sink.tableRows( new int[] { Sink.JUSTIFY_RIGHT, Sink.JUSTIFY_LEFT }, false );
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.status" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
ArtifactVersion[] versions = plugin.getAllUpdates( empty() );
if ( plugin.getNewestUpdate( of( SUBINCREMENTAL ) ) != null )
if ( details.getNewestUpdate( of( SUBINCREMENTAL ) ) != null )
{
renderWarningIcon();
sink.nonBreakingSpace();
sink.text( getText( "report.otherUpdatesAvailable" ) );
}
else if ( plugin.getNewestUpdate( of( INCREMENTAL ) ) != null )
else if ( details.getNewestUpdate( of( INCREMENTAL ) ) != null )
{
renderWarningIcon();
sink.nonBreakingSpace();
sink.text( getText( "report.incrementalUpdatesAvailable" ) );
}
else if ( plugin.getNewestUpdate( of( MINOR ) ) != null )
else if ( details.getNewestUpdate( of( MINOR ) ) != null )
{
renderWarningIcon();
sink.nonBreakingSpace();
sink.text( getText( "report.minorUpdatesAvailable" ) );
}
else if ( plugin.getNewestUpdate( of( MAJOR ) ) != null )
else if ( details.getNewestUpdate( of( MAJOR ) ) != null )
{
renderWarningIcon();
sink.nonBreakingSpace();
Expand All @@ -315,44 +316,44 @@ else if ( plugin.getNewestUpdate( of( MAJOR ) ) != null )
sink.text( getText( "report.groupId" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
sink.text( plugin.getGroupId() );
sink.text( details.getGroupId() );
sink.tableCell_();
sink.tableRow_();
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.artifactId" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
sink.text( plugin.getArtifactId() );
sink.text( details.getArtifactId() );
sink.tableCell_();
sink.tableRow_();
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.currentVersion" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
sink.text( plugin.getVersion() );
sink.text( details.getVersion() );
sink.tableCell_();
sink.tableRow_();
if ( versions != null && versions.length > 0 )
if ( !upToDate )
{
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.updateVersions" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
for ( int i = 0; i < versions.length; i++ )
for ( int i = 0; i < allUpdates.length; i++ )
{
if ( i > 0 )
{
sink.lineBreak();
}
String label = getLabel( versions[i], plugin );
String label = getLabel( allUpdates[i], details );
if ( label != null )
{
safeBold();
}
sink.text( versions[i].toString() );
sink.text( allUpdates[i].toString() );
if ( label != null )
{
safeBold_();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ else if ( newestUpdateCache.get( details, of( MAJOR ) ) != null )
sink.tableRow_();
if ( !upToDate )
{
Set<String> rangeVersions = getVersionsInRange( property, details, allUpdates );
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.updateVersions" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
Set<String> rangeVersions = getVersionsInRange( property, details, allUpdates );
boolean someNotAllowed = false;
for ( int i = 0; i < allUpdates.length; i++ )
{
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public class DependencyUpdatesXmlReportRenderer implements ReportRenderer
{
private final DependencyUpdatesModel model;
private final Path outputFile;
private final ArtifactVersionsCache oldestUpdateCache
= new ArtifactVersionsCache( AbstractVersionDetails::getOldestUpdate );
private final ArtifactVersionsCache newestUpdateCache
= new ArtifactVersionsCache( AbstractVersionDetails::getNewestUpdate );
/**
* Creates a new instance
* @param model object containing the updates model
Expand All @@ -89,7 +89,7 @@ public void render()
setSummary( new DependencyReportSummary()
{{
OverviewStats overviewStats = OverviewStats.fromUpdates( model.getAllUpdates().values(),
oldestUpdateCache );
newestUpdateCache );
setUsingLastVersion( String.valueOf( overviewStats.getUpToDate() ) );
setNextVersionAvailable( String.valueOf( overviewStats.getAny() ) );
setNextIncrementalAvailable( String.valueOf( overviewStats.getIncremental() ) );
Expand Down Expand Up @@ -127,14 +127,14 @@ private static List<DependencyInfo> createDependencyInfo( Map<Dependency, Artifa
setType( e.getKey().getType() );
setClassifier( e.getKey().getClassifier() );

ofNullable( e.getValue().getOldestUpdate( empty() ) )
.map( ArtifactVersion::toString ).ifPresent( this::setNextVersion );
ofNullable( e.getValue().getNewestUpdate( empty() ) )
.map( ArtifactVersion::toString ).ifPresent( this::setLastVersion );

setSection( e.getValue(), INCREMENTAL, this::setIncrementals );
setSection( e.getValue(), MINOR, this::setMinors );
setSection( e.getValue(), MAJOR, this::setMajors );

setStatus( getNextVersion() == null
setStatus( getLastVersion() == null
? "no new available"
: getIncrementals() != null
? "incremental available"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public class PluginUpdatesXmlReportRenderer implements ReportRenderer
{
private final PluginUpdatesModel model;
private final Path outputFile;
private final ArtifactVersionsCache oldestUpdateCache
= new ArtifactVersionsCache( AbstractVersionDetails::getOldestUpdate );
private final ArtifactVersionsCache newestUpdateCache
= new ArtifactVersionsCache( AbstractVersionDetails::getNewestUpdate );
/**
* Creates a new instance
* @param model object containing the updates model
Expand All @@ -90,7 +90,7 @@ public void render()
setSummary( new PluginReportSummary()
{{
PluginOverviewStats overviewStats = PluginOverviewStats.fromUpdates(
model.getAllUpdates().values(), oldestUpdateCache );
model.getAllUpdates().values(), newestUpdateCache );
setUsingLastVersion( String.valueOf( overviewStats.getUpToDate() ) );
setNextVersionAvailable( String.valueOf( overviewStats.getAny() ) );
setNextIncrementalAvailable( String.valueOf( overviewStats.getIncremental() ) );
Expand Down Expand Up @@ -129,14 +129,14 @@ private static List<PluginInfo> createPluginInfo( Map<Dependency, PluginUpdatesD
setType( e.getKey().getType() );
setClassifier( e.getKey().getClassifier() );

ofNullable( e.getValue().getOldestUpdate( empty() ) )
.map( ArtifactVersion::toString ).ifPresent( this::setNextVersion );
ofNullable( e.getValue().getNewestUpdate( empty() ) )
.map( ArtifactVersion::toString ).ifPresent( this::setLastVersion );

setSection( e.getValue(), INCREMENTAL, this::setIncrementals );
setSection( e.getValue(), MINOR, this::setMinors );
setSection( e.getValue(), MAJOR, this::setMajors );

setStatus( getNextVersion() == null
setStatus( getLastVersion() == null
? "no new available"
: getIncrementals() != null
? "incremental available"
Expand Down
2 changes: 1 addition & 1 deletion src/main/mdo/dependency-updates-report.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ under the License.
<type>String</type>
</field>
<field>
<name>nextVersion</name>
<name>lastVersion</name>
<type>String</type>
</field>
<field>
Expand Down
2 changes: 1 addition & 1 deletion src/main/mdo/plugin-updates-report.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ under the License.
<type>String</type>
</field>
<field>
<name>nextVersion</name>
<name>lastVersion</name>
<type>String</type>
</field>
<field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testReportGeneration() throws IOException
assertThat( output, containsString( "<nextMajorAvailable>0</nextMajorAvailable>" ) );

assertThat( output, containsString( "<currentVersion>1.0.0</currentVersion>" ) );
assertThat( output, containsString( "<nextVersion>1.0.1</nextVersion>" ) );
assertThat( output, containsString( "<lastVersion>1.0.1</lastVersion>" ) );
assertThat( output, containsString( "<incremental>1.0.1</incremental>" ) );
assertThat( output, containsString( "<minor>1.1.0</minor>" ) );
assertThat( output, containsString( "<major>2.0.0</major>" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void testReportGeneration() throws IOException
assertThat( output, containsString( "<dependencyUpdates>1</dependencyUpdates>" ) );

assertThat( output, containsString( "<currentVersion>1.0.0</currentVersion>" ) );
assertThat( output, containsString( "<nextVersion>1.0.1</nextVersion>" ) );
assertThat( output, containsString( "<lastVersion>2.0.0</lastVersion>" ) );
assertThat( output, containsString( "<incremental>1.0.1</incremental>" ) );
assertThat( output, containsString( "<minor>1.1.0</minor>" ) );
assertThat( output, containsString( "<major>2.0.0</major>" ) );
Expand Down

0 comments on commit 2e1b614

Please sign in to comment.