// Copyright (C) 2024, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using CoCoL;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace Duplicati.Library.UsageReporter
{
public static class EventProcessor
{
///
/// The maximum number of events to collect before transmitting
///
private const int MAX_ITEMS_IN_SET = 20;
///
/// Max number of events to queue up before giving up
///
private const int MAX_QUEUE_SIZE = 500;
///
/// The maximum number of backlog files to consider
///
private const int MAX_BACKLOG = 6;
///
/// The maximum time a backlog file is considered valid
///
private static readonly TimeSpan MAX_BACKLOG_AGE = TimeSpan.FromDays(30);
///
/// The time to wait before sending event
///
private static readonly TimeSpan WAIT_TIME = TimeSpan.FromMinutes(5);
///
/// The prefix for generated filenames
///
private const string FILENAME_PREFIX = "dupl-usagereport";
///
/// The template used to create filenames
///
private const string FILENAME_TEMPLATE = FILENAME_PREFIX + "-{0}-{1}.json";
///
/// The regular expression that matches a filename
///
private static readonly Regex FILNAME_MATCHER = new Regex(string.Format(FILENAME_TEMPLATE, "(?[0-9]+)", "(?