<commit_before>﻿//******************************************************************************************************
//  EventOperation.cs - Gbtc
//
//  Copyright © 2014, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the Eclipse Public License -v 1.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.opensource.org/licenses/eclipse-1.0.php
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  07/22/2014 - <NAME>
//       Generated original version of source code.
//
//******************************************************************************************************

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using FaultData.DataAnalysis;
using FaultData.DataResources;
using FaultData.DataSets;
using GSF.Collections;
using GSF.Data;
using GSF.Data.Model;
using log4net;
using openXDA.Model;
using DbDisturbance = openXDA.Model.Disturbance;
using Disturbance = FaultData.DataAnalysis.Disturbance;
using PQDPhase = GSF.PQDIF.Logical.Phase;

namespace FaultData.DataOperations
{
    public class EventOperation : DataOperationBase<MeterDataSet>
    {
        #region [ Members ]

        // Constants
        private const double Sqrt3 = 1.7320508075688772935274463415059D;

        // Fields
        private double m_systemFrequency;
        private TimeZoneInfo m_timeZone;

        #endregion

        #region [ Properties ]

        [Setting]
        public double SystemFrequency
        {
            get
            {
                return m_systemFrequency;
            }
            set
            {
                m_systemFrequency = value;
            }
        }

        [Setting]
        public string XDATimeZone
        {
            get
            {
                return m_timeZone.Id;
            }
            set
            {
                m_timeZone = TimeZoneInfo.FindSystemTimeZoneById(value);
            }
        }

        #endregion

        #region [ Methods ]

        public override void Execute(MeterDataSet meterDataSet)
        {
            DataGroupsResource dataGroupsResource = meterDataSet.GetResource<DataGroupsResource>();
            CycleDataResource cycleDataResource = meterDataSet.GetResource<CycleDataResource>();
            EventClassificationResource eventClassificationResource = meterDataSet.GetResource<EventClassificationResource>();

            using (AdoDataConnection connection = meterDataSet.CreateDbConnection())
            {
                List<DataGroup> dataGroups = new List<DataGroup>(cycleDataResource.DataGroups);
                dataGroups.AddRange(dataGroupsResource.DataGroups.Where(dataGroup => dataGroup.DataSeries.Count == 0));

                List<Event> events = GetEvents(connection, meterDataSet, dataGroups, cycleDataResource.VICycleDataGroups, eventClassificationResource.Classifications);
                LoadEvents(connection, events, meterDataSet);
            }
        }

