Skip to content

Commit

Permalink
Load report renderers/senders from config (#215)
Browse files Browse the repository at this point in the history
* add code to look up Renderers and pass results to Senders

* wip

* improve documentation

* remove unnecessary inner class

* add mimeType() method to RenderedReport

* remove architectural-playground comment and unused class

* put type bounds on Renderer type arguments

* update existing Job implementations to insert Injectors instead of ActorRefs

* coalesce Senders and Renderers classes into ReportExecution; add methods&impl for RenderedReport; send to each Recipient separately

* move getMimeType() method from RenderedReport to ReportFormat

* javadoc

* fix javadoc error

* do not expose raw byte[] from RenderedReport

* findbugs

* add equals/hashCode/toString to some classes

* make ReportExecution.execute throw IllegalArgumentException if it can't find a Renderer/Sender

* fix case-conversion of enum names

* add ReportExecutionTest

* ReportExecution.execute: return new DefaultReportResult

* use ConcurrentMap instead of Map when populating format->report map

* eliminate one-line render() method

* javadoc

* tweak a couple toString() methods

* rename parameter to Sender.send

* nits

* give RecipientType a name field instead of using CaseFormat conversion

* checkstyle

* ReportExecution.execute only throws asynchronously

* use mimetype for format-classification; add ReportSource.getTypeName

* typo

* remove RecipientType.getName() in favor of .name()

* make ReportSource.Visitor an abstract class instead of an interface

* findbugs

* findbugs

* fix SuppressFBWarnings

* merge conflicts

* checkstyle

* fixup

* add config

* wip -- tests, bugfix

* partial rebase

* bugfix

* bugfix

* use Jackson instead of Guice and everything is beautiful

* findbugs, checkstyle

* small pr comments

* small pr comments

* small pr comments

* refactor, fix tests

* fixup

* undo unnecessary changes / Guicing

* delete old version of renamed file

* fixup

* checkstyle

* implement classes referred to by config

* findbugs

* guice

* back to Guice

* checkstyle

* remove unused Jackson annotations

* comment

* make it okay if config has no reports section

* cleanup

* comment

* remove unnecessary Guice registrations

* rename config section reports->reporting

* make SourceType an enum

* checkstyle

* findbugs
  • Loading branch information
speezepearson authored and vjkoskela committed May 31, 2019
1 parent 3a58754 commit f837c95
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@

package com.arpnetworking.metrics.portal.reports;

import com.arpnetworking.play.configuration.ConfigurationHelper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Names;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigObject;
import models.internal.impl.DefaultReportResult;
import models.internal.reports.Recipient;
import models.internal.reports.Report;
import models.internal.reports.ReportFormat;
import models.internal.reports.ReportSource;
import play.Environment;

import java.time.Instant;
import java.util.Collection;
Expand All @@ -42,7 +45,7 @@
*
* @author Spencer Pearson (spencerpearson at dropbox dot com)
*/
public final class ReportExecution {
public final class ReportExecutionContext {

/**
* Render and send a report.
Expand All @@ -51,11 +54,10 @@ public final class ReportExecution {
* and then sends each recipient their requested formats of {@link RenderedReport} (using the Injector to look up the {@link Sender}s).
*
* @param report The report to render and execute.
* @param injector A Guice injector to pull dependencies out of.
* @param scheduled The instant the report was scheduled for.
* @return A CompletionStage that completes when sending has completed for every recipient.
*/
public static CompletionStage<Report.Result> execute(final Report report, final Injector injector, final Instant scheduled) {
public CompletionStage<Report.Result> execute(final Report report, final Instant scheduled) {
final ImmutableMultimap<ReportFormat, Recipient> formatToRecipients = report.getRecipientsByFormat()
.entrySet()
.stream()
Expand All @@ -65,12 +67,12 @@ public static CompletionStage<Report.Result> execute(final Report report, final
final ImmutableMultimap<Recipient, ReportFormat> recipientToFormats = formatToRecipients.inverse();

return CompletableFuture.completedFuture(null).thenApply(nothing -> {
verifyDependencies(report, injector);
verifyDependencies(report);
return null;
}).thenCompose(nothing ->
renderAll(injector, formatToRecipients.keySet(), report.getSource(), scheduled)
renderAll(formatToRecipients.keySet(), report.getSource(), scheduled)
).thenCompose(formatToRendered ->
sendAll(injector, recipientToFormats, formatToRendered)
sendAll(recipientToFormats, formatToRendered)
).thenApply(nothing ->
new DefaultReportResult()
);
Expand All @@ -81,15 +83,15 @@ public static CompletionStage<Report.Result> execute(final Report report, final
*
* @throws IllegalArgumentException if anything is missing.
*/
/* package private */ static void verifyDependencies(final Report report, final Injector injector) {
/* package private */ void verifyDependencies(final Report report) {
for (final ReportFormat format : report.getRecipientsByFormat().keySet()) {
getRenderer(injector, report.getSource(), format);
getRenderer(report.getSource(), format);
}
final Collection<Recipient> allRecipients = report.getRecipientsByFormat().values().stream()
.flatMap(Collection::stream)
.collect(Collectors.toSet());
for (final Recipient recipient : allRecipients) {
getSender(injector, recipient);
getSender(recipient);
}
}

Expand All @@ -99,16 +101,15 @@ public static CompletionStage<Report.Result> execute(final Report report, final
* @return a CompletionStage mapping each given format to its RenderedReport. Completes when every render has finished.
* @throws IllegalArgumentException if any necessary {@link Renderer} is missing from the given Injector.
*/
/* package private */ static CompletionStage<ImmutableMap<ReportFormat, RenderedReport>> renderAll(
final Injector injector,
/* package private */ CompletionStage<ImmutableMap<ReportFormat, RenderedReport>> renderAll(
final ImmutableSet<ReportFormat> formats,
final ReportSource source,
final Instant scheduled
) {
final Map<ReportFormat, RenderedReport> result = Maps.newConcurrentMap();
final CompletableFuture<?>[] resultSettingFutures = formats
.stream()
.map(format -> getRenderer(injector, source, format)
.map(format -> getRenderer(source, format)
.render(source, format, scheduled)
.thenApply(rendered -> result.put(format, rendered))
.toCompletableFuture())
Expand All @@ -125,47 +126,43 @@ public static CompletionStage<Report.Result> execute(final Report report, final
* @return a CompletionStage that completes when every send has finished.
* @throws IllegalArgumentException if any necessary {@link Sender} is missing from the given Injector.
*/
/* package private */ static CompletionStage<Void> sendAll(
final Injector injector,
/* package private */ CompletionStage<Void> sendAll(
final ImmutableMultimap<Recipient, ReportFormat> recipientToFormats,
final ImmutableMap<ReportFormat, RenderedReport> formatToRendered
) {
final CompletableFuture<?>[] futures = recipientToFormats
.asMap()
.entrySet()
.stream()
.map(entry -> getSender(injector, entry.getKey())
.map(entry -> getSender(entry.getKey())
.send(entry.getKey(), mask(formatToRendered, entry.getValue()))
.toCompletableFuture())
.toArray(CompletableFuture[]::new);
return CompletableFuture.allOf(futures);
}

private static <S extends ReportSource, F extends ReportFormat> Renderer<S, F> getRenderer(
final Injector injector,
private <S extends ReportSource, F extends ReportFormat> Renderer<S, F> getRenderer(
final S source,
final F format
) {
final String keyName = getRendererKeyName(source, format);
@SuppressWarnings("unchecked")
final Renderer<S, F> renderer = injector.getInstance(Key.get(Renderer.class, Names.named(keyName)));
if (renderer == null) {
throw new IllegalArgumentException("no Renderer exists for key name '" + keyName + "'");
final Renderer<S, F> result = _renderers.getOrDefault(source.getType(), ImmutableMap.of()).get(format.getMimeType());
if (result == null) {
throw new IllegalArgumentException(
"no Renderer exists for source type " + source.getType() + ", MIME type " + format.getMimeType()
);
}
return renderer;
return result;
}

private static Sender getSender(final Injector injector, final Recipient recipient) {
final String keyName = recipient.getType().name();
final Sender sender = injector.getInstance(Key.get(Sender.class, Names.named(keyName)));
if (sender == null) {
throw new IllegalArgumentException("no Sender exists for key name '" + keyName + "'");
private Sender getSender(final Recipient recipient) {
final Sender result = _senders.get(recipient.getType());
if (result == null) {
throw new IllegalArgumentException(
"no Sender exists for recipient type " + recipient.getType()
);
}
return sender;
}

/* package private */ static String getRendererKeyName(final ReportSource source, final ReportFormat format) {
return source.getTypeName() + " " + format.getMimeType();
return result;
}

/**
Expand All @@ -175,5 +172,67 @@ private static <K, V> ImmutableMap<K, V> mask(final ImmutableMap<K, V> map, fina
return keys.stream().collect(ImmutableMap.toImmutableMap(key -> key, map::get));
}

private ReportExecution() {}
/**
* Public constructor.
*
* @param injector Guice Injector to load the classes specified in the config.
* @param environment Environment used to load the classes specified in the config.
* @param config Config to identify {@link Renderer}s / {@link Sender}s / other necessary objects for report execution.
*/
@Inject
public ReportExecutionContext(final Injector injector, final Environment environment, final Config config) {
if (config.hasPath("reporting")) {
_renderers = this.<Renderer>loadMapMapObject(injector, environment, config.getObject("reporting.renderers"))
.entrySet()
.stream()
.collect(ImmutableMap.toImmutableMap(
e -> SourceType.valueOf(e.getKey()),
Map.Entry::getValue
));
_senders = this.<Sender>loadMapObject(injector, environment, config.getObject("reporting.senders"))
.entrySet()
.stream()
.collect(ImmutableMap.toImmutableMap(
e -> RecipientType.valueOf(e.getKey()),
Map.Entry::getValue
));
} else {
_renderers = ImmutableMap.of();
_senders = ImmutableMap.of();
}
}

/**
* Instantiates a POJO from a ConfigObject specification like {@code {type: "com.foo..."}}.
* Someday, will probably allow the ConfigObject to specify other parameters too, and use reflective Builder magic
* to plumb them into the instantiated object.
*/
private <T> T loadObject(final Injector injector, final Environment environment, final ConfigObject config) {
final Class<? extends T> senderClass = ConfigurationHelper.getType(environment, config.toConfig(), "type");
return injector.getInstance(senderClass);
}
private <T> ImmutableMap<String, T> loadMapObject(
final Injector injector,
final Environment environment,
final ConfigObject config
) {
return config.entrySet().stream().collect(ImmutableMap.toImmutableMap(
Map.Entry::getKey,
e -> loadObject(injector, environment, (ConfigObject) e.getValue())
));
}
private <T> ImmutableMap<String, ImmutableMap<String, T>> loadMapMapObject(
final Injector injector,
final Environment environment,
final ConfigObject config
) {
return config.entrySet().stream().collect(ImmutableMap.toImmutableMap(
Map.Entry::getKey,
e -> loadMapObject(injector, environment, (ConfigObject) e.getValue())
));
}

private final ImmutableMap<SourceType, ImmutableMap<String, Renderer>> _renderers;
private final ImmutableMap<RecipientType, Sender> _senders;

}
29 changes: 29 additions & 0 deletions app/com/arpnetworking/metrics/portal/reports/SourceType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2019 Dropbox, Inc.
*
* 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.arpnetworking.metrics.portal.reports;

/**
* Mechanisms by which reports can be generated.
*
* @author Spencer Pearson (spencerpearson at dropbox dot com)
*/
public enum SourceType {
/**
* A report generated from a web page.
*/
WEB_PAGE,
}
43 changes: 43 additions & 0 deletions app/com/arpnetworking/metrics/portal/reports/impl/EmailSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2019 Dropbox, Inc.
*
* 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.arpnetworking.metrics.portal.reports.impl;

import com.arpnetworking.metrics.portal.reports.RenderedReport;
import com.arpnetworking.metrics.portal.reports.Sender;
import com.google.common.collect.ImmutableMap;
import models.internal.reports.Recipient;
import models.internal.reports.ReportFormat;

import java.util.concurrent.CompletionStage;

/**
* Sends reports over email.
*
* @author Spencer Pearson (spencerpearson at dropbox dot com)
*/
public class EmailSender implements Sender {
@Override
public CompletionStage<Void> send(
final Recipient recipient,
final ImmutableMap<ReportFormat, RenderedReport> formatsToSend
) {
throw new RuntimeException();
}

EmailSender() {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019 Dropbox, Inc.
*
* 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.arpnetworking.metrics.portal.reports.impl.chrome;

import com.arpnetworking.metrics.portal.reports.RenderedReport;
import com.arpnetworking.metrics.portal.reports.Renderer;
import models.internal.impl.DefaultRenderedReport;
import models.internal.impl.WebPageReportSource;
import models.internal.reports.ReportFormat;

import java.time.Instant;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

/**
* Common code for renderers that use Chrome to load and scrape pages.
*
* @author Spencer Pearson (spencerpearson at dropbox dot com)
*/
/* package private */ abstract class BaseScreenshotRenderer<F extends ReportFormat> implements Renderer<WebPageReportSource, F> {

protected abstract byte[] getPageContent(WebPageReportSource source, F format, Object todo);

@Override
public CompletionStage<RenderedReport> render(
final WebPageReportSource source,
final F format,
final Instant scheduled
) {
return CompletableFuture.completedFuture(new DefaultRenderedReport.Builder()
.setScheduledFor(scheduled)
.setGeneratedAt(Instant.now())
.setFormat(format)
.build() // TODO(spencerpearson)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2019 Dropbox, Inc.
*
* 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.arpnetworking.metrics.portal.reports.impl.chrome;

import models.internal.impl.HtmlReportFormat;
import models.internal.impl.WebPageReportSource;

/**
* Uses a headless Chrome instance to capture a page.
*
* @author Spencer Pearson (spencerpearson at dropbox dot com)
*/
public final class HtmlScreenshotRenderer extends BaseScreenshotRenderer<HtmlReportFormat> {
@Override
protected byte[] getPageContent(final WebPageReportSource source, final HtmlReportFormat format, final Object todo) {
return new byte[0]; // TODO(spencerpearson)
}
}
Loading

0 comments on commit f837c95

Please sign in to comment.