private List <MeterDataSet> LoadMeterDataSets(DbAdapterContainer dbAdapterContainer, FileGroup fileGroup) { List <MeterDataSet> meterDataSets = new List <MeterDataSet>(); MeterInfoDataContext meterInfo = dbAdapterContainer.GetAdapter <MeterInfoDataContext>(); EventTableAdapter eventAdapter = dbAdapterContainer.GetAdapter <EventTableAdapter>(); EventDataTableAdapter eventDataAdapter = dbAdapterContainer.GetAdapter <EventDataTableAdapter>(); MeterData.EventDataTable eventTable = eventAdapter.GetDataByFileGroup(fileGroup.ID); MeterDataSet meterDataSet; DataGroup dataGroup; foreach (IGrouping <int, MeterData.EventRow> eventGroup in eventTable.GroupBy(evt => evt.MeterID)) { meterDataSet = new MeterDataSet(); meterDataSet.Meter = meterInfo.Meters.SingleOrDefault(meter => meter.ID == eventGroup.Key); foreach (MeterData.EventRow evt in eventGroup) { dataGroup = new DataGroup(); dataGroup.FromData(meterDataSet.Meter, eventDataAdapter.GetTimeDomainData(evt.EventDataID)); foreach (DataSeries dataSeries in dataGroup.DataSeries) { meterDataSet.DataSeries.Add(dataSeries); } } meterDataSets.Add(meterDataSet); } return(meterDataSets); }
public override void Load(DbAdapterContainer dbAdapterContainer) { BulkLoader bulkLoader; EventTableAdapter eventAdapter; EventDataTableAdapter eventDataAdapter; Dictionary <EventKey, MeterData.EventRow> eventLookup; Dictionary <WaveformKey, MeterData.EventDataRow> eventDataLookup; MeterData.EventRow eventRow; if (m_eventDataTable.Count == 0) { return; } Log.Info("Loading event data into the database..."); // Create the bulk loader for loading data into the database bulkLoader = new BulkLoader(); bulkLoader.Connection = dbAdapterContainer.Connection; bulkLoader.CommandTimeout = dbAdapterContainer.CommandTimeout; // Write event data to the database bulkLoader.Load(m_eventDataTable); // Query database for event data IDs and store them in a lookup table by waveform key eventDataAdapter = dbAdapterContainer.GetAdapter <EventDataTableAdapter>(); eventDataAdapter.FillByFileGroup(m_eventDataTable, m_meterDataSet.FileGroup.ID); eventDataLookup = m_eventDataTable .GroupBy(CreateWaveformKey) .ToDictionary(grouping => grouping.Key, grouping => { if (grouping.Count() > 1) { Log.Warn($"Duplicate waveform found for meter {m_meterDataSet.Meter.AssetKey}: {string.Join(", ", grouping.Select(waveform => waveform.ID))}"); } return(grouping.First()); }); // Update the event rows with the IDs from the event data table foreach (Tuple <WaveformKey, MeterData.EventRow> tuple in m_eventList) { tuple.Item2.EventDataID = eventDataLookup[tuple.Item1].ID; m_eventTable.AddEventRow(tuple.Item2); } // Write events to the database bulkLoader.Load(m_eventTable); // Query database for events and store them in a lookup table by event key eventAdapter = dbAdapterContainer.GetAdapter <EventTableAdapter>(); eventAdapter.FillByFileGroup(m_eventTable, m_meterDataSet.FileGroup.ID); eventLookup = m_eventTable .GroupBy(CreateEventKey) .ToDictionary(grouping => grouping.Key, grouping => { if (grouping.Count() > 1) { Log.Warn($"Duplicate event found for meter {m_meterDataSet.Meter.AssetKey}: {string.Join(", ", grouping.Select(evt => evt.ID))}"); } return(grouping.First()); }); // Update the disturbance rows with the IDs from the event table foreach (Tuple <EventKey, MeterData.DisturbanceRow> tuple in m_disturbanceList) { if (eventLookup.TryGetValue(tuple.Item1, out eventRow)) { tuple.Item2.EventID = eventRow.ID; m_disturbanceTable.AddDisturbanceRow(tuple.Item2); } } // Write disturbances to the database bulkLoader.Load(m_disturbanceTable); Log.Info(string.Format("Loaded {0} events into the database.", m_eventTable.Count)); }