2021-04-12 18:11:05 +02:00
|
|
|
package org.gcube.dataharvest;
|
2021-03-26 13:24:45 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
2021-04-12 18:11:05 +02:00
|
|
|
import java.util.HashMap;
|
2021-03-26 13:24:45 +01:00
|
|
|
import java.util.List;
|
2021-04-12 18:11:05 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2021-04-12 10:35:08 +02:00
|
|
|
import java.util.SortedSet;
|
2021-05-31 17:39:27 +02:00
|
|
|
import java.util.TreeSet;
|
2021-03-26 13:24:45 +01:00
|
|
|
|
2021-04-12 18:11:05 +02:00
|
|
|
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;
|
2021-03-26 13:24:45 +01:00
|
|
|
import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord;
|
2021-04-12 18:11:05 +02:00
|
|
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
2021-05-31 17:39:27 +02:00
|
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
2021-03-26 13:24:45 +01:00
|
|
|
import org.gcube.dataharvest.harvester.JupyterAccessesHarvester;
|
|
|
|
import org.gcube.dataharvest.utils.AggregationType;
|
2021-04-12 10:35:08 +02:00
|
|
|
import org.gcube.dataharvest.utils.ContextAuthorization;
|
2021-03-26 13:24:45 +01:00
|
|
|
import org.gcube.dataharvest.utils.ContextTest;
|
|
|
|
import org.gcube.dataharvest.utils.DateUtils;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-03-31 15:21:45 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Giancarlo Panichi (ISTI CNR)
|
|
|
|
*
|
|
|
|
*/
|
2021-03-26 13:24:45 +01:00
|
|
|
public class AccountingDataHarvesterJupyterTest extends ContextTest {
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterJupyterTest.class);
|
|
|
|
public static final String ROOT = "/d4science.research-infrastructures.eu";
|
2021-04-12 10:35:08 +02:00
|
|
|
// private static final String SCOPE = "/d4science.research-infrastructures.eu/D4OS/Blue-CloudLab";
|
2021-03-26 16:04:07 +01:00
|
|
|
|
2021-04-12 18:11:05 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-21 15:24:13 +02:00
|
|
|
//@Ignore
|
2021-03-26 13:24:45 +01:00
|
|
|
@Test
|
2021-03-26 16:35:15 +01:00
|
|
|
public void testJupyterAccessesHarvester() throws Exception {
|
2021-03-26 13:24:45 +01:00
|
|
|
try {
|
2021-04-12 18:11:05 +02:00
|
|
|
|
2021-05-21 15:24:13 +02:00
|
|
|
ContextTest.setContextByName(ROOT);
|
|
|
|
AccountingDao dao = getAccountingDao();
|
2021-03-26 13:24:45 +01:00
|
|
|
|
|
|
|
List<Date> starts = new ArrayList<>();
|
2021-04-12 18:11:05 +02:00
|
|
|
starts.add(DateUtils.getStartCalendar(2021, Calendar.JANUARY, 1).getTime());
|
|
|
|
starts.add(DateUtils.getStartCalendar(2021, Calendar.FEBRUARY, 1).getTime());
|
2021-03-26 13:24:45 +01:00
|
|
|
starts.add(DateUtils.getStartCalendar(2021, Calendar.MARCH, 1).getTime());
|
2021-05-21 15:24:13 +02:00
|
|
|
starts.add(DateUtils.getStartCalendar(2021, Calendar.APRIL, 1).getTime());
|
2021-05-31 17:39:27 +02:00
|
|
|
starts.add(DateUtils.getStartCalendar(2021, Calendar.MAY, 1).getTime());
|
2021-05-21 15:24:13 +02:00
|
|
|
|
2021-03-26 13:24:45 +01:00
|
|
|
AggregationType measureType = AggregationType.MONTHLY;
|
|
|
|
|
2021-04-12 18:11:05 +02:00
|
|
|
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
2021-05-31 17:39:27 +02:00
|
|
|
|
2021-04-12 18:11:05 +02:00
|
|
|
SortedSet<String> contexts = contextAuthorization.getContexts();
|
2021-05-31 17:39:27 +02:00
|
|
|
/*
|
|
|
|
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");
|
|
|
|
*/
|
2021-04-12 18:11:05 +02:00
|
|
|
|
2021-03-26 13:24:45 +01:00
|
|
|
List<AccountingRecord> accountingRecords = new ArrayList<>();
|
|
|
|
|
2021-05-31 17:39:27 +02:00
|
|
|
Set<ScopeDescriptor> scopeDescriptorSet = dao.getContexts();
|
|
|
|
Map<String, ScopeDescriptor> scopeDescriptorMap = new HashMap<>();
|
|
|
|
for (ScopeDescriptor scopeDescriptor : scopeDescriptorSet) {
|
|
|
|
scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor);
|
|
|
|
}
|
|
|
|
AccountingDashboardHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap);
|
|
|
|
|
|
|
|
|
2021-03-26 13:24:45 +01:00
|
|
|
for (Date start : starts) {
|
|
|
|
Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1, false);
|
|
|
|
|
|
|
|
ContextTest.setContextByName(ROOT);
|
|
|
|
JupyterAccessesHarvester jupyterAccessesHarvester = new JupyterAccessesHarvester(start, end);
|
|
|
|
|
2021-04-12 10:35:08 +02:00
|
|
|
for(String context : contexts) {
|
|
|
|
ContextTest.setContext(contextAuthorization.getTokenForContext(context));
|
|
|
|
|
2021-05-31 17:39:27 +02:00
|
|
|
ScopeBean scopeBean = new ScopeBean(context);
|
|
|
|
|
|
|
|
ScopeDescriptor actualScopeDescriptor = scopeDescriptorMap.get(context);
|
|
|
|
if (actualScopeDescriptor == null) {
|
|
|
|
actualScopeDescriptor = new ScopeDescriptor(scopeBean.name(), context);
|
|
|
|
}
|
|
|
|
|
|
|
|
AccountingDashboardHarvesterPlugin.scopeDescriptor.set(actualScopeDescriptor);
|
|
|
|
|
2021-04-12 10:35:08 +02:00
|
|
|
List<AccountingRecord> harvested = jupyterAccessesHarvester.getAccountingRecords();
|
|
|
|
accountingRecords.addAll(harvested);
|
|
|
|
}
|
2021-03-26 13:24:45 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-12 10:39:08 +02:00
|
|
|
// logger.debug("{}", accountingRecords);
|
2021-03-26 13:24:45 +01:00
|
|
|
|
2021-04-12 18:11:05 +02:00
|
|
|
logger.debug("Going to insert {}", accountingRecords);
|
|
|
|
|
2021-03-26 13:24:45 +01:00
|
|
|
ContextTest.setContextByName(ROOT);
|
2021-05-21 15:24:13 +02:00
|
|
|
dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1]));
|
2021-03-26 13:24:45 +01:00
|
|
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
logger.error(e.getLocalizedMessage(), e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|