src/org/ohmage/reporting/AuditReporter.java
@Override
		public void run() {
			DateTime currentDate = new DateTime();
			
			DateTime endDate = 
					new DateTime(
						currentDate.getYear(), 
						currentDate.getMonthOfYear(), 
						currentDate.getDayOfMonth(), 
						0, 
						0);
			
			DateTime startDate = endDate.minusDays(1);
			
			// Use the service to aggregate the results.
			List<Audit> audits;
			try {
				audits = AuditServices.instance().getAuditInformation(
						null, 
						null, 
						null, 
						null, 
						null, 
						null, 
						startDate, 
						endDate);
			}
			catch(ServiceException e) {
				LOGGER
					.error(
						"There was an error generating the audit information.",
						e);
				return;
			}
			
			long numberOfValidRequests = 0;
			long numberOfInvalidRequests = 0;
			long numberOfSuccessfulValidRequests = 0;
			long numberOfFailedValidRequests = 0;
			long timeToProcessValidRequests = 0;

			Map<String, Integer> numberUriRequests = new HashMap<String, Integer>();
			Map<String, Integer> numberCampaignReads = new HashMap<String, Integer>();
			Map<String, Integer> numberClassReads = new HashMap<String, Integer>();
			
			// Cycle through all of the audit entries.
			for(Audit audit : audits) {
				// First, get the URI and determine if the request is even 
				// valid.
				String uri = audit.getUri();
				
				// Either way, make a note of it in the list of URIs.
				Integer uriCount = numberUriRequests.get(uri);
				if(uriCount == null) {
					numberUriRequests.put(uri, 1);
				}
				else {
					numberUriRequests.put(uri, uriCount + 1);
				}
				
				// If the request is known, note it and continue processing.
				if(RequestBuilder.getInstance().knownUri(uri)) {
					numberOfValidRequests++;
					
					// Calculate the time it took to process the request.
					timeToProcessValidRequests += audit.getRespondedMillis() - audit.getReceivedMillis();
					
					// Get the audit's response. If there is an issue parsing
					// the response, note it and continue to the next audit.
					JSONObject response = audit.getResponse();
					String result;
					try {
						result = response.getString("result");
					}
					catch(JSONException e) {
						LOGGER.error("Error reading an audit's response.");
						continue;
					}
					
					// If the request was successful, continue evaluating the
					// audit to see if any other substantial data was returned.
					if("success".equals(result)) {
						numberOfSuccessfulValidRequests++;
						
						// Check if it's a class read request.
						if(RequestBuilder.getInstance().getApiClassRead().equals(uri) ||
								RequestBuilder.getInstance().getApiClassRosterRead().equals(uri)) {
							// Get the class ID parameter if it exists.
							Collection<String> classIdCollection = audit.getExtras(InputKeys.CLASS_URN);
							if(classIdCollection != null) {
								for(String classId : classIdCollection) {
									Integer count = numberClassReads.get(classId);
									if(count == null) {
										numberClassReads.put(classId, 1);
									}
									else {
										numberClassReads.put(classId, count + 1);
									}
								}
							}
						}
						// Check if it's a campaign read request.
						else if(RequestBuilder.getInstance().getApiCampaignRead().equals(uri)) {
							Collection<String> campaignIdCollection = audit.getExtras(InputKeys.CAMPAIGN_URN);
							if(campaignIdCollection != null) {
								for(String campaignId : campaignIdCollection) {
									Integer count = numberCampaignReads.get(campaignId);
									if(count == null) {
										numberCampaignReads.put(campaignId, 1);
									}
									else {
										numberCampaignReads.put(campaignId, count + 1);
									}
								}
							}
						}
					}
					// If the request was unsuccessful, note it and move on to
					// the next request.
					else {
						numberOfFailedValidRequests++;
					}
				}
				// If the request is unknown, note it and move on to the next
				// request.
				else {
					numberOfInvalidRequests++;
				}
			}
			
			try {
				// Retrieve the output file to write the results.
				FileWriter fileWriter = new FileWriter(saveLocation + "/" + DateTimeUtils.getIso8601DateString(startDate, false));
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				
				bufferedWriter.write("invalid_requests=");
				bufferedWriter.write(Long.toString(numberOfInvalidRequests));
				bufferedWriter.write('\n');
				
				bufferedWriter.write("valid_requests=");
				bufferedWriter.write(Long.toString(numberOfValidRequests));
				bufferedWriter.write('\n');
				
				bufferedWriter.write("successful_valid_requests=");
				bufferedWriter.write(Long.toString(numberOfSuccessfulValidRequests));
				bufferedWriter.write('\n');
				
				bufferedWriter.write("failed_valid_requests=");
				bufferedWriter.write(Long.toString(numberOfFailedValidRequests));
				bufferedWriter.write('\n');
				
				bufferedWriter.write("average_request_time=");
				if(numberOfValidRequests == 0) {
					bufferedWriter.write('0');
				}
				else {
					bufferedWriter.write(Long.toString(timeToProcessValidRequests / numberOfValidRequests));
				}
				bufferedWriter.write('\n');
				
				bufferedWriter.write("number of requests per uri\n");
				for(String uri : numberUriRequests.keySet()) {
					bufferedWriter.write(uri);
					bufferedWriter.write('=');
					bufferedWriter.write(numberUriRequests.get(uri).toString());
					bufferedWriter.write('\n');
				}
				
				bufferedWriter.write("number of reads per campaign ID\n");
				for(String campaignId : numberCampaignReads.keySet()) {
					bufferedWriter.write(campaignId);
					bufferedWriter.write('=');
					bufferedWriter.write(numberCampaignReads.get(campaignId).toString());
					bufferedWriter.write('\n');
				}
				
				bufferedWriter.write("number of reads per class ID\n");
				for(String classId : numberClassReads.keySet()) {
					bufferedWriter.write(classId);
					bufferedWriter.write('=');
					bufferedWriter.write(numberClassReads.get(classId).toString());
					bufferedWriter.write('\n');
				}
				
				bufferedWriter.flush();
				bufferedWriter.close();
				fileWriter.close();
			}
			catch(IOException e) {
				LOGGER.error("Error while writing the audit information.", e);
			}
		}
