예제 #1
0
 protected SitkaController(RioDbContext dbContext, ILogger <T> logger, KeystoneService keystoneService, IOptions <RioConfiguration> rioConfiguration)
 {
     _dbContext        = dbContext;
     _logger           = logger;
     _keystoneService  = keystoneService;
     _rioConfiguration = rioConfiguration.Value;
 }
예제 #2
0
 public OfferController(RioDbContext dbContext, ILogger <OfferController> logger, KeystoneService keystoneService, IOptions <RioConfiguration> rioConfiguration)
 {
     _dbContext        = dbContext;
     _logger           = logger;
     _keystoneService  = keystoneService;
     _rioConfiguration = rioConfiguration.Value;
 }
예제 #3
0
 public OpenETRetrieveFromBucketJob(ILogger <OpenETRetrieveFromBucketJob> logger,
                                    IWebHostEnvironment webHostEnvironment, RioDbContext rioDbContext,
                                    IOptions <RioConfiguration> rioConfiguration) : base("Parcel Evapotranspiration Update Job", logger, webHostEnvironment,
                                                                                         rioDbContext)
 {
     _rioConfiguration = rioConfiguration.Value;
 }
예제 #4
0
 protected ScheduledBackgroundJobBase(string jobName, ILogger <T> logger, IWebHostEnvironment webHostEnvironment, RioDbContext rioDbContext)
 {
     _jobName            = jobName;
     _logger             = logger;
     _webHostEnvironment = webHostEnvironment;
     _rioDbContext       = rioDbContext;
 }
예제 #5
0
 public OpenETTriggerBucketRefreshJob(ILogger <OpenETTriggerBucketRefreshJob> logger,
                                      IWebHostEnvironment webHostEnvironment, RioDbContext rioDbContext,
                                      IOptions <RioConfiguration> rioConfiguration, IBackgroundJobClient backgroundJobClient) : base("Trigger OpenET Google Bucket Update Job", logger, webHostEnvironment,
                                                                                                                                     rioDbContext)
 {
     _rioConfiguration    = rioConfiguration.Value;
     _backgroundJobClient = backgroundJobClient;
 }
예제 #6
0
        public static UserDto GetUserFromHttpContext(RioDbContext dbContext, HttpContext httpContext)
        {
            var claimsPrincipal = httpContext.User;

            if (!claimsPrincipal.Claims.Any())
            {
                return(null);
            }

            var userGuid     = Guid.Parse(claimsPrincipal.Claims.Single(c => c.Type == "sub").Value);
            var keystoneUser = Rio.EFModels.Entities.User.GetByUserGuid(dbContext, userGuid);

            return(keystoneUser);
        }
예제 #7
0
        public static bool RasterUpdatedSinceMinimumLastUpdatedDate(RioConfiguration rioConfiguration,
                                                                    RioDbContext rioDbContext, int year, OpenETSyncHistoryDto newSyncHistory)
        {
            var openETRequestURL =
                $"{rioConfiguration.OpenETCheckRasterUpdatedDateBaseURL}?geom=-120.30921936035156,36.99542364399086,-120.30887603759766,36.98143783973302,-120.2918815612793,36.982260605282676,-120.29170989990234,36.99556074698967,-120.30921936035156,36.99542364399086&api_key={rioConfiguration.OpenETAPIKey}&start_date={new DateTime(year, 1, 1):yyyy-MM-dd}&end_date={new DateTime(year, 12, 31):yyyy-MM-dd}&model=ensemble&vars=et&prop=date_ingested";

            var httpClient = new HttpClient
            {
                Timeout = new TimeSpan(60 * TimeSpan.TicksPerSecond)
            };

            var response = httpClient.GetAsync(openETRequestURL).Result;

            //If we don't get a good response, let's just say that there needs to be an update to cover our bases
            if (!response.IsSuccessStatusCode)
            {
                return(true);
            }

            var responseObject = JsonConvert.DeserializeObject <RasterMetadataDateIngested>(response.Content.ReadAsStringAsync().Result);

            if (string.IsNullOrEmpty(responseObject.DateIngested) ||
                !DateTime.TryParse(responseObject.DateIngested, out DateTime responseDate))
            {
                OpenETSyncHistory.UpdateSyncResultByID(rioDbContext, newSyncHistory.OpenETSyncHistoryID, OpenETSyncResultTypeEnum.DataNotAvailable);
                return(false);
            }

            var openETSyncHistoriesThatHaventFailed = rioDbContext.OpenETSyncHistory
                                                      .Include(x => x.WaterYear)
                                                      .Where(x => x.WaterYear.Year == year &&
                                                             (x.OpenETSyncResultTypeID != (int)OpenETSyncResultTypeEnum.Failed) && x.OpenETSyncHistoryID != newSyncHistory.OpenETSyncHistoryID);

            if (!openETSyncHistoriesThatHaventFailed.Any())
            {
                return(true);
            }

            var mostRecentSyncHistory = openETSyncHistoriesThatHaventFailed?.OrderByDescending(x => x.UpdateDate).First();

            if (responseDate > mostRecentSyncHistory.UpdateDate)
            {
                return(true);
            }

            OpenETSyncHistory.UpdateSyncResultByID(rioDbContext, newSyncHistory.OpenETSyncHistoryID, OpenETSyncResultTypeEnum.NoNewData);
            return(false);
        }
