Program: 14435
Project: 14435
Buggy source:
public ReportArtEntity generateReportArt(final ReportArtEntity report){
  log.debug("String_Node_Str");
  final Map<String,InstanceUsageArtEntity> usageEntities=new HashMap<String,InstanceUsageArtEntity>();
  final Map<String,Long> instanceStartTimes=new HashMap<String,Long>();
  foreachInstanceCreateEvent(report.getEndMs(),new Predicate<ReportingInstanceCreateEvent>(){
    @Override public boolean apply(    final ReportingInstanceCreateEvent createEvent){
      if (!report.getZones().containsKey(createEvent.getAvailabilityZone())) {
        report.getZones().put(createEvent.getAvailabilityZone(),new AvailabilityZoneArtEntity());
      }
      AvailabilityZoneArtEntity zone=report.getZones().get(createEvent.getAvailabilityZone());
      ReportingUser reportingUser=ReportingUserDao.getInstance().getReportingUser(createEvent.getUserId());
      if (reportingUser == null) {
        log.error("String_Node_Str" + createEvent.getUserId());
      }
      ReportingAccount reportingAccount=ReportingAccountDao.getInstance().getReportingAccount(reportingUser.getAccountId());
      if (reportingAccount == null) {
        log.error("String_Node_Str" + reportingUser.getAccountId());
      }
      if (!zone.getAccounts().containsKey(reportingAccount.getName())) {
        zone.getAccounts().put(reportingAccount.getName(),new AccountArtEntity());
      }
      AccountArtEntity account=zone.getAccounts().get(reportingAccount.getName());
      if (!account.getUsers().containsKey(reportingUser.getName())) {
        account.getUsers().put(reportingUser.getName(),new UserArtEntity());
      }
      UserArtEntity user=account.getUsers().get(reportingUser.getName());
      if (!user.getInstances().containsKey(createEvent.getUuid())) {
        user.getInstances().put(createEvent.getUuid(),new InstanceArtEntity(createEvent.getInstanceType(),createEvent.getInstanceId()));
      }
      InstanceArtEntity instance=user.getInstances().get(createEvent.getUuid());
      instance.getUsage().addInstanceCnt(1);
      usageEntities.put(createEvent.getUuid(),instance.getUsage());
      instanceStartTimes.put(createEvent.getUuid(),createEvent.getTimestampMs());
      return true;
    }
  }
);
  final Map<InstanceMetricDimensionKey,MetricPrevData> prevDataMap=new HashMap<InstanceMetricDimensionKey,MetricPrevData>();
  foreachInstanceUsageEvent(report.getBeginMs() - USAGE_SEARCH_PERIOD,report.getEndMs() + USAGE_SEARCH_PERIOD,new Predicate<ReportingInstanceUsageEvent>(){
    @Override public boolean apply(    final ReportingInstanceUsageEvent event){
      final InstanceMetricDimensionKey key=new InstanceMetricDimensionKey(event.getUuid(),event.getMetric(),event.getDimension());
      final long eventMs=event.getTimestampMs();
      if (event.getValue() == null)       return true;
      final InstanceUsageArtEntity usageEntity=usageEntities.get(event.getUuid());
      if (usageEntity == null)       return true;
      if (!prevDataMap.containsKey(key)) {
        if (instanceStartTimes.containsKey(event.getUuid())) {
          Double fractionalVal=fractionalUsage(report.getBeginMs(),report.getEndMs(),instanceStartTimes.get(event.getUuid()),eventMs,event.getValue());
          addMetricValueToUsageEntity(usageEntity,event.getMetric(),event.getDimension(),fractionalVal);
        }
        prevDataMap.put(key,new MetricPrevData(eventMs,eventMs,event.getValue()));
      }
 else {
        final MetricPrevData prevData=prevDataMap.get(key);
        usageEntity.setDurationMs(Math.max(usageEntity.getDurationMs(),overlap(report.getBeginMs(),report.getEndMs(),prevData.firstMs,eventMs)));
        if (prevData.lastVal < event.getValue()) {
          Double fractionalVal=fractionalUsage(report.getBeginMs(),report.getEndMs(),prevData.lastMs,eventMs,event.getValue());
          addMetricValueToUsageEntity(usageEntity,event.getMetric(),event.getDimension(),fractionalVal);
        }
 else {
          Double fractionalVal=fractionalUsage(report.getBeginMs(),report.getEndMs(),prevData.lastMs,eventMs,event.getValue() - prevData.lastVal);
          addMetricValueToUsageEntity(usageEntity,event.getMetric(),event.getDimension(),fractionalVal);
        }
        prevDataMap.put(key,new MetricPrevData(prevData.firstMs,eventMs,event.getValue()));
      }
      return true;
    }
  }
);
  for (  final AvailabilityZoneArtEntity zone : report.getZones().values()) {
    final UsageTotalsArtEntity zoneUsage=zone.getUsageTotals();
    for (    final AccountArtEntity account : zone.getAccounts().values()) {
      final UsageTotalsArtEntity accountUsage=account.getUsageTotals();
      for (      final UserArtEntity user : account.getUsers().values()) {
        final UsageTotalsArtEntity userUsage=user.getUsageTotals();
        for (        final InstanceArtEntity instance : user.getInstances().values()) {
          updateUsageTotals(userUsage,instance);
          updateUsageTotals(accountUsage,instance);
          updateUsageTotals(zoneUsage,instance);
        }
      }
    }
  }
  return report;
}

