<commit_before>﻿#region Copyright
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2016
// by DotNetNuke Corporation
// All Rights Reserved
#endregion

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Dnn.PersonaBar.AdminLogs.Services.Dto;
using Dnn.PersonaBar.Library;
using Dnn.PersonaBar.Library.Attributes;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Instrumentation;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.Web.Api;
using Newtonsoft.Json.Linq;

namespace Dnn.PersonaBar.AdminLogs.Services
{
    [MenuPermission(Scope = ServiceScope.Admin)]
    public class AdminLogsController : PersonaBarApiController
    {
        private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof (AdminLogsController));
        private readonly Components.AdminLogsController _controller = new Components.AdminLogsController();

        /// GET: api/AdminLogs/GetLogTypes
        /// <summary>
        /// Gets log types
        /// </summary>
        /// <param></param>
        /// <returns>List of log types</returns>
        [HttpGet]
        public HttpResponseMessage GetLogTypes()
        {
            try
            {
                var logTypes =
                    LogController.Instance.GetLogTypeInfoDictionary()
                        .Values.OrderBy(t => t.LogTypeFriendlyName)
                        .ToList();

                logTypes.Insert(0, new LogTypeInfo
                {
                    LogTypeFriendlyName = Localization.GetString("AllTypes", Components.Constants.LocalResourcesFile),
                LogTypeKey = "*"
                });

                var types = logTypes.Select(v => new
                {
                    v.LogTypeFriendlyName,
                    v.LogTypeKey
                }).ToList();

                var response = new
                {
                    Success = true,
                    Results = types,
                    TotalResults = types.Count
                };

                return Request.CreateResponse(HttpStatusCode.OK, response);
            }
            catch (Exception exc)
            {
                Logger.Error(exc);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
            }
        }


        #region Event Viewer

        /// GET: api/AdminLogs/GetLogItems
        /// <summary>
        /// Gets log items
        /// </summary>
        /// <param></param>
        /// <param name="logType"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="portalId"></param>
        /// <returns>List of log items</returns>
        [HttpGet]
        public HttpResponseMessage GetLogItems(string logType, int pageSize, int pageIndex, int portalId = -1)
        {
            try
            {
                var totalRecords = 0;
                var logItems = LogController.Instance.GetLogs(UserInfo.IsSuperUser ? portalId : PortalId,
                    logType == "*" ? string.Empty : logType,
                    pageSize, pageIndex, ref totalRecords);

                var items = logItems.Select(v => new
                {
                    v.LogGUID,
                    v.LogFileID,
                    _controller.GetMyLogType(v.LogTypeKey).LogTypeCSSClass,
                    _controller.GetMyLogType(v.LogTypeKey).LogTypeFriendlyName,
                    v.LogUserName,
                    v.LogPortalName,
                    LogCreateDate = v.LogCreateDate.ToString("G", CultureInfo.InvariantCulture),
                    v.LogProperties.Summary,
                    LogProperties = _controller.GetPropertiesText(v)
                });

                var response = new
                {
                    Success = true,
                    Results = items,
                    TotalResults = totalRecords
                };

                return Request.CreateResponse(HttpStatusCode.OK, response);
            }
            catch (Exception exc)
            {<commit_msg>DNN-9804: move out portalid from method parameters.<commit_after>﻿#region Copyright
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2016
// by DotNetNuke Corporation
// All Rights Reserved
#endregion

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Dnn.PersonaBar.AdminLogs.Services.Dto;
using Dnn.PersonaBar.Library;
using Dnn.PersonaBar.Library.Attributes;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Instrumentation;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.Web.Api;
using Newtonsoft.Json.Linq;

namespace Dnn.PersonaBar.AdminLogs.Services
{
    [MenuPermission(Scope = ServiceScope.Admin)]
    public class AdminLogsController : PersonaBarApiController
    {
        private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof (AdminLogsController));
        private readonly Components.AdminLogsController _controller = new Components.AdminLogsController();

        /// GET: api/AdminLogs/GetLogTypes
        /// <summary>
        /// Gets log types
        /// </summary>
        /// <param></param>
        /// <returns>List of log types</returns>
        [HttpGet]
        public HttpResponseMessage GetLogTypes()
        {
            try
            {
                var logTypes =
                    LogController.Instance.GetLogTypeInfoDictionary()
                        .Values.OrderBy(t => t.LogTypeFriendlyName)
                        .ToList();

                logTypes.Insert(0, new LogTypeInfo
                {
                    LogTypeFriendlyName = Localization.GetString("AllTypes", Components.Constants.LocalResourcesFile),
                LogTypeKey = "*"
                });

                var types = logTypes.Select(v => new
                {
                    v.LogTypeFriendlyName,
                    v.LogTypeKey
                }).ToList();

                var response = new
                {
                    Success = true,
                    Results = types,
                    TotalResults = types.Count
                };

                return Request.CreateResponse(HttpStatusCode.OK, response);
            }
            catch (Exception exc)
            {
                Logger.Error(exc);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
            }
        }


        #region Event Viewer

        /// GET: api/AdminLogs/GetLogItems
        /// <summary>
        /// Gets log items
        /// </summary>
        /// <param></param>
        /// <param name="logType"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns>List of log items</returns>
        [HttpGet]
        public HttpResponseMessage GetLogItems(string logType, int pageSize, int pageIndex)
        {
            try
            {
                var portalId = UserInfo.IsSuperUser ? Request.GetQueryStringAsInteger("portalid") : PortalId;
                var totalRecords = 0;
                var logItems = LogController.Instance.GetLogs(portalId,
                    logType == "*" ? string.Empty : logType,
                    pageSize, pageIndex, ref totalRecords);

                var items = logItems.Select(v => new
                {
                    v.LogGUID,
                    v.LogFileID,
                    _controller.GetMyLogType(v.LogTypeKey).LogTypeCSSClass,
                    _controller.GetMyLogType(v.LogTypeKey).LogTypeFriendlyName,
                    v.LogUserName,
                    v.LogPortalName,
                    LogCreateDate = v.LogCreateDate.ToString("G", CultureInfo.InvariantCulture),
                    v.LogProperties.Summary,
                    LogProperties = _controller.GetPropertiesText(v)
                });

                var response = new
                {
                    Success = true,
                    Results = items,
                    TotalResults = totalRecords
                };

                return Request.CreateResponse(HttpStatusCode.OK, response);
            }
            catch (Exception exc)
            {
{'fraction_non_alphanumeric': 0.09442958483544599, 'fraction_numerical': 0.002907314804046982, 'mean_word_length': 1.606060606060606, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 47, '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': '6963341', 'n_tokens_mistral': 2273, 'n_tokens_neox': 2132, 'n_words': 422}