<commit_before>﻿using System;
using Couchbase;
using Couchbase.Core;
using Tweek.ApiService.Interfaces;

namespace Tweek.ApiService.Services
{
    public class BucketConnectionIsAlive : IDisposable, IDiagnosticsProvider
    {
        public string Name { get; } = "CouchbaseConnectionIsAlive";

        private readonly Cluster _cluster;
        private readonly string _bucketName;
        private IBucket _bucket;

        public BucketConnectionIsAlive(Cluster cluster, string bucketNameToCheck)
        {
            _cluster = cluster;
            _bucketName = bucketNameToCheck;
        }

        public object GetDetails()
        {
            return new { IsConnectionAlive = IsAlive() };
        }

        public bool IsAlive()
        {
            if (!_cluster.IsOpen(_bucketName))
            {
                _bucket = _cluster.OpenBucket(_bucketName);
            }

            return _cluster.IsOpen(_bucketName);
        }

        public void Dispose()
        {
            if (_bucket != null) _cluster.CloseBucket(_bucket);
        }
    }
}<commit_msg>add tracing for opening couchbase connection<commit_after>﻿using System;
using System.Diagnostics;
using Couchbase;
using Couchbase.Core;
using Tweek.ApiService.Interfaces;

namespace Tweek.ApiService.Services
{
    public class BucketConnectionIsAlive : IDisposable, IDiagnosticsProvider
    {
        public string Name { get; } = "CouchbaseConnectionIsAlive";

        private readonly Cluster _cluster;
        private readonly string _bucketName;
        private IBucket _bucket;

        public BucketConnectionIsAlive(Cluster cluster, string bucketNameToCheck)
        {
            _cluster = cluster;
            _bucketName = bucketNameToCheck;
        }

        public object GetDetails()
        {
            return new { IsConnectionAlive = IsAlive() };
        }

        public bool IsAlive()
        {
            if (!_cluster.IsOpen(_bucketName))
            {
                Trace.TraceInformation("Opening couchbase connection");
                _bucket = _cluster.OpenBucket(_bucketName);
            }

            return _cluster.IsOpen(_bucketName);
        }

        public void Dispose()
        {
            if (_bucket != null) _cluster.CloseBucket(_bucket);
        }
    }
}
{'fraction_non_alphanumeric': 0.07426823940585409, 'fraction_numerical': 0.0, 'mean_word_length': 2.098782138024357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2554517', 'n_tokens_mistral': 603, 'n_tokens_neox': 585, 'n_words': 136}