Fixed version (ground truth):
public ReportArtEntity generateReportArt(final ReportArtEntity report){
  log.debug("String_Node_Str");
  final Map<String,InstanceUsageArtEntity> usageEntities=new HashMap<String,InstanceUsageArtEntity>();
  final Map<String,Long> instanceStartTimes=new HashMap<String,Long>();
  foreachInstanceCreateEvent(report.getEndMs(),new Predicate<ReportingInstanceCreateEvent>(){
    @Override public boolean apply(    final ReportingInstanceCreateEvent createEvent){
      if (!report.getZones().containsKey(createEvent.getAvailabilityZone())) {
        report.getZones().put(createEvent.getAvailabilityZone(),new AvailabilityZoneArtEntity());
      }
      AvailabilityZoneArtEntity zone=report.getZones().get(createEvent.getAvailabilityZone());
      ReportingUser reportingUser=ReportingUserDao.getInstance().getReportingUser(createEvent.getUserId());
      if (reportingUser == null) {
        log.error("String_Node_Str" + createEvent.getUserId());
      }
      ReportingAccount reportingAccount=ReportingAccountDao.getInstance().getReportingAccount(reportingUser.getAccountId());
      if (reportingAccount == null) {
        log.error("String_Node_Str" + reportingUser.getAccountId());
      }
      if (!zone.getAccounts().containsKey(reportingAccount.getName())) {
        zone.getAccounts().put(reportingAccount.getName(),new AccountArtEntity());
      }
      AccountArtEntity account=zone.getAccounts().get(reportingAccount.getName());
      if (!account.getUsers().containsKey(reportingUser.getName())) {
        account.getUsers().put(reportingUser.getName(),new UserArtEntity());
      }
      UserArtEntity user=account.getUsers().get(reportingUser.getName());
      if (!user.getInstances().containsKey(createEvent.getUuid())) {
        user.getInstances().put(createEvent.getUuid(),new InstanceArtEntity(createEvent.getInstanceType(),createEvent.getInstanceId()));
      }
      InstanceArtEntity instance=user.getInstances().get(createEvent.getUuid());
      instance.getUsage().addInstanceCnt(1);
      usageEntities.put(createEvent.getUuid(),instance.getUsage());
      instanceStartTimes.put(createEvent.getUuid(),createEvent.getTimestampMs());
      return true;
    }
  }
);
  final Map<InstanceMetricDimensionKey,MetricPrevData> prevDataMap=new HashMap<InstanceMetricDimensionKey,MetricPrevData>();
  foreachInstanceUsageEvent(report.getBeginMs() - USAGE_SEARCH_PERIOD,report.getEndMs() + USAGE_SEARCH_PERIOD,new Predicate<ReportingInstanceUsageEvent>(){
    @Override public boolean apply(    final ReportingInstanceUsageEvent event){
      final InstanceMetricDimensionKey key=new InstanceMetricDimensionKey(event.getUuid(),event.getMetric(),event.getDimension());
      final long eventMs=event.getTimestampMs();
      if (event.getValue() == null)       return true;
      final InstanceUsageArtEntity usageEntity=usageEntities.get(event.getUuid());
      if (usageEntity == null)       return true;
      if (!prevDataMap.containsKey(key)) {
        if (instanceStartTimes.containsKey(event.getUuid())) {
          Double fractionalVal=fractionalUsage(report.getBeginMs(),report.getEndMs(),instanceStartTimes.get(event.getUuid()),eventMs,event.getValue());
          addMetricValueToUsageEntity(usageEntity,event.getMetric(),event.getDimension(),fractionalVal);
        }
        prevDataMap.put(key,new MetricPrevData(eventMs,eventMs,event.getValue()));
      }
 else {
        final MetricPrevData prevData=prevDataMap.get(key);
        usageEntity.setDurationMs(Math.max(usageEntity.getDurationMs(),overlap(report.getBeginMs(),report.getEndMs(),prevData.firstMs,eventMs)));
        if (event.getValue() < prevData.lastVal) {
          Double fractionalVal=fractionalUsage(report.getBeginMs(),report.getEndMs(),prevData.lastMs,eventMs,event.getValue());
          addMetricValueToUsageEntity(usageEntity,event.getMetric(),event.getDimension(),fractionalVal);
        }
 else {
          Double fractionalVal=fractionalUsage(report.getBeginMs(),report.getEndMs(),prevData.lastMs,eventMs,event.getValue() - prevData.lastVal);
          log.debug(String.format("String_Node_Str",prevData.lastMs,eventMs,report.getBeginMs(),report.getEndMs(),event.getUuid(),event.getMetric(),event.getDimension(),event.getValue(),prevData.lastVal,fractionalVal));
          addMetricValueToUsageEntity(usageEntity,event.getMetric(),event.getDimension(),fractionalVal);
        }
        prevDataMap.put(key,new MetricPrevData(prevData.firstMs,eventMs,event.getValue()));
      }
      return true;
    }
  }
);
  for (  final AvailabilityZoneArtEntity zone : report.getZones().values()) {
    final UsageTotalsArtEntity zoneUsage=zone.getUsageTotals();
    for (    final AccountArtEntity account : zone.getAccounts().values()) {
      final UsageTotalsArtEntity accountUsage=account.getUsageTotals();
      for (      final UserArtEntity user : account.getUsers().values()) {
        final UsageTotalsArtEntity userUsage=user.getUsageTotals();
        for (        final InstanceArtEntity instance : user.getInstances().values()) {
          updateUsageTotals(userUsage,instance);
          updateUsageTotals(accountUsage,instance);
          updateUsageTotals(zoneUsage,instance);
        }
      }
    }
  }
  return report;
}