예제 #8
0
        private MailMessage GenerateUserCreatedEmail(string rioUrl, UserDto user, RioDbContext dbContext,
                                                     SitkaSmtpClientService smtpClient)
        {
            var messageBody = $@"A new user has signed up to the {_rioConfiguration.PlatformLongName}: <br/><br/>
 {user.FullName} ({user.Email}) <br/><br/>
As an administrator of the {_rioConfiguration.PlatformShortName}, you can assign them a role and associate them with a Billing Account by following <a href='{rioUrl}/users/{user.UserID}'>this link</a>. <br/><br/>
{smtpClient.GetSupportNotificationEmailSignature()}";

            var mailMessage = new MailMessage
            {
                Subject = $"New User in {_rioConfiguration.PlatformLongName}",
                Body    = $"Hello,<br /><br />{messageBody}",
            };

            mailMessage.To.Add(smtpClient.GetDefaultEmailFrom());
            return(mailMessage);
        }
 public AccountReconciliationController(RioDbContext dbContext, ILogger <AccountReconciliationController> logger, KeystoneService keystoneService, IOptions <RioConfiguration> rioConfiguration) : base(dbContext, logger, keystoneService, rioConfiguration)
 {
 }
예제 #10
0
 public CimisPrecipJob(ILogger <CimisPrecipJob> logger, IWebHostEnvironment webHostEnvironment, RioDbContext rioDbContext, IOptions <RioConfiguration> rioConfiguration) : base("Precipitation Update Job", logger, webHostEnvironment, rioDbContext)
 {
     _rioConfiguration = rioConfiguration.Value;
 }
예제 #11
0
 public ParcelAllocationController(RioDbContext dbContext, ILogger <ParcelAllocationController> logger, KeystoneService keystoneService, IOptions <RioConfiguration> rioConfiguration) : base(dbContext, logger, keystoneService, rioConfiguration)
 {
 }
예제 #12
0
 public PostingController(RioDbContext dbContext, ILogger <PostingController> logger, KeystoneService keystoneService, IOptions <RioConfiguration> rioConfiguration) : base(dbContext, logger, keystoneService, rioConfiguration)
 {
     _allowTrading = _rioConfiguration.ALLOW_TRADING;
 }
예제 #13
0
 public WaterYearController(RioDbContext dbContext, ILogger <WaterYearController> logger, KeystoneService keystoneService, IOptions <RioConfiguration> rioConfiguration) : base(dbContext, logger, keystoneService, rioConfiguration)
 {
 }
예제 #14
0
 public static int GetDefaultWaterYearToDisplay(RioDbContext dbContext)
 {
     return(DateTime.Today.Year);
 }
예제 #15
0
        public static async Task <FileResource> MakeFileResourceFromHttpRequest(HttpRequest httpRequest, RioDbContext rioDbContext, HttpContext httpContext)
        {
            var bytes = await httpRequest.GetData();

            var userDto         = UserContext.GetUserFromHttpContext(rioDbContext, httpContext);
            var queryCollection = httpRequest.Query;

            var fileResourceMimeType = FileResourceMimeType.GetFileResourceMimeTypeByContentTypeName(rioDbContext,
                                                                                                     queryCollection["mimeType"].ToString());

            var clientFilename   = queryCollection["clientFilename"].ToString();
            var extension        = clientFilename.Split('.').Last();
            var fileResourceGuid = Guid.NewGuid();

            return(new FileResource
            {
                CreateDate = DateTime.Now,
                CreateUserID = userDto.UserID,
                FileResourceData = bytes,
                FileResourceGUID = fileResourceGuid,
                FileResourceMimeTypeID = fileResourceMimeType.FileResourceMimeTypeID,
                OriginalBaseFilename = clientFilename,
                OriginalFileExtension = extension,
            });
        }
