fix #2316 remove google-api-services-analyticsreporting
This commit is contained in:
parent
5a44d0cffc
commit
4b9b89d970
3 changed files with 2 additions and 284 deletions
15
pom.xml
15
pom.xml
|
@ -1162,7 +1162,7 @@
|
|||
<dependency>
|
||||
<groupId>com.google.oauth-client</groupId>
|
||||
<artifactId>google-oauth-client</artifactId>
|
||||
<version>1.25.0</version>
|
||||
<version>1.30.4</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
|
@ -1177,7 +1177,7 @@
|
|||
<dependency>
|
||||
<groupId>com.google.http-client</groupId>
|
||||
<artifactId>google-http-client-jackson2</artifactId>
|
||||
<version>1.25.0</version>
|
||||
<version>1.33.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codelibs</groupId>
|
||||
|
@ -1219,17 +1219,6 @@
|
|||
<artifactId>icu4j</artifactId>
|
||||
<version>65.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.apis</groupId>
|
||||
<artifactId>google-api-services-analyticsreporting</artifactId>
|
||||
<version>v4-rev153-1.25.0</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava-jdk5</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tika</groupId>
|
||||
<artifactId>tika-langdetect</artifactId>
|
||||
|
|
|
@ -1,205 +0,0 @@
|
|||
/*
|
||||
* Copyright 2012-2019 CodeLibs Project and the Others.
|
||||
*
|
||||
* 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 org.codelibs.fess.score;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.codelibs.core.misc.Pair;
|
||||
import org.codelibs.fess.exception.FessSystemException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
||||
import com.google.api.client.http.HttpTransport;
|
||||
import com.google.api.client.json.GenericJson;
|
||||
import com.google.api.client.json.JsonFactory;
|
||||
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReporting;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReportingScopes;
|
||||
import com.google.api.services.analyticsreporting.v4.model.ColumnHeader;
|
||||
import com.google.api.services.analyticsreporting.v4.model.DateRangeValues;
|
||||
import com.google.api.services.analyticsreporting.v4.model.GetReportsRequest;
|
||||
import com.google.api.services.analyticsreporting.v4.model.GetReportsResponse;
|
||||
import com.google.api.services.analyticsreporting.v4.model.MetricHeaderEntry;
|
||||
import com.google.api.services.analyticsreporting.v4.model.Report;
|
||||
import com.google.api.services.analyticsreporting.v4.model.ReportRequest;
|
||||
import com.google.api.services.analyticsreporting.v4.model.ReportRow;
|
||||
|
||||
public class GoogleAnalyticsScoreBooster extends ScoreBooster {
|
||||
private static final Logger logger = LoggerFactory.getLogger(GoogleAnalyticsScoreBooster.class);
|
||||
|
||||
private JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
|
||||
|
||||
private String applicationName = "Fess Score Booster";
|
||||
|
||||
private String keyFileLocation;
|
||||
|
||||
private String serviceAccountEmail;
|
||||
|
||||
private AnalyticsReporting analyticsReporting = null;
|
||||
|
||||
private final List<Pair<String[], ReportRequest>> reportRequesList = new ArrayList<>();
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Initialize {}", this.getClass().getSimpleName());
|
||||
}
|
||||
if (!Paths.get(keyFileLocation).toFile().exists()) {
|
||||
logger.info("GA Key File does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (reportRequesList.isEmpty()) {
|
||||
logger.info("No reports.");
|
||||
return;
|
||||
}
|
||||
|
||||
analyticsReporting = initializeAnalyticsReporting();
|
||||
enable();
|
||||
}
|
||||
|
||||
private AnalyticsReporting initializeAnalyticsReporting() {
|
||||
try {
|
||||
final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
|
||||
final GoogleCredential credential =
|
||||
new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory)
|
||||
.setServiceAccountId(serviceAccountEmail).setServiceAccountPrivateKeyFromP12File(new File(keyFileLocation))
|
||||
.setServiceAccountScopes(AnalyticsReportingScopes.all()).build();
|
||||
|
||||
return new AnalyticsReporting.Builder(httpTransport, jsonFactory, credential).setApplicationName(applicationName).build();
|
||||
} catch (final Exception e) {
|
||||
throw new FessSystemException("Failed to initialize GA.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setJsonFactory(final JsonFactory jsonFactory) {
|
||||
this.jsonFactory = jsonFactory;
|
||||
}
|
||||
|
||||
public void setApplicationName(final String applicationName) {
|
||||
this.applicationName = applicationName;
|
||||
}
|
||||
|
||||
public void setKeyFileLocation(final String keyFileLocation) {
|
||||
this.keyFileLocation = keyFileLocation;
|
||||
}
|
||||
|
||||
public void setServiceAccountEmail(final String serviceAccountEmail) {
|
||||
this.serviceAccountEmail = serviceAccountEmail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long process() {
|
||||
long counter = 0;
|
||||
for (final Pair<String[], ReportRequest> entry : reportRequesList) {
|
||||
final GetReportsRequest getReport = new GetReportsRequest().setReportRequests(Arrays.asList(entry.getSecond()));
|
||||
try {
|
||||
final GetReportsResponse response = analyticsReporting.reports().batchGet(getReport).execute();
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(toPrettyString(response));
|
||||
}
|
||||
for (final Report report : response.getReports()) {
|
||||
final List<ReportRow> rows = report.getData().getRows();
|
||||
final String[] baseUrls = entry.getFirst();
|
||||
if (rows == null) {
|
||||
logger.info("No data found for " + String.join(",", baseUrls));
|
||||
continue;
|
||||
}
|
||||
|
||||
final ColumnHeader header = report.getColumnHeader();
|
||||
final List<String> dimensionHeaders = header.getDimensions();
|
||||
final List<MetricHeaderEntry> metricHeaders = header.getMetricHeader().getMetricHeaderEntries();
|
||||
for (final ReportRow row : rows) {
|
||||
final List<DateRangeValues> metrics = row.getMetrics();
|
||||
for (final DateRangeValues values : metrics) {
|
||||
String path = null;
|
||||
Long count = null;
|
||||
|
||||
final List<String> dimensions = row.getDimensions();
|
||||
for (int i = 0; i < dimensionHeaders.size() && i < dimensions.size(); i++) {
|
||||
final String name = dimensionHeaders.get(i);
|
||||
if ("ga:pagePath".equals(name)) {
|
||||
path = dimensions.get(i);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 0; k < values.getValues().size() && k < metricHeaders.size(); k++) {
|
||||
final String name = metricHeaders.get(k).getName();
|
||||
if ("ga:pageviews".equals(name)) {
|
||||
count = Long.parseLong(values.getValues().get(k));
|
||||
}
|
||||
}
|
||||
|
||||
if (path != null && count != null) {
|
||||
for (final String baseUrl : baseUrls) {
|
||||
try {
|
||||
final String url = normalizeUrl(path, baseUrl);
|
||||
final Map<String, Object> params = new HashMap<>();
|
||||
params.put("url", url);
|
||||
params.put("count", count);
|
||||
counter += updateScore(params);
|
||||
} catch (final Exception e) {
|
||||
logger.warn("Invalid url: " + baseUrl + " + " + path, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
logger.warn("Failed to access GA.", e);
|
||||
}
|
||||
}
|
||||
flush();
|
||||
return counter;
|
||||
}
|
||||
|
||||
protected String normalizeUrl(final String path, final String baseUrl) throws MalformedURLException {
|
||||
return new URL(baseUrl + path.toString()).toString();
|
||||
}
|
||||
|
||||
private String toPrettyString(final GenericJson json) {
|
||||
try {
|
||||
return json.toPrettyString();
|
||||
} catch (final IOException e) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Failed to parse json.", e);
|
||||
}
|
||||
return e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public void addReportRequest(final List<String> urls, final ReportRequest request) {
|
||||
reportRequesList.add(new Pair<>(urls.toArray(new String[urls.size()]), request));
|
||||
}
|
||||
|
||||
public void addReportRequest(final String url, final ReportRequest request) {
|
||||
reportRequesList.add(new Pair<>(new String[] { url }, request));
|
||||
}
|
||||
}
|
|
@ -2,72 +2,6 @@
|
|||
<!DOCTYPE components PUBLIC "-//DBFLUTE//DTD LastaDi 1.0//EN"
|
||||
"http://dbflute.org/meta/lastadi10.dtd">
|
||||
<components>
|
||||
<!-- https://developers.google.com/analytics/devguides/reporting/core/v4/ -->
|
||||
<!--
|
||||
<component name="metric1"
|
||||
class="com.google.api.services.analyticsreporting.v4.model.Metric">
|
||||
<postConstruct name="setExpression">
|
||||
<arg>"ga:pageviews"</arg>
|
||||
</postConstruct>
|
||||
</component>
|
||||
<component name="dimension1"
|
||||
class="com.google.api.services.analyticsreporting.v4.model.Dimension">
|
||||
<postConstruct name="setName">
|
||||
<arg>"ga:pagePath"</arg>
|
||||
</postConstruct>
|
||||
</component>
|
||||
<component name="dateRange1"
|
||||
class="com.google.api.services.analyticsreporting.v4.model.DateRange">
|
||||
<postConstruct name="setStartDate">
|
||||
<arg>"7DaysAgo"</arg>
|
||||
</postConstruct>
|
||||
<postConstruct name="setEndDate">
|
||||
<arg>"today"</arg>
|
||||
</postConstruct>
|
||||
</component>
|
||||
<component name="orderBy1"
|
||||
class="com.google.api.services.analyticsreporting.v4.model.OrderBy">
|
||||
<postConstruct name="setFieldName">
|
||||
<arg>"ga:pageviews"</arg>
|
||||
</postConstruct>
|
||||
<postConstruct name="setSortOrder">
|
||||
<arg>"DESCENDING"</arg>
|
||||
</postConstruct>
|
||||
</component>
|
||||
<component name="report1"
|
||||
class="com.google.api.services.analyticsreporting.v4.model.ReportRequest">
|
||||
<postConstruct name="setViewId">
|
||||
<arg>"<REPLACE_WITH_VIEW_ID>"</arg>
|
||||
</postConstruct>
|
||||
<postConstruct name="setMetrics">
|
||||
<arg>[metric1]</arg>
|
||||
</postConstruct>
|
||||
<postConstruct name="setDimensions">
|
||||
<arg>[dimension1]</arg>
|
||||
</postConstruct>
|
||||
<postConstruct name="setDateRanges">
|
||||
<arg>[dateRange1]</arg>
|
||||
</postConstruct>
|
||||
<postConstruct name="setOrderBys">
|
||||
<arg>[orderBy1]</arg>
|
||||
</postConstruct>
|
||||
<postConstruct name="setPageSize">
|
||||
<arg>10000</arg>
|
||||
</postConstruct>
|
||||
</component>
|
||||
<component name="googleAnalyticsScoreBooster" class="org.codelibs.fess.score.GoogleAnalyticsScoreBooster">
|
||||
<property name="keyFileLocation">org.codelibs.core.io.ResourceUtil.getResourceAsFile("ga_client_secrets.p12").getAbsolutePath()
|
||||
</property>
|
||||
<property name="serviceAccountEmail">"<REPLACE_WITH_EMAIL>"</property>
|
||||
<property name="scriptCode">"ctx._source.boost = Math.log(params.count.doubleValue() + 1.0)"</property>
|
||||
<postConstruct name="addReportRequest">
|
||||
<arg>"<BASE_URL>"</arg>
|
||||
<arg>report1</arg>
|
||||
</postConstruct>
|
||||
</component>
|
||||
-->
|
||||
|
||||
<component name="scoreUpdater" class="org.codelibs.fess.score.ScoreUpdater">
|
||||
</component>
|
||||
|
||||
</components>
|
||||
|
|
Loading…
Add table
Reference in a new issue