<commit_before>﻿using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using SharePoint.Modernization.Scanner.Results;
using SharePoint.Scanning.Framework;
using SharePointPnP.Modernization.Framework;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace SharePoint.Modernization.Scanner.Telemetry
{
    /// <summary>
    /// Class that's responsible for handling telemetry
    /// </summary>
    public class ScannerTelemetry
    {
        private readonly TelemetryClient telemetryClient;

        #region Construction
        /// <summary>
        /// Instantiates the telemetry client
        /// </summary>
        public ScannerTelemetry()
        {
            try
            {
                this.telemetryClient = new TelemetryClient
                {
                    InstrumentationKey = "70f0a42a-e1ae-4dc5-ad9a-380ce98dc30a"
                };
                
                // Setting this is needed to make metric tracking work
                TelemetryConfiguration.Active.InstrumentationKey = this.telemetryClient.InstrumentationKey;

                this.telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
                this.telemetryClient.Context.Cloud.RoleInstance = "SharePointPnPModernizationScanner";
                this.telemetryClient.Context.Device.OperatingSystem = Environment.OSVersion.ToString();
                var coreAssembly = Assembly.GetExecutingAssembly();
                this.telemetryClient.Context.GlobalProperties.Add("Version", ((AssemblyFileVersionAttribute)coreAssembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Telemetry setup failed: {ex.Message}. Continuing without telemetry");
            }
        }
        #endregion

        /// <summary>
        /// Scan start logging
        /// </summary>
        /// <param name="options">Scanner options</param>
        public void LogScanStart(Options options)
        {
            if (this.telemetryClient == null)
            {
                return;
            }

            try
            {
                // Prepare event data<commit_msg>Ensure telemetry is disabled on error<commit_after>﻿using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using SharePoint.Modernization.Scanner.Results;
using SharePoint.Scanning.Framework;
using SharePointPnP.Modernization.Framework;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace SharePoint.Modernization.Scanner.Telemetry
{
    /// <summary>
    /// Class that's responsible for handling telemetry
    /// </summary>
    public class ScannerTelemetry
    {
        private readonly TelemetryClient telemetryClient;

        #region Construction
        /// <summary>
        /// Instantiates the telemetry client
        /// </summary>
        public ScannerTelemetry()
        {
            try
            {
                this.telemetryClient = new TelemetryClient
                {
                    InstrumentationKey = "70f0a42a-e1ae-4dc5-ad9a-380ce98dc30a"
                };
                
                // Setting this is needed to make metric tracking work
                TelemetryConfiguration.Active.InstrumentationKey = this.telemetryClient.InstrumentationKey;

                this.telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
                this.telemetryClient.Context.Cloud.RoleInstance = "SharePointPnPModernizationScanner";
                this.telemetryClient.Context.Device.OperatingSystem = Environment.OSVersion.ToString();
                var coreAssembly = Assembly.GetExecutingAssembly();
                this.telemetryClient.Context.GlobalProperties.Add("Version", ((AssemblyFileVersionAttribute)coreAssembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version.ToString());
            }
            catch (Exception ex)
            {
                this.telemetryClient = null;
                Console.WriteLine($"Telemetry setup failed: {ex.Message}. Continuing without telemetry");
            }
        }
        #endregion

        /// <summary>
        /// Scan start logging
        /// </summary>
        /// <param name="options">Scanner options</param>
        public void LogScanStart(Options options)
        {
            if (this.telemetryClient == null)
            {
                return;
            }

            try
            {
                // Prepare event data
{'fraction_non_alphanumeric': 0.0882414588136659, 'fraction_numerical': 0.00670718926849717, 'mean_word_length': 2.0906735751295336, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '1896498', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1126, 'n_words': 222}