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
8 changed files
with
139 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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/run/halo/umami/RssTelemetryConfiguration.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,21 @@ | ||
package run.halo.umami; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import run.halo.app.infra.ExternalUrlSupplier; | ||
import run.halo.app.plugin.ReactiveSettingFetcher; | ||
|
||
@Configuration | ||
@ConditionalOnClass(name = "run.halo.feed.TelemetryRecorder") | ||
@RequiredArgsConstructor | ||
public class RssTelemetryConfiguration { | ||
private final ExternalUrlSupplier externalUrlSupplier; | ||
private final ReactiveSettingFetcher settingFetcher; | ||
|
||
@Bean | ||
RssTelemetryUmamiRecorder rssTelemetryUmamiRecorder() { | ||
return new RssTelemetryUmamiRecorder(settingFetcher, externalUrlSupplier); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
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,79 @@ | ||
package run.halo.umami; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
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.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static org.apache.commons.lang3.StringUtils.defaultIfBlank; | ||
|
||
@Slf4j | ||
@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.isBlank(webSiteId)) { | ||
return; | ||
} | ||
// https://umami.is/docs/api/sending-stats | ||
webClient.post() | ||
.uri(StringUtils.removeEnd(siteUrl, "/") + "/api/send") | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.header(HttpHeaders.USER_AGENT, genUserAgent()) | ||
.body(Mono.just(createBody(webSiteId, eventInfo)), Map.class) | ||
.retrieve() | ||
.bodyToMono(String.class) | ||
.doOnNext(response -> log.debug("Umami send response: {}", response)) | ||
.doOnError(e -> log.debug("Failed to send telemetry event to Umami.", e)) | ||
.block(); | ||
} | ||
|
||
private String genUserAgent() { | ||
// umami has bot detection to avoid filtering page views | ||
return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) " | ||
+ "Chrome/131.0.0.0 Safari/537.36"; | ||
} | ||
|
||
private Map<String, Object> createBody(String webSiteId, TelemetryEventInfo eventInfo) { | ||
var hostname = Optional.ofNullable(externalUrlSupplier.getRaw()) | ||
.map(URL::getHost) | ||
.orElse(StringUtils.EMPTY); | ||
var payload = new HashMap<>(Map.of( | ||
"hostname", hostname, | ||
"language", defaultIfBlank(eventInfo.getLanguageRegion(), Locale.CHINESE.toLanguageTag()), | ||
"referrer", StringUtils.defaultString(eventInfo.getReferrer()), | ||
"screen", StringUtils.defaultString(eventInfo.getScreen()), | ||
"title", eventInfo.getTitle(), | ||
"url", eventInfo.getPageUrl(), | ||
"website", webSiteId, | ||
"tag", "RSS Telemetry" | ||
)); | ||
if (StringUtils.isNotBlank(eventInfo.getIp())) { | ||
payload.put("ip", eventInfo.getIp()); | ||
} | ||
return Map.of("payload", payload, "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: plugin.halo.run/v1alpha1 | ||
kind: ExtensionDefinition | ||
metadata: | ||
name: feed-rss-telemetry-umami-recorder | ||
spec: | ||
className: run.halo.umami.RssTelemetryUmamiRecorder | ||
extensionPointName: feed-telemetry-recorder | ||
displayName: "Umami RSS 访问量记录器" | ||
description: "将 RSS 的内容访问量上报到 Umami" |
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