Program: 14466
Project: 14466
Buggy source:
public ReportArtEntity generateReportArt(ReportArtEntity report){
  log.debug("String_Node_Str");
  EntityWrapper wrapper=EntityWrapper.get(ReportingInstanceCreateEvent.class);
  Iterator iter=wrapper.scanWithNativeQuery("String_Node_Str");
  Map<String,InstanceArtEntity> instanceEntities=new HashMap<String,InstanceArtEntity>();
  while (iter.hasNext()) {
    ReportingInstanceCreateEvent createEvent=(ReportingInstanceCreateEvent)iter.next();
    if (!report.getZones().containsKey(createEvent.getAvailabilityZone())) {
      report.getZones().put(createEvent.getAvailabilityZone(),new AvailabilityZoneArtEntity());
    }
    AvailabilityZoneArtEntity zone=report.getZones().get(createEvent.getAvailabilityZone());
    if (!zone.getClusters().containsKey(createEvent.getClusterName())) {
      zone.getClusters().put(createEvent.getClusterName(),new ClusterArtEntity());
    }
    ClusterArtEntity cluster=zone.getClusters().get(createEvent.getClusterName());
    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 (!cluster.getAccounts().containsKey(reportingAccount.getName())) {
      cluster.getAccounts().put(reportingAccount.getName(),new AccountArtEntity());
    }
    AccountArtEntity account=cluster.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());
    instanceEntities.put(createEvent.getUuid(),instance);
  }
  Map<String,ReportingInstanceUsageEvent> lastEvents=new HashMap<String,ReportingInstanceUsageEvent>();
  iter=wrapper.scanWithNativeQuery("String_Node_Str");
  while (iter.hasNext()) {
    ReportingInstanceUsageEvent usageEvent=(ReportingInstanceUsageEvent)iter.next();
    ReportingInstanceUsageEvent lastEvent=lastEvents.get(usageEvent.getUuid());
    lastEvents.put(usageEvent.getUuid(),usageEvent);
    InstanceArtEntity instance=instanceEntities.get(usageEvent.getUuid());
    if (instance == null) {
      log.error("String_Node_Str" + usageEvent.getUuid());
      continue;
    }
    InstanceUsageArtEntity usage=instance.getUsage();
    if (lastEvent != null) {
      long lastMs=lastEvent.getTimestampMs();
      usage.setDiskIoMegs(plus(usage.getDiskIoMegs(),subtract(usageEvent.getCumulativeDiskIoMegs(),lastEvent.getCumulativeDiskIoMegs())));
      usage.setNetIoWithinZoneInMegs(plus(usage.getNetIoWithinZoneInMegs(),subtract(usageEvent.getCumulativeNetIncomingMegsWithinZone(),lastEvent.getCumulativeNetIncomingMegsWithinZone())));
      usage.setNetIoBetweenZoneInMegs(plus(usage.getNetIoBetweenZoneInMegs(),subtract(usageEvent.getCumulativeNetIncomingMegsBetweenZones(),lastEvent.getCumulativeNetIncomingMegsBetweenZones())));
      usage.setNetIoPublicIpInMegs(plus(usage.getNetIoPublicIpInMegs(),subtract(usageEvent.getCumulativeNetIncomingMegsPublic(),lastEvent.getCumulativeNetIncomingMegsPublic())));
      usage.setNetIoWithinZoneOutMegs(plus(usage.getNetIoWithinZoneOutMegs(),subtract(usageEvent.getCumulativeNetOutgoingMegsWithinZone(),lastEvent.getCumulativeNetOutgoingMegsWithinZone())));
      usage.setNetIoBetweenZoneOutMegs(plus(usage.getNetIoBetweenZoneOutMegs(),subtract(usageEvent.getCumulativeNetOutgoingMegsBetweenZones(),lastEvent.getCumulativeNetOutgoingMegsBetweenZones())));
      usage.setNetIoPublicIpOutMegs(plus(usage.getNetIoPublicIpOutMegs(),subtract(usageEvent.getCumulativeNetOutgoingMegsPublic(),usageEvent.getCumulativeNetOutgoingMegsPublic())));
      long durationMs=Math.min(report.getEndMs(),usageEvent.getTimestampMs()) - Math.max(report.getBeginMs(),lastMs);
      if (usage.getCpuPercentAvg() == null && usageEvent.getCpuUtilizationPercent() != null) {
        usage.setCpuPercentAvg((double)usageEvent.getCpuUtilizationPercent());
        usage.addDurationMs(durationMs);
      }
 else       if (usage.getCpuPercentAvg() != null && usageEvent.getCpuUtilizationPercent() != null) {
        double newWeightedAverage=(usage.getCpuPercentAvg() * (double)usage.getDurationMs() + (double)usageEvent.getCpuUtilizationPercent() * (double)durationMs) / ((double)usage.getDurationMs() + (double)durationMs);
        usage.setCpuPercentAvg(newWeightedAverage);
        usage.addDurationMs(durationMs);
      }
    }
  }
  for (  String zoneName : report.getZones().keySet()) {
    AvailabilityZoneArtEntity zone=report.getZones().get(zoneName);
    UsageTotalsArtEntity zoneUsage=zone.getUsageTotals();
    for (    String clusterName : zone.getClusters().keySet()) {
      ClusterArtEntity cluster=zone.getClusters().get(clusterName);
      UsageTotalsArtEntity clusterUsage=cluster.getUsageTotals();
      for (      String accountName : cluster.getAccounts().keySet()) {
        AccountArtEntity account=cluster.getAccounts().get(accountName);
        UsageTotalsArtEntity accountUsage=account.getUsageTotals();
        for (        String userName : account.getUsers().keySet()) {
          UserArtEntity user=account.getUsers().get(userName);
          UsageTotalsArtEntity userUsage=user.getUsageTotals();
          for (          String instanceUuid : user.getInstances().keySet()) {
            InstanceArtEntity instance=user.getInstances().get(instanceUuid);
            updateUsageTotals(userUsage,instance);
            updateUsageTotals(accountUsage,instance);
            updateUsageTotals(clusterUsage,instance);
            updateUsageTotals(zoneUsage,instance);
          }
        }
      }
    }
  }
  return report;
}