        private List<Event> GetEvents(AdoDataConnection connection, MeterDataSet meterDataSet, List<DataGroup> dataGroups, List<VICycleDataGroup> viCycleDataGroups, Dictionary<DataGroup, EventClassification> eventClassifications)
        {
            int count = dataGroups
                .Where(dataGroup => dataGroup.Classification != DataClassification.Trend)
                .Where(dataGroup => dataGroup.Classification != DataClassification.Unknown)
                .Where(dataGroup => eventClassifications.ContainsKey(dataGroup))
                .Count();

            if (count == 0)
            {
                Log.Info($"No events found for file '{meterDataSet.FilePath}'.");
                return new List<Event>();
            }

            Log.Info(string.Format("Processing {0} events...", count));

            List<Event> events = new List<Event>(count);

            TableOperations<Event> eventTable = new TableOperations<Event>(connection);
            TableOperations<EventType> eventTypeTable = new TableOperations<EventType>(connection);
            TableOperations<EventData> eventDataTable = new TableOperations<EventData>(connection);

            for (int i = 0; i < dataGroups.Count; i++)
            {
                DataGroup dataGroup = dataGroups[i];
                EventClassification eventClassification;

                if (dataGroup.Classification == DataClassification.Trend)
                    continue;

                if (dataGroup.Classification == DataClassification.Unknown)
                    continue;

                if (!eventClassifications.TryGetValue(dataGroup, out eventClassification))
                    continue;

                if ((object)dataGroup.Line == null && meterDataSet.Meter.MeterLocation.MeterLocationLines.Count != 1)
                    continue;

                Line line = dataGroup.Line ?? meterDataSet.Meter.MeterLocation.MeterLocationLines.Single().Line;
                IDbDataParameter startTime2 = ToDateTime2(connection, dataGroup.StartTime);
                IDbDataParameter endTime2 = ToDateTime2(connection, dataGroup.EndTime);

                if (eventTable.QueryRecordCountWhere("StartTime = {0} AND EndTime = {1} AND Samples = {2} AND MeterID = {3} AND LineID = {4}", startTime2, endTime2, dataGroup.Samples, meterDataSet.Meter.ID, line.ID) > 0)
                    continue;

                EventType eventType = eventTypeTable.GetOrAdd(eventClassification.ToString());

                Event evt = new Event()
                {
                    FileGroupID = meterDataSet.FileGroup.ID,
                    MeterID = meterDataSet.Meter.ID,
                    LineID = line.ID,
                    EventTypeID = eventType.ID,
                    EventDataID = null,
                    Name = string.Empty,
                    StartTime = dataGroup.StartTime,
                    EndTime = dataGroup.EndTime,
                    Samples = dataGroup.Samples,
                    TimeZoneOffset = (int)m_timeZone.GetUtcOffset(dataGroup.StartTime).TotalMinutes,
                    SamplesPerSecond = 0,
                    SamplesPerCycle = 0
                };

                if (dataGroup.Samples > 0)
                {<commit_msg>Added logic to classify all events as Test during a maintenance window.<commit_after>﻿//******************************************************************************************************
//  EventOperation.cs - Gbtc
//
//  Copyright © 2014, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the Eclipse Public License -v 1.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.opensource.org/licenses/eclipse-1.0.php
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  07/22/2014 - <NAME>
//       Generated original version of source code.
//
//******************************************************************************************************

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using FaultData.DataAnalysis;
using FaultData.DataResources;
using FaultData.DataSets;
using GSF.Collections;
using GSF.Data;
using GSF.Data.Model;
using log4net;
using openXDA.Model;
using DbDisturbance = openXDA.Model.Disturbance;
using Disturbance = FaultData.DataAnalysis.Disturbance;
using PQDPhase = GSF.PQDIF.Logical.Phase;

namespace FaultData.DataOperations
{
    public class EventOperation : DataOperationBase<MeterDataSet>
    {
        #region [ Members ]

        // Constants
        private const double Sqrt3 = 1.7320508075688772935274463415059D;

        // Fields
        private double m_systemFrequency;
        private TimeZoneInfo m_timeZone;

        #endregion

        #region [ Properties ]

        [Setting]
        public double SystemFrequency
        {
            get
            {
                return m_systemFrequency;
            }
            set
            {
                m_systemFrequency = value;
            }
        }

        [Setting]
        public string XDATimeZone
        {
            get
            {
                return m_timeZone.Id;
            }
            set
            {
                m_timeZone = TimeZoneInfo.FindSystemTimeZoneById(value);
            }
        }

        #endregion

        #region [ Methods ]

        public override void Execute(MeterDataSet meterDataSet)
        {
            DataGroupsResource dataGroupsResource = meterDataSet.GetResource<DataGroupsResource>();
            CycleDataResource cycleDataResource = meterDataSet.GetResource<CycleDataResource>();
            EventClassificationResource eventClassificationResource = meterDataSet.GetResource<EventClassificationResource>();

            using (AdoDataConnection connection = meterDataSet.CreateDbConnection())
            {
                List<DataGroup> dataGroups = new List<DataGroup>(cycleDataResource.DataGroups);
                dataGroups.AddRange(dataGroupsResource.DataGroups.Where(dataGroup => dataGroup.DataSeries.Count == 0));

                List<Event> events = GetEvents(connection, meterDataSet, dataGroups, cycleDataResource.VICycleDataGroups, eventClassificationResource.Classifications);
                LoadEvents(connection, events, meterDataSet);
            }
        }

        private List<Event> GetEvents(AdoDataConnection connection, MeterDataSet meterDataSet, List<DataGroup> dataGroups, List<VICycleDataGroup> viCycleDataGroups, Dictionary<DataGroup, EventClassification> eventClassifications)
        {
            int count = dataGroups
                .Where(dataGroup => dataGroup.Classification != DataClassification.Trend)
                .Where(dataGroup => dataGroup.Classification != DataClassification.Unknown)
                .Where(dataGroup => eventClassifications.ContainsKey(dataGroup))
                .Count();

            if (count == 0)
            {
                Log.Info($"No events found for file '{meterDataSet.FilePath}'.");
                return new List<Event>();
            }

            Log.Info(string.Format("Processing {0} events...", count));

            List<Event> events = new List<Event>(count);

            TableOperations<Event> eventTable = new TableOperations<Event>(connection);
            TableOperations<EventType> eventTypeTable = new TableOperations<EventType>(connection);
            TableOperations<EventData> eventDataTable = new TableOperations<EventData>(connection);

            for (int i = 0; i < dataGroups.Count; i++)
            {
                DataGroup dataGroup = dataGroups[i];
                EventClassification eventClassification;

                if (dataGroup.Classification == DataClassification.Trend)
                    continue;

                if (dataGroup.Classification == DataClassification.Unknown)
                    continue;

                if (!eventClassifications.TryGetValue(dataGroup, out eventClassification))
                    continue;

                if ((object)dataGroup.Line == null && meterDataSet.Meter.MeterLocation.MeterLocationLines.Count != 1)
                    continue;

                Line line = dataGroup.Line ?? meterDataSet.Meter.MeterLocation.MeterLocationLines.Single().Line;
                IDbDataParameter startTime2 = ToDateTime2(connection, dataGroup.StartTime);
                IDbDataParameter endTime2 = ToDateTime2(connection, dataGroup.EndTime);

                if (eventTable.QueryRecordCountWhere("StartTime = {0} AND EndTime = {1} AND Samples = {2} AND MeterID = {3} AND LineID = {4}", startTime2, endTime2, dataGroup.Samples, meterDataSet.Meter.ID, line.ID) > 0)
                    continue;

                TableOperations<MaintenanceWindow> maintenanceWindowTable = new TableOperations<MaintenanceWindow>(connection);
                int maintenanceWindowCount = maintenanceWindowTable.QueryRecordCountWhere("MeterID = {0} AND (StartTime IS NULL OR StartTime <= {1}) AND (EndTime IS NULL OR EndTime >= {1})", meterDataSet.Meter.ID, DateTime.UtcNow);
                string eventTypeName = (maintenanceWindowCount == 0) ? eventClassification.ToString() : "Test";
                EventType eventType = eventTypeTable.GetOrAdd(eventTypeName);

                Event evt = new Event()
                {
                    FileGroupID = meterDataSet.FileGroup.ID,
                    MeterID = meterDataSet.Meter.ID,
                    LineID = line.ID,
                    EventTypeID = eventType.ID,
                    EventDataID = null,
                    Name = string.Empty,
                    StartTime = dataGroup.StartTime,
                    EndTime = dataGroup.EndTime,
                    Samples = dataGroup.Samples,
                    TimeZoneOffset = (int)m_timeZone.GetUtcOffset(dataGroup.StartTime).TotalMinutes,
                    SamplesPerSecond = 0,
                    SamplesPerCycle = 0
                };

                if (dataGroup.Samples > 0)
                {
{'fraction_non_alphanumeric': 0.12111087909793276, 'fraction_numerical': 0.010022969304656505, 'mean_word_length': 2.195729537366548, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 60, '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': '3399955', 'n_tokens_mistral': 3590, 'n_tokens_neox': 3381, 'n_words': 847}