generated from halo-dev/plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rss telemetry recorder implementation
- Loading branch information
Showing
6 changed files
with
90 additions
and
17 deletions.
There are no files selected for viewing
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
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
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,17 @@ | ||
package run.halo.umami; | ||
|
||
import lombok.Data; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.plugin.ReactiveSettingFetcher; | ||
|
||
@Data | ||
public class BasicProp { | ||
private String websiteId; | ||
private String endpoint; | ||
private String scriptName; | ||
private String url; | ||
|
||
public static Mono<BasicProp> fetch(ReactiveSettingFetcher settingFetcher) { | ||
return settingFetcher.fetch("basic", BasicProp.class); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/run/halo/umami/RssTelemetryUmamiRecorder.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,63 @@ | ||
package run.halo.umami; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.infra.ExternalUrlSupplier; | ||
import run.halo.app.plugin.ReactiveSettingFetcher; | ||
import run.halo.feed.TelemetryEventInfo; | ||
import run.halo.feed.TelemetryRecorder; | ||
|
||
import java.net.URL; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
@RequiredArgsConstructor | ||
public class RssTelemetryUmamiRecorder implements TelemetryRecorder { | ||
private final WebClient webClient = WebClient.builder().build(); | ||
private final ReactiveSettingFetcher settingFetcher; | ||
private final ExternalUrlSupplier externalUrlSupplier; | ||
|
||
@Override | ||
public void record(TelemetryEventInfo eventInfo) { | ||
var propOpt = BasicProp.fetch(settingFetcher).blockOptional(); | ||
if (propOpt.isEmpty()) { | ||
return; | ||
} | ||
var siteUrl = propOpt.get().getEndpoint(); | ||
var webSiteId = propOpt.get().getWebsiteId(); | ||
if (StringUtils.isBlank(siteUrl) || StringUtils.isNotBlank(webSiteId)) { | ||
return; | ||
} | ||
|
||
// https://umami.is/docs/api/sending-stats | ||
webClient.post() | ||
.uri(StringUtils.removeEnd(siteUrl, "/") + "/api/send") | ||
.body(Mono.just(createBody(webSiteId, eventInfo)), Map.class) | ||
.retrieve() | ||
.bodyToMono(String.class) | ||
.block(); | ||
} | ||
|
||
private Map<String, Object> createBody(String webSiteId, TelemetryEventInfo eventInfo) { | ||
var hostname = Optional.ofNullable(externalUrlSupplier.getRaw()) | ||
.map(URL::getAuthority) | ||
.orElse(StringUtils.EMPTY); | ||
|
||
return Map.of("payload", Map.of( | ||
"hostname", hostname, | ||
"language", eventInfo.getLanguage(), | ||
"referrer", eventInfo.getReferrer(), | ||
"screen", eventInfo.getScreen(), | ||
"title", eventInfo.getTitle(), | ||
"url", eventInfo.getPageUrl(), | ||
"website", webSiteId, | ||
"name", "Rss Reader" | ||
), | ||
"type", "event" | ||
); | ||
} | ||
} |
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
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