Search in sources :

Example 1 with StatisticKeyEntity

use of org.orcid.statistics.jpa.entities.StatisticKeyEntity in project ORCID-Source by ORCID.

the class StatisticsManagerReadOnlyImpl method getLatestStatisticsModel.

/**
     * Get the list of the latest statistics as a domain model
     * 
     * @return a list that contains the latest set of statistics
     */
@Override
public StatisticsSummary getLatestStatisticsModel() {
    StatisticKeyEntity latestKey = statisticsDaoReadOnly.getLatestKey();
    if (latestKey == null)
        return null;
    List<StatisticValuesEntity> list = statisticsDaoReadOnly.getStatistic(latestKey.getId());
    if (list == null || list.size() == 0)
        return null;
    // convert to model
    StatisticsSummary summary = new StatisticsSummary();
    Map<String, Long> map = new TreeMap<String, Long>();
    for (StatisticValuesEntity entry : list) {
        map.put(entry.getStatisticName(), entry.getStatisticValue());
    }
    summary.setStatistics(map);
    summary.setDate(latestKey.getGenerationDate());
    return summary;
}
Also used : StatisticValuesEntity(org.orcid.statistics.jpa.entities.StatisticValuesEntity) StatisticsSummary(org.orcid.jaxb.model.statistics.StatisticsSummary) StatisticKeyEntity(org.orcid.statistics.jpa.entities.StatisticKeyEntity) TreeMap(java.util.TreeMap)

Example 2 with StatisticKeyEntity

use of org.orcid.statistics.jpa.entities.StatisticKeyEntity in project ORCID-Source by ORCID.

the class StatsApiServiceBaseImplTest method init.

