 import HealthKit
 import Async
 
-
 /**
-  This is the manager of information for the comparison population.
-  By providing this comparison we provide our study participants with a greater ability to view themselves in context.
-
-  Initially this is defined by the NHANES data.
-  With sufficient enrolled subjects, this will be determined by aggregates over the ongoing study population.
-
+ This is the manager of information for the comparison population.
+ By providing this comparison we provide our study participants with a greater ability to view themselves in context.
+ 
+ Initially this is defined by the NHANES data.
+ With sufficient enrolled subjects, this will be determined by aggregates over the ongoing study population.
+ 
  - remark: pulls from PostgreSQL store (AWS RDS) -- see MCRouter --
  */
 public class PopulationHealthManager {
-
+    
     public static let sharedManager = PopulationHealthManager()
-
+    
     public var aggregateRefreshDate : NSDate = NSDate()
-
+    
     public var mostRecentAggregates = [HKSampleType: [MCSample]]() {
         didSet {
             aggregateRefreshDate = NSDate()
         }
     }
-
+    
     // MARK: - Population query execution.
-
+    
     // Clear all aggregates.
     public func resetAggregates() { mostRecentAggregates = [:] }
-
+    
     // Retrieve aggregates for all previewed rows.
     public func fetchAggregates() {
         var columnIndex = 0
         var columns : [String:AnyObject] = [:]
-
+        
         for hksType in PreviewManager.supportedTypes {
             if let column = HMConstants.sharedInstance.hkToMCDB[hksType.identifier] {
                 columns[String(columnIndex)] = column
                 columnIndex += 1
             }
         }
-
+        
         // TODO: set filtering constraints from query view controllers.
         let tstart  : NSDate             = NSDate(timeIntervalSince1970: 0)
         let tend    : NSDate             = NSDate()
         var filter  : [String:AnyObject] = [:]
-
+        
         // Add population filter parameters.
         let popQueryIndex = QueryManager.sharedManager.getSelectedQuery()
         let popQueries = QueryManager.sharedManager.getQueries()

//                        let err = NSError(domain: "App error", code: 0, userInfo: [NSLocalizedDescriptionKey:kvdict.description])
//                        let dict = ["title":"population data error", "error":err]
//                        NSNotificationCenter.defaultCenter().postNotificationName("ncAppLogNotification", object: nil, userInfo: dict)
                        break
                    }
                }
            }
            if ( !failed ) {
                Async.main {
                    //addSALogObj("Setting population data", ["some key":"some value"])
                    let dict = ["title":"population data", "obj":populationAggregates.description ?? ""]
                    NSNotificationCenter.defaultCenter().postNotificationName("ncAppLogNotification", object: nil, userInfo: dict)
                    self.mostRecentAggregates = populationAggregates
                    NSNotificationCenter.defaultCenter().postNotificationName(HMDidUpdateRecentSamplesNotification, object: self)
                }
            }
            else{
                
            }
        }
    }
}
                for (column, val) in sample {
                    log.info("Refreshing population aggregate for \(column)")
                    if let sampleValue = val as? Double,
                        typeIdentifier = HMConstants.sharedInstance.mcdbToHK[column]
                    {
                        switch typeIdentifier {
                        case HKCategoryTypeIdentifierSleepAnalysis:
                            let sampleType = HKObjectType.categoryTypeForIdentifier(HKCategoryTypeIdentifierSleepAnalysis)!
                            populationAggregates[sampleType] = [MCAggregateSample(value: sampleValue, sampleType: sampleType)]
                            
                        case HKCategoryTypeIdentifierAppleStandHour:
                            let sampleType = HKObjectType.categoryTypeForIdentifier(HKCategoryTypeIdentifierAppleStandHour)!
                            populationAggregates[sampleType] = [MCAggregateSample(value: sampleValue, sampleType: sampleType)]
                            
                        default:
                            let sampleType = HKObjectType.quantityTypeForIdentifier(typeIdentifier)!
                            populationAggregates[sampleType] = [MCAggregateSample(value: sampleValue, sampleType: sampleType)]