예제 #16
0
 public MarketMetricsController(RioDbContext dbContext, ILogger <MarketMetricsController> logger, KeystoneService keystoneService, IOptions <RioConfiguration> rioConfiguration) : base(dbContext, logger, keystoneService, rioConfiguration)
 {
 }
예제 #17
0
        public static HttpResponseMessage TriggerOpenETGoogleBucketRefresh(RioConfiguration rioConfiguration,
                                                                           RioDbContext rioDbContext, int waterYearID)
        {
            if (!rioConfiguration.AllowOpenETSync)
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.BadRequest,
                    Content = new StringContent("Syncing with OpenET is not enabled at this time")
                });
            }

            var waterYearDto = WaterYear.GetByWaterYearID(rioDbContext, waterYearID);

            if (waterYearDto == null)
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.BadRequest,
                    Content = new StringContent("Requested Water Year not found")
                });
            }

            var year = waterYearDto.Year;

            if (rioDbContext.OpenETSyncHistory
                .Include(x => x.WaterYear)
                .Any(x => x.WaterYear.Year == year && x.OpenETSyncResultTypeID == (int)OpenETSyncResultTypeEnum.InProgress))
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.BadRequest,
                    Content = new StringContent($"Sync already in progress for {year}")
                });
            }

            var newSyncHistory = OpenETSyncHistory.New(rioDbContext, year);

            if (!RasterUpdatedSinceMinimumLastUpdatedDate(rioConfiguration, rioDbContext, year, newSyncHistory))
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.UnprocessableEntity,
                    Content = new StringContent($"The request was successful, however the sync for {year} will not be completed for the following reason: {OpenETSyncHistory.GetByOpenETSyncHistoryID(rioDbContext, newSyncHistory.OpenETSyncHistoryID).OpenETSyncResultType.OpenETSyncResultTypeDisplayName}")
                });
            }

            var openETRequestURL =
                $"{rioConfiguration.OpenETTriggerTimeSeriesBaseURL}?shapefile_fn={rioConfiguration.OpenETShapefilePath}&start_date={new DateTime(year, 1, 1):yyyy-MM-dd}&end_date={new DateTime(year, 12, 31):yyyy-MM-dd}&model=ensemble&vars=et&aggregation_type=mean&api_key={rioConfiguration.OpenETAPIKey}&to_cloud=openet_raster_api_storage&suffix={newSyncHistory.GetFileSuffixForOpenETSyncHistoryDto(rioConfiguration.LeadOrganizationShortName)}&out_columns=ParcelNumb";

            var httpClient = new HttpClient
            {
                Timeout = new TimeSpan(60 * TimeSpan.TicksPerSecond)
            };

            var responseResult = httpClient.GetAsync(openETRequestURL).Result;

            if (!responseResult.IsSuccessStatusCode)
            {
                OpenETSyncHistory.UpdateSyncResultByID(rioDbContext, newSyncHistory.OpenETSyncHistoryID,
                                                       OpenETSyncResultTypeEnum.Failed);
            }

            return(responseResult);
        }
