    :scheduled-time (tc/from-long (apply min (map :scheduled_date jobs)))
    :job-names (map :name jobs)})
 
-(defn stage-instances-from [sync-start-time {stage-name :stage pipeline-name :pipeline}]
+(defn stage-instances-from [go-url sync-start-time {stage-name :stage pipeline-name :pipeline}]
   (let [safe-build-start-date (t/minus sync-start-time (t/millis 1))]
     (->> (goapi/get-stage-history go-url pipeline-name stage-name)
          (map parse-stage-instance)

  (println "done")
  pipeline-stages)

(defn -main [& c-args]
  (def args (parse-opts c-args cli-options))

  (when (or (:help (:options args))
            (empty? (:arguments args)))
    (println (usage (:summary args)))
    (System/exit 0))

  (def go-url (first (:arguments args)))
  (def buildviz-url (:buildviz-url (:options args)))

  (let [sync-start-time (get-start-date (:sync-start-time (:options args)))
        sync-jobs-for-pipelines (set (:sync-jobs-for-pipelines (:options args)))
        selected-pipeline-group-names (set (:pipeline-groups (:options args)))]

    (->> (goapi/get-stages go-url)
         (select-stages selected-pipeline-group-names)
         (emit-start sync-start-time)
         (mapcat #(stage-instances-from sync-start-time %))
         (sort-by :scheduled-time)
         (take-while :finished)
         (emit-sync-start)
         (progress/init "Syncing")
         (map add-inputs-for-stage-instance)
         (map add-job-instances-for-stage-instance)
         (map #(aggregate-jobs-for-stage-instance % sync-jobs-for-pipelines))
         (mapcat stage-instances->builds)
         (map put-to-buildviz)
         (map progress/tick)
         dorun
         (progress/done))))

         (take-while #(t/after? (:scheduled-time %) safe-build-start-date)))))


(defn add-inputs-for-stage-instance [go-url {:keys [pipeline-run, pipeline-name] :as stage-instance}]
  (let [inputs (goapi/get-inputs-for-pipeline-run go-url pipeline-name pipeline-run)]
    (assoc stage-instance :inputs inputs)))

