writer.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "writer.h"
  2. namespace NMonitoring {
  3. TDeprecatedJsonWriter::TDeprecatedJsonWriter(IOutputStream* out)
  4. : JsonWriter(out, false)
  5. , State(STATE_ROOT)
  6. {
  7. }
  8. void TDeprecatedJsonWriter::TransitionState(EState current, EState next) {
  9. if (State != current) {
  10. ythrow yexception() << "wrong state";
  11. }
  12. State = next;
  13. }
  14. void TDeprecatedJsonWriter::OpenDocument() {
  15. TransitionState(STATE_ROOT, STATE_DOCUMENT);
  16. JsonWriter.OpenMap();
  17. }
  18. void TDeprecatedJsonWriter::CloseDocument() {
  19. TransitionState(STATE_DOCUMENT, STATE_ROOT);
  20. JsonWriter.CloseMap();
  21. JsonWriter.Flush();
  22. }
  23. void TDeprecatedJsonWriter::OpenCommonLabels() {
  24. TransitionState(STATE_DOCUMENT, STATE_COMMON_LABELS);
  25. JsonWriter.Write("commonLabels");
  26. JsonWriter.OpenMap();
  27. }
  28. void TDeprecatedJsonWriter::CloseCommonLabels() {
  29. TransitionState(STATE_COMMON_LABELS, STATE_DOCUMENT);
  30. JsonWriter.CloseMap();
  31. }
  32. void TDeprecatedJsonWriter::WriteCommonLabel(TStringBuf name, TStringBuf value) {
  33. TransitionState(STATE_COMMON_LABELS, STATE_COMMON_LABELS);
  34. JsonWriter.Write(name, value);
  35. }
  36. void TDeprecatedJsonWriter::OpenMetrics() {
  37. TransitionState(STATE_DOCUMENT, STATE_METRICS);
  38. JsonWriter.Write("sensors");
  39. JsonWriter.OpenArray();
  40. }
  41. void TDeprecatedJsonWriter::CloseMetrics() {
  42. TransitionState(STATE_METRICS, STATE_DOCUMENT);
  43. JsonWriter.CloseArray();
  44. }
  45. void TDeprecatedJsonWriter::OpenMetric() {
  46. TransitionState(STATE_METRICS, STATE_METRIC);
  47. JsonWriter.OpenMap();
  48. }
  49. void TDeprecatedJsonWriter::CloseMetric() {
  50. TransitionState(STATE_METRIC, STATE_METRICS);
  51. JsonWriter.CloseMap();
  52. }
  53. void TDeprecatedJsonWriter::OpenLabels() {
  54. TransitionState(STATE_METRIC, STATE_LABELS);
  55. JsonWriter.Write("labels");
  56. JsonWriter.OpenMap();
  57. }
  58. void TDeprecatedJsonWriter::CloseLabels() {
  59. TransitionState(STATE_LABELS, STATE_METRIC);
  60. JsonWriter.CloseMap();
  61. }
  62. void TDeprecatedJsonWriter::WriteLabel(TStringBuf name, TStringBuf value) {
  63. TransitionState(STATE_LABELS, STATE_LABELS);
  64. JsonWriter.Write(name, value);
  65. }
  66. void TDeprecatedJsonWriter::WriteModeDeriv() {
  67. TransitionState(STATE_METRIC, STATE_METRIC);
  68. JsonWriter.Write("mode", "deriv");
  69. }
  70. void TDeprecatedJsonWriter::WriteValue(long long value) {
  71. TransitionState(STATE_METRIC, STATE_METRIC);
  72. JsonWriter.Write("value", value);
  73. }
  74. void TDeprecatedJsonWriter::WriteDoubleValue(double value) {
  75. TransitionState(STATE_METRIC, STATE_METRIC);
  76. JsonWriter.Write("value", value);
  77. }
  78. void TDeprecatedJsonWriter::WriteTs(ui64 ts) {
  79. TransitionState(STATE_METRIC, STATE_METRIC);
  80. JsonWriter.Write("ts", ts);
  81. }
  82. }