예제 #18
0
        public static void UpdateParcelMonthlyEvapotranspirationWithETData(RioDbContext rioDbContext, RioConfiguration rioConfiguration, int syncHistoryID)
        {
            var syncHistoryObject = OpenETSyncHistory.GetByOpenETSyncHistoryID(rioDbContext, syncHistoryID);

            if (syncHistoryObject == null || syncHistoryObject.OpenETSyncResultType.OpenETSyncResultTypeID !=
                (int)OpenETSyncResultTypeEnum.InProgress)
            {
                //Bad request, we completed already and somehow were called again, or someone else decided we were done
                return;
            }

            var openETRequestURL =
                $"{rioConfiguration.OpenETGoogleBucketBaseURL}/testing_multi_mean_{rioConfiguration.OpenETAPIKey}_{syncHistoryObject.GetFileSuffixForOpenETSyncHistoryDto(rioConfiguration.LeadOrganizationShortName)}.csv";

            var httpClient = new HttpClient
            {
                Timeout = new TimeSpan(60 * TimeSpan.TicksPerSecond)
            };

            var response = httpClient.GetAsync(openETRequestURL).Result;

            if (!response.IsSuccessStatusCode)
            {
                var timeBetweenSyncCreationAndNow = DateTime.UtcNow.Subtract(syncHistoryObject.CreateDate).Hours;

                OpenETSyncHistory.UpdateSyncResultByID(rioDbContext, syncHistoryObject.OpenETSyncHistoryID, timeBetweenSyncCreationAndNow > 2 ? OpenETSyncResultTypeEnum.Failed : OpenETSyncResultTypeEnum.InProgress);

                return;
            }

            List <OpenETGoogleBucketResponseEvapotranspirationData> distinctRecords;

            using (var reader = new StreamReader(response.Content.ReadAsStreamAsync().Result))
            {
                var csvr = new CsvReader(reader, CultureInfo.CurrentCulture);
                var finalizedWaterYears = rioDbContext.WaterYear
                                          .Where(x => x.FinalizeDate.HasValue)
                                          .Select(x => x.Year)
                                          .ToList();
                distinctRecords = csvr.GetRecords <OpenETCSVFormat>().Where(x => !finalizedWaterYears.Contains(x.Date.Year))
                                  .Distinct(new DistinctOpenETCSVFormatComparer())
                                  .Select(x => x.AsOpenETGoogleBucketResponseEvapotranspirationData())
                                  .ToList();;
            }

            if (!distinctRecords.Any())
            {
                OpenETSyncHistory.UpdateSyncResultByID(rioDbContext, syncHistoryObject.OpenETSyncHistoryID, OpenETSyncResultTypeEnum.NoNewData);
                return;
            }

            try
            {
                rioDbContext.Database.ExecuteSqlRaw(
                    "TRUNCATE TABLE dbo.OpenETGoogleBucketResponseEvapotranspirationData");
                DataTable table = new DataTable();
                table.Columns.Add("OpenETGoogleBucketResponseEvapotranspirationDataID", typeof(int));
                table.Columns.Add("ParcelNumber", typeof(string));
                table.Columns.Add("WaterMonth", typeof(int));
                table.Columns.Add("WaterYear", typeof(int));
                table.Columns.Add("EvapotranspirationRateInMM", typeof(decimal));

                int i = 0;
                distinctRecords.ForEach(x =>
                {
                    table.Rows.Add(++i, x.ParcelNumber, x.WaterMonth, x.WaterYear, x.EvapotranspirationRateInMM);
                });

                using (SqlConnection con = new SqlConnection(rioConfiguration.DB_CONNECTION_STRING))
                {
                    using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                    {
                        sqlBulkCopy.DestinationTableName = "dbo.OpenETGoogleBucketResponseEvapotranspirationData";
                        con.Open();
                        sqlBulkCopy.WriteToServer(table);
                        con.Close();
                    }
                }

                rioDbContext.Database.ExecuteSqlRaw("EXECUTE dbo.pUpdateParcelMonthlyEvapotranspirationWithETData");

                OpenETSyncHistory.UpdateSyncResultByID(rioDbContext, syncHistoryObject.OpenETSyncHistoryID,
                                                       OpenETSyncResultTypeEnum.Succeeded);
            }
            catch (Exception ex)
            {
                OpenETSyncHistory.UpdateSyncResultByID(rioDbContext, syncHistoryObject.OpenETSyncHistoryID,
                                                       OpenETSyncResultTypeEnum.Failed);
            }
        }
예제 #19
0
 public CustomRichTextController(RioDbContext dbContext, ILogger <CustomRichTextController> logger, KeystoneService keystoneService, IOptions <RioConfiguration> rioConfiguration) : base(dbContext, logger, keystoneService, rioConfiguration)
 {
 }
예제 #20
0
 public FileResourceController(RioDbContext dbContext, ILogger <FileResourceController> logger, KeystoneService keystoneService, IOptions <RioConfiguration> rioConfiguration) : base(dbContext, logger, keystoneService, rioConfiguration)
 {
 }
예제 #21
0
 public PostingTypeController(RioDbContext dbContext, ILogger <PostingTypeController> logger, KeystoneService keystoneService, IOptions <RioConfiguration> rioConfiguration) : base(dbContext, logger, keystoneService, rioConfiguration)
 {
 }
예제 #22
0
 public BulkSetAllocationCSVMap(RioDbContext dbContext, string parcelAllocationTypeDisplayName)
 {
     Map(m => m.APN).Name("APN");
     Map(m => m.AllocationQuantity).Name(parcelAllocationTypeDisplayName + " Quantity");
 }