Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var configuration   = GetConfiguration();
            var telemetryClient = new TelemetryClient()
            {
                InstrumentationKey = configuration["APPINSIGHTS_INSTRUMENTATIONKEY"]
            };

            var logger = new LoggerConfiguration()
                         .ReadFrom.Configuration(configuration)
                         .WriteTo
                         .ApplicationInsightsTraces(configuration["APPINSIGHTS_INSTRUMENTATIONKEY"])
                         .Enrich.WithProperty("WebJob", "UcasCourseImporter")
                         .Enrich.WithProperty("WebJob_Identifer", Guid.NewGuid())
                         .Enrich.WithProperty("WebJob_Triggered_Date", DateTime.UtcNow)
                         .CreateLogger();

            var folder = Path.Combine(Path.GetTempPath(), "ucasfiles", Guid.NewGuid().ToString());

            try
            {
                logger.Information("UcasCourseImporter started.");

                var configOptions = new UcasCourseImporterConfigurationOptions(configuration);
                configOptions.Validate();
                var mcConfig = new McConfig(configuration);


                Directory.CreateDirectory(folder);

                var downloadAndExtractor = new DownloaderAndExtractor(logger, folder, configOptions.AzureUrl,
                                                                      configOptions.AzureSignature);

                var unzipFolder         = downloadAndExtractor.DownloadAndExtractLatest("NetupdateExtract");
                var unzipFolderProfiles = downloadAndExtractor.DownloadAndExtractLatest("EntryProfilesExtract_test");

                var xlsReader = new XlsReader(logger);

                // only used to avoid importing orphaned data
                // i.e. we do not import institutions but need them to determine which campuses to import
                var subjects = xlsReader.ReadSubjects("data");

                // entry profile data - used to correct institution data
                var institutionProfiles = ReadInstitutionProfiles(unzipFolderProfiles);

                // data to import
                var institutions = xlsReader.ReadInstitutions(unzipFolder);
                UpdateContactDetails(institutions, institutionProfiles);
                var campuses       = xlsReader.ReadCampuses(unzipFolder, institutions);
                var courses        = xlsReader.ReadCourses(unzipFolder, campuses);
                var courseSubjects = xlsReader.ReadCourseSubjects(unzipFolder, courses, subjects);
                var courseNotes    = xlsReader.ReadCourseNotes(unzipFolder);
                var noteTexts      = xlsReader.ReadNoteText(unzipFolder);

                var payload = new UcasPayload
                {
                    Institutions   = new List <Xls.Domain.UcasInstitution>(institutions),
                    Courses        = new List <UcasCourse>(courses),
                    CourseSubjects = new List <UcasCourseSubject>(courseSubjects),
                    Campuses       = new List <UcasCampus>(campuses),
                    CourseNotes    = new List <UcasCourseNote>(courseNotes),
                    NoteTexts      = new List <UcasNoteText>(noteTexts),
                    Subjects       = new List <UcasSubject>(subjects)
                };
                var context          = GetDbContext(mcConfig);
                var ucasDataMigrator = new UcasDataMigrator(context, logger, payload);
                ucasDataMigrator.UpdateUcasData();
            }
            catch (Exception e)
            {
                logger.Error(e, "UcasCourseImporter error.");
            }
            finally
            {
                CleanupTempData(folder, logger);
                logger.Information("UcasCourseImporter finished.");

                // flush logs and wait for them to be written. https://github.com/serilog/serilog-sinks-applicationinsights#how-when-and-why-to-flush-messages-manually
                telemetryClient.Flush();
                Thread.Sleep(5000);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var configuration = GetConfiguration();

            var logger = new LoggerConfiguration()
                         .ReadFrom.Configuration(configuration)
                         .WriteTo
                         .ApplicationInsightsTraces(configuration["APPINSIGHTS_INSTRUMENTATIONKEY"])
                         .CreateLogger();

            logger.Information("UcasCourseImporter started.");

            var configOptions = new UcasCourseImporterConfigurationOptions(configuration);

            configOptions.Validate();

            var folder = Path.Combine(Path.GetTempPath(), "ucasfiles", Guid.NewGuid().ToString());

            try
            {
                Directory.CreateDirectory(folder);

                var downloadAndExtractor = new DownloaderAndExtractor(logger, folder, configOptions.AzureUrl,
                                                                      configOptions.AzureSignature);

                var unzipFolder         = downloadAndExtractor.DownloadAndExtractLatest("NetupdateExtract");
                var unzipFolderProfiles = downloadAndExtractor.DownloadAndExtractLatest("EntryProfilesExtract_test");

                var xlsReader = new XlsReader(logger);

                // only used to avoid importing orphaned data
                // i.e. we do not import institutions but need them to determine which campuses to import
                var subjects = xlsReader.ReadSubjects("data");

                // entry profile data - used to correct institution data
                var institutionProfiles = ReadInstitutionProfiles(unzipFolderProfiles);

                // data to import
                var institutions = xlsReader.ReadInstitutions(unzipFolder);
                UpdateContactDetails(institutions, institutionProfiles);
                var campuses       = xlsReader.ReadCampuses(unzipFolder, institutions);
                var courses        = xlsReader.ReadCourses(unzipFolder, campuses);
                var courseSubjects = xlsReader.ReadCourseSubjects(unzipFolder, courses, subjects);
                var courseNotes    = xlsReader.ReadCourseNotes(unzipFolder);
                var noteTexts      = xlsReader.ReadNoteText(unzipFolder);

                var payload = new UcasPayload
                {
                    Institutions   = new ObservableCollection <UcasInstitution>(institutions),
                    Courses        = new ObservableCollection <UcasCourse>(courses),
                    CourseSubjects = new ObservableCollection <UcasCourseSubject>(courseSubjects),
                    Campuses       = new ObservableCollection <UcasCampus>(campuses),
                    CourseNotes    = new ObservableCollection <UcasCourseNote>(courseNotes),
                    NoteTexts      = new ObservableCollection <UcasNoteText>(noteTexts)
                };

                var manageApi = new ManageApi(logger, configOptions.ManageApiUrl, configOptions.ManageApiKey);
                manageApi.PostPayload(payload);
            }
            catch (Exception e)
            {
                logger.Error(e, "UcasCourseImporter error.");
            }
            finally
            {
                CleanupTempData(folder, logger);
                logger.Information("UcasCourseImporter finished.");
            }
        }