<commit_before>﻿// Copyright 2013-2014 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Graphene.Util;
using Newtonsoft.Json.Bson;

namespace Graphene.Tracking
{
    internal class Bucket
    {
        private readonly ConcurrentDictionary<string, Counter> _counters;
        private readonly DateTime _expiresAfter;
        private readonly object _syncLock = new object();
        private readonly List<Bucket> _lowRezBuckets = new List<Bucket>();
        private readonly List<Resolution> _coveredResolutions = new List<Resolution>(); 

        internal Bucket(int lifeTimeInSeconds, Resolution minResolution, DateTime? timeNow = null, bool isLowerResolutionBucket = false)
        {
            DateTime timeNow1 = DateTime.Now;
            if (timeNow.HasValue)
                timeNow1 = timeNow.Value;
            _expiresAfter = timeNow1.AddSeconds(lifeTimeInSeconds);
            _counters = new ConcurrentDictionary<string, Counter>();
            if (isLowerResolutionBucket)
                _coveredResolutions.Add(minResolution);
            switch (minResolution)
            {
                case Resolution.FiveMinute:
                    TimeSlot = timeNow1.Round(TimeSpan.FromMinutes(5));
                    if (!isLowerResolutionBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.FiveMinute, Resolution.Minute});
                    break;
                case Resolution.FifteenMinute:
                    TimeSlot = timeNow1.Round(TimeSpan.FromMinutes(15));
                    if (!isLowerResolutionBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.FifteenMinute, Resolution.FiveMinute, Resolution.Minute });
                    break;
                case Resolution.Hour:
                    TimeSlot = timeNow1.Round(TimeSpan.FromHours(1));
                    if (!isLowerResolutionBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.Hour, Resolution.ThirtyMinute, Resolution.FifteenMinute, Resolution.FiveMinute, Resolution.Minute });
                    break;
                case Resolution.ThirtyMinute:
                    TimeSlot = timeNow1.Round(TimeSpan.FromMinutes(30));
                    if (!isLowerResolutionBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.ThirtyMinute, Resolution.FifteenMinute, Resolution.FiveMinute, Resolution.Minute });
                    break;
                case Resolution.Minute:
                    TimeSlot = timeNow1.Round(TimeSpan.FromMinutes(1));
                    if (!isLowerResolutionBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.Minute });
                    break;
                case Resolution.Day:
                    TimeSlot = new DateTime(timeNow1.Year, timeNow1.Month, timeNow1.Day);
                    if (!isLowerResolutionBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.Day, Resolution.Hour, Resolution.ThirtyMinute, Resolution.FifteenMinute, Resolution.FiveMinute, Resolution.Minute });
                    break;
                case Resolution.Month:
                    TimeSlot = new DateTime(timeNow1.Year, timeNow1.Month, 1);
                    if (!isLowerResolutionBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.Month, Resolution.Day, Resolution.Hour, Resolution.ThirtyMinute, Resolution.FifteenMinute, Resolution.FiveMinute, Resolution.Minute });
                    break;
            } 
            if (!isLowerResolutionBucket)
                initalizeLowRezBuckets(lifeTimeInSeconds, minResolution);
        }

        private void initalizeLowRezBuckets(int lifeTimeInSeconds, Resolution minResolution)
        {
            switch (minResolution)
            {
                case Resolution.FiveMinute:
                    _lowRezBuckets.AddRange(new Bucket[]
                    {
                        new Bucket(lifeTimeInSeconds, Resolution.FifteenMinute, TimeSlot, true),
                        new Bucket(lifeTimeInSeconds, Resolution.Hour, TimeSlot, true),
                        new Bucket(lifeTimeInSeconds, Resolution.Day, TimeSlot, true),
                        new Bucket(lifeTimeInSeconds, Resolution.Month, TimeSlot, true),
                    });
            
                    break;
                case Resolution.FifteenMinute:
                    _lowRezBuckets.AddRange(new Bucket[]
                    {
                        new Bucket(lifeTimeInSeconds, Resolution.Hour, TimeSlot, true),
                        new Bucket(lifeTimeInSeconds, Resolution.Day, TimeSlot, true),
                        new Bucket(lifeTimeInSeconds, Resolution.Month, TimeSlot, true),
                    });
                    break;
                case Resolution.Hour:
                    _lowRezBuckets.AddRange(new Bucket[]
                    {<commit_msg>Updated variable name<commit_after>﻿// Copyright 2013-2014 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Graphene.Util;
using Newtonsoft.Json.Bson;

namespace Graphene.Tracking
{
    internal class Bucket
    {
        private readonly ConcurrentDictionary<string, Counter> _counters;
        private readonly DateTime _expiresAfter;
        private readonly object _syncLock = new object();
        private readonly List<Bucket> _lowRezBuckets = new List<Bucket>();
        private readonly List<Resolution> _coveredResolutions = new List<Resolution>(); 

        internal Bucket(int lifeTimeInSeconds, Resolution minResolution, DateTime? timeNow = null, bool isLargerTimespanBucket = false)
        {
            DateTime timeNow1 = DateTime.Now;
            if (timeNow.HasValue)
                timeNow1 = timeNow.Value;
            _expiresAfter = timeNow1.AddSeconds(lifeTimeInSeconds);
            _counters = new ConcurrentDictionary<string, Counter>();
            if (isLargerTimespanBucket)
                _coveredResolutions.Add(minResolution);
            switch (minResolution)
            {
                case Resolution.FiveMinute:
                    TimeSlot = timeNow1.Round(TimeSpan.FromMinutes(5));
                    if (!isLargerTimespanBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.FiveMinute, Resolution.Minute});
                    break;
                case Resolution.FifteenMinute:
                    TimeSlot = timeNow1.Round(TimeSpan.FromMinutes(15));
                    if (!isLargerTimespanBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.FifteenMinute, Resolution.FiveMinute, Resolution.Minute });
                    break;
                case Resolution.Hour:
                    TimeSlot = timeNow1.Round(TimeSpan.FromHours(1));
                    if (!isLargerTimespanBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.Hour, Resolution.ThirtyMinute, Resolution.FifteenMinute, Resolution.FiveMinute, Resolution.Minute });
                    break;
                case Resolution.ThirtyMinute:
                    TimeSlot = timeNow1.Round(TimeSpan.FromMinutes(30));
                    if (!isLargerTimespanBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.ThirtyMinute, Resolution.FifteenMinute, Resolution.FiveMinute, Resolution.Minute });
                    break;
                case Resolution.Minute:
                    TimeSlot = timeNow1.Round(TimeSpan.FromMinutes(1));
                    if (!isLargerTimespanBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.Minute });
                    break;
                case Resolution.Day:
                    TimeSlot = new DateTime(timeNow1.Year, timeNow1.Month, timeNow1.Day);
                    if (!isLargerTimespanBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.Day, Resolution.Hour, Resolution.ThirtyMinute, Resolution.FifteenMinute, Resolution.FiveMinute, Resolution.Minute });
                    break;
                case Resolution.Month:
                    TimeSlot = new DateTime(timeNow1.Year, timeNow1.Month, 1);
                    if (!isLargerTimespanBucket)
                        _coveredResolutions.AddRange(new[] { Resolution.Month, Resolution.Day, Resolution.Hour, Resolution.ThirtyMinute, Resolution.FifteenMinute, Resolution.FiveMinute, Resolution.Minute });
                    break;
            } 
            if (!isLargerTimespanBucket)
                initalizeLowRezBuckets(lifeTimeInSeconds, minResolution);
        }

        private void initalizeLowRezBuckets(int lifeTimeInSeconds, Resolution minResolution)
        {
            switch (minResolution)
            {
                case Resolution.FiveMinute:
                    _lowRezBuckets.AddRange(new Bucket[]
                    {
                        new Bucket(lifeTimeInSeconds, Resolution.FifteenMinute, TimeSlot, true),
                        new Bucket(lifeTimeInSeconds, Resolution.Hour, TimeSlot, true),
                        new Bucket(lifeTimeInSeconds, Resolution.Day, TimeSlot, true),
                        new Bucket(lifeTimeInSeconds, Resolution.Month, TimeSlot, true),
                    });
            
                    break;
                case Resolution.FifteenMinute:
                    _lowRezBuckets.AddRange(new Bucket[]
                    {
                        new Bucket(lifeTimeInSeconds, Resolution.Hour, TimeSlot, true),
                        new Bucket(lifeTimeInSeconds, Resolution.Day, TimeSlot, true),
                        new Bucket(lifeTimeInSeconds, Resolution.Month, TimeSlot, true),
                    });
                    break;
                case Resolution.Hour:
                    _lowRezBuckets.AddRange(new Bucket[]
                    {
{'fraction_non_alphanumeric': 0.07685336233460213, 'fraction_numerical': 0.005981511691136488, 'mean_word_length': 1.9497460572039562, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5030026', 'n_tokens_mistral': 2901, 'n_tokens_neox': 2600, 'n_words': 614}