From d96250b444fca115978a5798fcdc11abf6aa1845 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Sun, 6 Dec 2015 11:13:11 +0100 Subject: [PATCH] Generate SVG images --- src/mailhead/routes/home.clj | 7 +++++- src/mailhead/visualize.clj | 48 ++++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/mailhead/routes/home.clj b/src/mailhead/routes/home.clj index a36ca75..48082aa 100644 --- a/src/mailhead/routes/home.clj +++ b/src/mailhead/routes/home.clj @@ -1,5 +1,6 @@ (ns mailhead.routes.home (:require [mailhead.layout :as layout] + [mailhead.visualize :as v] [compojure.core :refer [defroutes GET POST]] [ring.util.http-response :refer [ok]] [clojure.java.io :as io])) @@ -11,7 +12,11 @@ (layout/render "about.html")) (defn analyze-mailheader [mailheader] - (layout/render "result.html" {:mailheader mailheader})) + (let [token (v/draw mailheader)] + ;; TODO: Generate token + ;; TODO: Generate image + ;; TODO: Show image and other data on result.html + (layout/render "result.html" {:mailheader mailheader}))) (defroutes home-routes (GET "/" [] (home-page)) diff --git a/src/mailhead/visualize.clj b/src/mailhead/visualize.clj index 749191a..85bbdd1 100644 --- a/src/mailhead/visualize.clj +++ b/src/mailhead/visualize.clj @@ -9,28 +9,38 @@ (partition 2 (interleave cleaned-chain (rest cleaned-chain))))) (defn edges [path] - (let [p (map #(concat % [{:label "ms" :fillcolor "gray" :color "gray"}]) (edge-path path))] + (let [p (map + #(concat % [{:label "time" :fillcolor "gray" :color "gray"}]) + (edge-path path))] (into [] (map #(into [] %)) p))) (defn draw-path [receive-path] - (-> (graph->dot - [] - (edges receive-path) - {:node {:shape :box - :fontname "Arial" - :fontsize 12 - :style "filled" - :fillcolor "darkgoldenrod1" - :color "darkgoldenrod3"} - :edge {:fontname "Arial" - :fontsize 10} - :directed? true - :graph {:rankdir :LR - :color "transparent"}}) - (dot->image "png"))) + (graph->dot + [] + (edges receive-path) + {:node {:shape :box + :fontname "Arial" + :fontsize 12 + :style "filled" + :fillcolor "darkgoldenrod1" + :color "darkgoldenrod3"} + :edge {:fontname "Arial" + :fontsize 10} + :directed? true + :graph {:rankdir :LR + :color "transparent"}})) -(defn draw [filename] - (draw-path (parser/parse-from-file filename))) +(defn save-image [dot token] + (let [path (str "generated-images/" token ".svg")] + (clojure.java.io/make-parents path) + (spit path (dot->svg dot)))) -(draw "/home/aaron/workbench/clojurecup2015/sample-data/header1.eml") +(defn next-token [] + (str (java.util.UUID/randomUUID))) + ;; TODO: Make it work with strings + (defn draw [mailheader] + (let [filename "/home/aaron/workbench/clojurecup2015/sample-data/header1.eml" + token (next-token)] + (save-image (draw-path (parser/parse-from-file filename)) token) + token))