/**
     * Build timeline JS code for the given profile.
     *
     * @param \Sandstorm\PhpProfiler\Domain\Model\ProfilingRun $profile
     * @param integer $eventSourceIndex
     * @return string
     */
    protected function buildJavaScriptForProfile(ProfilingRun $profile, $eventSourceIndex)
    {
        $javaScript = array();
        foreach ($profile->getTimersAsDuration() as $event) {
            $javaScript[] = sprintf('timelineRunner.addEvent(%s, new Timeline.DefaultEventSource.Event({
				start: new Date(%s),
				end:  new Date(%s),
				durationEvent: true,
				caption: %s,
				description: %s,
				color: "#%s"
			}));', $eventSourceIndex, (int) ($event['start'] * 1000), (int) ($event['stop'] * 1000), json_encode($event['name']), json_encode($event['data']), $this->getColorForEventName($event['name']));
        }
        foreach ($profile->getTimestamps() as $event) {
            $javaScript[] = sprintf('timelineRunner.addEvent(%s, new Timeline.DefaultEventSource.Event({
				start: new Date(%s),
				durationEvent: false,
				text: %s,
				caption: %s,
				description: %s,
				color: "#%s"
			}));', $eventSourceIndex, (int) ($event['time'] * 1000), json_encode($event['name']), json_encode($event['name']), json_encode($event['data']), $this->getColorForEventName($event['name']));
        }
        $memory = $profile->getMemory();
        foreach ($memory as &$record) {
            $record['time'] = (int) ($record['time'] * 1000);
        }
        $javaScript[] = sprintf('timelineRunner.setMemory(%s, %s);', $eventSourceIndex, json_encode($memory));
        return implode("\n", $javaScript);
    }
 /**
  * Calculate the total for the specified timer in the profile.
  *
  * @param \Sandstorm\PhpProfiler\Domain\Model\ProfilingRun $profile
  * @param array $calculationOptions
  * @return array
  * @throws \Sandstorm\Plumber\Exception
  */
 protected function calculateTimerSum(ProfilingRun $profile, array $calculationOptions)
 {
     if (!isset($calculationOptions['timerName'])) {
         throw new Exception('The "timerName" option must be set for "timerSum" calculations.', 1361305369);
     }
     $sum = 0;
     foreach ($profile->getTimersAsDuration() as $duration) {
         if ($duration['name'] === $calculationOptions['timerName']) {
             $sum += $duration['stop'] * 1000 - $duration['start'] * 1000;
         }
     }
     return array('value' => round($sum));
 }