<commit_before>﻿namespace DfBAdminToolkit.Presenter {

    using Common.Services;
    using Common.Utils;
    using CsvHelper;
    using CsvHelper.Configuration;
    using Model;
    using View;
    using Newtonsoft.Json;
    using System;
    using System.IO;
    using System.Net;
    using System.Threading;
    using System.Collections.Generic;

    public class TeamAuditingPresenter
        : PresenterBase, ITeamAuditingPresenter {

        public TeamAuditingPresenter(ITeamAuditingModel model, ITeamAuditingView view)
            : base(model, view)
        {
        }

        public int EventCount { get; set; }

        List<MemberListViewItemModel> members;
        List<TeamAuditingListViewItemModel> newAudit;

        protected override void Initialize()
        {
            ITeamAuditingView view = base._view as ITeamAuditingView;
            ITeamAuditingModel model = base._model as ITeamAuditingModel;
            PresenterBase.SetViewPropertiesFromModel<ITeamAuditingView, ITeamAuditingModel>(
                ref view, model
            );

            SyncContext.Post(delegate 
            {
                view.RefreshAccessToken();
            }, null);
        }

        protected override void WireViewEvents()
        {
            if (!IsViewEventsWired) {
                ITeamAuditingView view = base._view as ITeamAuditingView;
                view.DataChanged += DataChanged;
                view.CommandLoadTeamEvents += OnCommandLoadEvents;
                view.CommandLoadCSV += OnCommandLoadCSV;
                view.CommandExportToCSV += OnCommandExportEvents;
                view.CommandFilterMembers += OnCommandFilterMembers;
                IsViewEventsWired = true;
            }
        }

        protected override void UnWireViewEvents()
        {
            if (IsViewEventsWired)
            {
                ITeamAuditingView view = base._view as ITeamAuditingView;
                view.DataChanged -= DataChanged;
                view.CommandLoadTeamEvents -= OnCommandLoadEvents;
                view.CommandLoadCSV -= OnCommandLoadCSV;
                view.CommandExportToCSV -= OnCommandExportEvents;
                view.CommandFilterMembers -= OnCommandFilterMembers;
                IsViewEventsWired = false;
            }
        }

        protected override void CleanUp()
        {
        }

        public void UpdateSettings()
        {
            DataChanged(this, new EventArgs());
        }

        #region REST Service

        private void GetEvents(ITeamAuditingModel model, ITeamAuditingView view, IMainPresenter presenter)
        {
            string eventCategory = view.EventCategory;
            EventCount = 0;
            IMemberServices service = new MemberServices(ApplicationResource.BaseUrl, ApplicationResource.ApiVersion);
            service.GetEventsUrl = ApplicationResource.ActionGetEvents;
            service.UserAgentVersion = ApplicationResource.UserAgent;

            IDataResponse response = service.GetEvents(new MemberData()
            {
                SearchLimit = ApplicationResource.SearchDefaultLimit
            }, ApplicationResource.DefaultAccessToken, view.StartTime, view.EndTime);

            if (SyncContext != null)
            {
                SyncContext.Post(delegate
                {
                    presenter.UpdateProgressInfo("Gathering events...");
                }, null);
            }
            if (response.StatusCode == HttpStatusCode.OK)
            {
                if (response.Data != null)
                {
                    string data = response.Data.ToString();
                    dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(data);

                    int resultCount = jsonData["events"].Count;
                    for (int i = 0; i < resultCount; i++)
                    {
                        bool inCategories = false;
                        dynamic events = jsonData["events"][i];
                        dynamic timestampObj = events["timestamp"];

                        //go through event categories and compare to combobox filter
                        int eventCategoryCount = events["event_categories"].Count;
                        for (int i2 = 0; i2 < eventCategoryCount; i2++)
                        {
                            dynamic eventCategories = events["event_categories"][i2];
                            dynamic eventCategoryObj = eventCategories[".tag"];
                            string eventCategoryReturn = eventCategoryObj.Value as string;

                            if (eventCategoryReturn == eventCategory.ToLower())
                            {
                                inCategories = true;
                            }
                        }
                        dynamic actorType = null;
                        string actorTypeString = string.Empty;
                        if (events["actor"][".tag"] != null)
                        {
                            actorType = events["actor"][".tag"];
                            actorTypeString = actorType.Value as string;
                        }
                        dynamic emailObj = null;
                        string email = string.Empty;
                        if (actorTypeString == "user")
                        {
                            //non team member check first
                            if (events["actor"]["user"][".tag"] != null)
                            {
                                emailObj = "Non Team Member";
                                email = emailObj;
                            }
                            //team member
                            if (events["actor"]["user"]["email"] != null)
                            {
                                emailObj = events["actor"]["user"]["email"];
                                email = emailObj.Value as string;
                            }
                        }
                        if (actorTypeString == "admin")
                        {
                            emailObj = events["actor"]["app"]["display_name"];
                            email = emailObj;
                        }
                        if (actorTypeString == "app")
                        {
                            emailObj = events["actor"]["app"]["display_name"];
                            email = emailObj;
                        }
                        if (actorTypeString == "reseller")
                        {
                            emailObj = events["actor"]["reseller"]["reseller_name"];
                            email = emailObj;
                        }
                        if (actorTypeString == "dropbox")
                        {
                            emailObj = "Dropbox";
                            email = emailObj;
                        }
                        dynamic contextTypeObj = null;
                        string contextTypeString = string.Empty;<commit_msg>Auditing bug fix for actor admin<commit_after>﻿namespace DfBAdminToolkit.Presenter {

    using Common.Services;
    using Common.Utils;
    using CsvHelper;
    using CsvHelper.Configuration;
    using Model;
    using View;
    using Newtonsoft.Json;
    using System;
    using System.IO;
    using System.Net;
    using System.Threading;
    using System.Collections.Generic;

    public class TeamAuditingPresenter
        : PresenterBase, ITeamAuditingPresenter {

        public TeamAuditingPresenter(ITeamAuditingModel model, ITeamAuditingView view)
            : base(model, view)
        {
        }

        public int EventCount { get; set; }

        List<MemberListViewItemModel> members;
        List<TeamAuditingListViewItemModel> newAudit;

        protected override void Initialize()
        {
            ITeamAuditingView view = base._view as ITeamAuditingView;
            ITeamAuditingModel model = base._model as ITeamAuditingModel;
            PresenterBase.SetViewPropertiesFromModel<ITeamAuditingView, ITeamAuditingModel>(
                ref view, model
            );

            SyncContext.Post(delegate 
            {
                view.RefreshAccessToken();
            }, null);
        }

        protected override void WireViewEvents()
        {
            if (!IsViewEventsWired) {
                ITeamAuditingView view = base._view as ITeamAuditingView;
                view.DataChanged += DataChanged;
                view.CommandLoadTeamEvents += OnCommandLoadEvents;
                view.CommandLoadCSV += OnCommandLoadCSV;
                view.CommandExportToCSV += OnCommandExportEvents;
                view.CommandFilterMembers += OnCommandFilterMembers;
                IsViewEventsWired = true;
            }
        }

        protected override void UnWireViewEvents()
        {
            if (IsViewEventsWired)
            {
                ITeamAuditingView view = base._view as ITeamAuditingView;
                view.DataChanged -= DataChanged;
                view.CommandLoadTeamEvents -= OnCommandLoadEvents;
                view.CommandLoadCSV -= OnCommandLoadCSV;
                view.CommandExportToCSV -= OnCommandExportEvents;
                view.CommandFilterMembers -= OnCommandFilterMembers;
                IsViewEventsWired = false;
            }
        }

        protected override void CleanUp()
        {
        }

        public void UpdateSettings()
        {
            DataChanged(this, new EventArgs());
        }

        #region REST Service

        private void GetEvents(ITeamAuditingModel model, ITeamAuditingView view, IMainPresenter presenter)
        {
            string eventCategory = view.EventCategory;
            EventCount = 0;
            IMemberServices service = new MemberServices(ApplicationResource.BaseUrl, ApplicationResource.ApiVersion);
            service.GetEventsUrl = ApplicationResource.ActionGetEvents;
            service.UserAgentVersion = ApplicationResource.UserAgent;

            IDataResponse response = service.GetEvents(new MemberData()
            {
                SearchLimit = ApplicationResource.SearchDefaultLimit
            }, ApplicationResource.DefaultAccessToken, view.StartTime, view.EndTime);

            if (SyncContext != null)
            {
                SyncContext.Post(delegate
                {
                    presenter.UpdateProgressInfo("Gathering events...");
                }, null);
            }
            if (response.StatusCode == HttpStatusCode.OK)
            {
                if (response.Data != null)
                {
                    string data = response.Data.ToString();
                    dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(data);

                    int resultCount = jsonData["events"].Count;
                    for (int i = 0; i < resultCount; i++)
                    {
                        bool inCategories = false;
                        dynamic events = jsonData["events"][i];
                        dynamic timestampObj = events["timestamp"];

                        //go through event categories and compare to combobox filter
                        int eventCategoryCount = events["event_categories"].Count;
                        for (int i2 = 0; i2 < eventCategoryCount; i2++)
                        {
                            dynamic eventCategories = events["event_categories"][i2];
                            dynamic eventCategoryObj = eventCategories[".tag"];
                            string eventCategoryReturn = eventCategoryObj.Value as string;

                            if (eventCategoryReturn == eventCategory.ToLower())
                            {
                                inCategories = true;
                            }
                        }
                        dynamic actorType = null;
                        string actorTypeString = string.Empty;
                        if (events["actor"][".tag"] != null)
                        {
                            actorType = events["actor"][".tag"];
                            actorTypeString = actorType.Value as string;
                        }
                        dynamic emailObj = null;
                        string email = string.Empty;
                        if (actorTypeString == "user")
                        {
                            //non team member check first
                            if (events["actor"]["user"][".tag"] != null)
                            {
                                emailObj = "Non Team Member";
                                email = emailObj;
                            }
                            //team member
                            if (events["actor"]["user"]["email"] != null)
                            {
                                emailObj = events["actor"]["user"]["email"];
                                email = emailObj.Value as string;
                            }
                        }
                        if (actorTypeString == "admin")
                        {
                            emailObj = events["actor"]["admin"]["display_name"];
                            email = emailObj;
                        }
                        if (actorTypeString == "app")
                        {
                            emailObj = events["actor"]["app"]["display_name"];
                            email = emailObj;
                        }
                        if (actorTypeString == "reseller")
                        {
                            emailObj = events["actor"]["reseller"]["reseller_name"];
                            email = emailObj;
                        }
                        if (actorTypeString == "dropbox")
                        {
                            emailObj = "Dropbox";
                            email = emailObj;
                        }
                        dynamic contextTypeObj = null;
                        string contextTypeString = string.Empty;
{'fraction_non_alphanumeric': 0.07570259694059053, 'fraction_numerical': 0.0009960868018498755, 'mean_word_length': 1.2079798931825323, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, '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': '7484394', 'n_tokens_mistral': 3127, 'n_tokens_neox': 2967, 'n_words': 634}