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
183 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); | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
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,122 @@ | ||
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; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class RssTelemetryUmamiRecorder implements TelemetryRecorder { | ||
static final Map<String, String> LANG_TO_COUNTRY = Map.of( | ||
"zh", "CN", | ||
"en", "US", | ||
"ja", "JP", | ||
"ko", "KR", | ||
"fr", "FR", | ||
"de", "DE", | ||
"es", "ES", | ||
"it", "IT", | ||
"ru", "RU" | ||
); | ||
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) | ||
.doOnError(e -> log.debug("Failed to send telemetry event to Umami.", e)) | ||
.block(); | ||
} | ||
|
||
private String genUserAgent() { | ||
// umami has bot detection to avoid filtering out 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", convertToLanguageTag(eventInfo.getLanguage()), | ||
"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"); | ||
} | ||
|
||
private static String convertToLanguageTag(String languageCode) { | ||
try { | ||
return convertToLanguageTagInternal(languageCode); | ||
} catch (Throwable e) { | ||
// ignore | ||
return Locale.CHINA.toLanguageTag(); | ||
} | ||
} | ||
|
||
/** | ||
* <p>Convert language code to language code with region.</p> | ||
* | ||
* @param languageCode original code (such as zh or en) | ||
* @return Standardized language code (such as zh-CN or en-US) | ||
*/ | ||
private static String convertToLanguageTagInternal(String languageCode) { | ||
if (languageCode == null || languageCode.isEmpty()) { | ||
return Locale.CHINA.toLanguageTag(); | ||
} | ||
|
||
Locale locale = Locale.forLanguageTag(languageCode); | ||
|
||
// if locale has no country, fill it with the default country | ||
if (locale.getCountry().isEmpty()) { | ||
var country = LANG_TO_COUNTRY.get(locale.getLanguage()); | ||
if (country == null) { | ||
return Locale.CHINA.toLanguageTag(); | ||
} | ||
return locale.toLanguageTag() + "-" + country; | ||
} | ||
|
||
// Return the standardized language tag (such as zh-CN) | ||
return locale.toLanguageTag(); | ||
} | ||
} |
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