Fixed version (ground truth):
public ReportArtEntity generateReportArt(ReportArtEntity report){
  log.debug("String_Node_Str");
  EntityWrapper wrapper=EntityWrapper.get(ReportingInstanceCreateEvent.class);
  Iterator iter=wrapper.scanWithNativeQuery("String_Node_Str");
  Map<String,InstanceArtEntity> instanceEntities=new HashMap<String,InstanceArtEntity>();
  while (iter.hasNext()) {
    ReportingInstanceCreateEvent createEvent=(ReportingInstanceCreateEvent)iter.next();
    if (!report.getZones().containsKey(createEvent.getAvailabilityZone())) {
      report.getZones().put(createEvent.getAvailabilityZone(),new AvailabilityZoneArtEntity());
    }
    AvailabilityZoneArtEntity zone=report.getZones().get(createEvent.getAvailabilityZone());
    if (!zone.getClusters().containsKey(createEvent.getClusterName())) {
      zone.getClusters().put(createEvent.getClusterName(),new ClusterArtEntity());
    }
    ClusterArtEntity cluster=zone.getClusters().get(createEvent.getClusterName());
    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 (!cluster.getAccounts().containsKey(reportingAccount.getName())) {
      cluster.getAccounts().put(reportingAccount.getName(),new AccountArtEntity());
    }
    AccountArtEntity account=cluster.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());
    instanceEntities.put(createEvent.getUuid(),instance);
  }
  Map<String,ReportingInstanceUsageEvent> lastEvents=new HashMap<String,ReportingInstanceUsageEvent>();
  iter=wrapper.scanWithNativeQuery("String_Node_Str");
  while (iter.hasNext()) {
    ReportingInstanceUsageEvent usageEvent=(ReportingInstanceUsageEvent)iter.next();
    ReportingInstanceUsageEvent lastEvent=lastEvents.get(usageEvent.getUuid());
    lastEvents.put(usageEvent.getUuid(),usageEvent);
    InstanceArtEntity instance=instanceEntities.get(usageEvent.getUuid());
    if (instance == null) {
      log.error("String_Node_Str" + usageEvent.getUuid());
      continue;
    }
    InstanceUsageArtEntity usage=instance.getUsage();
    if (lastEvent != null) {
      long lastMs=lastEvent.getTimestampMs();
      usage.setDiskIoMegs(plus(usage.getDiskIoMegs(),subtract(usageEvent.getCumulativeDiskIoMegs(),lastEvent.getCumulativeDiskIoMegs())));
      usage.setNetIoWithinZoneInMegs(plus(usage.getNetIoWithinZoneInMegs(),subtract(usageEvent.getCumulativeNetIncomingMegsWithinZone(),lastEvent.getCumulativeNetIncomingMegsWithinZone())));
      usage.setNetIoBetweenZoneInMegs(plus(usage.getNetIoBetweenZoneInMegs(),subtract(usageEvent.getCumulativeNetIncomingMegsBetweenZones(),lastEvent.getCumulativeNetIncomingMegsBetweenZones())));
      usage.setNetIoPublicIpInMegs(plus(usage.getNetIoPublicIpInMegs(),subtract(usageEvent.getCumulativeNetIncomingMegsPublic(),lastEvent.getCumulativeNetIncomingMegsPublic())));
      usage.setNetIoWithinZoneOutMegs(plus(usage.getNetIoWithinZoneOutMegs(),subtract(usageEvent.getCumulativeNetOutgoingMegsWithinZone(),lastEvent.getCumulativeNetOutgoingMegsWithinZone())));
      usage.setNetIoBetweenZoneOutMegs(plus(usage.getNetIoBetweenZoneOutMegs(),subtract(usageEvent.getCumulativeNetOutgoingMegsBetweenZones(),lastEvent.getCumulativeNetOutgoingMegsBetweenZones())));
      usage.setNetIoPublicIpOutMegs(plus(usage.getNetIoPublicIpOutMegs(),subtract(usageEvent.getCumulativeNetOutgoingMegsPublic(),lastEvent.getCumulativeNetOutgoingMegsPublic())));
      long durationMs=Math.min(report.getEndMs(),usageEvent.getTimestampMs()) - Math.max(report.getBeginMs(),lastMs);
      if (usage.getCpuPercentAvg() == null && usageEvent.getCpuUtilizationPercent() != null) {
        usage.setCpuPercentAvg((double)usageEvent.getCpuUtilizationPercent());
        usage.addDurationMs(durationMs);
      }
 else       if (usage.getCpuPercentAvg() != null && usageEvent.getCpuUtilizationPercent() != null) {
        double newWeightedAverage=(usage.getCpuPercentAvg() * (double)usage.getDurationMs() + (double)usageEvent.getCpuUtilizationPercent() * (double)durationMs) / ((double)usage.getDurationMs() + (double)durationMs);
        usage.setCpuPercentAvg(newWeightedAverage);
        usage.addDurationMs(durationMs);
      }
    }
  }
  for (  String zoneName : report.getZones().keySet()) {
    AvailabilityZoneArtEntity zone=report.getZones().get(zoneName);
    UsageTotalsArtEntity zoneUsage=zone.getUsageTotals();
    for (    String clusterName : zone.getClusters().keySet()) {
      ClusterArtEntity cluster=zone.getClusters().get(clusterName);
      UsageTotalsArtEntity clusterUsage=cluster.getUsageTotals();
      for (      String accountName : cluster.getAccounts().keySet()) {
        AccountArtEntity account=cluster.getAccounts().get(accountName);
        UsageTotalsArtEntity accountUsage=account.getUsageTotals();
        for (        String userName : account.getUsers().keySet()) {
          UserArtEntity user=account.getUsers().get(userName);
          UsageTotalsArtEntity userUsage=user.getUsageTotals();
          for (          String instanceUuid : user.getInstances().keySet()) {
            InstanceArtEntity instance=user.getInstances().get(instanceUuid);
            updateUsageTotals(userUsage,instance);
            updateUsageTotals(accountUsage,instance);
            updateUsageTotals(clusterUsage,instance);
            updateUsageTotals(zoneUsage,instance);
          }
        }
      }
    }
  }
  return report;
}