/**
		 * Generates a report for the previous day.
		 */
Generates a report for the previous day.
Generates a report for the previous day .
@Override
		public void run() {
			DateTime currentDate = new DateTime();
			DateTime endDate = 
					new DateTime(
						currentDate.getYear(), 
						currentDate.getMonthOfYear(), 
						currentDate.getDayOfMonth(), 
						0, 
						0);
			DateTime startDate = endDate.minusDays(1);
			List<Audit> audits;
			try {
				audits = AuditServices.instance().getAuditInformation(
						null, 
						null, 
						null, 
						null, 
						null, 
						null, 
						startDate, 
						endDate);
			}
			catch(ServiceException e) {
				LOGGER
					.error(
						"There was an error generating the audit information.",
						e);
				return;
			}
			long numberOfValidRequests = 0;
			long numberOfInvalidRequests = 0;
			long numberOfSuccessfulValidRequests = 0;
			long numberOfFailedValidRequests = 0;
			long timeToProcessValidRequests = 0;
			Map<String, Integer> numberUriRequests = new HashMap<String, Integer>();
			Map<String, Integer> numberCampaignReads = new HashMap<String, Integer>();
			Map<String, Integer> numberClassReads = new HashMap<String, Integer>();
			for(Audit audit : audits) {
				String uri = audit.getUri();
				Integer uriCount = numberUriRequests.get(uri);
				if(uriCount == null) {
					numberUriRequests.put(uri, 1);
				}
				else {
					numberUriRequests.put(uri, uriCount + 1);
				}
				if(RequestBuilder.getInstance().knownUri(uri)) {
					numberOfValidRequests++;
					timeToProcessValidRequests += audit.getRespondedMillis() - audit.getReceivedMillis();
					JSONObject response = audit.getResponse();
					String result;
					try {
						result = response.getString("result");
					}
					catch(JSONException e) {
						LOGGER.error("Error reading an audit's response.");
						continue;
					}
					if("success".equals(result)) {
						numberOfSuccessfulValidRequests++;
						if(RequestBuilder.getInstance().getApiClassRead().equals(uri) ||
								RequestBuilder.getInstance().getApiClassRosterRead().equals(uri)) {
							Collection<String> classIdCollection = audit.getExtras(InputKeys.CLASS_URN);
							if(classIdCollection != null) {
								for(String classId : classIdCollection) {
									Integer count = numberClassReads.get(classId);
									if(count == null) {
										numberClassReads.put(classId, 1);
									}
									else {
										numberClassReads.put(classId, count + 1);
									}
								}
							}
						}
						else if(RequestBuilder.getInstance().getApiCampaignRead().equals(uri)) {
							Collection<String> campaignIdCollection = audit.getExtras(InputKeys.CAMPAIGN_URN);
							if(campaignIdCollection != null) {
								for(String campaignId : campaignIdCollection) {
									Integer count = numberCampaignReads.get(campaignId);
									if(count == null) {
										numberCampaignReads.put(campaignId, 1);
									}
									else {
										numberCampaignReads.put(campaignId, count + 1);
									}
								}
							}
						}
					}
					else {
						numberOfFailedValidRequests++;
					}
				}
				else {
					numberOfInvalidRequests++;
				}
			}
			try {
				FileWriter fileWriter = new FileWriter(saveLocation + "/" + DateTimeUtils.getIso8601DateString(startDate, false));
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write("invalid_requests=");
				bufferedWriter.write(Long.toString(numberOfInvalidRequests));
				bufferedWriter.write('\n');
				bufferedWriter.write("valid_requests=");
				bufferedWriter.write(Long.toString(numberOfValidRequests));
				bufferedWriter.write('\n');
				bufferedWriter.write("successful_valid_requests=");
				bufferedWriter.write(Long.toString(numberOfSuccessfulValidRequests));
				bufferedWriter.write('\n');
				bufferedWriter.write("failed_valid_requests=");
				bufferedWriter.write(Long.toString(numberOfFailedValidRequests));
				bufferedWriter.write('\n');
				bufferedWriter.write("average_request_time=");
				if(numberOfValidRequests == 0) {
					bufferedWriter.write('0');
				}
				else {
					bufferedWriter.write(Long.toString(timeToProcessValidRequests / numberOfValidRequests));
				}
				bufferedWriter.write('\n');
				bufferedWriter.write("number of requests per uri\n");
				for(String uri : numberUriRequests.keySet()) {
					bufferedWriter.write(uri);
					bufferedWriter.write('=');
					bufferedWriter.write(numberUriRequests.get(uri).toString());
					bufferedWriter.write('\n');
				}
				bufferedWriter.write("number of reads per campaign ID\n");
				for(String campaignId : numberCampaignReads.keySet()) {
					bufferedWriter.write(campaignId);
					bufferedWriter.write('=');
					bufferedWriter.write(numberCampaignReads.get(campaignId).toString());
					bufferedWriter.write('\n');
				}
				bufferedWriter.write("number of reads per class ID\n");
				for(String classId : numberClassReads.keySet()) {
					bufferedWriter.write(classId);
					bufferedWriter.write('=');
					bufferedWriter.write(numberClassReads.get(classId).toString());
					bufferedWriter.write('\n');
				}
				bufferedWriter.flush();
				bufferedWriter.close();
				fileWriter.close();
			}
			catch(IOException e) {
				LOGGER.error("Error while writing the audit information.", e);
			}
		}
