예제 #1
0
        /// <summary>
        /// Hent nye datafiler fra disk og legg dem i databasen. Etterhvert: Arkivér dem også.
        /// </summary>
        public void ReadAndIngestData()
        {
            var eventsOnDisk = GetEventsFromDisk();

            foreach (var diskEvent in eventsOnDisk)
            {
                var dbEvent = _eventRepository.GetByBillettServiceId(diskEvent.EventNumber);
                if (dbEvent == null)
                {
                    _eventRepository.Store(diskEvent);

                    dbEvent = diskEvent;
                }

                var diskMeasurements     = _eventDataFetcher.GetMeasurements(diskEvent).ToList();
                var existingMeasurements = _measurementRepository.GetForEventAndDateTimes(dbEvent,
                                                                                          diskMeasurements.Select(dm => dm.MeasurementTime)).ToList();

                foreach (var diskMeasurement in diskMeasurements)
                {
                    if (existingMeasurements.Any(e => e.MeasurementTime == diskMeasurement.MeasurementTime))
                    {
                        Console.WriteLine("Vi vil ikke ha denne: " + diskMeasurement.FullPath);
                        _fileArchiver.MoveToArchive(diskMeasurement);
                        continue;
                    }
                    var measurement = _measurementReader.ReadMeasurement(diskMeasurement);

                    if (measurement != null) // Er denne null, så var .xml-fila ikke brukbar for oss - vedlikehold hos BS, f.eks.
                    {
                        dbEvent.Measurements.Add(measurement);
                    }

                    _fileArchiver.MoveToArchive(diskMeasurement);
                }
            }

            _eventRepository.SaveChanges();

            _fileArchiver.PerformCleanup();
        }
예제 #2
0
        /// <summary>
        /// Hent nye datafiler fra disk og legg dem i databasen. Etterhvert: Arkivér dem også.
        /// </summary>
        /// <returns>A sequence of events that had data added to them</returns>
        public IEnumerable <BillettServiceEvent> ReadAndIngestData()
        {
            List <BillettServiceEvent> eventsOnDisk;

            try
            {
                eventsOnDisk = GetEventsFromDisk().ToList();
            }
            catch (Exception e)
            {
                Trace.TraceError("Fetching events failed, exiting: {0}", e.Message);
                return(Enumerable.Empty <BillettServiceEvent>());
            }

            var updatedEvents = new List <BillettServiceEvent>();


            foreach (var diskEvent in eventsOnDisk)
            {
                var dbEvent = _eventRepository.GetByBillettServiceId(diskEvent.EventNumber);
                if (dbEvent == null)
                {
                    _eventRepository.Store(diskEvent);

                    dbEvent = diskEvent;
                }

                var diskMeasurements = _eventDataFetcher.GetMeasurements(diskEvent).ToList();
                if (!diskMeasurements.Any())
                {
                    continue;
                }

                Trace.TraceInformation("Got {0} new measurement{2} to process for event {1}: ", diskMeasurements.Count, diskEvent.EventNumber, diskMeasurements.Count == 1 ? "" : "s");
                foreach (var measurement in diskMeasurements)
                {
                    Trace.TraceInformation(measurement.FullPath);
                }

                var existingMeasurements = _measurementRepository.GetForEventAndDateTimes(dbEvent,
                                                                                          diskMeasurements.Select(dm => dm.MeasurementTime)).ToList();

                foreach (var diskMeasurement in diskMeasurements)
                {
                    if (existingMeasurements.Any(e => e.MeasurementTime == diskMeasurement.MeasurementTime))
                    {
                        Trace.TraceInformation("File skipped because of duplicate time: " + diskMeasurement.FullPath);
                        _fileArchiver.MoveToArchive(diskMeasurement);
                        continue;
                    }
                    var measurement = _measurementReader.ReadMeasurement(diskMeasurement);

                    if (measurement != null) // Er denne null, så var .xml-fila ikke brukbar for oss - vedlikehold hos BS, f.eks.
                    {
                        dbEvent.Measurements.Add(measurement);
                        Trace.TraceInformation("Added measurement for time {0} to event {1}", measurement.MeasurementTime, dbEvent.EventNumber);
                        if (!updatedEvents.Contains(dbEvent))
                        {
                            updatedEvents.Add(dbEvent);
                            Trace.TraceInformation("Adding {0} to list of updated events", dbEvent.EventNumber);
                        }
                    }

                    _fileArchiver.MoveToArchive(diskMeasurement);
                }
            }

            _eventRepository.SaveChanges();

            _fileArchiver.PerformCleanup();

            return(updatedEvents);
        }