     # FIXME: handle. Maybe outside/before
 
 
-analyzeStartups = (filename, started, callback) ->
+calculateStats = (state) ->
+  starts = state.startups.map (s) -> s.duration/1000
+  stops = state.shutdowns.map (s) -> s.duration/1000
+  scaleups = state.scaleups.map (s) -> s.duration/1000
+  uptimes = state.uptimes.map (s) -> s.duration/1000
+  results =
+    scaleup: statistics.median scaleups
+    scaleup_stddev: statistics.standard_deviation scaleups
+    scaleup_length: scaleups.length
+    uptime: statistics.median uptimes
+    uptime_stddev: statistics.standard_deviation uptimes
+    uptime_length: uptimes.length
+    startup: statistics.mean starts
+    startup_stddev: statistics.standard_deviation starts
+    startup_length: starts.length
+    shutdown: statistics.mean stops
+    shutdown_stddev: statistics.standard_deviation stops
+    shutdown_length: stops.length
+  wasted = results.scaleup + results.startup + results.shutdown # assumes Heroku charges for all these steps
+  results.utilization = results.uptime / (results.uptime + wasted)
+
+  return results
+
+hasDynos = (roles) ->
+  return (s) ->
+    hasRoles = roles? and roles.length # empty array means include everything
+    return true if not hasRoles
+    [role, number] = s.dyno.split '.'
+    include = role in roles
+
+    return include
+
+analyzeStartups = (filename, started, dynos, callback) ->
   fs = require 'fs'
 
   state = {}

    for e in events
      applyEvent state, e

    starts = state.startups.map (s) -> s.duration/1000
    stops = state.shutdowns.map (s) -> s.duration/1000
    scaleups = state.scaleups.map (s) -> s.duration/1000
    uptimes = state.uptimes.map (s) -> s.duration/1000
    results =
      scaleup: statistics.median scaleups
      scaleup_stddev: statistics.standard_deviation scaleups
      scaleup_length: scaleups.length
      uptime: statistics.median uptimes
      uptime_stddev: statistics.standard_deviation uptimes
      uptime_length: uptimes.length
      startup: statistics.mean starts
      startup_stddev: statistics.standard_deviation starts
      startup_length: starts.length
      shutdown: statistics.mean stops
      shutdown_stddev: statistics.standard_deviation stops
      shutdown_length: stops.length
    wasted = results.scaleup + results.startup + results.shutdown # assumes Heroku charges for all these steps
    results.utilization = results.uptime / (results.uptime + wasted)

    return callback null, results

# TODO: calculate whole delay from scaling to up by default, and scaling down to down
# TODO: allow to separate between (module) loading time, and startup time


  started = (info) ->
    return program.started.test info
  roles = program.role
  analyzeStartups filename, started, roles, (err, res) ->
    throw err if err
    console.log res

