Respond to a query that wants the output in ASCII. When a query specifies the "ascii" query string parameter, we send the data points back to the client in plain text instead of sending a PNG. @param query The query we're currently serving. @param max_age The maximum time (in seconds) we wanna a
(final HttpQuery query,
final int max_age,
final String basepath,
final Plot plot)
| 918 | * @param plot The plot object to generate Gnuplot's input files. |
| 919 | */ |
| 920 | private static void respondAsciiQuery(final HttpQuery query, |
| 921 | final int max_age, |
| 922 | final String basepath, |
| 923 | final Plot plot) { |
| 924 | final String path = basepath + ".txt"; |
| 925 | PrintWriter asciifile; |
| 926 | try { |
| 927 | asciifile = new PrintWriter(path); |
| 928 | } catch (IOException e) { |
| 929 | query.internalError(e); |
| 930 | return; |
| 931 | } |
| 932 | try { |
| 933 | final StringBuilder tagbuf = new StringBuilder(); |
| 934 | for (final DataPoints dp : plot.getDataPoints()) { |
| 935 | final String metric = dp.metricName(); |
| 936 | tagbuf.setLength(0); |
| 937 | for (final Map.Entry<String, String> tag : dp.getTags().entrySet()) { |
| 938 | tagbuf.append(' ').append(tag.getKey()) |
| 939 | .append('=').append(tag.getValue()); |
| 940 | } |
| 941 | for (final DataPoint d : dp) { |
| 942 | if (d.isInteger()) { |
| 943 | printMetricHeader(asciifile, metric, d.timestamp()); |
| 944 | asciifile.print(d.longValue()); |
| 945 | } else { |
| 946 | // Doubles require extra processing. |
| 947 | final double value = d.doubleValue(); |
| 948 | |
| 949 | // Value might be NaN or infinity. |
| 950 | if (Double.isInfinite(value)) { |
| 951 | // Infinity is invalid. |
| 952 | throw new IllegalStateException("Infinity:" + value |
| 953 | + " d=" + d + ", query=" + query); |
| 954 | } else if (Double.isNaN(value)) { |
| 955 | // NaNs should be skipped. |
| 956 | continue; |
| 957 | } |
| 958 | |
| 959 | printMetricHeader(asciifile, metric, d.timestamp()); |
| 960 | asciifile.print(value); |
| 961 | } |
| 962 | |
| 963 | asciifile.print(tagbuf); |
| 964 | asciifile.print('\n'); |
| 965 | } |
| 966 | } |
| 967 | } finally { |
| 968 | asciifile.close(); |
| 969 | } |
| 970 | try { |
| 971 | query.sendFile(path, max_age); |
| 972 | } catch (IOException e) { |
| 973 | query.internalError(e); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | /** |
no test coverage detected