package org.gcube.dataharvest.utils; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Properties; import java.util.SortedSet; import java.util.TreeSet; import java.util.concurrent.TimeUnit; import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord; import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.dataharvest.AccountingDashboardHarvesterPlugin; import org.gcube.dataharvest.harvester.VREAccessesHarvester; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestMassi extends ContextTest { private static Logger logger = LoggerFactory.getLogger(TestMassi.class); public static final String ROOT = "/d4science.research-infrastructures.eu"; public TestMassi() { // TODO Auto-generated constructor stub } public static void main(String[] args) { try { ContextTest.setContextByName(ROOT); // AccountingDao dao = AccountingDao.get(); AccountingDashboardHarvesterPlugin accountingDataHarvesterPlugin = new AccountingDashboardHarvesterPlugin(); Properties properties = accountingDataHarvesterPlugin.getConfigParameters(); AccountingDashboardHarvesterPlugin.getProperties().set(properties); ContextAuthorization contextAuthorization = new ContextAuthorization(); SortedSet contexts = contextAuthorization.getContexts(); AggregationType aggregationType = AggregationType.MONTHLY; Calendar from = DateUtils.getStartCalendar(2023, Calendar.APRIL, 1); Calendar runbeforeDate = DateUtils.getStartCalendar(2023, Calendar.JULY, 1); while (from.before(runbeforeDate)) { Date start = from.getTime(); Date end = DateUtils.getEndDateFromStartDate(aggregationType, start, 1); logger.debug("Harvesting from {} to {}", DateUtils.format(start), DateUtils.format(end)); ArrayList accountingRecords = new ArrayList<>(); VREAccessesHarvester vreAccessesHarvester = null; for (String context : contexts) { // Setting the token for the context ContextTest.set(contextAuthorization.getSecretForContext(context)); logger.debug("Context set: {} ", context); ScopeBean scopeBean = new ScopeBean(context); if (vreAccessesHarvester == null) { if (scopeBean.is(Type.INFRASTRUCTURE)) { vreAccessesHarvester = new VREAccessesHarvester(start, end); } else { // This code should be never used because the scopes // are sorted by fullname ScopeBean parent = scopeBean.enclosingScope(); while (!parent.is(Type.INFRASTRUCTURE)) { parent = scopeBean.enclosingScope(); } // Setting back token for the context ContextTest.set(contextAuthorization.getSecretForContext(parent.toString())); vreAccessesHarvester = new VREAccessesHarvester(start, end); // Setting back token for the context ContextTest.set(contextAuthorization.getSecretForContext(context)); } } try { if (context.startsWith(AccountingDashboardHarvesterPlugin.SO_BIG_DATA_VO) && start.before(DateUtils.getStartCalendar(2018, Calendar.APRIL, 1).getTime())) { logger.info("Not Harvesting VREs Accesses for {} from {} to {}", context, DateUtils.format(start), DateUtils.format(end)); } else { // Collecting Google Analytics Data for VREs // Accesses List harvested = vreAccessesHarvester.getAccountingRecords(); accountingRecords.addAll(harvested); } } catch (Exception e) { logger.error("Error harvesting Social Interactions for {}", context, e); } } logger.debug("Harvest Measures from {} to {} are {}", DateUtils.format(start), DateUtils.format(end), accountingRecords); ContextTest.setContextByName(ROOT); // dao.insertRecords(accountingRecords.toArray(new // AccountingRecord[1])); Thread.sleep(TimeUnit.SECONDS.toMillis(10)); from.add(aggregationType.getCalendarField(), 1); } ContextTest.setContextByName(ROOT); } catch (Exception e) { logger.error("", e); } logger.info("End."); } }