MetricsAttribute.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #pragma once
  9. #include <AzCore/JSON/document.h>
  10. #include <AzCore/RTTI/TypeInfo.h>
  11. #include <AzCore/std/containers/variant.h>
  12. #include <AzCore/std/string/string.h>
  13. namespace AWSCore
  14. {
  15. class JsonWriter;
  16. }
  17. namespace AWSMetrics
  18. {
  19. //! MetricsAttribute represents one attribute of the metrics.
  20. //! Attribute value can be int, double or string.
  21. //! e.g. name: event_name, value: login
  22. class MetricsAttribute
  23. {
  24. public:
  25. AZ_TYPE_INFO(MetricsAttribute, "{6483F481-0C18-4171-8B59-A44F2F28EAE5}")
  26. MetricsAttribute(const AZStd::string& name, int intVal);
  27. MetricsAttribute(const AZStd::string& name, double doubleVal);
  28. MetricsAttribute(const AZStd::string& name, const AZStd::string& strVal);
  29. MetricsAttribute();
  30. void SetName(const AZStd::string& name);
  31. AZStd::string GetName() const;
  32. void SetVal(const AZStd::string& val);
  33. void SetVal(int val);
  34. void SetVal(double val);
  35. AZStd::variant<int, double, AZStd::string> GetVal() const;
  36. //! Get the metrics attribute size serialized to json.
  37. //! @return metrics attribute size in bytes.
  38. size_t GetSizeInBytes() const;
  39. //! Check whether the attribute is one of the default attributes.
  40. //! @return Whether the attribute is a default one.
  41. bool IsDefault() const;
  42. //! Serialize the metrics attribute to JSON for sending requests.
  43. //! @param writer JSON writer for the serialization.
  44. //! @return Whether the metrics attribute is serialized successfully.
  45. bool SerializeToJson(AWSCore::JsonWriter& writer) const;
  46. //! Read from a JSON value to the metrics attribute.
  47. //! @param name JSON value used for the attribute name.
  48. //! @param name JSON value used for the attribute value.
  49. //! @return Whether the metrics attribute is created successfully
  50. bool ReadFromJson(const rapidjson::Value& name, const rapidjson::Value& val);
  51. private:
  52. //! Set the attribute as default if its name is in the default attributes list.
  53. //! @param name Name of the attribute.
  54. void SetIfDefault(const AZStd::string& name);
  55. enum class VAL_TYPE
  56. {
  57. INT,
  58. DOUBLE,
  59. STR
  60. };
  61. AZStd::string m_name; //!< Name of the attribute.
  62. AZStd::variant<int, double, AZStd::string> m_val; //!< Value of the attribute.
  63. bool m_isDefault; //!< Whether the attribute is one of the default attributes.
  64. };
  65. } // namespace AWSMetrics