140 lines
4.9 KiB
Java
140 lines
4.9 KiB
Java
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;
|
|
}
|
|
}
|
|
|
|
|
|
}
|