MetricsQueue.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <MetricsEvent.h>
  10. #include <AzCore/std/containers/deque.h>
  11. namespace AWSMetrics
  12. {
  13. //! MetricsQueue is used to buffer the submitted metrics before sending them in batch to the backend or local file.
  14. class MetricsQueue
  15. {
  16. public:
  17. const MetricsEvent& operator[](int index) const;
  18. //! Add a new metrics to the queue.
  19. //! @param metrics Metrics to add.
  20. void AddMetrics(const MetricsEvent& metrics);
  21. //! Append an existing metrics queue to the current queue.
  22. //! @param metricsQueue metrics queue to append.
  23. void AppendMetrics(MetricsQueue& metricsQueue);
  24. //! Push an existing metrics queue to the front of the current queue.
  25. //! @param metricsQueue metrics queue to push.
  26. void PushMetricsToFront(MetricsQueue& metricsQueue);
  27. //! Filter out lower priority metrics event in the queue if the queue size reaches the maximum capacity.
  28. //! @param maxSizeInBytes Maximum capacity of the queue.
  29. //! @return Total number of metrics events dropped because of the size limit.
  30. int FilterMetricsByPriority(size_t maxSizeInBytes);
  31. //! Empty the metrics queue.
  32. //! Unsubmitted metrics will be lost after this operation.
  33. void ClearMetrics();
  34. int GetNumMetrics() const;
  35. //! Get the total size of all the metrics inside the queue.
  36. //! @return size of the queue in bytes
  37. size_t GetSizeInBytes() const;
  38. //! Serialize the metrics events queue to a string.
  39. //! @return Serialized string.
  40. AZStd::string SerializeToJson();
  41. //! Serialize the metrics queue to JSON for sending requests.
  42. //! @param writer JSON writer for the serialization.
  43. //! @return Whether the metrics queue is serialized successfully.
  44. bool SerializeToJson(AWSCore::JsonWriter& writer) const;
  45. //! Pop buffered metrics events by the payload size and record count limits and add them to a new queue.
  46. //! @param bufferedEvents The metrics queue to store poped metrics events.
  47. //! @param maxPayloadSizeInMb Maximum size for the service API request payload.
  48. //! @param maxBatchedRecordsCount Maximum number of records sent in a service API request.
  49. void PopBufferedEventsByServiceLimits(MetricsQueue& bufferedEvents, int maxPayloadSizeInMb, int maxBatchedRecordsCount);
  50. //! Read from a local JSON file to the metrics queue.
  51. //! @param filePath Path to the local JSON file.
  52. //! @return Whether the metrics queue is created successfully.
  53. bool ReadFromJson(const AZStd::string& filePath);
  54. private:
  55. bool ReadFromJsonDocument(rapidjson::Document& doc);
  56. AZStd::deque<MetricsEvent> m_metrics; //!< Metrics included in the queue.
  57. size_t m_sizeSerializedToJson = 0; //! < Metrics queue size serialized to json.
  58. };
  59. } // namespace AWSMetrics