| 1 | package org.lttng.scope.tmf2.views.core.timegraph.view.json; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.Writer; |
| 5 | import java.nio.file.Files; |
| 6 | import java.nio.file.Path; |
| 7 | import java.nio.file.Paths; |
| 8 | import java.util.List; |
| 9 | |
| 10 | import org.json.JSONArray; |
| 11 | import org.json.JSONException; |
| 12 | import org.json.JSONObject; |
| 13 | import org.lttng.scope.tmf2.views.core.timegraph.model.render.states.TimeGraphStateRender; |
| 14 | |
| 15 | import com.google.common.base.Charsets; |
| 16 | import com.google.gson.Gson; |
| 17 | import com.google.gson.GsonBuilder; |
| 18 | |
| 19 | final class RenderToJson { |
| 20 | |
| 21 | private RenderToJson() {} |
| 22 | |
| 23 | private static final boolean PRETTY_PRINT = true; |
| 24 | private static final int VERSION = 1; |
| 25 | |
| 26 | private static final String VERSION_KEY = "version"; //$NON-NLS-1$ |
| 27 | private static final String INTERVALS_KEY = "intervals"; //$NON-NLS-1$ |
| 28 | |
| 29 | private static final String TREE_ELEMENT_KEY = "elem"; //$NON-NLS-1$ |
| 30 | private static final String START_TIME_KEY = "start"; //$NON-NLS-1$ |
| 31 | private static final String END_TIME_KEY = "end"; //$NON-NLS-1$ |
| 32 | private static final String STATE_NAME_KEY = "state"; //$NON-NLS-1$ |
| 33 | private static final String COLOR_KEY = "color"; //$NON-NLS-1$ |
| 34 | |
| 35 | private static final Path OUTPUT_FILE = Paths.get("/home/alexandre/json-output"); //$NON-NLS-1$ |
| 36 | static { |
| 37 | try { |
| 38 | if (Files.exists(OUTPUT_FILE)) { |
| 39 | Files.delete(OUTPUT_FILE); |
| 40 | } |
| 41 | Files.createFile(OUTPUT_FILE); |
| 42 | } catch (IOException e) { |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private static final Gson GSON; |
| 47 | static { |
| 48 | if (PRETTY_PRINT) { |
| 49 | GSON = new GsonBuilder().setPrettyPrinting().create(); |
| 50 | } else { |
| 51 | GSON = new Gson(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | public static void printRenderTo1(List<TimeGraphStateRender> renders) { |
| 56 | String json = GSON.toJson(renders); |
| 57 | try (Writer bw = Files.newBufferedWriter(OUTPUT_FILE, Charsets.UTF_8)) { |
| 58 | bw.write(json); |
| 59 | bw.flush(); |
| 60 | } catch (IOException e1) { |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public static void printRenderTo(List<TimeGraphStateRender> renders) { |
| 65 | try (Writer bw = Files.newBufferedWriter(OUTPUT_FILE, Charsets.UTF_8)) { |
| 66 | JSONObject root = new JSONObject(); |
| 67 | root.put(VERSION_KEY, VERSION); |
| 68 | |
| 69 | JSONArray intervalsRoot = new JSONArray(); |
| 70 | root.put(INTERVALS_KEY, intervalsRoot); |
| 71 | |
| 72 | renders.stream() |
| 73 | .flatMap(render -> render.getStateIntervals().stream()) |
| 74 | .forEach(interval -> { |
| 75 | try { |
| 76 | JSONObject intervalObject = new JSONObject(); |
| 77 | intervalObject.put(TREE_ELEMENT_KEY, interval.getStartEvent().getTreeElement().getName()); |
| 78 | intervalObject.put(START_TIME_KEY, interval.getStartEvent().getTimestamp()); |
| 79 | intervalObject.put(END_TIME_KEY, interval.getEndEvent().getTimestamp()); |
| 80 | intervalObject.put(STATE_NAME_KEY, interval.getStateName()); |
| 81 | intervalObject.put(COLOR_KEY, interval.getColorDefinition().toString()); |
| 82 | |
| 83 | intervalsRoot.put(intervalObject); |
| 84 | } catch (JSONException e) { |
| 85 | /* Skip this interval */ |
| 86 | } |
| 87 | }); |
| 88 | |
| 89 | String json = (PRETTY_PRINT ? root.toString(1) : root.toString()); |
| 90 | bw.write(json); |
| 91 | bw.flush(); |
| 92 | |
| 93 | } catch (JSONException | IOException e) { |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | } |