Compare commits
22 Commits
Author | SHA1 | Date |
---|---|---|
Luca Frosini | c138a3fbd2 | |
Luca Frosini | 1618cb9c10 | |
Luca Frosini | f3c2b6197c | |
Luca Frosini | 00bce762f3 | |
Luca Frosini | 9ef16294ff | |
Luca Frosini | fa7be6370a | |
Luca Frosini | 34db58abe2 | |
Luca Frosini | abb35b6e7b | |
Luca Frosini | 77cfbf6a8a | |
luca.frosini | e76b1c3af3 | |
luca.frosini | 009083f335 | |
luca.frosini | dedf11256a | |
Massimiliano Assante | 8c26deb82f | |
Massimiliano Assante | deb8937a10 | |
Massimiliano Assante | 850d6674e5 | |
Massimiliano Assante | 284431ca8d | |
Massimiliano Assante | 4cbdef880e | |
luca.frosini | 89e744d769 | |
luca.frosini | 43865106df | |
luca.frosini | e0fd599c80 | |
Massimiliano Assante | d3ad4f43ae | |
luca.frosini | 9b27e35676 |
|
@ -1,4 +1,5 @@
|
|||
target
|
||||
.classpath
|
||||
.project
|
||||
.settings
|
||||
.settings
|
||||
/.DS_Store
|
||||
|
|
16
CHANGELOG.md
16
CHANGELOG.md
|
@ -2,6 +2,22 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|||
|
||||
# Changelog for "accounting-dashboard-harvester-se-plugin"
|
||||
|
||||
## [v2.5.0]
|
||||
|
||||
- Upgraded smart-executor-bom to 3.3.0 [#27999]
|
||||
|
||||
|
||||
## [v2.4.0]
|
||||
|
||||
- Removed filter restriction on JobUsageRecord harvesting to get MethodInvocation
|
||||
- Fixed bug on getting ScopeDescriptor for new scopes.
|
||||
|
||||
|
||||
## [v2.3.0]
|
||||
|
||||
- Ported GA harvesters to Analytics Data API (GA4)
|
||||
|
||||
|
||||
## [v2.2.0]
|
||||
|
||||
- Switching security to the new IAM [#21904]
|
||||
|
|
75
pom.xml
75
pom.xml
|
@ -5,14 +5,14 @@
|
|||
<parent>
|
||||
<artifactId>maven-parent</artifactId>
|
||||
<groupId>org.gcube.tools</groupId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.2.0</version>
|
||||
<relativePath />
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.gcube.accounting</groupId>
|
||||
<artifactId>accounting-dashboard-harvester-se-plugin</artifactId>
|
||||
<version>2.2.0</version>
|
||||
<version>2.5.0</version>
|
||||
<name>Accounting Dashboard Harvester Smart Executor Plugin</name>
|
||||
<description>
|
||||
Accounting Dashboard Harvester Smart Executor Plugin harvest accounting
|
||||
|
@ -36,7 +36,7 @@
|
|||
<dependency>
|
||||
<groupId>org.gcube.vremanagement</groupId>
|
||||
<artifactId>smart-executor-bom</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<version>3.3.0</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
@ -54,30 +54,40 @@
|
|||
<version>[2.7.2,3.0.0-SNAPSHOT)</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.apis</groupId>
|
||||
<artifactId>google-api-services-analyticsreporting</artifactId>
|
||||
<version>v4-rev124-1.23.0</version>
|
||||
<groupId>com.google.analytics</groupId>
|
||||
<artifactId>google-analytics-data</artifactId>
|
||||
<version>0.16.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.8.4</version>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>23.6-jre</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.auth</groupId>
|
||||
<artifactId>google-auth-library-oauth2-http</artifactId>
|
||||
<version>1.12.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.8.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.http-client</groupId>
|
||||
<artifactId>google-http-client-gson</artifactId>
|
||||
<version>1.21.0</version>
|
||||
</dependency>
|
||||
<!-- <dependency> -->
|
||||
<!-- <groupId>org.gcube.common</groupId> -->
|
||||
<!-- <artifactId>storagehub-client-library</artifactId> -->
|
||||
<!-- <exclusions> -->
|
||||
<!-- <exclusion> -->
|
||||
<!-- <groupId>com.fasterxml.jackson.core</groupId> -->
|
||||
<!-- <artifactId>jackson-core</artifactId> -->
|
||||
<!-- </exclusion> -->
|
||||
<!-- </exclusions> -->
|
||||
<!-- </dependency> -->
|
||||
<!-- <dependency> -->
|
||||
<!-- <groupId>org.gcube.common</groupId> -->
|
||||
<!-- <artifactId>storagehub-client-library</artifactId> -->
|
||||
<!-- <exclusions> -->
|
||||
<!-- <exclusion> -->
|
||||
<!-- <groupId>com.fasterxml.jackson.core</groupId> -->
|
||||
<!-- <artifactId>jackson-core</artifactId> -->
|
||||
<!-- </exclusion> -->
|
||||
<!-- </exclusions> -->
|
||||
<!-- </dependency> -->
|
||||
<dependency>
|
||||
<groupId>org.gcube.data-catalogue</groupId>
|
||||
<artifactId>gcat-client</artifactId>
|
||||
|
@ -118,8 +128,8 @@
|
|||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.social-networking</groupId>
|
||||
<artifactId>social-service-client</artifactId>
|
||||
<groupId>org.gcube.social-networking</groupId>
|
||||
<artifactId>social-service-client</artifactId>
|
||||
<version>[1.0.0, 2.0.0-SNAPSHOT)</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -131,10 +141,11 @@
|
|||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>authorization-utils</artifactId>
|
||||
<version>[2.0.0, 3.0.0-SNAPSHOT)</version>
|
||||
<version>[2.2.0, 3.0.0-SNAPSHOT)</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Test Dependencies. Setting scope to provided to allow proper creation of uber-jar -->
|
||||
<!-- Test Dependencies. Setting scope to provided to allow proper creation
|
||||
of uber-jar -->
|
||||
<dependency>
|
||||
<groupId>org.gcube.vremanagement</groupId>
|
||||
<artifactId>smart-executor-client</artifactId>
|
||||
|
@ -197,6 +208,24 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<configuration>
|
||||
<additionalparam>-Xdoclint:none</additionalparam>
|
||||
<additionalJOption>-Xdoclint:none</additionalJOption>
|
||||
</configuration>
|
||||
<version>3.1.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>generate-doc</id>
|
||||
<phase>install</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
@ -78,20 +78,7 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
|
|||
public AccountingDashboardHarvesterPlugin() {
|
||||
super();
|
||||
}
|
||||
|
||||
private static final InheritableThreadLocal<Properties> properties = new InheritableThreadLocal<Properties>() {
|
||||
|
||||
@Override
|
||||
protected Properties initialValue() {
|
||||
return new Properties();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public static InheritableThreadLocal<Properties> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
|
||||
public static Dimension getDimension(String key) {
|
||||
Dimension dimension = dimensions.get().get(key);
|
||||
if (dimension == null) {
|
||||
|
@ -122,21 +109,8 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
|
|||
|
||||
};
|
||||
|
||||
public static ScopeDescriptor getScopeDescriptor() {
|
||||
return scopeDescriptor.get();
|
||||
}
|
||||
|
||||
public static final InheritableThreadLocal<ScopeDescriptor> scopeDescriptor = new InheritableThreadLocal<ScopeDescriptor>() {
|
||||
|
||||
@Override
|
||||
protected ScopeDescriptor initialValue() {
|
||||
return new ScopeDescriptor("", "");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@JsonIgnore
|
||||
public Properties getConfigParameters() throws IOException {
|
||||
public static Properties getConfigParameters() throws IOException {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
InputStream input = AccountingDashboardHarvesterPlugin.class.getClassLoader()
|
||||
|
@ -210,9 +184,6 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
|
|||
logger.debug("Harvesting from {} to {} (ReRun:{} - GetVREUsers:{} - DryRun:{})", DateUtils.format(start),
|
||||
DateUtils.format(end), reRun, getVREUsers, dryRun);
|
||||
|
||||
Properties properties = getConfigParameters();
|
||||
getProperties().set(properties);
|
||||
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
SortedSet<String> contexts = contextAuthorization.getContexts();
|
||||
String root = contexts.first();
|
||||
|
@ -253,10 +224,9 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
|
|||
ScopeDescriptor actualScopeDescriptor = scopeDescriptorMap.get(context);
|
||||
if (actualScopeDescriptor == null) {
|
||||
actualScopeDescriptor = new ScopeDescriptor(scopeBean.name(), context);
|
||||
scopeDescriptorMap.put(actualScopeDescriptor.getId(), actualScopeDescriptor);
|
||||
}
|
||||
|
||||
scopeDescriptor.set(actualScopeDescriptor);
|
||||
|
||||
if (scopeBean.is(Type.INFRASTRUCTURE)) {
|
||||
try {
|
||||
|
||||
|
|
|
@ -59,12 +59,12 @@ public class AnalyticsReportCredentials {
|
|||
/**
|
||||
* Please note:
|
||||
* The key is stored in the resource with blanks " " instead of "\n" as it causes issues and
|
||||
* without the BEGIN and END Delimiters (e.g. -----END PRIVATE KEY-----) which myst be readded
|
||||
* without the BEGIN and END Delimiters (e.g. -----END PRIVATE KEY-----) which must be readded
|
||||
* @param privateKeyPem
|
||||
*/
|
||||
public void setPrivateKeyPem(String privateKeyPem) {
|
||||
privateKeyPem = privateKeyPem.replace(" ", "\n");
|
||||
this.privateKeyPem = "-----BEGIN PRIVATE KEY-----\n"+privateKeyPem+"\n-----END PRIVATE KEY-----";
|
||||
this.privateKeyPem = privateKeyPem.replace(" ", "\n");
|
||||
this.privateKeyPem = "-----BEGIN PRIVATE KEY-----\n"+this.privateKeyPem+"\n-----END PRIVATE KEY-----\n";
|
||||
}
|
||||
|
||||
public String getPrivateKeyId() {
|
||||
|
|
|
@ -4,22 +4,14 @@ import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|||
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -49,26 +41,20 @@ import org.slf4j.LoggerFactory;
|
|||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder;
|
||||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
||||
import com.google.api.client.googleapis.util.Utils;
|
||||
import com.google.api.client.http.HttpTransport;
|
||||
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
|
||||
import com.google.analytics.data.v1beta.BetaAnalyticsDataSettings;
|
||||
import com.google.analytics.data.v1beta.DateRange;
|
||||
import com.google.analytics.data.v1beta.DateRange.Builder;
|
||||
import com.google.analytics.data.v1beta.Dimension;
|
||||
import com.google.analytics.data.v1beta.Metric;
|
||||
import com.google.analytics.data.v1beta.Row;
|
||||
import com.google.analytics.data.v1beta.RunReportRequest;
|
||||
import com.google.analytics.data.v1beta.RunReportResponse;
|
||||
import com.google.api.client.json.JsonFactory;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.google.api.client.util.PemReader;
|
||||
import com.google.api.client.util.PemReader.Section;
|
||||
import com.google.api.client.util.SecurityUtils;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReporting;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReportingScopes;
|
||||
import com.google.api.services.analyticsreporting.v4.model.DateRange;
|
||||
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.Metric;
|
||||
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;
|
||||
import com.google.api.gax.core.FixedCredentialsProvider;
|
||||
import com.google.auth.oauth2.ServiceAccountCredentials;
|
||||
|
||||
|
||||
public class CatalogueAccessesHarvester extends BasicHarvester {
|
||||
|
||||
|
@ -76,13 +62,13 @@ public class CatalogueAccessesHarvester extends BasicHarvester {
|
|||
|
||||
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||
|
||||
|
||||
private static final String MAPPING_RESOURCE_CATEGORY = "BigGAnalyticsMapping";
|
||||
private static final String SERVICE_ENDPOINT_CATEGORY = "OnlineService";
|
||||
private static final String SERVICE_ENDPOINT_NAME = "BigGAnalyticsReportService";
|
||||
private static final String SERVICE_ENDPOINT_NAME = "GA4AnalyticsDataService";
|
||||
private static final String AP_CATALOGUE_PAGEVIEWS_PROPERTY = "catalogue-pageviews";
|
||||
private static final String AP_CLIENT_PROPERTY = "clientId";
|
||||
private static final String AP_PRIVATEKEY_PROPERTY = "privateKeyId";
|
||||
private static final String APPLICATION_NAME = "Analytics Reporting";
|
||||
private static final String AP_CLIENT_PROPERTY = "client_id";
|
||||
private static final String AP_PRIVATEKEY_PROPERTY = "private_key_id";
|
||||
|
||||
private static final String REGEX_CATALOGUE_ACCESSES = "^\\/$";
|
||||
private static final String REGEX_CATALOGUE_DATASET_LIST_ACCESSES = "^\\/dataset(\\?([a-zA-Z0-9_.-]*.+))*";
|
||||
|
@ -107,7 +93,7 @@ public class CatalogueAccessesHarvester extends BasicHarvester {
|
|||
int catalogueResourceAccesses = 0;
|
||||
logger.debug("Catalogue accesses for {} ", dashboardContext);
|
||||
for(CatalogueAccessesReportRow row : catalogueAccesses.get(dashboardContext)) {
|
||||
// String pagePath = row.getPagePath();
|
||||
// String pagePath = row.getPagePath();
|
||||
switch (row.getKey()) {
|
||||
case CATALOGUE_ACCESSES:
|
||||
catalogueTotalAccesses += row.getVisitNumber();
|
||||
|
@ -126,23 +112,29 @@ public class CatalogueAccessesHarvester extends BasicHarvester {
|
|||
}
|
||||
}
|
||||
ScopeDescriptor scopeDescriptor = new ScopeDescriptor();
|
||||
ScopeBean scopeBean = new ScopeBean(dashboardContext);
|
||||
scopeDescriptor.setId(dashboardContext);
|
||||
scopeDescriptor.setName(scopeBean.name());
|
||||
|
||||
AccountingRecord ar1 = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.CATALOGUE_ACCESSES), (long) catalogueTotalAccesses);
|
||||
AccountingRecord ar2 = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.CATALOGUE_DATASET_LIST_ACCESSES), (long) catalogueDatasetListAccesses);
|
||||
AccountingRecord ar3 = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.CATALOGUE_DATASET_ACCESSES), (long) catalogueDatasetAccesses);
|
||||
AccountingRecord ar4 = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.CATALOGUE_RESOURCE_ACCESSES), (long) catalogueResourceAccesses);
|
||||
logger.debug("{} : {}", ar1.getDimension().getId(), ar1.getMeasure());
|
||||
accountingRecords.add(ar1);
|
||||
logger.debug("{} : {}", ar2.getDimension().getId(), ar2.getMeasure());
|
||||
accountingRecords.add(ar2);
|
||||
logger.debug("{} : {}", ar3.getDimension().getId(), ar3.getMeasure());
|
||||
accountingRecords.add(ar3);
|
||||
logger.debug("{} : {}", ar4.getDimension().getId(), ar4.getMeasure());
|
||||
accountingRecords.add(ar4);
|
||||
|
||||
try {
|
||||
ScopeBean scopeBean = new ScopeBean(dashboardContext);
|
||||
scopeDescriptor.setId(dashboardContext);
|
||||
scopeDescriptor.setName(scopeBean.name());
|
||||
|
||||
AccountingRecord ar1 = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.CATALOGUE_ACCESSES), (long) catalogueTotalAccesses);
|
||||
AccountingRecord ar2 = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.CATALOGUE_DATASET_LIST_ACCESSES), (long) catalogueDatasetListAccesses);
|
||||
AccountingRecord ar3 = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.CATALOGUE_DATASET_ACCESSES), (long) catalogueDatasetAccesses);
|
||||
AccountingRecord ar4 = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.CATALOGUE_RESOURCE_ACCESSES), (long) catalogueResourceAccesses);
|
||||
logger.debug("{} : {}", ar1.getDimension().getId(), ar1.getMeasure());
|
||||
accountingRecords.add(ar1);
|
||||
logger.debug("{} : {}", ar2.getDimension().getId(), ar2.getMeasure());
|
||||
accountingRecords.add(ar2);
|
||||
logger.debug("{} : {}", ar3.getDimension().getId(), ar3.getMeasure());
|
||||
accountingRecords.add(ar3);
|
||||
logger.debug("{} : {}", ar4.getDimension().getId(), ar4.getMeasure());
|
||||
accountingRecords.add(ar4);
|
||||
} catch (NullPointerException e) {
|
||||
logger.warn("I found no correspondance in the Genereric Resource for a PropertyId you should check this, type: BigGAnalyticsMapping name: AccountingDashboardMapping");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
logger.debug("Returning {} accountingRecords ", accountingRecords.size());
|
||||
return accountingRecords;
|
||||
|
@ -156,17 +148,24 @@ public class CatalogueAccessesHarvester extends BasicHarvester {
|
|||
*
|
||||
*/
|
||||
private static HashMap<String, List<CatalogueAccessesReportRow>> getAllAccesses(Date start, Date end) throws Exception {
|
||||
DateRange dateRange = getDateRangeForAnalytics(start, end);
|
||||
logger.trace("Getting catalogue accesses in this time range {}", dateRange.toPrettyString());
|
||||
Builder dateRangeBuilder = getDateRangeBuilderForAnalytics(start, end);
|
||||
logger.debug("Getting Catalogue accesses in this time range {}", dateRangeBuilder.toString());
|
||||
|
||||
AnalyticsReportCredentials credentialsFromD4S = getAuthorisedApplicationInfoFromIs();
|
||||
AnalyticsReporting service = initializeAnalyticsReporting(credentialsFromD4S);
|
||||
HashMap<String,List<GetReportsResponse>> responses = getReportResponses(service, credentialsFromD4S.getViewIds(), dateRange);
|
||||
|
||||
logger.debug("Getting credentials credentialsFromD4S");
|
||||
|
||||
BetaAnalyticsDataSettings serviceSettings = initializeAnalyticsReporting(credentialsFromD4S);
|
||||
|
||||
logger.debug("initializeAnalyticsReporting service settings");
|
||||
|
||||
|
||||
HashMap<String,List<RunReportResponse>> responses = getReportResponses(serviceSettings, credentialsFromD4S.getViewIds(), dateRangeBuilder);
|
||||
HashMap<String, List<CatalogueAccessesReportRow>> toReturn = new HashMap<>();
|
||||
|
||||
for(String view : responses.keySet()) {
|
||||
String dashboardContext = getAccountingDashboardContextGivenGAViewID(view);
|
||||
logger.trace("Parsing responses for this catalogue view, which corresponds to Dashboard Context: " + dashboardContext);
|
||||
logger.info("\n\n**************** Parsing responses for this catalogue view, which corresponds to Dashboard Context: " + dashboardContext);
|
||||
List<CatalogueAccessesReportRow> viewReport = parseResponse(view, responses.get(view), dashboardContext);
|
||||
logger.trace("Got {} entries from view id={}", viewReport.size(), view);
|
||||
toReturn.put(dashboardContext, viewReport);
|
||||
|
@ -175,175 +174,102 @@ public class CatalogueAccessesHarvester extends BasicHarvester {
|
|||
}
|
||||
|
||||
/**
|
||||
* Initializes an Analytics Reporting API V4 service object.
|
||||
* Initializes an Google Analytics Data API service object.
|
||||
*
|
||||
* @return An authorized Analytics Reporting API V4 service object.
|
||||
* @return An authorized Google Analytics Data API
|
||||
* @throws IOException
|
||||
* @throws GeneralSecurityException
|
||||
*/
|
||||
private static AnalyticsReporting initializeAnalyticsReporting(AnalyticsReportCredentials cred)
|
||||
throws GeneralSecurityException, IOException {
|
||||
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
|
||||
GoogleCredential credential = fromD4SServiceEndpoint(cred).createScoped(AnalyticsReportingScopes.all());
|
||||
|
||||
// Construct the Analytics Reporting service object.
|
||||
return new AnalyticsReporting.Builder(httpTransport, JSON_FACTORY, credential)
|
||||
.setApplicationName(APPLICATION_NAME).build();
|
||||
private static BetaAnalyticsDataSettings initializeAnalyticsReporting(AnalyticsReportCredentials cred) throws IOException {
|
||||
return BetaAnalyticsDataSettings.newBuilder()
|
||||
.setCredentialsProvider(FixedCredentialsProvider.create(
|
||||
ServiceAccountCredentials.fromPkcs8(cred.getClientId(), cred.getClientEmail(), cred.getPrivateKeyPem(), cred.getPrivateKeyId(), null)))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries the Analytics Reporting API V4.
|
||||
* Queries Analytics Data API service
|
||||
*
|
||||
* @param service An authorized Analytics Reporting API V4 service object.
|
||||
* @return GetReportResponse The Analytics Reporting API V4 response.
|
||||
* @param service Analytics Data API service service settings.
|
||||
* @return Row Analytics Data API service
|
||||
* @throws IOException
|
||||
*/
|
||||
private static HashMap<String,List<GetReportsResponse>> getReportResponses(AnalyticsReporting service,
|
||||
List<String> viewIDs, DateRange dateRange) throws IOException {
|
||||
private static HashMap<String,List<RunReportResponse>> getReportResponses(BetaAnalyticsDataSettings betaAnalyticsDataSettings,
|
||||
List<String> viewIDs, Builder dateRangeBuilder) throws IOException {
|
||||
|
||||
HashMap<String,List<GetReportsResponse>> reports = new HashMap<>();
|
||||
HashMap<String,List<RunReportResponse>> reports = new HashMap<>();
|
||||
|
||||
// Create the Metrics object.
|
||||
Metric sessions = new Metric().setExpression("ga:pageviews").setAlias("pages");
|
||||
com.google.api.services.analyticsreporting.v4.model.Dimension pageTitle = new com.google.api.services.analyticsreporting.v4.model.Dimension().setName("ga:pagePath");
|
||||
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create(betaAnalyticsDataSettings)) {
|
||||
|
||||
for(String view : viewIDs) {
|
||||
List<GetReportsResponse> gReportResponses = new ArrayList<>();
|
||||
logger.info("Getting data from Google Analytics for catalogue viewid: " + view);
|
||||
boolean iterateMorePages = true;
|
||||
String nextPageToken = null;
|
||||
while (iterateMorePages) {
|
||||
// Create the ReportRequest object.
|
||||
ReportRequest request = new ReportRequest().setViewId(view.trim()).setDateRanges(Arrays.asList(dateRange))
|
||||
.setMetrics(Arrays.asList(sessions)).setDimensions(Arrays.asList(pageTitle));
|
||||
request.setPageSize(1000);
|
||||
request.setPageToken(nextPageToken);
|
||||
ArrayList<ReportRequest> requests = new ArrayList<ReportRequest>();
|
||||
requests.add(request);
|
||||
// Create the GetReportsRequest object.
|
||||
GetReportsRequest getReport = new GetReportsRequest().setReportRequests(requests);
|
||||
// Call the batchGet method.
|
||||
GetReportsResponse response = service.reports().batchGet(getReport).execute();
|
||||
nextPageToken = response.getReports().get(0).getNextPageToken();
|
||||
iterateMorePages = (nextPageToken != null);
|
||||
logger.debug("got nextPageToken: "+nextPageToken);
|
||||
for(String propertyId : viewIDs) {
|
||||
List<RunReportResponse> gReportResponses = new ArrayList<>();
|
||||
logger.debug("Getting data from Analytics Data API for propertyId: " + propertyId);
|
||||
RunReportRequest request =
|
||||
RunReportRequest.newBuilder()
|
||||
.setProperty("properties/" + propertyId)
|
||||
.addDimensions(Dimension.newBuilder().setName("pagePath"))
|
||||
.addMetrics(Metric.newBuilder().setName("screenPageViews"))
|
||||
.addDateRanges(dateRangeBuilder)
|
||||
.build();
|
||||
|
||||
// Make the request.
|
||||
RunReportResponse response = analyticsData.runReport(request);
|
||||
gReportResponses.add(response);
|
||||
// Iterate through every row of the API response.
|
||||
// for (Row row : response.getRowsList()) {
|
||||
// System.out.printf(
|
||||
// "%s, %s%n", row.getDimensionValues(0).getValue(), row.getMetricValues(0).getValue());
|
||||
// }
|
||||
reports.put(propertyId, gReportResponses);
|
||||
}
|
||||
reports.put(view, gReportResponses);
|
||||
}
|
||||
// Return the response.
|
||||
return reports;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses and prints the Analytics Reporting API V4 response.
|
||||
* @param dashboardContext
|
||||
* Parses and prints the Analytics Data API service respose
|
||||
*
|
||||
* @param response An Analytics Reporting API V4 response.
|
||||
* @param dashboardContext
|
||||
*/
|
||||
private static List<CatalogueAccessesReportRow> parseResponse(String viewId, List<GetReportsResponse> responses, String dashboardContext) {
|
||||
private static List<CatalogueAccessesReportRow> parseResponse(String viewId, List<RunReportResponse> responses, String dashboardContext) {
|
||||
logger.debug("parsing Response for " + viewId);
|
||||
|
||||
List<CatalogueAccessesReportRow> toReturn = new ArrayList<>();
|
||||
for (GetReportsResponse response : responses) {
|
||||
for (Report report: response.getReports()) {
|
||||
List<ReportRow> rows = report.getData().getRows();
|
||||
if (rows == null) {
|
||||
logger.warn("No data found for " + viewId);
|
||||
for (RunReportResponse response : responses) {
|
||||
for (Row row: response.getRowsList()) {
|
||||
String dimension = row.getDimensionValues(0).getValue();
|
||||
String metric = row.getMetricValues(0).getValue();
|
||||
CatalogueAccessesReportRow var = new CatalogueAccessesReportRow();
|
||||
boolean validEntry = false;
|
||||
String pagePath = dimension;
|
||||
logger.trace("parsing pagepath {}: value: {}", pagePath, Integer.parseInt(metric));
|
||||
if (pagePath.matches(REGEX_CATALOGUE_RESOURCE_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.CATALOGUE_RESOURCE_ACCESSES);
|
||||
validEntry = true;
|
||||
}
|
||||
else {
|
||||
for (ReportRow row: rows) {
|
||||
String dimension = row.getDimensions().get(0);
|
||||
DateRangeValues metric = row.getMetrics().get(0);
|
||||
CatalogueAccessesReportRow var = new CatalogueAccessesReportRow();
|
||||
boolean validEntry = false;
|
||||
String pagePath = dimension;
|
||||
logger.trace("parsing pagepath {}: value: {}", pagePath, Integer.parseInt(metric.getValues().get(0)));
|
||||
|
||||
if (pagePath.matches(REGEX_CATALOGUE_RESOURCE_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.CATALOGUE_RESOURCE_ACCESSES);
|
||||
validEntry = true;
|
||||
}
|
||||
else if (pagePath.matches(REGEX_CATALOGUE_DATASET_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.CATALOGUE_DATASET_ACCESSES);
|
||||
validEntry = true;
|
||||
}
|
||||
else if (pagePath.matches(REGEX_CATALOGUE_DATASET_LIST_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.CATALOGUE_DATASET_LIST_ACCESSES);
|
||||
validEntry = true;
|
||||
}
|
||||
else if (pagePath.matches(REGEX_CATALOGUE_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.CATALOGUE_ACCESSES);
|
||||
validEntry = true;
|
||||
}
|
||||
if (validEntry) {
|
||||
var.setDashboardContext(dashboardContext);
|
||||
var.setPagePath(dimension);
|
||||
var.setVisitNumber(Integer.parseInt(metric.getValues().get(0)));
|
||||
toReturn.add(var);
|
||||
}
|
||||
}
|
||||
else if (pagePath.matches(REGEX_CATALOGUE_DATASET_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.CATALOGUE_DATASET_ACCESSES);
|
||||
validEntry = true;
|
||||
}
|
||||
else if (pagePath.matches(REGEX_CATALOGUE_DATASET_LIST_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.CATALOGUE_DATASET_LIST_ACCESSES);
|
||||
validEntry = true;
|
||||
}
|
||||
else if (pagePath.matches(REGEX_CATALOGUE_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.CATALOGUE_ACCESSES);
|
||||
validEntry = true;
|
||||
}
|
||||
if (validEntry) {
|
||||
var.setDashboardContext(dashboardContext);
|
||||
var.setPagePath(dimension);
|
||||
var.setVisitNumber(Integer.parseInt(metric));
|
||||
toReturn.add(var);
|
||||
}
|
||||
}
|
||||
}
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private static GoogleCredential fromD4SServiceEndpoint(AnalyticsReportCredentials cred) throws IOException {
|
||||
|
||||
String clientId = cred.getClientId();
|
||||
String clientEmail = cred.getClientEmail();
|
||||
String privateKeyPem = cred.getPrivateKeyPem();
|
||||
String privateKeyId = cred.getPrivateKeyId();
|
||||
String tokenUri = cred.getTokenUri();
|
||||
String projectId = cred.getProjectId();
|
||||
|
||||
if(clientId == null || clientEmail == null || privateKeyPem == null || privateKeyId == null) {
|
||||
throw new IOException("Error reading service account credential from stream, "
|
||||
+ "expecting 'client_id', 'client_email', 'private_key' and 'private_key_id'.");
|
||||
}
|
||||
|
||||
PrivateKey privateKey = privateKeyFromPkcs8(privateKeyPem);
|
||||
|
||||
Collection<String> emptyScopes = Collections.emptyList();
|
||||
|
||||
Builder credentialBuilder = new GoogleCredential.Builder().setTransport(Utils.getDefaultTransport())
|
||||
.setJsonFactory(Utils.getDefaultJsonFactory()).setServiceAccountId(clientEmail)
|
||||
.setServiceAccountScopes(emptyScopes).setServiceAccountPrivateKey(privateKey)
|
||||
.setServiceAccountPrivateKeyId(privateKeyId);
|
||||
|
||||
if(tokenUri != null) {
|
||||
credentialBuilder.setTokenServerEncodedUrl(tokenUri);
|
||||
}
|
||||
|
||||
if(projectId != null) {
|
||||
credentialBuilder.setServiceAccountProjectId(projectId);
|
||||
}
|
||||
|
||||
// Don't do a refresh at this point, as it will always fail before the scopes are added.
|
||||
return credentialBuilder.build();
|
||||
}
|
||||
|
||||
private static PrivateKey privateKeyFromPkcs8(String privateKeyPem) throws IOException {
|
||||
Reader reader = new StringReader(privateKeyPem);
|
||||
Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
|
||||
if(section == null) {
|
||||
throw new IOException("Invalid PKCS8 data.");
|
||||
}
|
||||
byte[] bytes = section.getBase64DecodedBytes();
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
|
||||
Exception unexpectedException = null;
|
||||
try {
|
||||
KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
|
||||
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
||||
return privateKey;
|
||||
} catch(NoSuchAlgorithmException exception) {
|
||||
unexpectedException = exception;
|
||||
} catch(InvalidKeySpecException exception) {
|
||||
unexpectedException = exception;
|
||||
}
|
||||
throw new IOException("Unexpected exception reading PKCS data", unexpectedException);
|
||||
}
|
||||
|
||||
private static List<ServiceEndpoint> getAnalyticsReportingConfigurationFromIS(String infrastructureScope)
|
||||
throws Exception {
|
||||
|
@ -405,7 +331,6 @@ public class CatalogueAccessesHarvester extends BasicHarvester {
|
|||
}
|
||||
|
||||
/**
|
||||
* l
|
||||
* @throws Exception
|
||||
*/
|
||||
private static AnalyticsReportCredentials getAuthorisedApplicationInfoFromIs() throws Exception {
|
||||
|
@ -423,13 +348,13 @@ public class CatalogueAccessesHarvester extends BasicHarvester {
|
|||
} else {
|
||||
|
||||
for(ServiceEndpoint res : list) {
|
||||
reportCredentials.setTokenUri(res.profile().runtime().hostedOn());
|
||||
Group<AccessPoint> apGroup = res.profile().accessPoints();
|
||||
AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]);
|
||||
AccessPoint found = accessPoints[0];
|
||||
reportCredentials.setClientEmail(found.address());
|
||||
reportCredentials.setProjectId(found.username());
|
||||
reportCredentials.setPrivateKeyPem(StringEncrypter.getEncrypter().decrypt(found.password()));
|
||||
reportCredentials.setClientEmail(found.username());
|
||||
String decryptedPrivateKey = StringEncrypter.getEncrypter().decrypt(found.password());
|
||||
reportCredentials.setPrivateKeyPem(decryptedPrivateKey.trim());
|
||||
|
||||
for(Property prop : found.properties()) {
|
||||
if(prop.name().compareTo(AP_CATALOGUE_PAGEVIEWS_PROPERTY) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
|
@ -458,14 +383,13 @@ public class CatalogueAccessesHarvester extends BasicHarvester {
|
|||
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
}
|
||||
|
||||
private static DateRange getDateRangeForAnalytics(Date start, Date end) {
|
||||
private static Builder getDateRangeBuilderForAnalytics(Date start, Date end) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); //required by Analytics
|
||||
String startDate = asLocalDate(start).format(formatter);
|
||||
String endDate = asLocalDate(end).format(formatter);
|
||||
DateRange dateRange = new DateRange();// date format `yyyy-MM-dd`
|
||||
dateRange.setStartDate(startDate);
|
||||
dateRange.setEndDate(endDate);
|
||||
return dateRange;
|
||||
Builder dateRangeBuilder = DateRange.newBuilder().setStartDate(startDate).setEndDate(endDate);
|
||||
|
||||
return dateRangeBuilder;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,22 +4,14 @@ import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|||
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -49,26 +41,17 @@ import org.slf4j.LoggerFactory;
|
|||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder;
|
||||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
||||
import com.google.api.client.googleapis.util.Utils;
|
||||
import com.google.api.client.http.HttpTransport;
|
||||
import com.google.api.client.json.JsonFactory;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.google.api.client.util.PemReader;
|
||||
import com.google.api.client.util.PemReader.Section;
|
||||
import com.google.api.client.util.SecurityUtils;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReporting;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReportingScopes;
|
||||
import com.google.api.services.analyticsreporting.v4.model.DateRange;
|
||||
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.Metric;
|
||||
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;
|
||||
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
|
||||
import com.google.analytics.data.v1beta.BetaAnalyticsDataSettings;
|
||||
import com.google.analytics.data.v1beta.DateRange;
|
||||
import com.google.analytics.data.v1beta.DateRange.Builder;
|
||||
import com.google.analytics.data.v1beta.Dimension;
|
||||
import com.google.analytics.data.v1beta.Metric;
|
||||
import com.google.analytics.data.v1beta.Row;
|
||||
import com.google.analytics.data.v1beta.RunReportRequest;
|
||||
import com.google.analytics.data.v1beta.RunReportResponse;
|
||||
import com.google.api.gax.core.FixedCredentialsProvider;
|
||||
import com.google.auth.oauth2.ServiceAccountCredentials;
|
||||
|
||||
/**
|
||||
* @author Massimiliano Assante (ISTI - CNR)
|
||||
|
@ -77,16 +60,12 @@ public class CoreServicesAccessesHarvester extends BasicHarvester {
|
|||
|
||||
private static Logger logger = LoggerFactory.getLogger(CoreServicesAccessesHarvester.class);
|
||||
|
||||
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||
|
||||
private static final String MAPPING_RESOURCE_CATEGORY = "BigGAnalyticsMapping";
|
||||
private static final String SERVICE_ENDPOINT_CATEGORY = "OnlineService";
|
||||
private static final String SERVICE_ENDPOINT_NAME = "BigGAnalyticsReportService";
|
||||
private static final String SERVICE_ENDPOINT_NAME = "GA4AnalyticsDataService";
|
||||
private static final String AP_VIEWS_PROPERTY = "views";
|
||||
private static final String AP_CLIENT_PROPERTY = "clientId";
|
||||
private static final String AP_PRIVATEKEY_PROPERTY = "privateKeyId";
|
||||
private static final String APPLICATION_NAME = "Analytics Reporting";
|
||||
|
||||
private static final String AP_CLIENT_ID = "client_id";
|
||||
private static final String AP_PRIVATEKEY_ID_PROPERTY = "private_key_id";
|
||||
|
||||
private static final String PAGE_WORKSPACE_ACCESSES = "/workspace";
|
||||
private static final String PAGE_MESSAGES_ACCESSES = "/messages";
|
||||
|
@ -160,18 +139,18 @@ public class CoreServicesAccessesHarvester extends BasicHarvester {
|
|||
*
|
||||
*/
|
||||
private static HashMap<String, List<CoreServiceAccessesReportRow>> getAllAccesses(Date start, Date end) throws Exception {
|
||||
DateRange dateRange = getDateRangeForAnalytics(start, end);
|
||||
logger.trace("Getting core services accesses in this time range {}", dateRange.toPrettyString());
|
||||
Builder dateRangeBuilder = getDateRangeBuilderForAnalytics(start, end);
|
||||
logger.trace("Getting core services accesses in this time range {}", dateRangeBuilder.toString());
|
||||
|
||||
AnalyticsReportCredentials credentialsFromD4S = getAuthorisedApplicationInfoFromIs();
|
||||
|
||||
logger.trace("gotten credentialsFromD4S id = {}", credentialsFromD4S.getClientId());
|
||||
|
||||
AnalyticsReporting service = initializeAnalyticsReporting(credentialsFromD4S);
|
||||
BetaAnalyticsDataSettings serviceSettings = initializeAnalyticsReporting(credentialsFromD4S);
|
||||
|
||||
logger.trace("gotten credentialsFromD4S viewIds= {}", credentialsFromD4S.getViewIds().toString());
|
||||
|
||||
HashMap<String,List<GetReportsResponse>> responses = getReportResponses(service, credentialsFromD4S.getViewIds(), dateRange);
|
||||
HashMap<String,List<RunReportResponse>> responses = getReportResponses(serviceSettings, credentialsFromD4S.getViewIds(), dateRangeBuilder);
|
||||
HashMap<String, List<CoreServiceAccessesReportRow>> toReturn = new HashMap<>();
|
||||
|
||||
int i = 1;
|
||||
|
@ -191,181 +170,106 @@ public class CoreServicesAccessesHarvester extends BasicHarvester {
|
|||
}
|
||||
|
||||
/**
|
||||
* Initializes an Analytics Reporting API V4 service object.
|
||||
* Initializes an Google Analytics Data API service object.
|
||||
*
|
||||
* @return An authorized Analytics Reporting API V4 service object.
|
||||
* @return An authorized Google Analytics Data API
|
||||
* @throws IOException
|
||||
* @throws GeneralSecurityException
|
||||
*/
|
||||
private static AnalyticsReporting initializeAnalyticsReporting(AnalyticsReportCredentials cred)
|
||||
throws GeneralSecurityException, IOException {
|
||||
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
|
||||
GoogleCredential credential = fromD4SServiceEndpoint(cred).createScoped(AnalyticsReportingScopes.all());
|
||||
|
||||
// Construct the Analytics Reporting service object.
|
||||
return new AnalyticsReporting.Builder(httpTransport, JSON_FACTORY, credential)
|
||||
.setApplicationName(APPLICATION_NAME).build();
|
||||
private static BetaAnalyticsDataSettings initializeAnalyticsReporting(AnalyticsReportCredentials cred) throws IOException {
|
||||
return BetaAnalyticsDataSettings.newBuilder()
|
||||
.setCredentialsProvider(FixedCredentialsProvider.create(
|
||||
ServiceAccountCredentials.fromPkcs8(cred.getClientId(), cred.getClientEmail(), cred.getPrivateKeyPem(), cred.getPrivateKeyId(), null)))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries the Analytics Reporting API V4.
|
||||
* Queries Analytics Data API service
|
||||
*
|
||||
* @param service An authorized Analytics Reporting API V4 service object.
|
||||
* @return GetReportResponse The Analytics Reporting API V4 response.
|
||||
* @param service Analytics Data API service service settings.
|
||||
* @return Row Analytics Data API service
|
||||
* @throws IOException
|
||||
*/
|
||||
private static HashMap<String,List<GetReportsResponse>> getReportResponses(AnalyticsReporting service,
|
||||
List<String> viewIDs, DateRange dateRange) throws IOException {
|
||||
private static HashMap<String,List<RunReportResponse>> getReportResponses(BetaAnalyticsDataSettings betaAnalyticsDataSettings,
|
||||
List<String> viewIDs, Builder dateRangeBuilder) throws IOException {
|
||||
|
||||
HashMap<String,List<GetReportsResponse>> reports = new HashMap<>();
|
||||
HashMap<String,List<RunReportResponse>> reports = new HashMap<>();
|
||||
|
||||
// Create the Metrics object.
|
||||
Metric sessions = new Metric().setExpression("ga:pageviews").setAlias("pages");
|
||||
com.google.api.services.analyticsreporting.v4.model.Dimension pageTitle = new com.google.api.services.analyticsreporting.v4.model.Dimension().setName("ga:pagePath");
|
||||
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create(betaAnalyticsDataSettings)) {
|
||||
|
||||
for(String view : viewIDs) {
|
||||
List<GetReportsResponse> gReportResponses = new ArrayList<>();
|
||||
logger.info("Getting data from Google Analytics for gateway viewid: " + view);
|
||||
boolean iterateMorePages = true;
|
||||
String nextPageToken = null;
|
||||
while (iterateMorePages) {
|
||||
// Create the ReportRequest object.
|
||||
ReportRequest request = new ReportRequest().setViewId(view.trim()).setDateRanges(Arrays.asList(dateRange))
|
||||
.setMetrics(Arrays.asList(sessions)).setDimensions(Arrays.asList(pageTitle));
|
||||
request.setPageSize(1000);
|
||||
request.setPageToken(nextPageToken);
|
||||
ArrayList<ReportRequest> requests = new ArrayList<ReportRequest>();
|
||||
requests.add(request);
|
||||
// Create the GetReportsRequest object.
|
||||
GetReportsRequest getReport = new GetReportsRequest().setReportRequests(requests);
|
||||
// Call the batchGet method.
|
||||
GetReportsResponse response = service.reports().batchGet(getReport).execute();
|
||||
nextPageToken = response.getReports().get(0).getNextPageToken();
|
||||
iterateMorePages = (nextPageToken != null);
|
||||
logger.debug("got nextPageToken: "+nextPageToken);
|
||||
for(String propertyId : viewIDs) {
|
||||
List<RunReportResponse> gReportResponses = new ArrayList<>();
|
||||
logger.debug("Getting data from Analytics Data API for propertyId: " + propertyId);
|
||||
RunReportRequest request =
|
||||
RunReportRequest.newBuilder()
|
||||
.setProperty("properties/" + propertyId)
|
||||
.addDimensions(Dimension.newBuilder().setName("pagePath"))
|
||||
.addMetrics(Metric.newBuilder().setName("screenPageViews"))
|
||||
.addDateRanges(dateRangeBuilder)
|
||||
.build();
|
||||
|
||||
// Make the request.
|
||||
RunReportResponse response = analyticsData.runReport(request);
|
||||
gReportResponses.add(response);
|
||||
reports.put(propertyId, gReportResponses);
|
||||
}
|
||||
reports.put(view, gReportResponses);
|
||||
}
|
||||
// Return the response.
|
||||
return reports;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses and prints the Analytics Reporting API V4 response.
|
||||
* @param dashboardContext
|
||||
* Parses and prints the Analytics Data API service respose
|
||||
*
|
||||
* @param response An Analytics Reporting API V4 response.
|
||||
* @param response An Analytics Data API service response.
|
||||
*/
|
||||
private static List<CoreServiceAccessesReportRow> parseResponse(String viewId, List<GetReportsResponse> responses, String dashboardContext) {
|
||||
logger.debug("parsing Response for " + viewId);
|
||||
|
||||
private static List<CoreServiceAccessesReportRow> parseResponse(String viewId, List<RunReportResponse> responses, String dashboardContext) {
|
||||
logger.debug("parsing Response for propertyID=" + viewId);
|
||||
List<CoreServiceAccessesReportRow> toReturn = new ArrayList<>();
|
||||
for (GetReportsResponse response : responses) {
|
||||
for (Report report: response.getReports()) {
|
||||
List<ReportRow> rows = report.getData().getRows();
|
||||
if (rows == null) {
|
||||
logger.warn("No data found for " + viewId);
|
||||
}
|
||||
else {
|
||||
for (ReportRow row: rows) {
|
||||
String dimension = row.getDimensions().get(0);
|
||||
DateRangeValues metric = row.getMetrics().get(0);
|
||||
CoreServiceAccessesReportRow var = new CoreServiceAccessesReportRow();
|
||||
boolean validEntry = false;
|
||||
String pagePath = dimension;
|
||||
logger.trace("parsing pagepath {}: value: {}", pagePath, Integer.parseInt(metric.getValues().get(0)));
|
||||
|
||||
for (RunReportResponse response : responses) {
|
||||
for (Row row: response.getRowsList()) {
|
||||
String dimension = row.getDimensionValues(0).getValue();
|
||||
String metric = row.getMetricValues(0).getValue();
|
||||
CoreServiceAccessesReportRow var = new CoreServiceAccessesReportRow();
|
||||
boolean validEntry = false;
|
||||
String pagePath = dimension;
|
||||
logger.trace("parsing pagepath {}: value: {}", pagePath, Integer.parseInt(metric));
|
||||
|
||||
if (!pagePath.contains("_redirect=/group")) {
|
||||
if ( pagePath.contains(PAGE_WORKSPACE_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.WORKSPACE_ACCESSES);
|
||||
logger.trace("**matched "+pagePath);
|
||||
validEntry = true;
|
||||
}
|
||||
else if ( pagePath.contains(PAGE_MESSAGES_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.MESSAGES_ACCESSES);
|
||||
logger.trace("**matched "+pagePath);
|
||||
validEntry = true;
|
||||
}
|
||||
else if ( pagePath.contains(PAGE_PROFILE_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.PROFILE_ACCESSES);
|
||||
logger.trace("**matched "+pagePath);
|
||||
validEntry = true;
|
||||
}
|
||||
else if ( pagePath.contains(PAGE_NOTIFICATION_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.NOTIFICATIONS_ACCESSES);
|
||||
logger.trace("**matched "+pagePath);
|
||||
validEntry = true;
|
||||
}
|
||||
}
|
||||
if (validEntry) {
|
||||
var.setDashboardContext(dashboardContext);
|
||||
var.setPagePath(dimension);
|
||||
var.setVisitNumber(Integer.parseInt(metric.getValues().get(0)));
|
||||
toReturn.add(var);
|
||||
}
|
||||
if (!pagePath.contains("_redirect=/group")) {
|
||||
if ( pagePath.contains(PAGE_WORKSPACE_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.WORKSPACE_ACCESSES);
|
||||
logger.trace("**matched "+pagePath);
|
||||
validEntry = true;
|
||||
}
|
||||
else if ( pagePath.contains(PAGE_MESSAGES_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.MESSAGES_ACCESSES);
|
||||
logger.trace("**matched "+pagePath);
|
||||
validEntry = true;
|
||||
}
|
||||
else if ( pagePath.contains(PAGE_PROFILE_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.PROFILE_ACCESSES);
|
||||
logger.trace("**matched "+pagePath);
|
||||
validEntry = true;
|
||||
}
|
||||
else if ( pagePath.contains(PAGE_NOTIFICATION_ACCESSES)) {
|
||||
var.setKey(HarvestedDataKey.NOTIFICATIONS_ACCESSES);
|
||||
logger.trace("**matched "+pagePath);
|
||||
validEntry = true;
|
||||
}
|
||||
}
|
||||
if (validEntry) {
|
||||
var.setDashboardContext(dashboardContext);
|
||||
var.setPagePath(dimension);
|
||||
var.setVisitNumber(Integer.parseInt(metric));
|
||||
toReturn.add(var);
|
||||
}
|
||||
}
|
||||
}
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private static GoogleCredential fromD4SServiceEndpoint(AnalyticsReportCredentials cred) throws IOException {
|
||||
|
||||
String clientId = cred.getClientId();
|
||||
String clientEmail = cred.getClientEmail();
|
||||
String privateKeyPem = cred.getPrivateKeyPem();
|
||||
String privateKeyId = cred.getPrivateKeyId();
|
||||
String tokenUri = cred.getTokenUri();
|
||||
String projectId = cred.getProjectId();
|
||||
|
||||
if(clientId == null || clientEmail == null || privateKeyPem == null || privateKeyId == null) {
|
||||
throw new IOException("Error reading service account credential from stream, "
|
||||
+ "expecting 'client_id', 'client_email', 'private_key' and 'private_key_id'.");
|
||||
}
|
||||
|
||||
PrivateKey privateKey = privateKeyFromPkcs8(privateKeyPem);
|
||||
|
||||
Collection<String> emptyScopes = Collections.emptyList();
|
||||
|
||||
Builder credentialBuilder = new GoogleCredential.Builder().setTransport(Utils.getDefaultTransport())
|
||||
.setJsonFactory(Utils.getDefaultJsonFactory()).setServiceAccountId(clientEmail)
|
||||
.setServiceAccountScopes(emptyScopes).setServiceAccountPrivateKey(privateKey)
|
||||
.setServiceAccountPrivateKeyId(privateKeyId);
|
||||
|
||||
if(tokenUri != null) {
|
||||
credentialBuilder.setTokenServerEncodedUrl(tokenUri);
|
||||
}
|
||||
|
||||
if(projectId != null) {
|
||||
credentialBuilder.setServiceAccountProjectId(projectId);
|
||||
}
|
||||
|
||||
// Don't do a refresh at this point, as it will always fail before the scopes are added.
|
||||
return credentialBuilder.build();
|
||||
}
|
||||
|
||||
private static PrivateKey privateKeyFromPkcs8(String privateKeyPem) throws IOException {
|
||||
Reader reader = new StringReader(privateKeyPem);
|
||||
Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
|
||||
if(section == null) {
|
||||
throw new IOException("Invalid PKCS8 data.");
|
||||
}
|
||||
byte[] bytes = section.getBase64DecodedBytes();
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
|
||||
Exception unexpectedException = null;
|
||||
try {
|
||||
KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
|
||||
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
||||
return privateKey;
|
||||
} catch(NoSuchAlgorithmException exception) {
|
||||
unexpectedException = exception;
|
||||
} catch(InvalidKeySpecException exception) {
|
||||
unexpectedException = exception;
|
||||
}
|
||||
throw new IOException("Unexpected exception reading PKCS data", unexpectedException);
|
||||
}
|
||||
|
||||
|
||||
private static List<ServiceEndpoint> getAnalyticsReportingConfigurationFromIS(String infrastructureScope)
|
||||
throws Exception {
|
||||
|
@ -445,24 +349,24 @@ public class CoreServicesAccessesHarvester extends BasicHarvester {
|
|||
} else {
|
||||
|
||||
for(ServiceEndpoint res : list) {
|
||||
reportCredentials.setTokenUri(res.profile().runtime().hostedOn());
|
||||
Group<AccessPoint> apGroup = res.profile().accessPoints();
|
||||
AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]);
|
||||
AccessPoint found = accessPoints[0];
|
||||
reportCredentials.setClientEmail(found.address());
|
||||
reportCredentials.setProjectId(found.username());
|
||||
reportCredentials.setPrivateKeyPem(StringEncrypter.getEncrypter().decrypt(found.password()));
|
||||
reportCredentials.setClientEmail(found.username());
|
||||
String decryptedPrivateKey = StringEncrypter.getEncrypter().decrypt(found.password());
|
||||
reportCredentials.setPrivateKeyPem(decryptedPrivateKey.trim());
|
||||
|
||||
for(Property prop : found.properties()) {
|
||||
if(prop.name().compareTo(AP_VIEWS_PROPERTY) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
String[] views = decryptedValue.split(";");
|
||||
reportCredentials.setViewIds(Arrays.asList(views));
|
||||
}
|
||||
if(prop.name().compareTo(AP_CLIENT_PROPERTY) == 0) {
|
||||
if(prop.name().compareTo(AP_CLIENT_ID) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
reportCredentials.setClientId(decryptedValue);
|
||||
}
|
||||
if(prop.name().compareTo(AP_PRIVATEKEY_PROPERTY) == 0) {
|
||||
if(prop.name().compareTo(AP_PRIVATEKEY_ID_PROPERTY) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
reportCredentials.setPrivateKeyId(decryptedValue);
|
||||
}
|
||||
|
@ -476,18 +380,18 @@ public class CoreServicesAccessesHarvester extends BasicHarvester {
|
|||
return reportCredentials;
|
||||
}
|
||||
|
||||
|
||||
private static LocalDate asLocalDate(Date date) {
|
||||
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
}
|
||||
|
||||
private static DateRange getDateRangeForAnalytics(Date start, Date end) {
|
||||
private static Builder getDateRangeBuilderForAnalytics(Date start, Date end) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); //required by Analytics
|
||||
String startDate = asLocalDate(start).format(formatter);
|
||||
String endDate = asLocalDate(end).format(formatter);
|
||||
DateRange dateRange = new DateRange();// date format `yyyy-MM-dd`
|
||||
dateRange.setStartDate(startDate);
|
||||
dateRange.setEndDate(endDate);
|
||||
return dateRange;
|
||||
Builder dateRangeBuilder = DateRange.newBuilder().setStartDate(startDate).setEndDate(endDate);
|
||||
|
||||
return dateRangeBuilder;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,22 +4,13 @@ import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|||
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -42,26 +33,19 @@ import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder;
|
||||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
||||
import com.google.api.client.googleapis.util.Utils;
|
||||
import com.google.api.client.http.HttpTransport;
|
||||
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
|
||||
import com.google.analytics.data.v1beta.BetaAnalyticsDataSettings;
|
||||
import com.google.analytics.data.v1beta.DateRange;
|
||||
import com.google.analytics.data.v1beta.DateRange.Builder;
|
||||
import com.google.analytics.data.v1beta.Dimension;
|
||||
import com.google.analytics.data.v1beta.Metric;
|
||||
import com.google.analytics.data.v1beta.Row;
|
||||
import com.google.analytics.data.v1beta.RunReportRequest;
|
||||
import com.google.analytics.data.v1beta.RunReportResponse;
|
||||
import com.google.api.client.json.JsonFactory;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.google.api.client.util.PemReader;
|
||||
import com.google.api.client.util.PemReader.Section;
|
||||
import com.google.api.client.util.SecurityUtils;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReporting;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReportingScopes;
|
||||
import com.google.api.services.analyticsreporting.v4.model.DateRange;
|
||||
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.Metric;
|
||||
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;
|
||||
import com.google.api.gax.core.FixedCredentialsProvider;
|
||||
import com.google.auth.oauth2.ServiceAccountCredentials;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -75,11 +59,10 @@ public class JupyterAccessesHarvester extends BasicHarvester {
|
|||
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||
|
||||
private static final String SERVICE_ENDPOINT_CATEGORY = "OnlineService";
|
||||
private static final String SERVICE_ENDPOINT_NAME = "BigGAnalyticsReportService";
|
||||
private static final String SERVICE_ENDPOINT_NAME = "GA4AnalyticsDataService";
|
||||
private static final String AP_VIEWS_PROPERTY = "views";
|
||||
private static final String AP_CLIENT_PROPERTY = "clientId";
|
||||
private static final String AP_PRIVATEKEY_PROPERTY = "privateKeyId";
|
||||
private static final String APPLICATION_NAME = "Analytics Reporting";
|
||||
private static final String AP_CLIENT_ID = "client_id";
|
||||
private static final String AP_PRIVATEKEY_ID_PROPERTY = "private_key_id";
|
||||
|
||||
private List<VREAccessesReportRow> vreAccesses;
|
||||
|
||||
|
@ -115,7 +98,7 @@ public class JupyterAccessesHarvester extends BasicHarvester {
|
|||
}
|
||||
}
|
||||
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor();
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor(context);
|
||||
|
||||
if (measure > 0) {
|
||||
AccountingRecord ar = new AccountingRecord(scopeDescriptor, instant,
|
||||
|
@ -143,188 +126,111 @@ public class JupyterAccessesHarvester extends BasicHarvester {
|
|||
* visitNumber=39]
|
||||
*/
|
||||
private static List<VREAccessesReportRow> getAllAccesses(Date start, Date end) throws Exception {
|
||||
DateRange dateRange = getDateRangeForAnalytics(start, end);
|
||||
logger.trace("Getting accesses in this time range {}", dateRange.toPrettyString());
|
||||
Builder dateRangeBuilder = getDateRangeBuilderForAnalytics(start, end);
|
||||
logger.debug("Getting accesses in this time range {}", dateRangeBuilder.toString());
|
||||
|
||||
AnalyticsReportCredentials credentialsFromD4S = getAuthorisedApplicationInfoFromIs();
|
||||
AnalyticsReporting service = initializeAnalyticsReporting(credentialsFromD4S);
|
||||
HashMap<String, List<GetReportsResponse>> responses = getReportResponses(service,
|
||||
credentialsFromD4S.getViewIds(), dateRange);
|
||||
|
||||
logger.debug("Getting credentials credentialsFromD4S");
|
||||
|
||||
BetaAnalyticsDataSettings serviceSettings = initializeAnalyticsReporting(credentialsFromD4S);
|
||||
|
||||
logger.debug("initializeAnalyticsReporting service settings");
|
||||
|
||||
|
||||
HashMap<String,List<RunReportResponse>> responses = getReportResponses(serviceSettings, credentialsFromD4S.getViewIds(), dateRangeBuilder);
|
||||
List<VREAccessesReportRow> totalAccesses = new ArrayList<>();
|
||||
|
||||
for (String view : responses.keySet()) {
|
||||
for(String view : responses.keySet()) {
|
||||
List<VREAccessesReportRow> viewReport = parseResponse(view, responses.get(view));
|
||||
logger.trace("Got {} entries from view id={}", viewReport.size(), view);
|
||||
logger.debug("Got {} entries from view id={}", viewReport.size(), view);
|
||||
totalAccesses.addAll(viewReport);
|
||||
}
|
||||
logger.trace("Merged in {} total entries from all views", totalAccesses.size());
|
||||
logger.debug("Merged in {} total entries from all views", totalAccesses.size());
|
||||
return totalAccesses;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Initializes an Analytics Reporting API V4 service object.
|
||||
* Initializes an Google Analytics Data API service object.
|
||||
*
|
||||
* @return An authorized Analytics Reporting API V4 service object.
|
||||
* @return An authorized Google Analytics Data API
|
||||
* @throws IOException
|
||||
* @throws GeneralSecurityException
|
||||
*/
|
||||
private static AnalyticsReporting initializeAnalyticsReporting(AnalyticsReportCredentials cred)
|
||||
throws GeneralSecurityException, IOException {
|
||||
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
|
||||
GoogleCredential credential = fromD4SServiceEndpoint(cred).createScoped(AnalyticsReportingScopes.all());
|
||||
|
||||
// Construct the Analytics Reporting service object.
|
||||
return new AnalyticsReporting.Builder(httpTransport, JSON_FACTORY, credential)
|
||||
.setApplicationName(APPLICATION_NAME).build();
|
||||
private static BetaAnalyticsDataSettings initializeAnalyticsReporting(AnalyticsReportCredentials cred) throws IOException {
|
||||
return BetaAnalyticsDataSettings.newBuilder()
|
||||
.setCredentialsProvider(FixedCredentialsProvider.create(
|
||||
ServiceAccountCredentials.fromPkcs8(cred.getClientId(), cred.getClientEmail(), cred.getPrivateKeyPem(), cred.getPrivateKeyId(), null)))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries the Analytics Reporting API V4.
|
||||
* Queries Analytics Data API service
|
||||
*
|
||||
* @param service
|
||||
* An authorized Analytics Reporting API V4 service object.
|
||||
* @return GetReportResponse The Analytics Reporting API V4 response.
|
||||
* @param service Analytics Data API service service settings.
|
||||
* @return Row Analytics Data API service
|
||||
* @throws IOException
|
||||
*/
|
||||
private static HashMap<String, List<GetReportsResponse>> getReportResponses(AnalyticsReporting service,
|
||||
List<String> viewIDs, DateRange dateRange) throws IOException {
|
||||
private static HashMap<String,List<RunReportResponse>> getReportResponses(BetaAnalyticsDataSettings betaAnalyticsDataSettings,
|
||||
List<String> viewIDs, Builder dateRangeBuilder) throws IOException {
|
||||
|
||||
HashMap<String, List<GetReportsResponse>> reports = new HashMap<>();
|
||||
HashMap<String,List<RunReportResponse>> reports = new HashMap<>();
|
||||
|
||||
// Create the Metrics object.
|
||||
Metric sessions = new Metric().setExpression("ga:pageviews").setAlias("pages");
|
||||
com.google.api.services.analyticsreporting.v4.model.Dimension pageTitle = new com.google.api.services.analyticsreporting.v4.model.Dimension()
|
||||
.setName("ga:pagePath");
|
||||
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create(betaAnalyticsDataSettings)) {
|
||||
|
||||
for (String view : viewIDs) {
|
||||
List<GetReportsResponse> gReportResponses = new ArrayList<>();
|
||||
logger.info("Getting data from Google Analytics for viewid: " + view);
|
||||
boolean iterateMorePages = true;
|
||||
String nextPageToken = null;
|
||||
while (iterateMorePages) {
|
||||
// Create the ReportRequest object.
|
||||
ReportRequest request = new ReportRequest().setViewId(view.trim())
|
||||
.setDateRanges(Arrays.asList(dateRange)).setMetrics(Arrays.asList(sessions))
|
||||
.setDimensions(Arrays.asList(pageTitle));
|
||||
request.setPageSize(1000);
|
||||
request.setPageToken(nextPageToken);
|
||||
ArrayList<ReportRequest> requests = new ArrayList<ReportRequest>();
|
||||
requests.add(request);
|
||||
// Create the GetReportsRequest object.
|
||||
GetReportsRequest getReport = new GetReportsRequest().setReportRequests(requests);
|
||||
// Call the batchGet method.
|
||||
GetReportsResponse response = service.reports().batchGet(getReport).execute();
|
||||
nextPageToken = response.getReports().get(0).getNextPageToken();
|
||||
iterateMorePages = (nextPageToken != null);
|
||||
logger.debug("got nextPageToken: " + nextPageToken);
|
||||
for(String propertyId : viewIDs) {
|
||||
List<RunReportResponse> gReportResponses = new ArrayList<>();
|
||||
logger.debug("Getting data from Analytics Data API for propertyId: " + propertyId);
|
||||
RunReportRequest request =
|
||||
RunReportRequest.newBuilder()
|
||||
.setProperty("properties/" + propertyId)
|
||||
.addDimensions(Dimension.newBuilder().setName("pagePath"))
|
||||
.addMetrics(Metric.newBuilder().setName("screenPageViews"))
|
||||
.addDateRanges(dateRangeBuilder)
|
||||
.build();
|
||||
|
||||
// Make the request.
|
||||
RunReportResponse response = analyticsData.runReport(request);
|
||||
gReportResponses.add(response);
|
||||
reports.put(propertyId, gReportResponses);
|
||||
}
|
||||
reports.put(view, gReportResponses);
|
||||
}
|
||||
// Return the response.
|
||||
return reports;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses and prints the Analytics Reporting API V4 response.
|
||||
* Parses and prints the Analytics Data API service respose
|
||||
*
|
||||
* @param response
|
||||
* An Analytics Reporting API V4 response.
|
||||
* @param response An Analytics Data API service response.
|
||||
*/
|
||||
/**
|
||||
* Parses and prints the Analytics Reporting API V4 response.
|
||||
*
|
||||
* @param response
|
||||
* An Analytics Reporting API V4 response.
|
||||
*/
|
||||
private static List<VREAccessesReportRow> parseResponse(String viewId, List<GetReportsResponse> responses) {
|
||||
logger.debug("parsing Response for " + viewId);
|
||||
private static List<VREAccessesReportRow> parseResponse(String viewId, List<RunReportResponse> responses) {
|
||||
logger.debug("parsing Response for propertyID=" + viewId);
|
||||
|
||||
List<VREAccessesReportRow> toReturn = new ArrayList<>();
|
||||
for (GetReportsResponse response : responses) {
|
||||
for (Report report : response.getReports()) {
|
||||
List<ReportRow> rows = report.getData().getRows();
|
||||
if (rows == null) {
|
||||
logger.warn("No data found for " + viewId);
|
||||
} else {
|
||||
for (ReportRow row : rows) {
|
||||
String dimension = row.getDimensions().get(0);
|
||||
DateRangeValues metric = row.getMetrics().get(0);
|
||||
VREAccessesReportRow var = new VREAccessesReportRow();
|
||||
boolean validEntry = false;
|
||||
String pagePath = dimension;
|
||||
if (pagePath.startsWith("/group") || pagePath.startsWith("/web")) {
|
||||
var.setPagePath(dimension);
|
||||
validEntry = true;
|
||||
}
|
||||
if (validEntry) {
|
||||
var.setVisitNumber(Integer.parseInt(metric.getValues().get(0)));
|
||||
toReturn.add(var);
|
||||
}
|
||||
}
|
||||
for (RunReportResponse response : responses) {
|
||||
for (Row row: response.getRowsList()) {
|
||||
String dimension = row.getDimensionValues(0).getValue();
|
||||
String metric = row.getMetricValues(0).getValue();
|
||||
VREAccessesReportRow var = new VREAccessesReportRow();
|
||||
boolean validEntry = false;
|
||||
String pagePath = dimension;
|
||||
if (pagePath.startsWith("/group") || pagePath.startsWith("/web")) {
|
||||
var.setPagePath(dimension);
|
||||
validEntry = true;
|
||||
}
|
||||
if (validEntry) {
|
||||
var.setVisitNumber(Integer.parseInt(metric));
|
||||
toReturn.add(var);
|
||||
}
|
||||
}
|
||||
}
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private static GoogleCredential fromD4SServiceEndpoint(AnalyticsReportCredentials cred) throws IOException {
|
||||
|
||||
String clientId = cred.getClientId();
|
||||
String clientEmail = cred.getClientEmail();
|
||||
String privateKeyPem = cred.getPrivateKeyPem();
|
||||
String privateKeyId = cred.getPrivateKeyId();
|
||||
String tokenUri = cred.getTokenUri();
|
||||
String projectId = cred.getProjectId();
|
||||
|
||||
if (clientId == null || clientEmail == null || privateKeyPem == null || privateKeyId == null) {
|
||||
throw new IOException("Error reading service account credential from stream, "
|
||||
+ "expecting 'client_id', 'client_email', 'private_key' and 'private_key_id'.");
|
||||
}
|
||||
|
||||
PrivateKey privateKey = privateKeyFromPkcs8(privateKeyPem);
|
||||
|
||||
Collection<String> emptyScopes = Collections.emptyList();
|
||||
|
||||
Builder credentialBuilder = new GoogleCredential.Builder().setTransport(Utils.getDefaultTransport())
|
||||
.setJsonFactory(Utils.getDefaultJsonFactory()).setServiceAccountId(clientEmail)
|
||||
.setServiceAccountScopes(emptyScopes).setServiceAccountPrivateKey(privateKey)
|
||||
.setServiceAccountPrivateKeyId(privateKeyId);
|
||||
|
||||
if (tokenUri != null) {
|
||||
credentialBuilder.setTokenServerEncodedUrl(tokenUri);
|
||||
}
|
||||
|
||||
if (projectId != null) {
|
||||
credentialBuilder.setServiceAccountProjectId(projectId);
|
||||
}
|
||||
|
||||
// Don't do a refresh at this point, as it will always fail before the
|
||||
// scopes are added.
|
||||
return credentialBuilder.build();
|
||||
}
|
||||
|
||||
private static PrivateKey privateKeyFromPkcs8(String privateKeyPem) throws IOException {
|
||||
Reader reader = new StringReader(privateKeyPem);
|
||||
Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
|
||||
if (section == null) {
|
||||
throw new IOException("Invalid PKCS8 data.");
|
||||
}
|
||||
byte[] bytes = section.getBase64DecodedBytes();
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
|
||||
Exception unexpectedException = null;
|
||||
try {
|
||||
KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
|
||||
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
||||
return privateKey;
|
||||
} catch (NoSuchAlgorithmException exception) {
|
||||
unexpectedException = exception;
|
||||
} catch (InvalidKeySpecException exception) {
|
||||
unexpectedException = exception;
|
||||
}
|
||||
throw new IOException("Unexpected exception reading PKCS data", unexpectedException);
|
||||
}
|
||||
|
||||
private static List<ServiceEndpoint> getAnalyticsReportingConfigurationFromIS(String infrastructureScope)
|
||||
throws Exception {
|
||||
String scope = infrastructureScope;
|
||||
|
@ -341,8 +247,7 @@ public class JupyterAccessesHarvester extends BasicHarvester {
|
|||
|
||||
/**
|
||||
* l
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
private static AnalyticsReportCredentials getAuthorisedApplicationInfoFromIs() throws Exception {
|
||||
AnalyticsReportCredentials reportCredentials = new AnalyticsReportCredentials();
|
||||
|
@ -350,60 +255,58 @@ public class JupyterAccessesHarvester extends BasicHarvester {
|
|||
String context = org.gcube.dataharvest.utils.Utils.getCurrentContext();
|
||||
try {
|
||||
List<ServiceEndpoint> list = getAnalyticsReportingConfigurationFromIS(context);
|
||||
if (list.size() > 1) {
|
||||
if(list.size() > 1) {
|
||||
logger.error("Too many Service Endpoints having name " + SERVICE_ENDPOINT_NAME
|
||||
+ " in this scope having Category " + SERVICE_ENDPOINT_CATEGORY);
|
||||
} else if (list.size() == 0) {
|
||||
} else if(list.size() == 0) {
|
||||
logger.warn("There is no Service Endpoint having name " + SERVICE_ENDPOINT_NAME + " and Category "
|
||||
+ SERVICE_ENDPOINT_CATEGORY + " in this context: " + context);
|
||||
} else {
|
||||
|
||||
for (ServiceEndpoint res : list) {
|
||||
reportCredentials.setTokenUri(res.profile().runtime().hostedOn());
|
||||
for(ServiceEndpoint res : list) {
|
||||
Group<AccessPoint> apGroup = res.profile().accessPoints();
|
||||
AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]);
|
||||
AccessPoint found = accessPoints[0];
|
||||
reportCredentials.setClientEmail(found.address());
|
||||
reportCredentials.setProjectId(found.username());
|
||||
reportCredentials.setPrivateKeyPem(StringEncrypter.getEncrypter().decrypt(found.password()));
|
||||
for (Property prop : found.properties()) {
|
||||
if (prop.name().compareTo(AP_VIEWS_PROPERTY) == 0) {
|
||||
reportCredentials.setClientEmail(found.username());
|
||||
String decryptedPrivateKey = StringEncrypter.getEncrypter().decrypt(found.password());
|
||||
reportCredentials.setPrivateKeyPem(decryptedPrivateKey.trim());
|
||||
|
||||
for(Property prop : found.properties()) {
|
||||
if(prop.name().compareTo(AP_VIEWS_PROPERTY) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
String[] views = decryptedValue.split(";");
|
||||
reportCredentials.setViewIds(Arrays.asList(views));
|
||||
}
|
||||
if (prop.name().compareTo(AP_CLIENT_PROPERTY) == 0) {
|
||||
if(prop.name().compareTo(AP_CLIENT_ID) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
reportCredentials.setClientId(decryptedValue);
|
||||
}
|
||||
if (prop.name().compareTo(AP_PRIVATEKEY_PROPERTY) == 0) {
|
||||
if(prop.name().compareTo(AP_PRIVATEKEY_ID_PROPERTY) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
reportCredentials.setPrivateKeyId(decryptedValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
return reportCredentials;
|
||||
}
|
||||
|
||||
|
||||
private static LocalDate asLocalDate(Date date) {
|
||||
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
}
|
||||
|
||||
private static DateRange getDateRangeForAnalytics(Date start, Date end) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // required
|
||||
// by
|
||||
// Analytics
|
||||
private static Builder getDateRangeBuilderForAnalytics(Date start, Date end) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); //required by Analytics
|
||||
String startDate = asLocalDate(start).format(formatter);
|
||||
String endDate = asLocalDate(end).format(formatter);
|
||||
DateRange dateRange = new DateRange();// date format `yyyy-MM-dd`
|
||||
dateRange.setStartDate(startDate);
|
||||
dateRange.setEndDate(endDate);
|
||||
return dateRange;
|
||||
Builder dateRangeBuilder = DateRange.newBuilder().setStartDate(startDate).setEndDate(endDate);
|
||||
|
||||
return dateRangeBuilder;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,7 +28,6 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Eric Perrone (ISTI - CNR)
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class MethodInvocationHarvester extends BasicHarvester {
|
||||
|
@ -58,7 +57,6 @@ public class MethodInvocationHarvester extends BasicHarvester {
|
|||
|
||||
SortedMap<Filter,SortedMap<Calendar,Info>> result = null;
|
||||
List<Filter> filters = new ArrayList<>();
|
||||
filters.add(new Filter(ServiceUsageRecord.SERVICE_NAME, DATAMINER_SERVICE_NAME));
|
||||
|
||||
Date newMethodInvocationHarvesterStartDate = DateUtils.getStartCalendar(2017, Calendar.DECEMBER, 31).getTime();
|
||||
|
||||
|
@ -68,11 +66,12 @@ public class MethodInvocationHarvester extends BasicHarvester {
|
|||
AggregatedJobUsageRecord.class, temporalConstraint, filters, contexts, true);
|
||||
} else {
|
||||
// Before 31/12/2017 accounting Method Invocation using ServiceUsageRecord
|
||||
filters.add(new Filter(ServiceUsageRecord.SERVICE_NAME, DATAMINER_SERVICE_NAME));
|
||||
result = accountingPersistenceQuery.getContextTimeSeries(
|
||||
AggregatedServiceUsageRecord.class, temporalConstraint, filters, contexts, true);
|
||||
}
|
||||
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor();
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor(context);
|
||||
Dimension dimension = getDimension(HarvestedDataKey.METHOD_INVOCATIONS);
|
||||
|
||||
if(result != null) {
|
||||
|
|
|
@ -4,22 +4,13 @@ import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|||
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -42,26 +33,20 @@ import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder;
|
||||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
||||
import com.google.api.client.googleapis.util.Utils;
|
||||
import com.google.api.client.http.HttpTransport;
|
||||
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
|
||||
import com.google.analytics.data.v1beta.BetaAnalyticsDataSettings;
|
||||
import com.google.analytics.data.v1beta.DateRange;
|
||||
import com.google.analytics.data.v1beta.DateRange.Builder;
|
||||
import com.google.analytics.data.v1beta.Dimension;
|
||||
import com.google.analytics.data.v1beta.Metric;
|
||||
import com.google.analytics.data.v1beta.Row;
|
||||
import com.google.analytics.data.v1beta.RunReportRequest;
|
||||
import com.google.analytics.data.v1beta.RunReportResponse;
|
||||
import com.google.api.client.json.JsonFactory;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.google.api.client.util.PemReader;
|
||||
import com.google.api.client.util.PemReader.Section;
|
||||
import com.google.api.client.util.SecurityUtils;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReporting;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReportingScopes;
|
||||
import com.google.api.services.analyticsreporting.v4.model.DateRange;
|
||||
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.Metric;
|
||||
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;
|
||||
import com.google.api.gax.core.FixedCredentialsProvider;
|
||||
import com.google.auth.oauth2.ServiceAccountCredentials;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -75,11 +60,10 @@ public class RStudioAccessesHarvester extends BasicHarvester {
|
|||
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||
|
||||
private static final String SERVICE_ENDPOINT_CATEGORY = "OnlineService";
|
||||
private static final String SERVICE_ENDPOINT_NAME = "BigGAnalyticsReportService";
|
||||
private static final String SERVICE_ENDPOINT_NAME = "GA4AnalyticsDataService";
|
||||
private static final String AP_VIEWS_PROPERTY = "views";
|
||||
private static final String AP_CLIENT_PROPERTY = "clientId";
|
||||
private static final String AP_PRIVATEKEY_PROPERTY = "privateKeyId";
|
||||
private static final String APPLICATION_NAME = "Analytics Reporting";
|
||||
private static final String AP_CLIENT_ID = "client_id";
|
||||
private static final String AP_PRIVATEKEY_ID_PROPERTY = "private_key_id";
|
||||
|
||||
private List<VREAccessesReportRow> vreAccesses;
|
||||
|
||||
|
@ -115,7 +99,7 @@ public class RStudioAccessesHarvester extends BasicHarvester {
|
|||
}
|
||||
}
|
||||
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor();
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor(context);
|
||||
|
||||
if (measure > 0) {
|
||||
AccountingRecord ar = new AccountingRecord(scopeDescriptor, instant,
|
||||
|
@ -143,188 +127,111 @@ public class RStudioAccessesHarvester extends BasicHarvester {
|
|||
* visitNumber=39]
|
||||
*/
|
||||
private static List<VREAccessesReportRow> getAllAccesses(Date start, Date end) throws Exception {
|
||||
DateRange dateRange = getDateRangeForAnalytics(start, end);
|
||||
logger.trace("Getting accesses in this time range {}", dateRange.toPrettyString());
|
||||
Builder dateRangeBuilder = getDateRangeBuilderForAnalytics(start, end);
|
||||
logger.debug("Getting accesses in this time range {}", dateRangeBuilder.toString());
|
||||
|
||||
AnalyticsReportCredentials credentialsFromD4S = getAuthorisedApplicationInfoFromIs();
|
||||
AnalyticsReporting service = initializeAnalyticsReporting(credentialsFromD4S);
|
||||
HashMap<String, List<GetReportsResponse>> responses = getReportResponses(service,
|
||||
credentialsFromD4S.getViewIds(), dateRange);
|
||||
|
||||
logger.debug("Getting credentials credentialsFromD4S");
|
||||
|
||||
BetaAnalyticsDataSettings serviceSettings = initializeAnalyticsReporting(credentialsFromD4S);
|
||||
|
||||
logger.debug("initializeAnalyticsReporting service settings");
|
||||
|
||||
|
||||
HashMap<String,List<RunReportResponse>> responses = getReportResponses(serviceSettings, credentialsFromD4S.getViewIds(), dateRangeBuilder);
|
||||
List<VREAccessesReportRow> totalAccesses = new ArrayList<>();
|
||||
|
||||
for (String view : responses.keySet()) {
|
||||
for(String view : responses.keySet()) {
|
||||
List<VREAccessesReportRow> viewReport = parseResponse(view, responses.get(view));
|
||||
logger.trace("Got {} entries from view id={}", viewReport.size(), view);
|
||||
logger.debug("Got {} entries from view id={}", viewReport.size(), view);
|
||||
totalAccesses.addAll(viewReport);
|
||||
}
|
||||
logger.trace("Merged in {} total entries from all views", totalAccesses.size());
|
||||
logger.debug("Merged in {} total entries from all views", totalAccesses.size());
|
||||
return totalAccesses;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Initializes an Analytics Reporting API V4 service object.
|
||||
* Initializes an Google Analytics Data API service object.
|
||||
*
|
||||
* @return An authorized Analytics Reporting API V4 service object.
|
||||
* @return An authorized Google Analytics Data API
|
||||
* @throws IOException
|
||||
* @throws GeneralSecurityException
|
||||
*/
|
||||
private static AnalyticsReporting initializeAnalyticsReporting(AnalyticsReportCredentials cred)
|
||||
throws GeneralSecurityException, IOException {
|
||||
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
|
||||
GoogleCredential credential = fromD4SServiceEndpoint(cred).createScoped(AnalyticsReportingScopes.all());
|
||||
|
||||
// Construct the Analytics Reporting service object.
|
||||
return new AnalyticsReporting.Builder(httpTransport, JSON_FACTORY, credential)
|
||||
.setApplicationName(APPLICATION_NAME).build();
|
||||
private static BetaAnalyticsDataSettings initializeAnalyticsReporting(AnalyticsReportCredentials cred) throws IOException {
|
||||
return BetaAnalyticsDataSettings.newBuilder()
|
||||
.setCredentialsProvider(FixedCredentialsProvider.create(
|
||||
ServiceAccountCredentials.fromPkcs8(cred.getClientId(), cred.getClientEmail(), cred.getPrivateKeyPem(), cred.getPrivateKeyId(), null)))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries the Analytics Reporting API V4.
|
||||
* Queries Analytics Data API service
|
||||
*
|
||||
* @param service
|
||||
* An authorized Analytics Reporting API V4 service object.
|
||||
* @return GetReportResponse The Analytics Reporting API V4 response.
|
||||
* @param service Analytics Data API service service settings.
|
||||
* @return Row Analytics Data API service
|
||||
* @throws IOException
|
||||
*/
|
||||
private static HashMap<String, List<GetReportsResponse>> getReportResponses(AnalyticsReporting service,
|
||||
List<String> viewIDs, DateRange dateRange) throws IOException {
|
||||
private static HashMap<String,List<RunReportResponse>> getReportResponses(BetaAnalyticsDataSettings betaAnalyticsDataSettings,
|
||||
List<String> viewIDs, Builder dateRangeBuilder) throws IOException {
|
||||
|
||||
HashMap<String, List<GetReportsResponse>> reports = new HashMap<>();
|
||||
HashMap<String,List<RunReportResponse>> reports = new HashMap<>();
|
||||
|
||||
// Create the Metrics object.
|
||||
Metric sessions = new Metric().setExpression("ga:pageviews").setAlias("pages");
|
||||
com.google.api.services.analyticsreporting.v4.model.Dimension pageTitle = new com.google.api.services.analyticsreporting.v4.model.Dimension()
|
||||
.setName("ga:pagePath");
|
||||
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create(betaAnalyticsDataSettings)) {
|
||||
|
||||
for (String view : viewIDs) {
|
||||
List<GetReportsResponse> gReportResponses = new ArrayList<>();
|
||||
logger.info("Getting data from Google Analytics for viewid: " + view);
|
||||
boolean iterateMorePages = true;
|
||||
String nextPageToken = null;
|
||||
while (iterateMorePages) {
|
||||
// Create the ReportRequest object.
|
||||
ReportRequest request = new ReportRequest().setViewId(view.trim())
|
||||
.setDateRanges(Arrays.asList(dateRange)).setMetrics(Arrays.asList(sessions))
|
||||
.setDimensions(Arrays.asList(pageTitle));
|
||||
request.setPageSize(1000);
|
||||
request.setPageToken(nextPageToken);
|
||||
ArrayList<ReportRequest> requests = new ArrayList<ReportRequest>();
|
||||
requests.add(request);
|
||||
// Create the GetReportsRequest object.
|
||||
GetReportsRequest getReport = new GetReportsRequest().setReportRequests(requests);
|
||||
// Call the batchGet method.
|
||||
GetReportsResponse response = service.reports().batchGet(getReport).execute();
|
||||
nextPageToken = response.getReports().get(0).getNextPageToken();
|
||||
iterateMorePages = (nextPageToken != null);
|
||||
logger.debug("got nextPageToken: " + nextPageToken);
|
||||
for(String propertyId : viewIDs) {
|
||||
List<RunReportResponse> gReportResponses = new ArrayList<>();
|
||||
logger.debug("Getting data from Analytics Data API for propertyId: " + propertyId);
|
||||
RunReportRequest request =
|
||||
RunReportRequest.newBuilder()
|
||||
.setProperty("properties/" + propertyId)
|
||||
.addDimensions(Dimension.newBuilder().setName("pagePath"))
|
||||
.addMetrics(Metric.newBuilder().setName("screenPageViews"))
|
||||
.addDateRanges(dateRangeBuilder)
|
||||
.build();
|
||||
|
||||
// Make the request.
|
||||
RunReportResponse response = analyticsData.runReport(request);
|
||||
gReportResponses.add(response);
|
||||
reports.put(propertyId, gReportResponses);
|
||||
}
|
||||
reports.put(view, gReportResponses);
|
||||
}
|
||||
// Return the response.
|
||||
return reports;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses and prints the Analytics Reporting API V4 response.
|
||||
* Parses and prints the Analytics Data API service respose
|
||||
*
|
||||
* @param response
|
||||
* An Analytics Reporting API V4 response.
|
||||
* @param response An Analytics Data API service response.
|
||||
*/
|
||||
/**
|
||||
* Parses and prints the Analytics Reporting API V4 response.
|
||||
*
|
||||
* @param response
|
||||
* An Analytics Reporting API V4 response.
|
||||
*/
|
||||
private static List<VREAccessesReportRow> parseResponse(String viewId, List<GetReportsResponse> responses) {
|
||||
logger.debug("parsing Response for " + viewId);
|
||||
private static List<VREAccessesReportRow> parseResponse(String viewId, List<RunReportResponse> responses) {
|
||||
logger.debug("parsing Response for propertyID=" + viewId);
|
||||
|
||||
List<VREAccessesReportRow> toReturn = new ArrayList<>();
|
||||
for (GetReportsResponse response : responses) {
|
||||
for (Report report : response.getReports()) {
|
||||
List<ReportRow> rows = report.getData().getRows();
|
||||
if (rows == null) {
|
||||
logger.warn("No data found for " + viewId);
|
||||
} else {
|
||||
for (ReportRow row : rows) {
|
||||
String dimension = row.getDimensions().get(0);
|
||||
DateRangeValues metric = row.getMetrics().get(0);
|
||||
VREAccessesReportRow var = new VREAccessesReportRow();
|
||||
boolean validEntry = false;
|
||||
String pagePath = dimension;
|
||||
if (pagePath.startsWith("/group") || pagePath.startsWith("/web")) {
|
||||
var.setPagePath(dimension);
|
||||
validEntry = true;
|
||||
}
|
||||
if (validEntry) {
|
||||
var.setVisitNumber(Integer.parseInt(metric.getValues().get(0)));
|
||||
toReturn.add(var);
|
||||
}
|
||||
}
|
||||
for (RunReportResponse response : responses) {
|
||||
for (Row row: response.getRowsList()) {
|
||||
String dimension = row.getDimensionValues(0).getValue();
|
||||
String metric = row.getMetricValues(0).getValue();
|
||||
VREAccessesReportRow var = new VREAccessesReportRow();
|
||||
boolean validEntry = false;
|
||||
String pagePath = dimension;
|
||||
if (pagePath.startsWith("/group") || pagePath.startsWith("/web")) {
|
||||
var.setPagePath(dimension);
|
||||
validEntry = true;
|
||||
}
|
||||
if (validEntry) {
|
||||
var.setVisitNumber(Integer.parseInt(metric));
|
||||
toReturn.add(var);
|
||||
}
|
||||
}
|
||||
}
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private static GoogleCredential fromD4SServiceEndpoint(AnalyticsReportCredentials cred) throws IOException {
|
||||
|
||||
String clientId = cred.getClientId();
|
||||
String clientEmail = cred.getClientEmail();
|
||||
String privateKeyPem = cred.getPrivateKeyPem();
|
||||
String privateKeyId = cred.getPrivateKeyId();
|
||||
String tokenUri = cred.getTokenUri();
|
||||
String projectId = cred.getProjectId();
|
||||
|
||||
if (clientId == null || clientEmail == null || privateKeyPem == null || privateKeyId == null) {
|
||||
throw new IOException("Error reading service account credential from stream, "
|
||||
+ "expecting 'client_id', 'client_email', 'private_key' and 'private_key_id'.");
|
||||
}
|
||||
|
||||
PrivateKey privateKey = privateKeyFromPkcs8(privateKeyPem);
|
||||
|
||||
Collection<String> emptyScopes = Collections.emptyList();
|
||||
|
||||
Builder credentialBuilder = new GoogleCredential.Builder().setTransport(Utils.getDefaultTransport())
|
||||
.setJsonFactory(Utils.getDefaultJsonFactory()).setServiceAccountId(clientEmail)
|
||||
.setServiceAccountScopes(emptyScopes).setServiceAccountPrivateKey(privateKey)
|
||||
.setServiceAccountPrivateKeyId(privateKeyId);
|
||||
|
||||
if (tokenUri != null) {
|
||||
credentialBuilder.setTokenServerEncodedUrl(tokenUri);
|
||||
}
|
||||
|
||||
if (projectId != null) {
|
||||
credentialBuilder.setServiceAccountProjectId(projectId);
|
||||
}
|
||||
|
||||
// Don't do a refresh at this point, as it will always fail before the
|
||||
// scopes are added.
|
||||
return credentialBuilder.build();
|
||||
}
|
||||
|
||||
private static PrivateKey privateKeyFromPkcs8(String privateKeyPem) throws IOException {
|
||||
Reader reader = new StringReader(privateKeyPem);
|
||||
Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
|
||||
if (section == null) {
|
||||
throw new IOException("Invalid PKCS8 data.");
|
||||
}
|
||||
byte[] bytes = section.getBase64DecodedBytes();
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
|
||||
Exception unexpectedException = null;
|
||||
try {
|
||||
KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
|
||||
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
||||
return privateKey;
|
||||
} catch (NoSuchAlgorithmException exception) {
|
||||
unexpectedException = exception;
|
||||
} catch (InvalidKeySpecException exception) {
|
||||
unexpectedException = exception;
|
||||
}
|
||||
throw new IOException("Unexpected exception reading PKCS data", unexpectedException);
|
||||
}
|
||||
|
||||
private static List<ServiceEndpoint> getAnalyticsReportingConfigurationFromIS(String infrastructureScope)
|
||||
throws Exception {
|
||||
String scope = infrastructureScope;
|
||||
|
@ -341,8 +248,7 @@ public class RStudioAccessesHarvester extends BasicHarvester {
|
|||
|
||||
/**
|
||||
* l
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
private static AnalyticsReportCredentials getAuthorisedApplicationInfoFromIs() throws Exception {
|
||||
AnalyticsReportCredentials reportCredentials = new AnalyticsReportCredentials();
|
||||
|
@ -350,60 +256,58 @@ public class RStudioAccessesHarvester extends BasicHarvester {
|
|||
String context = org.gcube.dataharvest.utils.Utils.getCurrentContext();
|
||||
try {
|
||||
List<ServiceEndpoint> list = getAnalyticsReportingConfigurationFromIS(context);
|
||||
if (list.size() > 1) {
|
||||
if(list.size() > 1) {
|
||||
logger.error("Too many Service Endpoints having name " + SERVICE_ENDPOINT_NAME
|
||||
+ " in this scope having Category " + SERVICE_ENDPOINT_CATEGORY);
|
||||
} else if (list.size() == 0) {
|
||||
} else if(list.size() == 0) {
|
||||
logger.warn("There is no Service Endpoint having name " + SERVICE_ENDPOINT_NAME + " and Category "
|
||||
+ SERVICE_ENDPOINT_CATEGORY + " in this context: " + context);
|
||||
} else {
|
||||
|
||||
for (ServiceEndpoint res : list) {
|
||||
reportCredentials.setTokenUri(res.profile().runtime().hostedOn());
|
||||
for(ServiceEndpoint res : list) {
|
||||
Group<AccessPoint> apGroup = res.profile().accessPoints();
|
||||
AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]);
|
||||
AccessPoint found = accessPoints[0];
|
||||
reportCredentials.setClientEmail(found.address());
|
||||
reportCredentials.setProjectId(found.username());
|
||||
reportCredentials.setPrivateKeyPem(StringEncrypter.getEncrypter().decrypt(found.password()));
|
||||
for (Property prop : found.properties()) {
|
||||
if (prop.name().compareTo(AP_VIEWS_PROPERTY) == 0) {
|
||||
reportCredentials.setClientEmail(found.username());
|
||||
String decryptedPrivateKey = StringEncrypter.getEncrypter().decrypt(found.password());
|
||||
reportCredentials.setPrivateKeyPem(decryptedPrivateKey.trim());
|
||||
|
||||
for(Property prop : found.properties()) {
|
||||
if(prop.name().compareTo(AP_VIEWS_PROPERTY) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
String[] views = decryptedValue.split(";");
|
||||
reportCredentials.setViewIds(Arrays.asList(views));
|
||||
}
|
||||
if (prop.name().compareTo(AP_CLIENT_PROPERTY) == 0) {
|
||||
if(prop.name().compareTo(AP_CLIENT_ID) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
reportCredentials.setClientId(decryptedValue);
|
||||
}
|
||||
if (prop.name().compareTo(AP_PRIVATEKEY_PROPERTY) == 0) {
|
||||
if(prop.name().compareTo(AP_PRIVATEKEY_ID_PROPERTY) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
reportCredentials.setPrivateKeyId(decryptedValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
return reportCredentials;
|
||||
}
|
||||
|
||||
|
||||
private static LocalDate asLocalDate(Date date) {
|
||||
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
}
|
||||
|
||||
private static DateRange getDateRangeForAnalytics(Date start, Date end) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // required
|
||||
// by
|
||||
// Analytics
|
||||
private static Builder getDateRangeBuilderForAnalytics(Date start, Date end) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); //required by Analytics
|
||||
String startDate = asLocalDate(start).format(formatter);
|
||||
String endDate = asLocalDate(end).format(formatter);
|
||||
DateRange dateRange = new DateRange();// date format `yyyy-MM-dd`
|
||||
dateRange.setStartDate(startDate);
|
||||
dateRange.setEndDate(endDate);
|
||||
return dateRange;
|
||||
Builder dateRangeBuilder = DateRange.newBuilder().setStartDate(startDate).setEndDate(endDate);
|
||||
|
||||
return dateRangeBuilder;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ public class SocialInteractionsHarvester extends BasicHarvester {
|
|||
|
||||
getJson();
|
||||
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor();
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor(context);
|
||||
|
||||
AccountingRecord likesAR = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.SOCIAL_LIKES), (long) likes);
|
||||
logger.debug("{} : {}", likesAR.getDimension().getId(), likesAR.getMeasure());
|
||||
|
|
|
@ -4,22 +4,13 @@ import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|||
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -42,56 +33,48 @@ import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder;
|
||||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
||||
import com.google.api.client.googleapis.util.Utils;
|
||||
import com.google.api.client.http.HttpTransport;
|
||||
import com.google.api.client.json.JsonFactory;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.google.api.client.util.PemReader;
|
||||
import com.google.api.client.util.PemReader.Section;
|
||||
import com.google.api.client.util.SecurityUtils;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReporting;
|
||||
import com.google.api.services.analyticsreporting.v4.AnalyticsReportingScopes;
|
||||
import com.google.api.services.analyticsreporting.v4.model.DateRange;
|
||||
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.Metric;
|
||||
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;
|
||||
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
|
||||
import com.google.analytics.data.v1beta.BetaAnalyticsDataSettings;
|
||||
import com.google.analytics.data.v1beta.DateRange;
|
||||
import com.google.analytics.data.v1beta.DateRange.Builder;
|
||||
import com.google.analytics.data.v1beta.Dimension;
|
||||
import com.google.analytics.data.v1beta.Metric;
|
||||
import com.google.analytics.data.v1beta.Row;
|
||||
import com.google.analytics.data.v1beta.RunReportRequest;
|
||||
import com.google.analytics.data.v1beta.RunReportResponse;
|
||||
import com.google.api.gax.core.FixedCredentialsProvider;
|
||||
import com.google.auth.oauth2.ServiceAccountCredentials;
|
||||
|
||||
|
||||
public class VREAccessesHarvester extends BasicHarvester {
|
||||
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(VREAccessesHarvester.class);
|
||||
|
||||
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||
|
||||
|
||||
// private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||
|
||||
private static final String SERVICE_ENDPOINT_CATEGORY = "OnlineService";
|
||||
private static final String SERVICE_ENDPOINT_NAME = "BigGAnalyticsReportService";
|
||||
private static final String SERVICE_ENDPOINT_NAME = "GA4AnalyticsDataService";
|
||||
private static final String AP_VIEWS_PROPERTY = "views";
|
||||
private static final String AP_CLIENT_PROPERTY = "clientId";
|
||||
private static final String AP_PRIVATEKEY_PROPERTY = "privateKeyId";
|
||||
private static final String APPLICATION_NAME = "Analytics Reporting";
|
||||
|
||||
private static final String AP_CLIENT_ID = "client_id";
|
||||
private static final String AP_PRIVATEKEY_ID_PROPERTY = "private_key_id";
|
||||
|
||||
|
||||
private List<VREAccessesReportRow> vreAccesses;
|
||||
|
||||
|
||||
public VREAccessesHarvester(Date start, Date end) throws Exception {
|
||||
super(start, end);
|
||||
vreAccesses = getAllAccesses(start, end);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<AccountingRecord> getAccountingRecords() throws Exception {
|
||||
try {
|
||||
String context = org.gcube.dataharvest.utils.Utils.getCurrentContext();
|
||||
|
||||
|
||||
ArrayList<AccountingRecord> accountingRecords = new ArrayList<AccountingRecord>();
|
||||
|
||||
|
||||
int measure = 0;
|
||||
|
||||
|
||||
ScopeBean scopeBean = new ScopeBean(context);
|
||||
String lowerCasedContext = scopeBean.name().toLowerCase();
|
||||
String case1 = lowerCasedContext + "/";
|
||||
|
@ -100,28 +83,28 @@ public class VREAccessesHarvester extends BasicHarvester {
|
|||
String pagePath = row.getPagePath();
|
||||
if (!pagePath.contains("_redirect=/group") && !pagePath.contains("workspace")) {
|
||||
if(pagePath.endsWith(lowerCasedContext)) {
|
||||
logger.trace("Matched endsWith({}) : {}", lowerCasedContext, pagePath);
|
||||
logger.debug("Matched endsWith({}) : {}", lowerCasedContext, pagePath);
|
||||
measure += row.getVisitNumber();
|
||||
} else if(pagePath.contains(case1) || pagePath.contains(case2)) {
|
||||
logger.trace("Matched contains({}) || contains({}) : {}", case1, case2, pagePath);
|
||||
logger.debug("Matched contains({}) || contains({}) : {}", case1, case2, pagePath);
|
||||
measure += row.getVisitNumber();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor();
|
||||
|
||||
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor(context);
|
||||
|
||||
AccountingRecord ar = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.ACCESSES), (long) measure);
|
||||
logger.debug("{} : {}", ar.getDimension().getId(), ar.getMeasure());
|
||||
accountingRecords.add(ar);
|
||||
|
||||
|
||||
return accountingRecords;
|
||||
|
||||
|
||||
} catch(Exception e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return a list of {@link VREAccessesReportRow} objects containing the pagePath and the visit number e.g.
|
||||
|
@ -130,182 +113,113 @@ public class VREAccessesHarvester extends BasicHarvester {
|
|||
* VREAccessesReportRow [pagePath=/group/agroclimaticmodelling/agroclimaticmodelling, visitNumber=39]
|
||||
*/
|
||||
private static List<VREAccessesReportRow> getAllAccesses(Date start, Date end) throws Exception {
|
||||
DateRange dateRange = getDateRangeForAnalytics(start, end);
|
||||
logger.trace("Getting accesses in this time range {}", dateRange.toPrettyString());
|
||||
|
||||
Builder dateRangeBuilder = getDateRangeBuilderForAnalytics(start, end);
|
||||
logger.debug("Getting accesses in this time range {}", dateRangeBuilder.toString());
|
||||
|
||||
AnalyticsReportCredentials credentialsFromD4S = getAuthorisedApplicationInfoFromIs();
|
||||
AnalyticsReporting service = initializeAnalyticsReporting(credentialsFromD4S);
|
||||
HashMap<String,List<GetReportsResponse>> responses = getReportResponses(service, credentialsFromD4S.getViewIds(), dateRange);
|
||||
List<VREAccessesReportRow> totalAccesses = new ArrayList<>();
|
||||
|
||||
logger.debug("Getting credentials credentialsFromD4S");
|
||||
|
||||
BetaAnalyticsDataSettings serviceSettings = initializeAnalyticsReporting(credentialsFromD4S);
|
||||
|
||||
logger.debug("initializeAnalyticsReporting service settings");
|
||||
|
||||
|
||||
HashMap<String,List<RunReportResponse>> responses = getReportResponses(serviceSettings, credentialsFromD4S.getViewIds(), dateRangeBuilder);
|
||||
List<VREAccessesReportRow> totalAccesses = new ArrayList<>();
|
||||
|
||||
for(String view : responses.keySet()) {
|
||||
List<VREAccessesReportRow> viewReport = parseResponse(view, responses.get(view));
|
||||
logger.trace("Got {} entries from view id={}", viewReport.size(), view);
|
||||
logger.debug("Got {} entries from view id={}", viewReport.size(), view);
|
||||
totalAccesses.addAll(viewReport);
|
||||
}
|
||||
logger.trace("Merged in {} total entries from all views", totalAccesses.size());
|
||||
logger.debug("Merged in {} total entries from all views", totalAccesses.size());
|
||||
return totalAccesses;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initializes an Analytics Reporting API V4 service object.
|
||||
* Initializes an Google Analytics Data API service object.
|
||||
*
|
||||
* @return An authorized Analytics Reporting API V4 service object.
|
||||
* @return An authorized Google Analytics Data API
|
||||
* @throws IOException
|
||||
* @throws GeneralSecurityException
|
||||
*/
|
||||
private static AnalyticsReporting initializeAnalyticsReporting(AnalyticsReportCredentials cred)
|
||||
throws GeneralSecurityException, IOException {
|
||||
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
|
||||
GoogleCredential credential = fromD4SServiceEndpoint(cred).createScoped(AnalyticsReportingScopes.all());
|
||||
|
||||
// Construct the Analytics Reporting service object.
|
||||
return new AnalyticsReporting.Builder(httpTransport, JSON_FACTORY, credential)
|
||||
.setApplicationName(APPLICATION_NAME).build();
|
||||
private static BetaAnalyticsDataSettings initializeAnalyticsReporting(AnalyticsReportCredentials cred) throws IOException {
|
||||
return BetaAnalyticsDataSettings.newBuilder()
|
||||
.setCredentialsProvider(FixedCredentialsProvider.create(
|
||||
ServiceAccountCredentials.fromPkcs8(cred.getClientId(), cred.getClientEmail(), cred.getPrivateKeyPem(), cred.getPrivateKeyId(), null)))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Queries the Analytics Reporting API V4.
|
||||
* Queries Analytics Data API service
|
||||
*
|
||||
* @param service An authorized Analytics Reporting API V4 service object.
|
||||
* @return GetReportResponse The Analytics Reporting API V4 response.
|
||||
* @param service Analytics Data API service service settings.
|
||||
* @return Row Analytics Data API service
|
||||
* @throws IOException
|
||||
*/
|
||||
private static HashMap<String,List<GetReportsResponse>> getReportResponses(AnalyticsReporting service,
|
||||
List<String> viewIDs, DateRange dateRange) throws IOException {
|
||||
|
||||
HashMap<String,List<GetReportsResponse>> reports = new HashMap<>();
|
||||
|
||||
// Create the Metrics object.
|
||||
Metric sessions = new Metric().setExpression("ga:pageviews").setAlias("pages");
|
||||
com.google.api.services.analyticsreporting.v4.model.Dimension pageTitle = new com.google.api.services.analyticsreporting.v4.model.Dimension().setName("ga:pagePath");
|
||||
|
||||
for(String view : viewIDs) {
|
||||
List<GetReportsResponse> gReportResponses = new ArrayList<>();
|
||||
logger.info("Getting data from Google Analytics for viewid: " + view);
|
||||
boolean iterateMorePages = true;
|
||||
String nextPageToken = null;
|
||||
while (iterateMorePages) {
|
||||
// Create the ReportRequest object.
|
||||
ReportRequest request = new ReportRequest().setViewId(view.trim()).setDateRanges(Arrays.asList(dateRange))
|
||||
.setMetrics(Arrays.asList(sessions)).setDimensions(Arrays.asList(pageTitle));
|
||||
request.setPageSize(1000);
|
||||
request.setPageToken(nextPageToken);
|
||||
ArrayList<ReportRequest> requests = new ArrayList<ReportRequest>();
|
||||
requests.add(request);
|
||||
// Create the GetReportsRequest object.
|
||||
GetReportsRequest getReport = new GetReportsRequest().setReportRequests(requests);
|
||||
// Call the batchGet method.
|
||||
GetReportsResponse response = service.reports().batchGet(getReport).execute();
|
||||
nextPageToken = response.getReports().get(0).getNextPageToken();
|
||||
iterateMorePages = (nextPageToken != null);
|
||||
logger.debug("got nextPageToken: "+nextPageToken);
|
||||
private static HashMap<String,List<RunReportResponse>> getReportResponses(BetaAnalyticsDataSettings betaAnalyticsDataSettings,
|
||||
List<String> viewIDs, Builder dateRangeBuilder) throws IOException {
|
||||
|
||||
HashMap<String,List<RunReportResponse>> reports = new HashMap<>();
|
||||
|
||||
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create(betaAnalyticsDataSettings)) {
|
||||
|
||||
for(String propertyId : viewIDs) {
|
||||
List<RunReportResponse> gReportResponses = new ArrayList<>();
|
||||
logger.debug("Getting data from Analytics Data API for propertyId: " + propertyId);
|
||||
RunReportRequest request =
|
||||
RunReportRequest.newBuilder()
|
||||
.setProperty("properties/" + propertyId)
|
||||
.addDimensions(Dimension.newBuilder().setName("pagePath"))
|
||||
.addMetrics(Metric.newBuilder().setName("screenPageViews"))
|
||||
.addDateRanges(dateRangeBuilder)
|
||||
.build();
|
||||
|
||||
// Make the request.
|
||||
RunReportResponse response = analyticsData.runReport(request);
|
||||
gReportResponses.add(response);
|
||||
// Iterate through every row of the API response.
|
||||
// for (Row row : response.getRowsList()) {
|
||||
// System.out.printf(
|
||||
// "%s, %s%n", row.getDimensionValues(0).getValue(), row.getMetricValues(0).getValue());
|
||||
// }
|
||||
reports.put(propertyId, gReportResponses);
|
||||
}
|
||||
reports.put(view, gReportResponses);
|
||||
}
|
||||
// Return the response.
|
||||
return reports;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses and prints the Analytics Reporting API V4 response.
|
||||
* Parses and prints the Analytics Data API service respose
|
||||
*
|
||||
* @param response An Analytics Reporting API V4 response.
|
||||
* @param response An Analytics Data API service response.
|
||||
*/
|
||||
/**
|
||||
* Parses and prints the Analytics Reporting API V4 response.
|
||||
*
|
||||
* @param response An Analytics Reporting API V4 response.
|
||||
*/
|
||||
private static List<VREAccessesReportRow> parseResponse(String viewId, List<GetReportsResponse> responses) {
|
||||
logger.debug("parsing Response for " + viewId);
|
||||
private static List<VREAccessesReportRow> parseResponse(String viewId, List<RunReportResponse> responses) {
|
||||
logger.debug("parsing Response for propertyID=" + viewId);
|
||||
|
||||
List<VREAccessesReportRow> toReturn = new ArrayList<>();
|
||||
for (GetReportsResponse response : responses) {
|
||||
for (Report report: response.getReports()) {
|
||||
List<ReportRow> rows = report.getData().getRows();
|
||||
if (rows == null) {
|
||||
logger.warn("No data found for " + viewId);
|
||||
for (RunReportResponse response : responses) {
|
||||
for (Row row: response.getRowsList()) {
|
||||
String dimension = row.getDimensionValues(0).getValue();
|
||||
String metric = row.getMetricValues(0).getValue();
|
||||
VREAccessesReportRow var = new VREAccessesReportRow();
|
||||
boolean validEntry = false;
|
||||
String pagePath = dimension;
|
||||
if (pagePath.startsWith("/group") || pagePath.startsWith("/web")) {
|
||||
var.setPagePath(dimension);
|
||||
validEntry = true;
|
||||
}
|
||||
else {
|
||||
for (ReportRow row: rows) {
|
||||
String dimension = row.getDimensions().get(0);
|
||||
DateRangeValues metric = row.getMetrics().get(0);
|
||||
VREAccessesReportRow var = new VREAccessesReportRow();
|
||||
boolean validEntry = false;
|
||||
String pagePath = dimension;
|
||||
if (pagePath.startsWith("/group") || pagePath.startsWith("/web")) {
|
||||
var.setPagePath(dimension);
|
||||
validEntry = true;
|
||||
}
|
||||
if (validEntry) {
|
||||
var.setVisitNumber(Integer.parseInt(metric.getValues().get(0)));
|
||||
toReturn.add(var);
|
||||
}
|
||||
}
|
||||
if (validEntry) {
|
||||
var.setVisitNumber(Integer.parseInt(metric));
|
||||
toReturn.add(var);
|
||||
}
|
||||
//System.out.printf("%s, %s%n", row.getDimensionValues(0).getValue(), row.getMetricValues(0).getValue());
|
||||
}
|
||||
}
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private static GoogleCredential fromD4SServiceEndpoint(AnalyticsReportCredentials cred) throws IOException {
|
||||
|
||||
String clientId = cred.getClientId();
|
||||
String clientEmail = cred.getClientEmail();
|
||||
String privateKeyPem = cred.getPrivateKeyPem();
|
||||
String privateKeyId = cred.getPrivateKeyId();
|
||||
String tokenUri = cred.getTokenUri();
|
||||
String projectId = cred.getProjectId();
|
||||
|
||||
if(clientId == null || clientEmail == null || privateKeyPem == null || privateKeyId == null) {
|
||||
throw new IOException("Error reading service account credential from stream, "
|
||||
+ "expecting 'client_id', 'client_email', 'private_key' and 'private_key_id'.");
|
||||
}
|
||||
|
||||
PrivateKey privateKey = privateKeyFromPkcs8(privateKeyPem);
|
||||
|
||||
Collection<String> emptyScopes = Collections.emptyList();
|
||||
|
||||
Builder credentialBuilder = new GoogleCredential.Builder().setTransport(Utils.getDefaultTransport())
|
||||
.setJsonFactory(Utils.getDefaultJsonFactory()).setServiceAccountId(clientEmail)
|
||||
.setServiceAccountScopes(emptyScopes).setServiceAccountPrivateKey(privateKey)
|
||||
.setServiceAccountPrivateKeyId(privateKeyId);
|
||||
|
||||
if(tokenUri != null) {
|
||||
credentialBuilder.setTokenServerEncodedUrl(tokenUri);
|
||||
}
|
||||
|
||||
if(projectId != null) {
|
||||
credentialBuilder.setServiceAccountProjectId(projectId);
|
||||
}
|
||||
|
||||
// Don't do a refresh at this point, as it will always fail before the scopes are added.
|
||||
return credentialBuilder.build();
|
||||
}
|
||||
|
||||
private static PrivateKey privateKeyFromPkcs8(String privateKeyPem) throws IOException {
|
||||
Reader reader = new StringReader(privateKeyPem);
|
||||
Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
|
||||
if(section == null) {
|
||||
throw new IOException("Invalid PKCS8 data.");
|
||||
}
|
||||
byte[] bytes = section.getBase64DecodedBytes();
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
|
||||
Exception unexpectedException = null;
|
||||
try {
|
||||
KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
|
||||
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
||||
return privateKey;
|
||||
} catch(NoSuchAlgorithmException exception) {
|
||||
unexpectedException = exception;
|
||||
} catch(InvalidKeySpecException exception) {
|
||||
unexpectedException = exception;
|
||||
}
|
||||
throw new IOException("Unexpected exception reading PKCS data", unexpectedException);
|
||||
}
|
||||
|
||||
|
||||
private static List<ServiceEndpoint> getAnalyticsReportingConfigurationFromIS(String infrastructureScope)
|
||||
throws Exception {
|
||||
String scope = infrastructureScope;
|
||||
|
@ -319,14 +233,13 @@ public class VREAccessesHarvester extends BasicHarvester {
|
|||
ScopeProvider.instance.set(currScope);
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* l
|
||||
* @throws Exception
|
||||
*/
|
||||
private static AnalyticsReportCredentials getAuthorisedApplicationInfoFromIs() throws Exception {
|
||||
AnalyticsReportCredentials reportCredentials = new AnalyticsReportCredentials();
|
||||
|
||||
|
||||
String context = org.gcube.dataharvest.utils.Utils.getCurrentContext();
|
||||
try {
|
||||
List<ServiceEndpoint> list = getAnalyticsReportingConfigurationFromIS(context);
|
||||
|
@ -337,26 +250,26 @@ public class VREAccessesHarvester extends BasicHarvester {
|
|||
logger.warn("There is no Service Endpoint having name " + SERVICE_ENDPOINT_NAME + " and Category "
|
||||
+ SERVICE_ENDPOINT_CATEGORY + " in this context: " + context);
|
||||
} else {
|
||||
|
||||
|
||||
for(ServiceEndpoint res : list) {
|
||||
reportCredentials.setTokenUri(res.profile().runtime().hostedOn());
|
||||
Group<AccessPoint> apGroup = res.profile().accessPoints();
|
||||
AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]);
|
||||
AccessPoint found = accessPoints[0];
|
||||
reportCredentials.setClientEmail(found.address());
|
||||
reportCredentials.setProjectId(found.username());
|
||||
reportCredentials.setPrivateKeyPem(StringEncrypter.getEncrypter().decrypt(found.password()));
|
||||
reportCredentials.setClientEmail(found.username());
|
||||
String decryptedPrivateKey = StringEncrypter.getEncrypter().decrypt(found.password());
|
||||
reportCredentials.setPrivateKeyPem(decryptedPrivateKey.trim());
|
||||
|
||||
for(Property prop : found.properties()) {
|
||||
if(prop.name().compareTo(AP_VIEWS_PROPERTY) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
String[] views = decryptedValue.split(";");
|
||||
reportCredentials.setViewIds(Arrays.asList(views));
|
||||
}
|
||||
if(prop.name().compareTo(AP_CLIENT_PROPERTY) == 0) {
|
||||
if(prop.name().compareTo(AP_CLIENT_ID) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
reportCredentials.setClientId(decryptedValue);
|
||||
}
|
||||
if(prop.name().compareTo(AP_PRIVATEKEY_PROPERTY) == 0) {
|
||||
if(prop.name().compareTo(AP_PRIVATEKEY_ID_PROPERTY) == 0) {
|
||||
String decryptedValue = StringEncrypter.getEncrypter().decrypt(prop.value());
|
||||
reportCredentials.setPrivateKeyId(decryptedValue);
|
||||
}
|
||||
|
@ -369,19 +282,18 @@ public class VREAccessesHarvester extends BasicHarvester {
|
|||
}
|
||||
return reportCredentials;
|
||||
}
|
||||
|
||||
|
||||
private static LocalDate asLocalDate(Date date) {
|
||||
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
}
|
||||
|
||||
private static DateRange getDateRangeForAnalytics(Date start, Date end) {
|
||||
|
||||
private static Builder getDateRangeBuilderForAnalytics(Date start, Date end) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); //required by Analytics
|
||||
String startDate = asLocalDate(start).format(formatter);
|
||||
String endDate = asLocalDate(end).format(formatter);
|
||||
DateRange dateRange = new DateRange();// date format `yyyy-MM-dd`
|
||||
dateRange.setStartDate(startDate);
|
||||
dateRange.setEndDate(endDate);
|
||||
return dateRange;
|
||||
Builder dateRangeBuilder = DateRange.newBuilder().setStartDate(startDate).setEndDate(endDate);
|
||||
|
||||
return dateRangeBuilder;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.gcube.dataharvest.harvester;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
@ -10,7 +9,6 @@ import org.gcube.accounting.accounting.summary.access.model.update.AccountingRec
|
|||
import org.gcube.dataharvest.AccountingDashboardHarvesterPlugin;
|
||||
import org.gcube.dataharvest.datamodel.HarvestedDataKey;
|
||||
import org.gcube.social_networking.social_networking_client_library.UserClient;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -32,12 +30,13 @@ public class VREUsersHarvester extends BasicHarvester {
|
|||
@Override
|
||||
public List<AccountingRecord> getAccountingRecords() throws Exception {
|
||||
try {
|
||||
// String context = Utils.getCurrentContext();
|
||||
String context = org.gcube.dataharvest.utils.Utils.getCurrentContext();
|
||||
|
||||
int measure = get();
|
||||
|
||||
ArrayList<AccountingRecord> accountingRecords = new ArrayList<AccountingRecord>();
|
||||
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor();
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor(context);
|
||||
|
||||
AccountingRecord ar = new AccountingRecord(scopeDescriptor, instant, getDimension(HarvestedDataKey.USERS), (long) measure);
|
||||
logger.debug("{} : {}", ar.getDimension().getId(), ar.getMeasure());
|
||||
|
|
|
@ -77,7 +77,7 @@ public abstract class SoBigDataHarvester extends BasicHarvester {
|
|||
* @throws ObjectNotFound
|
||||
*/
|
||||
protected void initMappingMaps() throws ObjectNotFound, Exception {
|
||||
Properties properties = AccountingDashboardHarvesterPlugin.getProperties().get();
|
||||
Properties properties = AccountingDashboardHarvesterPlugin.getConfigParameters();
|
||||
Set<String> keys = properties.stringPropertyNames();
|
||||
|
||||
mapSystemTypeToDBEntry = new HashMap<String,String>();
|
||||
|
|
|
@ -109,7 +109,7 @@ public class TagMeMethodInvocationHarvester extends BasicHarvester {
|
|||
|
||||
}
|
||||
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor();
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor(context);
|
||||
Dimension dimension = getDimension(HarvestedDataKey.METHOD_INVOCATIONS);
|
||||
|
||||
AccountingRecord ar = new AccountingRecord(scopeDescriptor, instant, dimension, numberOfInvocation);
|
||||
|
|
|
@ -57,7 +57,7 @@ public class ContextAuthorization {
|
|||
* Contains Properties used to generate tokens
|
||||
*/
|
||||
public ContextAuthorization() throws Exception {
|
||||
this.properties = AccountingDashboardHarvesterPlugin.getProperties().get();
|
||||
this.properties = AccountingDashboardHarvesterPlugin.getConfigParameters();
|
||||
this.contextToToken = new HashMap<>();
|
||||
this.tokenToContext = new HashMap<>();
|
||||
retrieveContextsAndTokens();
|
||||
|
@ -78,7 +78,7 @@ public class ContextAuthorization {
|
|||
}
|
||||
|
||||
private TokenResponse getJWTAccessToken(String context) throws Exception {
|
||||
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(CLIENT_ID, getClientSecret(context), context, null);
|
||||
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(context, CLIENT_ID, getClientSecret(context), context, null);
|
||||
return tr;
|
||||
}
|
||||
|
||||
|
|
|
@ -91,9 +91,7 @@ public class DateUtils {
|
|||
aggregationStartCalendar.set(Calendar.MINUTE, 0);
|
||||
aggregationStartCalendar.set(Calendar.SECOND, 0);
|
||||
aggregationStartCalendar.set(Calendar.MILLISECOND, 0);
|
||||
|
||||
logger.debug("{}", DEFAULT_DATE_FORMAT.format(aggregationStartCalendar.getTime()));
|
||||
|
||||
// logger.trace("{}", DEFAULT_DATE_FORMAT.format(aggregationStartCalendar.getTime()));
|
||||
return aggregationStartCalendar;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,5 @@
|
|||
package org.gcube.dataharvest.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManager;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
||||
|
|
|
@ -1,2 +1 @@
|
|||
USERNAME=luca.frosini
|
||||
SERVICE_NAME=accounting-harvester
|
||||
/d4science.research-infrastructures.eu=XXXXXXXXXX
|
|
@ -1,135 +0,0 @@
|
|||
package org.gcube.dataharvest;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.gcube.accounting.accounting.summary.access.AccountingDao;
|
||||
import org.gcube.accounting.accounting.summary.access.model.ScopeDescriptor;
|
||||
import org.gcube.accounting.accounting.summary.access.model.internal.Dimension;
|
||||
import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord;
|
||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||
import org.gcube.common.scope.impl.ScopeBean;
|
||||
import org.gcube.dataharvest.harvester.JupyterAccessesHarvester;
|
||||
import org.gcube.dataharvest.utils.AggregationType;
|
||||
import org.gcube.dataharvest.utils.ContextAuthorization;
|
||||
import org.gcube.dataharvest.utils.ContextTest;
|
||||
import org.gcube.dataharvest.utils.DateUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Giancarlo Panichi (ISTI CNR)
|
||||
*
|
||||
*/
|
||||
public class AccountingDataHarvesterJupyterTest extends ContextTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterJupyterTest.class);
|
||||
public static final String ROOT = "/d4science.research-infrastructures.eu";
|
||||
// private static final String SCOPE = "/d4science.research-infrastructures.eu/D4OS/Blue-CloudLab";
|
||||
|
||||
protected AccountingDao getAccountingDao() throws ObjectNotFound, Exception {
|
||||
AccountingDao dao = AccountingDao.get();
|
||||
|
||||
Set<ScopeDescriptor> scopeDescriptorSet = dao.getContexts();
|
||||
Map<String, ScopeDescriptor> scopeDescriptorMap = new HashMap<>();
|
||||
for (ScopeDescriptor scopeDescriptor : scopeDescriptorSet) {
|
||||
scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor);
|
||||
}
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap);
|
||||
|
||||
Set<Dimension> dimensionSet = dao.getDimensions();
|
||||
Map<String, Dimension> dimensionMap = new HashMap<>();
|
||||
for (Dimension dimension : dimensionSet) {
|
||||
dimensionMap.put(dimension.getId(), dimension);
|
||||
}
|
||||
|
||||
AccountingDashboardHarvesterPlugin.dimensions.set(dimensionMap);
|
||||
|
||||
return dao;
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testJupyterAccessesHarvester() throws Exception {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
List<Date> starts = new ArrayList<>();
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.JANUARY, 1).getTime());
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.FEBRUARY, 1).getTime());
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.MARCH, 1).getTime());
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.APRIL, 1).getTime());
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.MAY, 1).getTime());
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
|
||||
SortedSet<String> contexts = contextAuthorization.getContexts();
|
||||
/*
|
||||
SortedSet<String> contexts = new TreeSet<>();
|
||||
contexts.add("/d4science.research-infrastructures.eu/D4OS/Blue-CloudLab");
|
||||
contexts.add("/d4science.research-infrastructures.eu/D4OS/Zoo-Phytoplankton_EOV");
|
||||
contexts.add("/d4science.research-infrastructures.eu/D4OS/MarineEnvironmentalIndicators");
|
||||
*/
|
||||
|
||||
List<AccountingRecord> accountingRecords = new ArrayList<>();
|
||||
|
||||
Set<ScopeDescriptor> scopeDescriptorSet = dao.getContexts();
|
||||
Map<String, ScopeDescriptor> scopeDescriptorMap = new HashMap<>();
|
||||
for (ScopeDescriptor scopeDescriptor : scopeDescriptorSet) {
|
||||
scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor);
|
||||
}
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap);
|
||||
|
||||
|
||||
for (Date start : starts) {
|
||||
Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
JupyterAccessesHarvester jupyterAccessesHarvester = new JupyterAccessesHarvester(start, end);
|
||||
|
||||
for(String context : contexts) {
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(context));
|
||||
|
||||
ScopeBean scopeBean = new ScopeBean(context);
|
||||
|
||||
ScopeDescriptor actualScopeDescriptor = scopeDescriptorMap.get(context);
|
||||
if (actualScopeDescriptor == null) {
|
||||
actualScopeDescriptor = new ScopeDescriptor(scopeBean.name(), context);
|
||||
}
|
||||
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptor.set(actualScopeDescriptor);
|
||||
|
||||
List<AccountingRecord> harvested = jupyterAccessesHarvester.getAccountingRecords();
|
||||
accountingRecords.addAll(harvested);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// logger.debug("{}", accountingRecords);
|
||||
|
||||
logger.debug("Going to insert {}", accountingRecords);
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1]));
|
||||
|
||||
} catch (Throwable e) {
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,139 +0,0 @@
|
|||
package org.gcube.dataharvest;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.gcube.accounting.accounting.summary.access.AccountingDao;
|
||||
import org.gcube.accounting.accounting.summary.access.model.ScopeDescriptor;
|
||||
import org.gcube.accounting.accounting.summary.access.model.internal.Dimension;
|
||||
import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord;
|
||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||
import org.gcube.common.scope.impl.ScopeBean;
|
||||
import org.gcube.dataharvest.harvester.RStudioAccessesHarvester;
|
||||
import org.gcube.dataharvest.utils.AggregationType;
|
||||
import org.gcube.dataharvest.utils.ContextAuthorization;
|
||||
import org.gcube.dataharvest.utils.ContextTest;
|
||||
import org.gcube.dataharvest.utils.DateUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Giancarlo Panichi (ISTI CNR)
|
||||
*
|
||||
*/
|
||||
public class AccountingDataHarvesterRStudioTest extends ContextTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterRStudioTest.class);
|
||||
public static final String ROOT = "/d4science.research-infrastructures.eu";
|
||||
// private static final String SCOPE = "/d4science.research-infrastructures.eu/D4OS/Blue-CloudLab";
|
||||
|
||||
protected AccountingDao getAccountingDao() throws ObjectNotFound, Exception {
|
||||
AccountingDao dao = AccountingDao.get();
|
||||
|
||||
Set<ScopeDescriptor> scopeDescriptorSet = dao.getContexts();
|
||||
Map<String, ScopeDescriptor> scopeDescriptorMap = new HashMap<>();
|
||||
for (ScopeDescriptor scopeDescriptor : scopeDescriptorSet) {
|
||||
scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor);
|
||||
}
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap);
|
||||
|
||||
Set<Dimension> dimensionSet = dao.getDimensions();
|
||||
Map<String, Dimension> dimensionMap = new HashMap<>();
|
||||
for (Dimension dimension : dimensionSet) {
|
||||
dimensionMap.put(dimension.getId(), dimension);
|
||||
}
|
||||
|
||||
AccountingDashboardHarvesterPlugin.dimensions.set(dimensionMap);
|
||||
|
||||
return dao;
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testJupyterAccessesHarvester() throws Exception {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
List<Date> starts = new ArrayList<>();
|
||||
|
||||
LocalDate sdate = LocalDate.parse("2016-01-01"), edate = LocalDate.parse("2021-06-01");
|
||||
|
||||
Stream.iterate(sdate, date -> date.plusMonths(1)).limit(ChronoUnit.MONTHS.between(sdate, edate) + 1)
|
||||
.forEach(dateToConvert -> starts.add(java.util.Date
|
||||
.from(dateToConvert.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())));
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
|
||||
SortedSet<String> contexts = contextAuthorization.getContexts();
|
||||
/*
|
||||
SortedSet<String> contexts = new TreeSet<>();
|
||||
contexts.add("/d4science.research-infrastructures.eu/D4OS/Blue-CloudLab");
|
||||
contexts.add("/d4science.research-infrastructures.eu/D4OS/Zoo-Phytoplankton_EOV");
|
||||
contexts.add("/d4science.research-infrastructures.eu/D4OS/MarineEnvironmentalIndicators");
|
||||
*/
|
||||
|
||||
List<AccountingRecord> accountingRecords = new ArrayList<>();
|
||||
|
||||
Set<ScopeDescriptor> scopeDescriptorSet = dao.getContexts();
|
||||
Map<String, ScopeDescriptor> scopeDescriptorMap = new HashMap<>();
|
||||
for (ScopeDescriptor scopeDescriptor : scopeDescriptorSet) {
|
||||
scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor);
|
||||
}
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap);
|
||||
|
||||
|
||||
for (Date start : starts) {
|
||||
Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
RStudioAccessesHarvester rstudioAccessesHarvester = new RStudioAccessesHarvester(start, end);
|
||||
|
||||
for(String context : contexts) {
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(context));
|
||||
|
||||
ScopeBean scopeBean = new ScopeBean(context);
|
||||
|
||||
ScopeDescriptor actualScopeDescriptor = scopeDescriptorMap.get(context);
|
||||
if (actualScopeDescriptor == null) {
|
||||
actualScopeDescriptor = new ScopeDescriptor(scopeBean.name(), context);
|
||||
}
|
||||
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptor.set(actualScopeDescriptor);
|
||||
|
||||
List<AccountingRecord> harvested = rstudioAccessesHarvester.getAccountingRecords();
|
||||
accountingRecords.addAll(harvested);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// logger.debug("{}", accountingRecords);
|
||||
|
||||
logger.debug("Going to insert {}", accountingRecords);
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1]));
|
||||
|
||||
} catch (Throwable e) {
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,173 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.dataharvest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.gcube.common.authorization.utils.manager.SecretManager;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
||||
import org.gcube.common.authorization.utils.secret.JWTSecret;
|
||||
import org.gcube.common.authorization.utils.secret.Secret;
|
||||
import org.gcube.common.authorization.utils.secret.SecretUtility;
|
||||
import org.gcube.common.keycloak.KeycloakClientFactory;
|
||||
import org.gcube.common.keycloak.KeycloakClientHelper;
|
||||
import org.gcube.common.keycloak.model.TokenResponse;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class ContextTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
||||
|
||||
protected static final String CONFIG_INI_FILENAME = "config.ini";
|
||||
|
||||
public static final String DEFAULT_TEST_SCOPE;
|
||||
|
||||
public static final String GCUBE;
|
||||
public static final String DEVNEXT;
|
||||
public static final String NEXTNEXT;
|
||||
public static final String DEVSEC;
|
||||
public static final String DEVVRE;
|
||||
|
||||
public static final String ROOT_PROD;
|
||||
|
||||
protected static final Properties properties;
|
||||
|
||||
public static final String TYPE_PROPERTY_KEY = "type";
|
||||
public static final String USERNAME_PROPERTY_KEY = "username";
|
||||
public static final String PASSWORD_PROPERTY_KEY = "password";
|
||||
public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
|
||||
|
||||
static {
|
||||
GCUBE = "/gcube";
|
||||
DEVNEXT = GCUBE + "/devNext";
|
||||
NEXTNEXT = DEVNEXT + "/NextNext";
|
||||
DEVSEC = GCUBE + "/devsec";
|
||||
DEVVRE = DEVSEC + "/devVRE";
|
||||
|
||||
ROOT_PROD = "/d4science.research-infrastructures.eu";
|
||||
|
||||
DEFAULT_TEST_SCOPE = GCUBE;
|
||||
|
||||
|
||||
properties = new Properties();
|
||||
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME);
|
||||
try {
|
||||
// load the properties file
|
||||
properties.load(input);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private enum Type{
|
||||
USER, CLIENT_ID
|
||||
};
|
||||
|
||||
public static void set(Secret secret) throws Exception {
|
||||
SecretManagerProvider.instance.reset();
|
||||
SecretManager secretManager = new SecretManager();
|
||||
secretManager.addSecret(secret);
|
||||
SecretManagerProvider.instance.set(secretManager);
|
||||
SecretManagerProvider.instance.get().set();
|
||||
}
|
||||
|
||||
public static void setContextByName(String fullContextName) throws Exception {
|
||||
logger.debug("Going to set credentials for context {}", fullContextName);
|
||||
Secret secret = getSecretByContextName(fullContextName);
|
||||
set(secret);
|
||||
}
|
||||
|
||||
|
||||
private static TokenResponse getJWTAccessToken(String context) throws Exception {
|
||||
Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
|
||||
|
||||
TokenResponse tr = null;
|
||||
|
||||
int index = context.indexOf('/', 1);
|
||||
String root = context.substring(0, index == -1 ? context.length() : index);
|
||||
|
||||
switch (type) {
|
||||
case CLIENT_ID:
|
||||
String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
|
||||
String clientSecret = properties.getProperty(root);
|
||||
|
||||
tr = KeycloakClientFactory.newInstance().queryUMAToken(context, clientId, clientSecret, context, null);
|
||||
break;
|
||||
|
||||
case USER:
|
||||
default:
|
||||
String username = properties.getProperty(USERNAME_PROPERTY_KEY);
|
||||
String password = properties.getProperty(PASSWORD_PROPERTY_KEY);
|
||||
|
||||
switch (root) {
|
||||
case "/gcube":
|
||||
default:
|
||||
clientId = "next.d4science.org";
|
||||
break;
|
||||
|
||||
case "/pred4s":
|
||||
clientId = "pre.d4science.org";
|
||||
break;
|
||||
|
||||
case "/d4science.research-infrastructures.eu":
|
||||
clientId = "services.d4science.org";
|
||||
break;
|
||||
}
|
||||
clientSecret = null;
|
||||
|
||||
tr = KeycloakClientHelper.getTokenForUser(context, username, password);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return tr;
|
||||
|
||||
}
|
||||
|
||||
public static Secret getSecretByContextName(String context) throws Exception {
|
||||
TokenResponse tr = getJWTAccessToken(context);
|
||||
Secret secret = new JWTSecret(tr.getAccessToken());
|
||||
return secret;
|
||||
}
|
||||
|
||||
public static void setContext(String token) throws Exception {
|
||||
Secret secret = getSecret(token);
|
||||
set(secret);
|
||||
}
|
||||
|
||||
private static Secret getSecret(String token) throws Exception {
|
||||
Secret secret = SecretUtility.getSecretByTokenString(token);
|
||||
return secret;
|
||||
}
|
||||
|
||||
public static String getUser() {
|
||||
String user = "UNKNOWN";
|
||||
try {
|
||||
user = SecretManagerProvider.instance.get().getUser().getUsername();
|
||||
} catch(Exception e) {
|
||||
logger.error("Unable to retrieve user. {} will be used", user);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
setContextByName(ROOT_PROD);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
SecretManagerProvider.instance.reset();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package org.gcube.dataharvest.harvester;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.gcube.accounting.accounting.summary.access.AccountingDao;
|
||||
import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord;
|
||||
import org.gcube.dataharvest.ContextTest;
|
||||
import org.gcube.dataharvest.plugin.AccountingDataHarvesterPluginTest;
|
||||
import org.gcube.dataharvest.utils.AggregationType;
|
||||
import org.gcube.dataharvest.utils.ContextAuthorization;
|
||||
import org.gcube.dataharvest.utils.DateUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Giancarlo Panichi (ISTI CNR)
|
||||
*
|
||||
*/
|
||||
public class AccountingDataHarvesterJupyterTest extends AccountingDataHarvesterPluginTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterJupyterTest.class);
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testJupyterAccessesHarvester() throws Exception {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
List<Date> starts = new ArrayList<>();
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.JANUARY, 1).getTime());
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.FEBRUARY, 1).getTime());
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.MARCH, 1).getTime());
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.APRIL, 1).getTime());
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.MAY, 1).getTime());
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
SortedSet<String> contexts = contextAuthorization.getContexts();
|
||||
/*
|
||||
SortedSet<String> contexts = new TreeSet<>();
|
||||
contexts.add("/d4science.research-infrastructures.eu/D4OS/Blue-CloudLab");
|
||||
contexts.add("/d4science.research-infrastructures.eu/D4OS/Zoo-Phytoplankton_EOV");
|
||||
contexts.add("/d4science.research-infrastructures.eu/D4OS/MarineEnvironmentalIndicators");
|
||||
*/
|
||||
|
||||
List<AccountingRecord> accountingRecords = new ArrayList<>();
|
||||
|
||||
for (Date start : starts) {
|
||||
Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
||||
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
JupyterAccessesHarvester jupyterAccessesHarvester = new JupyterAccessesHarvester(start, end);
|
||||
|
||||
for(String context : contexts) {
|
||||
ContextTest.setContextByName(context);
|
||||
|
||||
List<AccountingRecord> harvested = jupyterAccessesHarvester.getAccountingRecords();
|
||||
accountingRecords.addAll(harvested);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// logger.debug("{}", accountingRecords);
|
||||
|
||||
logger.debug("Going to insert {}", accountingRecords);
|
||||
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
// dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1]));
|
||||
|
||||
} catch (Throwable e) {
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
package org.gcube.dataharvest.harvester;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.SortedSet;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.gcube.accounting.accounting.summary.access.AccountingDao;
|
||||
import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord;
|
||||
import org.gcube.dataharvest.ContextTest;
|
||||
import org.gcube.dataharvest.plugin.AccountingDataHarvesterPluginTest;
|
||||
import org.gcube.dataharvest.utils.AggregationType;
|
||||
import org.gcube.dataharvest.utils.ContextAuthorization;
|
||||
import org.gcube.dataharvest.utils.DateUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Giancarlo Panichi (ISTI CNR)
|
||||
*
|
||||
*/
|
||||
public class AccountingDataHarvesterRStudioTest extends AccountingDataHarvesterPluginTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterRStudioTest.class);
|
||||
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testJupyterAccessesHarvester() throws Exception {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
List<Date> starts = new ArrayList<>();
|
||||
|
||||
LocalDate sdate = LocalDate.parse("2016-01-01"), edate = LocalDate.parse("2021-06-01");
|
||||
|
||||
Stream.iterate(sdate, date -> date.plusMonths(1)).limit(ChronoUnit.MONTHS.between(sdate, edate) + 1)
|
||||
.forEach(dateToConvert -> starts.add(java.util.Date
|
||||
.from(dateToConvert.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())));
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
SortedSet<String> contexts = contextAuthorization.getContexts();
|
||||
|
||||
List<AccountingRecord> accountingRecords = new ArrayList<>();
|
||||
|
||||
|
||||
for (Date start : starts) {
|
||||
Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
||||
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
RStudioAccessesHarvester rstudioAccessesHarvester = new RStudioAccessesHarvester(start, end);
|
||||
|
||||
for(String context : contexts) {
|
||||
ContextTest.setContextByName(context);
|
||||
|
||||
List<AccountingRecord> harvested = rstudioAccessesHarvester.getAccountingRecords();
|
||||
accountingRecords.addAll(harvested);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// logger.debug("{}", accountingRecords);
|
||||
|
||||
logger.debug("Going to insert {}", accountingRecords);
|
||||
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1]));
|
||||
|
||||
} catch (Throwable e) {
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@ package org.gcube.dataharvest.harvester.sobigdata;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.dataharvest.utils.ContextTest;
|
||||
import org.gcube.dataharvest.ContextTest;
|
||||
import org.junit.Ignore;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
package org.gcube.dataharvest;
|
||||
package org.gcube.dataharvest.plugin;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
@ -19,8 +17,11 @@ import org.gcube.accounting.accounting.summary.access.model.ScopeDescriptor;
|
|||
import org.gcube.accounting.accounting.summary.access.model.internal.Dimension;
|
||||
import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord;
|
||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||
import org.gcube.common.authorization.utils.secret.Secret;
|
||||
import org.gcube.common.scope.impl.ScopeBean;
|
||||
import org.gcube.common.scope.impl.ScopeBean.Type;
|
||||
import org.gcube.dataharvest.AccountingDashboardHarvesterPlugin;
|
||||
import org.gcube.dataharvest.ContextTest;
|
||||
import org.gcube.dataharvest.datamodel.HarvestedDataKey;
|
||||
import org.gcube.dataharvest.harvester.CatalogueAccessesHarvester;
|
||||
import org.gcube.dataharvest.harvester.CoreServicesAccessesHarvester;
|
||||
|
@ -32,9 +33,7 @@ import org.gcube.dataharvest.harvester.sobigdata.ResourceCatalogueHarvester;
|
|||
import org.gcube.dataharvest.harvester.sobigdata.TagMeMethodInvocationHarvester;
|
||||
import org.gcube.dataharvest.utils.AggregationType;
|
||||
import org.gcube.dataharvest.utils.ContextAuthorization;
|
||||
import org.gcube.dataharvest.utils.ContextTest;
|
||||
import org.gcube.dataharvest.utils.DateUtils;
|
||||
import org.gcube.resourcemanagement.support.server.managers.context.ContextManager;
|
||||
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
|
||||
import org.gcube.vremanagement.executor.api.types.Scheduling;
|
||||
import org.gcube.vremanagement.executor.client.SmartExecutorClient;
|
||||
|
@ -50,32 +49,38 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterPluginTest.class);
|
||||
|
||||
public static final String ROOT = "/d4science.research-infrastructures.eu";
|
||||
public static final String SO_BIG_VO = "/d4science.research-infrastructures.eu/SoBigData";
|
||||
|
||||
public static final String TAGME_VRE = "/d4science.research-infrastructures.eu/SoBigData/TagMe";
|
||||
public static final String STOCK_ASSESMENT_VRE = "/d4science.research-infrastructures.eu/gCubeApps/StockAssessment";
|
||||
|
||||
public static final String BLUE_CLOUD_LAB = "/d4science.research-infrastructures.eu/D4OS/Blue-CloudLab";
|
||||
|
||||
protected AccountingDao getAccountingDao() throws ObjectNotFound, Exception {
|
||||
AccountingDao dao = AccountingDao.get();
|
||||
|
||||
public static SortedSet<String> getContexts() throws Exception {
|
||||
SortedSet<String> contexts = new TreeSet<>();
|
||||
LinkedHashMap<String, ScopeBean> map = ContextManager.readContexts();
|
||||
for (String scope : map.keySet()) {
|
||||
try {
|
||||
String context = map.get(scope).toString();
|
||||
contexts.add(context);
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
Set<ScopeDescriptor> scopeDescriptorSet = dao.getContexts();
|
||||
Map<String, ScopeDescriptor> scopeDescriptorMap = new HashMap<>();
|
||||
for (ScopeDescriptor scopeDescriptor : scopeDescriptorSet) {
|
||||
scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor);
|
||||
}
|
||||
return contexts;
|
||||
}
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap);
|
||||
|
||||
Set<Dimension> dimensionSet = dao.getDimensions();
|
||||
Map<String, Dimension> dimensionMap = new HashMap<>();
|
||||
for (Dimension dimension : dimensionSet) {
|
||||
dimensionMap.put(dimension.getId(), dimension);
|
||||
}
|
||||
|
||||
|
||||
return dao;
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void getDimensions() {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
AccountingDao dao = AccountingDao.get();
|
||||
|
||||
|
@ -92,11 +97,11 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
}
|
||||
|
||||
@Ignore
|
||||
//@Test
|
||||
// @Test
|
||||
public void launch() {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
AccountingDashboardHarvesterPlugin accountingDataHarvesterPlugin = new AccountingDashboardHarvesterPlugin();
|
||||
|
||||
|
@ -125,11 +130,11 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
// @Test
|
||||
public void launchPluginOnSmartExecutor() {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
String pluginName = new AccountingDashboardHarvesterPlugin().getName();
|
||||
|
||||
|
@ -182,7 +187,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
public void launchOldData() {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
AccountingDashboardHarvesterPlugin accountingDataHarvesterPlugin = new AccountingDashboardHarvesterPlugin();
|
||||
|
||||
|
@ -214,26 +219,23 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Ignore
|
||||
// @Test
|
||||
// @Ignore
|
||||
@Test
|
||||
public void launchOldDataVREAccessesHarvester() {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
// AccountingDao dao = AccountingDao.get();
|
||||
|
||||
AccountingDashboardHarvesterPlugin accountingDataHarvesterPlugin = new AccountingDashboardHarvesterPlugin();
|
||||
Properties properties = accountingDataHarvesterPlugin.getConfigParameters();
|
||||
AccountingDashboardHarvesterPlugin.getProperties().set(properties);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
SortedSet<String> contexts = contextAuthorization.getContexts();
|
||||
|
||||
AggregationType aggregationType = AggregationType.MONTHLY;
|
||||
|
||||
Calendar from = DateUtils.getStartCalendar(2018, Calendar.APRIL, 1);
|
||||
Calendar from = DateUtils.getStartCalendar(2023, Calendar.MAY, 1);
|
||||
|
||||
Calendar runbeforeDate = DateUtils.getStartCalendar(2018, Calendar.JUNE, 1);
|
||||
Calendar runbeforeDate = DateUtils.getStartCalendar(2023, Calendar.JULY, 1);
|
||||
|
||||
while (from.before(runbeforeDate)) {
|
||||
Date start = from.getTime();
|
||||
|
@ -244,10 +246,12 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
ArrayList<AccountingRecord> accountingRecords = new ArrayList<>();
|
||||
|
||||
VREAccessesHarvester vreAccessesHarvester = null;
|
||||
//JupyterAccessesHarvester vreAccessesHarvester = null;
|
||||
//RStudioAccessesHarvester vreAccessesHarvester = null;
|
||||
//CoreServicesAccessesHarvester vreAccessesHarvester = null;
|
||||
|
||||
for (String context : contexts) {
|
||||
// Setting the token for the context
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(context));
|
||||
ContextTest.setContextByName(context);
|
||||
|
||||
ScopeBean scopeBean = new ScopeBean(context);
|
||||
|
||||
|
@ -265,12 +269,12 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
}
|
||||
|
||||
// Setting back token for the context
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(parent.toString()));
|
||||
ContextTest.setContextByName(parent.toString());
|
||||
|
||||
vreAccessesHarvester = new VREAccessesHarvester(start, end);
|
||||
|
||||
// Setting back token for the context
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(context));
|
||||
ContextTest.setContextByName(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -294,7 +298,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
|
||||
logger.debug("Harvest Measures from {} to {} are {}", DateUtils.format(start), DateUtils.format(end),
|
||||
accountingRecords);
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
// dao.insertRecords(accountingRecords.toArray(new
|
||||
// AccountingRecord[1]));
|
||||
|
@ -305,7 +309,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
|
||||
}
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("", e);
|
||||
|
@ -315,60 +319,13 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
|
||||
}
|
||||
|
||||
@Ignore
|
||||
// @Test
|
||||
public void testScopeBean() throws Exception {
|
||||
ContextTest.setContextByName(ROOT);
|
||||
SortedSet<String> contexts = getContexts();
|
||||
|
||||
AggregationType aggregationType = AggregationType.MONTHLY;
|
||||
|
||||
Date start = DateUtils.getStartCalendar(2018, Calendar.MARCH, 1).getTime();
|
||||
// start = DateUtils.getPreviousPeriod(measureType).getTime();
|
||||
Date end = DateUtils.getEndDateFromStartDate(aggregationType, start, 1);
|
||||
|
||||
logger.info("\n\n\n");
|
||||
|
||||
for (String context : contexts) {
|
||||
ScopeBean scopeBean = new ScopeBean(context);
|
||||
// logger.debug("FullName {} - Name {}", scopeBean.toString(),
|
||||
// scopeBean.name());
|
||||
|
||||
try {
|
||||
|
||||
if (scopeBean.is(Type.VRE)
|
||||
&& start.equals(DateUtils.getPreviousPeriod(aggregationType, false).getTime())) {
|
||||
logger.info("Harvesting (VRE Users) for {} from {} to {}", context, DateUtils.format(start),
|
||||
DateUtils.format(end));
|
||||
} else {
|
||||
logger.info("--- Not Harvesting (VRE Users) for {} from {} to {}", context, DateUtils.format(start),
|
||||
DateUtils.format(end));
|
||||
}
|
||||
|
||||
if ((context.startsWith(AccountingDashboardHarvesterPlugin.SO_BIG_DATA_VO)
|
||||
|| context.startsWith(AccountingDashboardHarvesterPlugin.SO_BIG_DATA_EU_VRE)
|
||||
|| context.startsWith(AccountingDashboardHarvesterPlugin.SO_BIG_DATA_IT_VRE))
|
||||
&& start.before(DateUtils.getStartCalendar(2018, Calendar.APRIL, 1).getTime())) {
|
||||
logger.info("--- Not Harvesting (SoBigData Check) for {} from {} to {}", context,
|
||||
DateUtils.format(start), DateUtils.format(end));
|
||||
} else {
|
||||
logger.info("Harvesting (SoBigData Check) for {} from {} to {}", context, DateUtils.format(start),
|
||||
DateUtils.format(end));
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error harvesting Social Interactions for {}", context, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Ignore
|
||||
// @Test
|
||||
public void testVREAccessesHarvester() throws Exception {
|
||||
try {
|
||||
// AccountingDao dao = getAccountingDao();
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
List<Date> starts = new ArrayList<>();
|
||||
starts.add(DateUtils.getStartCalendar(2018, Calendar.SEPTEMBER, 1).getTime());
|
||||
|
@ -389,12 +346,12 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
for (Date start : starts) {
|
||||
Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
VREAccessesHarvester vreAccessesHarvester = new VREAccessesHarvester(start, end);
|
||||
|
||||
for (String contextFullname : contextFullNames) {
|
||||
|
||||
setContextByNameAndScopeDescriptor(contextFullname);
|
||||
ContextTest.setContextByName(contextFullname);
|
||||
|
||||
List<AccountingRecord> harvested = vreAccessesHarvester.getAccountingRecords();
|
||||
accountingRecords.addAll(harvested);
|
||||
|
@ -406,7 +363,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
|
||||
logger.debug("{}", accountingRecords);
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
// dao.insertRecords(accountingRecords.toArray(new
|
||||
// AccountingRecord[1]));
|
||||
|
||||
|
@ -421,7 +378,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
public void testVREAccessesHarvesterAll() {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
||||
|
@ -445,7 +402,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
|
||||
for (String context : contexts) {
|
||||
// Setting the token for the context
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(context));
|
||||
ContextTest.setContextByName(context);
|
||||
|
||||
ScopeBean scopeBean = new ScopeBean(context);
|
||||
|
||||
|
@ -462,13 +419,12 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
parent = scopeBean.enclosingScope();
|
||||
}
|
||||
|
||||
// Setting back token for the context
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(parent.toString()));
|
||||
ContextTest.setContextByName(parent.toString());
|
||||
|
||||
vreAccessesHarvester = new VREAccessesHarvester(start, end);
|
||||
|
||||
// Setting back token for the context
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(context));
|
||||
ContextTest.setContextByName(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -500,14 +456,9 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
public void testSocialInteraction() {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
// AccountingDao dao = AccountingDao.get();
|
||||
|
||||
AccountingDashboardHarvesterPlugin accountingDataHarvesterPlugin = new AccountingDashboardHarvesterPlugin();
|
||||
Properties properties = accountingDataHarvesterPlugin.getConfigParameters();
|
||||
AccountingDashboardHarvesterPlugin.getProperties().set(properties);
|
||||
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
SortedSet<String> contexts = new TreeSet<>();
|
||||
contexts.add("/d4science.research-infrastructures.eu/D4Research");
|
||||
|
@ -529,7 +480,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
|
||||
for (String context : contexts) {
|
||||
// Setting the token for the context
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(context));
|
||||
ContextTest.setContextByName(context);
|
||||
try {
|
||||
// Collecting info on social (posts, replies and likes)
|
||||
logger.info("Going to harvest Social Interactions for {}", context);
|
||||
|
@ -543,7 +494,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
|
||||
logger.debug("Harvest Measures from {} to {} are {}", DateUtils.format(start), DateUtils.format(end),
|
||||
accountingRecords);
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
// dao.insertRecords(accountingRecords.toArray(new
|
||||
// AccountingRecord[1]));
|
||||
|
||||
|
@ -555,40 +506,37 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
|
||||
}
|
||||
|
||||
@Ignore
|
||||
// @Test
|
||||
public void testMethodInvocation() {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(STOCK_ASSESMENT_VRE));
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
||||
Date start = DateUtils.getPreviousPeriod(measureType, false).getTime();
|
||||
Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
||||
|
||||
MethodInvocationHarvester methodInvocationHarvester = new MethodInvocationHarvester(start, end);
|
||||
List<AccountingRecord> accountingRecords = methodInvocationHarvester.getAccountingRecords();
|
||||
|
||||
logger.debug("{}", accountingRecords);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
// @Ignore
|
||||
// // @Test
|
||||
// public void testMethodInvocation() {
|
||||
// try {
|
||||
// ContextTest.setContextByName(STOCK_ASSESMENT_VRE);
|
||||
//
|
||||
// AggregationType measureType = AggregationType.MONTHLY;
|
||||
//
|
||||
// Date start = DateUtils.getPreviousPeriod(measureType, false).getTime();
|
||||
// Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
||||
//
|
||||
// MethodInvocationHarvester methodInvocationHarvester = new MethodInvocationHarvester(start, end);
|
||||
// List<AccountingRecord> accountingRecords = methodInvocationHarvester.getAccountingRecords();
|
||||
//
|
||||
// logger.debug("{}", accountingRecords);
|
||||
//
|
||||
// } catch (Exception e) {
|
||||
// logger.error("", e);
|
||||
// }
|
||||
// }
|
||||
|
||||
@Ignore
|
||||
//@Test
|
||||
// @Ignore
|
||||
@Test
|
||||
public void testMethodInvocationOldData() {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
List<Date> starts = new ArrayList<>();
|
||||
starts.add(DateUtils.getStartCalendar(2021, Calendar.JULY, 1).getTime());
|
||||
Date start = DateUtils.getStartCalendar(2023, Calendar.JANUARY, 1).getTime();
|
||||
Date last = DateUtils.getStartCalendar(2024, Calendar.FEBRUARY, 1).getTime();
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
||||
|
@ -600,40 +548,60 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
for (ScopeDescriptor scopeDescriptor : scopeDescriptorSet) {
|
||||
scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor);
|
||||
}
|
||||
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap);
|
||||
|
||||
|
||||
List<AccountingRecord> accountingRecords = new ArrayList<>();
|
||||
|
||||
for (Date start : starts) {
|
||||
while(start.before(last)) {
|
||||
Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
||||
|
||||
MethodInvocationHarvester methodInvocationHarvester = new MethodInvocationHarvester(start, end);
|
||||
|
||||
for (String context : contexts) {
|
||||
// Setting the token for the context
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(context));
|
||||
Secret s = contextAuthorization.getCatalogueSecretForContext(context);
|
||||
ContextTest.set(s);
|
||||
|
||||
|
||||
if (context.startsWith(AccountingDashboardHarvesterPlugin.TAGME_CONTEXT)) {
|
||||
try {
|
||||
// Collecting info on method invocation
|
||||
logger.info("Going to harvest Method Invocations for {}", context);
|
||||
TagMeMethodInvocationHarvester tagMeMethodInvocationHarvester = new TagMeMethodInvocationHarvester(
|
||||
start, end);
|
||||
|
||||
ScopeBean scopeBean = new ScopeBean(context);
|
||||
List<AccountingRecord> harvested = tagMeMethodInvocationHarvester.getAccountingRecords();
|
||||
logger.debug("{} - {}", context, harvested);
|
||||
accountingRecords.addAll(harvested);
|
||||
|
||||
ScopeDescriptor actualScopeDescriptor = scopeDescriptorMap.get(context);
|
||||
if (actualScopeDescriptor == null) {
|
||||
actualScopeDescriptor = new ScopeDescriptor(scopeBean.name(), context);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error harvesting Method Invocations for {}", context, e);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
// Collecting info on method invocation
|
||||
logger.info("Going to harvest Method Invocations for {}", context);
|
||||
MethodInvocationHarvester methodInvocationHarvester = new MethodInvocationHarvester(start, end);
|
||||
|
||||
List<AccountingRecord> harvested = methodInvocationHarvester.getAccountingRecords();
|
||||
logger.debug("{} - {}", context, harvested);
|
||||
accountingRecords.addAll(harvested);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error harvesting Method Invocations for {}", context, e);
|
||||
}
|
||||
}
|
||||
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptor.set(actualScopeDescriptor);
|
||||
|
||||
List<AccountingRecord> harvested = methodInvocationHarvester.getAccountingRecords();
|
||||
accountingRecords.addAll(harvested);
|
||||
|
||||
logger.debug("{} - {}", context, harvested);
|
||||
|
||||
|
||||
}
|
||||
|
||||
start = end;
|
||||
}
|
||||
|
||||
logger.debug("Going to insert {}", accountingRecords);
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
// dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1]));
|
||||
|
||||
} catch (Exception e) {
|
||||
|
@ -641,48 +609,20 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
}
|
||||
}
|
||||
|
||||
protected AccountingDao getAccountingDao() throws ObjectNotFound, Exception {
|
||||
AccountingDao dao = AccountingDao.get();
|
||||
|
||||
Set<ScopeDescriptor> scopeDescriptorSet = dao.getContexts();
|
||||
Map<String, ScopeDescriptor> scopeDescriptorMap = new HashMap<>();
|
||||
for (ScopeDescriptor scopeDescriptor : scopeDescriptorSet) {
|
||||
scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor);
|
||||
}
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap);
|
||||
|
||||
Set<Dimension> dimensionSet = dao.getDimensions();
|
||||
Map<String, Dimension> dimensionMap = new HashMap<>();
|
||||
for (Dimension dimension : dimensionSet) {
|
||||
dimensionMap.put(dimension.getId(), dimension);
|
||||
}
|
||||
|
||||
AccountingDashboardHarvesterPlugin.dimensions.set(dimensionMap);
|
||||
|
||||
return dao;
|
||||
}
|
||||
|
||||
protected void setContextByNameAndScopeDescriptor(String contextFullName) throws ObjectNotFound, Exception {
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(contextFullName));
|
||||
|
||||
ScopeBean scopeBean = new ScopeBean(contextFullName);
|
||||
ScopeDescriptor actualScopeDescriptor = AccountingDashboardHarvesterPlugin.scopeDescriptors.get()
|
||||
.get(contextFullName);
|
||||
if (actualScopeDescriptor == null) {
|
||||
actualScopeDescriptor = new ScopeDescriptor(scopeBean.name(), contextFullName);
|
||||
}
|
||||
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptor.set(actualScopeDescriptor);
|
||||
}
|
||||
|
||||
|
||||
@Ignore
|
||||
// @Test
|
||||
public void testTagMeMethodInvocation() throws Exception {
|
||||
try {
|
||||
// AccountingDao dao = getAccountingDao();
|
||||
setContextByNameAndScopeDescriptor(TAGME_VRE);
|
||||
|
||||
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
ContextTest.setContextByName(TAGME_VRE);
|
||||
|
||||
List<AccountingRecord> accountingRecords = new ArrayList<>();
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
@ -705,7 +645,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
logger.debug("{}", accountingRecords);
|
||||
}
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
// dao.insertRecords(accountingRecords.toArray(new
|
||||
// AccountingRecord[1]));
|
||||
|
||||
|
@ -719,16 +659,10 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
// @Test
|
||||
public void testGetVREUsersForSpecificVRE() {
|
||||
try {
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
AccountingDashboardHarvesterPlugin accountingDataHarvesterPlugin = new AccountingDashboardHarvesterPlugin();
|
||||
Properties properties = accountingDataHarvesterPlugin.getConfigParameters();
|
||||
AccountingDashboardHarvesterPlugin.getProperties().set(properties);
|
||||
|
||||
// AccountingDao dao = AccountingDao.get();
|
||||
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
ContextTest.set(contextAuthorization.getSecretForContext("/d4science.research-infrastructures.eu/SoBigData/SportsDataScience"));
|
||||
ContextTest.setContextByName("/d4science.research-infrastructures.eu/SoBigData/SportsDataScience");
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
||||
|
@ -740,7 +674,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
|
||||
logger.info("Harvested Data from {} to {} : {}", DateUtils.format(start), DateUtils.format(end), harvested);
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
// dao.insertRecords(accountingRecords.toArray(new
|
||||
// AccountingRecord[1]));
|
||||
|
||||
|
@ -754,7 +688,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
// @Test
|
||||
public void testFilteringGenericResource() {
|
||||
try {
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
// Utils.setContext(RESOURCE_CATALOGUE);
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
@ -762,10 +696,8 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
Date start = DateUtils.getPreviousPeriod(measureType, false).getTime();
|
||||
Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
||||
|
||||
SortedSet<String> contexts = getContexts();
|
||||
|
||||
AccountingDashboardHarvesterPlugin accountingDataHarvesterPlugin = new AccountingDashboardHarvesterPlugin();
|
||||
accountingDataHarvesterPlugin.getConfigParameters();
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
SortedSet<String> contexts = contextAuthorization.getContexts();
|
||||
|
||||
ResourceCatalogueHarvester resourceCatalogueHarvester = new ResourceCatalogueHarvester(start, end,
|
||||
contexts);
|
||||
|
@ -783,9 +715,8 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
public void testResourceCatalogueHarvester() {
|
||||
try {
|
||||
|
||||
// Utils.setContext(RESOURCE_CATALOGUE);
|
||||
ContextTest.setContextByName(ROOT);
|
||||
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
AggregationType measureType = AggregationType.MONTHLY;
|
||||
|
||||
// Date start = DateUtils.getStartCalendar(2015, Calendar.FEBRUARY,
|
||||
|
@ -796,10 +727,8 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
Date start = DateUtils.getPreviousPeriod(measureType, false).getTime();
|
||||
Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
||||
|
||||
AccountingDashboardHarvesterPlugin accountingDataHarvesterPlugin = new AccountingDashboardHarvesterPlugin();
|
||||
accountingDataHarvesterPlugin.getConfigParameters();
|
||||
|
||||
SortedSet<String> contexts = getContexts();
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
SortedSet<String> contexts = contextAuthorization.getContexts();
|
||||
|
||||
ResourceCatalogueHarvester resourceCatalogueHarvester = new ResourceCatalogueHarvester(start, end,
|
||||
contexts);
|
||||
|
@ -812,28 +741,29 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Ignore
|
||||
// @Test
|
||||
// @Ignore
|
||||
@Test
|
||||
public void testCoreServicesHarvester() {
|
||||
try {
|
||||
|
||||
String context = ROOT; // "/gcube";
|
||||
String context = ROOT_PROD;
|
||||
ContextTest.setContextByName(context);
|
||||
|
||||
AccountingDao dao = getAccountingDao();
|
||||
Calendar from = DateUtils.getStartCalendar(2023, Calendar.MAY, 1);
|
||||
Calendar finalEnd = DateUtils.getStartCalendar(2023, Calendar.JULY, 1);
|
||||
|
||||
Date start = DateUtils.getStartCalendar(2017, Calendar.MAY, 1).getTime();
|
||||
Date finalEnd = DateUtils.getStartCalendar(2020, Calendar.MAY, 1).getTime();
|
||||
|
||||
Date end = DateUtils.getEndDateFromStartDate(AggregationType.MONTHLY, start, 1);
|
||||
AggregationType aggregationType = AggregationType.MONTHLY;
|
||||
Date start = from.getTime();
|
||||
Date end = DateUtils.getEndDateFromStartDate(aggregationType, start, 1);
|
||||
|
||||
ScopeBean scopeBean = new ScopeBean(context);
|
||||
logger.debug("FullName {} - Name {}", scopeBean.toString(), scopeBean.name());
|
||||
|
||||
while (end.before(finalEnd)) {
|
||||
while (from.before(end)) {
|
||||
CoreServicesAccessesHarvester coreServicesHarvester = new CoreServicesAccessesHarvester(start, end);
|
||||
List<AccountingRecord> accountingRecords = coreServicesHarvester.getAccountingRecords();
|
||||
dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1]));
|
||||
// dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1]));
|
||||
|
||||
logger.debug("{} -> {} Data Inserted", DateUtils.format(start), DateUtils.format(end));
|
||||
logger.debug("---------------------------------------------------------------------------------------");
|
||||
|
@ -848,18 +778,22 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Ignore
|
||||
// @Test
|
||||
// @Ignore
|
||||
@Test
|
||||
public void testCatalogueHarvester() {
|
||||
try {
|
||||
|
||||
String context = ROOT; // "/gcube";
|
||||
String context = ROOT_PROD;
|
||||
ContextTest.setContextByName(context);
|
||||
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
Date start = DateUtils.getStartCalendar(2020, Calendar.JANUARY, 1).getTime();
|
||||
Date end = DateUtils.getStartCalendar(2020, Calendar.FEBRUARY, 1).getTime();
|
||||
Calendar from = DateUtils.getStartCalendar(2023, Calendar.JUNE, 1);
|
||||
Calendar runbeforeDate = DateUtils.getStartCalendar(2023, Calendar.JULY, 1);
|
||||
AggregationType aggregationType = AggregationType.MONTHLY;
|
||||
|
||||
Date start = from.getTime();
|
||||
Date end = DateUtils.getEndDateFromStartDate(aggregationType, start, 1);
|
||||
|
||||
/*
|
||||
* Date start = DateUtils.getPreviousPeriod(measureType, false).getTime(); Date
|
||||
|
@ -876,7 +810,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
}
|
||||
logger.debug("{}", accountingRecords);
|
||||
|
||||
dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1]));
|
||||
// dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1]));
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("", e);
|
||||
|
@ -890,43 +824,19 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
|
|||
public void addMissingVREAccesses() {
|
||||
try {
|
||||
|
||||
ContextTest.setContextByName(ROOT);
|
||||
ContextTest.setContextByName(ROOT_PROD);
|
||||
|
||||
AccountingDashboardHarvesterPlugin adhp = new AccountingDashboardHarvesterPlugin();
|
||||
Properties properties = adhp.getConfigParameters();
|
||||
AccountingDashboardHarvesterPlugin.getProperties().set(properties);
|
||||
AccountingDao dao = getAccountingDao();
|
||||
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
|
||||
// DatabaseManager dbaseManager = new DatabaseManager();
|
||||
AccountingDao dao = AccountingDao.get();
|
||||
|
||||
Set<ScopeDescriptor> scopeDescriptorSet = dao.getContexts();
|
||||
Map<String, ScopeDescriptor> scopeDescriptorMap = new HashMap<>();
|
||||
for (ScopeDescriptor scopeDescriptor : scopeDescriptorSet) {
|
||||
scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor);
|
||||
}
|
||||
AccountingDashboardHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap);
|
||||
|
||||
Set<Dimension> dimensionSet = dao.getDimensions();
|
||||
Map<String, Dimension> dimensionMap = new HashMap<>();
|
||||
for (Dimension dimension : dimensionSet) {
|
||||
dimensionMap.put(dimension.getId(), dimension);
|
||||
}
|
||||
|
||||
AccountingDashboardHarvesterPlugin.dimensions.set(dimensionMap);
|
||||
|
||||
// ArrayList<HarvestedData> data = new ArrayList<HarvestedData>();
|
||||
ArrayList<AccountingRecord> accountingRecords = new ArrayList<AccountingRecord>();
|
||||
|
||||
String context = E_LEARNING_AREA_VRE;
|
||||
|
||||
// Setting the token for the context
|
||||
ContextTest.set(contextAuthorization.getSecretForContext(context));
|
||||
|
||||
ScopeBean scopeBean = new ScopeBean(context);
|
||||
ScopeDescriptor scopeDescriptor = new ScopeDescriptor(scopeBean.name(), context);
|
||||
ContextTest.setContextByName(context);
|
||||
|
||||
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor(context);
|
||||
Dimension dimension = AccountingDashboardHarvesterPlugin.getDimension(HarvestedDataKey.ACCESSES.getKey());
|
||||
|
||||
Calendar calendar = DateUtils.getStartCalendar(2018, Calendar.JULY, 1);
|
|
@ -5,6 +5,7 @@ import java.io.InputStream;
|
|||
import java.util.Properties;
|
||||
|
||||
import org.gcube.dataharvest.AccountingDashboardHarvesterPlugin;
|
||||
import org.gcube.dataharvest.ContextTest;
|
||||
import org.junit.Ignore;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -24,7 +25,6 @@ public class ContextAuthorizationTest extends ContextTest {
|
|||
properties = new Properties();
|
||||
InputStream input = AccountingDashboardHarvesterPlugin.class.getClassLoader().getResourceAsStream(PROPERTY_FILENAME);
|
||||
properties.load(input);
|
||||
AccountingDashboardHarvesterPlugin.getProperties().set(properties);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
|
@ -35,7 +35,7 @@ public class ContextAuthorizationTest extends ContextTest {
|
|||
}catch (Exception e) {
|
||||
logger.warn("Unable to load {} file containing configuration properties. AccountingDataHarvesterPlugin will use defaults", PROPERTY_FILENAME);
|
||||
}
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
||||
ContextAuthorization contextAuthorization = new ContextAuthorization(properties);
|
||||
contextAuthorization.retrieveContextsAndTokens();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,89 +0,0 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.dataharvest.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.gcube.common.authorization.utils.manager.SecretManager;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
||||
import org.gcube.common.authorization.utils.secret.Secret;
|
||||
import org.gcube.common.authorization.utils.secret.SecretUtility;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class ContextTest {
|
||||
|
||||
protected static Properties properties;
|
||||
protected static final String PROPERTIES_FILENAME = "token.properties";
|
||||
|
||||
public static final String ROOT;
|
||||
public static final String VO;
|
||||
public static final String VRE;
|
||||
|
||||
static {
|
||||
properties = new Properties();
|
||||
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
|
||||
|
||||
try {
|
||||
// load the properties file
|
||||
properties.load(input);
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// DEFAULT_TEST_SCOPE_NAME = "/pred4s/preprod/preVRE";
|
||||
// DEFAULT_TEST_SCOPE_NAME = "/gcube/devsec/devVRE";
|
||||
|
||||
ROOT = "/gcube";
|
||||
VO = ROOT + "/devsec";
|
||||
VRE = VO + "/devVRE";
|
||||
// VO = ROOT + "/devNext";
|
||||
// VRE = VO + "/NextNext";
|
||||
|
||||
}
|
||||
|
||||
public static void set(Secret secret) throws Exception {
|
||||
SecretManagerProvider.instance.reset();
|
||||
SecretManager secretManager = new SecretManager();
|
||||
SecretManagerProvider.instance.set(secretManager);
|
||||
secretManager.addSecret(secret);
|
||||
secretManager.set();
|
||||
}
|
||||
|
||||
public static void setContext(String token) throws Exception {
|
||||
Secret secret = getSecret(token);
|
||||
set(secret);
|
||||
}
|
||||
|
||||
public static void setContextByName(String fullContextName) throws Exception {
|
||||
Secret secret = getSecretByContextName(fullContextName);
|
||||
set(secret);
|
||||
}
|
||||
|
||||
private static Secret getSecret(String token) throws Exception {
|
||||
Secret secret = SecretUtility.getSecretByTokenString(token);
|
||||
return secret;
|
||||
}
|
||||
|
||||
private static Secret getSecretByContextName(String fullContextName) throws Exception {
|
||||
String token = ContextTest.properties.getProperty(fullContextName);
|
||||
return getSecret(token);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
setContextByName(VRE);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
SecretManagerProvider.instance.reset();
|
||||
}
|
||||
|
||||
}
|
|
@ -2,3 +2,5 @@
|
|||
/*.key
|
||||
/*.properties
|
||||
/howto.txt
|
||||
/scopedata 2.xml
|
||||
/config.ini
|
||||
|
|
Loading…
Reference in New Issue