@Before
public void init() {
    // create our mock data
    List<StatisticValuesEntity> statsTimelineValues = new ArrayList<StatisticValuesEntity>();
    List<StatisticValuesEntity> statsSummaryValues = new ArrayList<StatisticValuesEntity>();
    StatisticValuesEntity a = new StatisticValuesEntity();
    a.setId(1l);
    a.setStatisticName(StatisticsEnum.KEY_LIVE_IDS.value());
    a.setStatisticValue(100l);
    StatisticKeyEntity akey = new StatisticKeyEntity();
    akey.setGenerationDate(new Date(2000, 1, 1));
    akey.setId(200L);
    a.setKey(akey);
    StatisticValuesEntity b = new StatisticValuesEntity();
    b.setId(1l);
    b.setStatisticName(StatisticsEnum.KEY_LIVE_IDS.value());
    b.setStatisticValue(101l);
    StatisticKeyEntity bkey = new StatisticKeyEntity();
    bkey.setGenerationDate(new Date(1999, 1, 1));
    bkey.setId(201L);
    b.setKey(bkey);
    StatisticValuesEntity c = new StatisticValuesEntity();
    c.setId(1l);
    c.setStatisticName(StatisticsEnum.KEY_NUMBER_OF_WORKS.value());
    c.setStatisticValue(102l);
    c.setKey(akey);
    statsTimelineValues.add(a);
    statsTimelineValues.add(b);
    statsSummaryValues.add(a);
    statsSummaryValues.add(c);
    // mock the methods used
    when(statisticsDao.getLatestKey()).thenReturn(akey);
    when(statisticsDao.getStatistic(StatisticsEnum.KEY_LIVE_IDS.value())).thenReturn(statsTimelineValues);
    when(statisticsDao.getStatistic(200l)).thenReturn(statsSummaryValues);
    // mock the methods used
    StatisticKeyEntity key200 = new StatisticKeyEntity();
    key200.setId(200L);
    key200.setGenerationDate(new Date(2000, 1, 1));
    StatisticKeyEntity key201 = new StatisticKeyEntity();
    key201.setId(201L);
    key201.setGenerationDate(new Date(1999, 1, 1));
    when(statisticsDao.getKey(200L)).thenReturn(key200);
    when(statisticsDao.getKey(201L)).thenReturn(key201);
    TargetProxyHelper.injectIntoProxy(statsManagerReadOnly, "statisticsDaoReadOnly", statisticsDao);
    // setup security context
    ArrayList<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
    roles.add(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
    Authentication auth = new AnonymousAuthenticationToken("anonymous", "anonymous", roles);
    SecurityContextHolder.getContext().setAuthentication(auth);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) StatisticValuesEntity(org.orcid.statistics.jpa.entities.StatisticValuesEntity) StatisticKeyEntity(org.orcid.statistics.jpa.entities.StatisticKeyEntity) Authentication(org.springframework.security.core.Authentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Date(java.util.Date) Before(org.junit.Before)

Example 3 with StatisticKeyEntity

use of org.orcid.statistics.jpa.entities.StatisticKeyEntity in project ORCID-Source by ORCID.

the class StatisticsDaoImpl method createKey.

/**
     * Creates a new statistics key
     * 
     * @return the statistic key object
     */
@Override
@Transactional
public StatisticKeyEntity createKey() {
    StatisticKeyEntity key = new StatisticKeyEntity();
    key.setGenerationDate(new Date());
    statisticsEntityManager.persist(key);
    return key;
}
Also used : StatisticKeyEntity(org.orcid.statistics.jpa.entities.StatisticKeyEntity) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with StatisticKeyEntity

use of org.orcid.statistics.jpa.entities.StatisticKeyEntity in project ORCID-Source by ORCID.

the class StatisticsGeneratorCronJobImpl method generateStatistics.

/**
     * Cron job that will generate statistics and store them on database
     */
@Override
public void generateStatistics() {
    LOG.debug("About to run statistics generator thread");
    boolean run = false;
    StatisticKeyEntity lastStatisticsKey = statisticsManagerReadOnly.getLatestKey();
    if (lastStatisticsKey != null && lastStatisticsKey.getGenerationDate() != null) {
        Date lastTimeJobRuns = lastStatisticsKey.getGenerationDate();
        boolean isTimeToRun = isFridayNearMidnight();
        long offset = System.currentTimeMillis() - lastTimeJobRuns.getTime();
        LOG.info("Last time the statistics were generated: {}", lastTimeJobRuns);
        LOG.info("Is time to run the scheduler? {}", isTimeToRun);
        if (offset > weekInMillis || (isTimeToRun && offset > halfHourInMillis)) {
            run = true;
        }
    } else {
        run = true;
        LOG.warn("There are no statistics generated yet.");
    }
    if (run) {
        Map<String, Long> statistics = statisticsGeneratorManager.generateStatistics();
        statisticsManager.saveStatistics(statistics);
        LOG.info("Last time the statistics cron job ran: {}", new Date());
    }
}
Also used : StatisticKeyEntity(org.orcid.statistics.jpa.entities.StatisticKeyEntity) Date(java.util.Date)

Example 5 with StatisticKeyEntity

use of org.orcid.statistics.jpa.entities.StatisticKeyEntity in project ORCID-Source by ORCID.

the class StatisticsManagerReadOnlyImpl method getStatisticsTimelineModel.

/**
     * Get all entries with a given name;
     * 
     * @param statisticName
     * @return all statistics values for the statistics name parameter
     */
public StatisticsTimeline getStatisticsTimelineModel(StatisticsEnum statisticName) {
    List<StatisticValuesEntity> list = statisticsDaoReadOnly.getStatistic(statisticName.value());
    if (list == null)
        return null;
    // convert to model
    StatisticsTimeline timeline = new StatisticsTimeline();
    timeline.setStatisticName(statisticName.value());
    Map<Long, Long> map = new TreeMap<Long, Long>();
    Map<Long, Date> generationDateMap = new HashMap<Long, Date>();
    for (StatisticValuesEntity entry : list) {
        if (!generationDateMap.containsKey(entry.getKey().getId())) {
            StatisticKeyEntity key = statisticsDaoReadOnly.getKey(entry.getKey().getId());
            Long time = key.getGenerationDate().getTime();
            map.put(time, entry.getStatisticValue());
            generationDateMap.put(key.getId(), key.getGenerationDate());
        } else {
            Date date = generationDateMap.get(entry.getKey().getId());
            map.put(date.getTime(), entry.getStatisticValue());
        }
    }
    timeline.setTimeline(map);
    return timeline;
}
Also used : StatisticValuesEntity(org.orcid.statistics.jpa.entities.StatisticValuesEntity) HashMap(java.util.HashMap) StatisticKeyEntity(org.orcid.statistics.jpa.entities.StatisticKeyEntity) StatisticsTimeline(org.orcid.jaxb.model.statistics.StatisticsTimeline) TreeMap(java.util.TreeMap) Date(java.util.Date)

Aggregations

StatisticKeyEntity (org.orcid.statistics.jpa.entities.StatisticKeyEntity)6 Date (java.util.Date)4 StatisticValuesEntity (org.orcid.statistics.jpa.entities.StatisticValuesEntity)4 TreeMap (java.util.TreeMap)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Before (org.junit.Before)1 Test (org.junit.Test)1 StatisticsSummary (org.orcid.jaxb.model.statistics.StatisticsSummary)1 StatisticsTimeline (org.orcid.jaxb.model.statistics.StatisticsTimeline)1 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1