@ Override public void run ( ) { DateTime currentDate = new DateTime ( ) ; DateTime endDate = new DateTime ( currentDate . getYear ( ) , currentDate . getMonthOfYear ( ) , currentDate . getDayOfMonth ( ) , 0 , 0 ) ; DateTime startDate = endDate . minusDays ( 1 ) ; List < Audit > audits ; try { audits = AuditServices . instance ( ) . getAuditInformation ( null , null , null , null , null , null , startDate , endDate ) ; } catch ( ServiceException e ) { LOGGER . error ( "There was an error generating the audit information." , e ) ; return ; } long numberOfValidRequests = 0 ; long numberOfInvalidRequests = 0 ; long numberOfSuccessfulValidRequests = 0 ; long numberOfFailedValidRequests = 0 ; long timeToProcessValidRequests = 0 ; Map < String , Integer > numberUriRequests = new HashMap < String , Integer > ( ) ; Map < String , Integer > numberCampaignReads = new HashMap < String , Integer > ( ) ; Map < String , Integer > numberClassReads = new HashMap < String , Integer > ( ) ; for ( Audit audit : audits ) { String uri = audit . getUri ( ) ; Integer uriCount = numberUriRequests . get ( uri ) ; if ( uriCount == null ) { numberUriRequests . put ( uri , 1 ) ; } else { numberUriRequests . put ( uri , uriCount + 1 ) ; } if ( RequestBuilder . getInstance ( ) . knownUri ( uri ) ) { numberOfValidRequests ++ ; timeToProcessValidRequests += audit . getRespondedMillis ( ) - audit . getReceivedMillis ( ) ; JSONObject response = audit . getResponse ( ) ; String result ; try { result = response . getString ( "result" ) ; } catch ( JSONException e ) { LOGGER . error ( "Error reading an audit's response." ) ; continue ; } if ( "success" . equals ( result ) ) { numberOfSuccessfulValidRequests ++ ; if ( RequestBuilder . getInstance ( ) . getApiClassRead ( ) . equals ( uri ) || RequestBuilder . getInstance ( ) . getApiClassRosterRead ( ) . equals ( uri ) ) { Collection < String > classIdCollection = audit . getExtras ( InputKeys . CLASS_URN ) ; if ( classIdCollection != null ) { for ( String classId : classIdCollection ) { Integer count = numberClassReads . get ( classId ) ; if ( count == null ) { numberClassReads . put ( classId , 1 ) ; } else { numberClassReads . put ( classId , count + 1 ) ; } } } } else if ( RequestBuilder . getInstance ( ) . getApiCampaignRead ( ) . equals ( uri ) ) { Collection < String > campaignIdCollection = audit . getExtras ( InputKeys . CAMPAIGN_URN ) ; if ( campaignIdCollection != null ) { for ( String campaignId : campaignIdCollection ) { Integer count = numberCampaignReads . get ( campaignId ) ; if ( count == null ) { numberCampaignReads . put ( campaignId , 1 ) ; } else { numberCampaignReads . put ( campaignId , count + 1 ) ; } } } } } else { numberOfFailedValidRequests ++ ; } } else { numberOfInvalidRequests ++ ; } } try { FileWriter fileWriter = new FileWriter ( saveLocation + "/" + DateTimeUtils . getIso8601DateString ( startDate , false ) ) ; BufferedWriter bufferedWriter = new BufferedWriter ( fileWriter ) ; bufferedWriter . write ( "invalid_requests=" ) ; bufferedWriter . write ( Long . toString ( numberOfInvalidRequests ) ) ; bufferedWriter . write ( '\n' ) ; bufferedWriter . write ( "valid_requests=" ) ; bufferedWriter . write ( Long . toString ( numberOfValidRequests ) ) ; bufferedWriter . write ( '\n' ) ; bufferedWriter . write ( "successful_valid_requests=" ) ; bufferedWriter . write ( Long . toString ( numberOfSuccessfulValidRequests ) ) ; bufferedWriter . write ( '\n' ) ; bufferedWriter . write ( "failed_valid_requests=" ) ; bufferedWriter . write ( Long . toString ( numberOfFailedValidRequests ) ) ; bufferedWriter . write ( '\n' ) ; bufferedWriter . write ( "average_request_time=" ) ; if ( numberOfValidRequests == 0 ) { bufferedWriter . write ( '0' ) ; } else { bufferedWriter . write ( Long . toString ( timeToProcessValidRequests / numberOfValidRequests ) ) ; } bufferedWriter . write ( '\n' ) ; bufferedWriter . write ( "number of requests per uri\n" ) ; for ( String uri : numberUriRequests . keySet ( ) ) { bufferedWriter . write ( uri ) ; bufferedWriter . write ( '=' ) ; bufferedWriter . write ( numberUriRequests . get ( uri ) . toString ( ) ) ; bufferedWriter . write ( '\n' ) ; } bufferedWriter . write ( "number of reads per campaign ID\n" ) ; for ( String campaignId : numberCampaignReads . keySet ( ) ) { bufferedWriter . write ( campaignId ) ; bufferedWriter . write ( '=' ) ; bufferedWriter . write ( numberCampaignReads . get ( campaignId ) . toString ( ) ) ; bufferedWriter . write ( '\n' ) ; } bufferedWriter . write ( "number of reads per class ID\n" ) ; for ( String classId : numberClassReads . keySet ( ) ) { bufferedWriter . write ( classId ) ; bufferedWriter . write ( '=' ) ; bufferedWriter . write ( numberClassReads . get ( classId ) . toString ( ) ) ; bufferedWriter . write ( '\n' ) ; } bufferedWriter . flush ( ) ; bufferedWriter . close ( ) ; fileWriter . close ( ) ; } catch ( IOException e ) { LOGGER . error ( "Error while writing the audit information." , e ) ; } }
Generates a report for the previous day.
Generates a report for the previous day .
// Use the service to aggregate the results. // Cycle through all of the audit entries. // First, get the URI and determine if the request is even  // valid. // Either way, make a note of it in the list of URIs. // If the request is known, note it and continue processing. // Calculate the time it took to process the request. // Get the audit's response. If there is an issue parsing // the response, note it and continue to the next audit. // If the request was successful, continue evaluating the // audit to see if any other substantial data was returned. // Check if it's a class read request. // Get the class ID parameter if it exists. // Check if it's a campaign read request. // If the request was unsuccessful, note it and move on to // the next request. // If the request is unknown, note it and move on to the next // request. // Retrieve the output file to write the results.