MetricsAttribute.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AWSMetricsConstant.h>
  9. #include <Framework/JsonWriter.h>
  10. #include <MetricsAttribute.h>
  11. #include <AzCore/std/containers/vector.h>
  12. namespace AWSMetrics
  13. {
  14. MetricsAttribute::MetricsAttribute(const AZStd::string& name, int intVal)
  15. : m_val(intVal)
  16. {
  17. SetName(name);
  18. }
  19. MetricsAttribute::MetricsAttribute(const AZStd::string& name, double doubleVal)
  20. : m_val(doubleVal)
  21. {
  22. SetName(name);
  23. }
  24. MetricsAttribute::MetricsAttribute(const AZStd::string& name, const AZStd::string& strVal)
  25. : m_val(strVal)
  26. {
  27. SetName(name);
  28. }
  29. MetricsAttribute::MetricsAttribute()
  30. : m_name("")
  31. , m_val("")
  32. , m_isDefault(false)
  33. {
  34. }
  35. void MetricsAttribute::SetName(const AZStd::string& name)
  36. {
  37. m_name = name;
  38. SetIfDefault(name);
  39. }
  40. void MetricsAttribute::SetIfDefault(const AZStd::string& name)
  41. {
  42. static const AZStd::array<AZStd::string, 7> DefaultAttributeNames =
  43. {
  44. AwsMetricsAttributeKeyClientId, AwsMetricsAttributeKeyEventId,
  45. AwsMetricsAttributeKeyEventName, AwsMetricsAttributeKeyEventType,
  46. AwsMetricsAttributeKeyEventSource, AwsMetricsAttributeKeyEventTimestamp
  47. };
  48. m_isDefault = AZStd::find(DefaultAttributeNames.begin(), DefaultAttributeNames.end(), name) != DefaultAttributeNames.end();
  49. }
  50. AZStd::string MetricsAttribute::GetName() const
  51. {
  52. return m_name;
  53. }
  54. void MetricsAttribute::SetVal(const AZStd::string& val)
  55. {
  56. m_val = val;
  57. }
  58. void MetricsAttribute::SetVal(int val)
  59. {
  60. m_val = val;
  61. }
  62. void MetricsAttribute::SetVal(double val)
  63. {
  64. m_val = val;
  65. }
  66. AZStd::variant<int, double, AZStd::string> MetricsAttribute::GetVal() const
  67. {
  68. return m_val;
  69. }
  70. bool MetricsAttribute::IsDefault() const
  71. {
  72. return m_isDefault;
  73. }
  74. size_t MetricsAttribute::GetSizeInBytes() const
  75. {
  76. size_t nameSize = m_name.size() * sizeof(char);
  77. // Calculate the value size based on the value type
  78. size_t valSize = 0;
  79. switch (static_cast<VAL_TYPE>(m_val.index()))
  80. {
  81. case VAL_TYPE::INT:
  82. valSize = sizeof(int);
  83. break;
  84. case VAL_TYPE::DOUBLE:
  85. valSize = sizeof(double);
  86. break;
  87. default:
  88. valSize = AZStd::get<AZStd::string>(m_val).size() * sizeof(char);
  89. }
  90. return nameSize + valSize;
  91. }
  92. bool MetricsAttribute::SerializeToJson(AWSCore::JsonWriter& writer) const
  93. {
  94. switch (static_cast<VAL_TYPE>(m_val.index()))
  95. {
  96. case VAL_TYPE::INT:
  97. return writer.Int(AZStd::get<int>(m_val));
  98. case VAL_TYPE::DOUBLE:
  99. return writer.Double(AZStd::get<double>(m_val));
  100. default:
  101. return writer.String(AZStd::get<AZStd::string>(m_val));
  102. }
  103. }
  104. bool MetricsAttribute::ReadFromJson(const rapidjson::Value& name, const rapidjson::Value& val)
  105. {
  106. if (val.IsInt())
  107. {
  108. m_val = val.GetInt();
  109. }
  110. else if (val.IsDouble())
  111. {
  112. m_val = val.GetDouble();
  113. }
  114. else if (val.IsString())
  115. {
  116. m_val = val.GetString();
  117. }
  118. else
  119. {
  120. return false;
  121. }
  122. SetName(name.GetString());
  123. return true;
  124. }
  125. }