123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AWSMetricsConstant.h>
- #include <Framework/JsonWriter.h>
- #include <MetricsAttribute.h>
- #include <AzCore/std/containers/vector.h>
- namespace AWSMetrics
- {
- MetricsAttribute::MetricsAttribute(const AZStd::string& name, int intVal)
- : m_val(intVal)
- {
- SetName(name);
- }
- MetricsAttribute::MetricsAttribute(const AZStd::string& name, double doubleVal)
- : m_val(doubleVal)
- {
- SetName(name);
- }
- MetricsAttribute::MetricsAttribute(const AZStd::string& name, const AZStd::string& strVal)
- : m_val(strVal)
- {
- SetName(name);
- }
- MetricsAttribute::MetricsAttribute()
- : m_name("")
- , m_val("")
- , m_isDefault(false)
- {
- }
- void MetricsAttribute::SetName(const AZStd::string& name)
- {
- m_name = name;
- SetIfDefault(name);
- }
- void MetricsAttribute::SetIfDefault(const AZStd::string& name)
- {
- static const AZStd::array<AZStd::string, 7> DefaultAttributeNames =
- {
- AwsMetricsAttributeKeyClientId, AwsMetricsAttributeKeyEventId,
- AwsMetricsAttributeKeyEventName, AwsMetricsAttributeKeyEventType,
- AwsMetricsAttributeKeyEventSource, AwsMetricsAttributeKeyEventTimestamp
- };
- m_isDefault = AZStd::find(DefaultAttributeNames.begin(), DefaultAttributeNames.end(), name) != DefaultAttributeNames.end();
- }
- AZStd::string MetricsAttribute::GetName() const
- {
- return m_name;
- }
- void MetricsAttribute::SetVal(const AZStd::string& val)
- {
- m_val = val;
- }
- void MetricsAttribute::SetVal(int val)
- {
- m_val = val;
- }
- void MetricsAttribute::SetVal(double val)
- {
- m_val = val;
- }
- AZStd::variant<int, double, AZStd::string> MetricsAttribute::GetVal() const
- {
- return m_val;
- }
- bool MetricsAttribute::IsDefault() const
- {
- return m_isDefault;
- }
- size_t MetricsAttribute::GetSizeInBytes() const
- {
- size_t nameSize = m_name.size() * sizeof(char);
- // Calculate the value size based on the value type
- size_t valSize = 0;
- switch (static_cast<VAL_TYPE>(m_val.index()))
- {
- case VAL_TYPE::INT:
- valSize = sizeof(int);
- break;
- case VAL_TYPE::DOUBLE:
- valSize = sizeof(double);
- break;
- default:
- valSize = AZStd::get<AZStd::string>(m_val).size() * sizeof(char);
- }
- return nameSize + valSize;
- }
- bool MetricsAttribute::SerializeToJson(AWSCore::JsonWriter& writer) const
- {
- switch (static_cast<VAL_TYPE>(m_val.index()))
- {
- case VAL_TYPE::INT:
- return writer.Int(AZStd::get<int>(m_val));
- case VAL_TYPE::DOUBLE:
- return writer.Double(AZStd::get<double>(m_val));
- default:
- return writer.String(AZStd::get<AZStd::string>(m_val));
- }
- }
- bool MetricsAttribute::ReadFromJson(const rapidjson::Value& name, const rapidjson::Value& val)
- {
- if (val.IsInt())
- {
- m_val = val.GetInt();
- }
- else if (val.IsDouble())
- {
- m_val = val.GetDouble();
- }
- else if (val.IsString())
- {
- m_val = val.GetString();
- }
- else
- {
- return false;
- }
- SetName(name.GetString());
- return true;
- }
- }
|