-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…d web api for logistic regression analysis
- Loading branch information
Showing
14 changed files
with
348 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
DriverTracker/Areas/Identity/Pages/Account/Login.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace DriverTracker.Areas.Identity.Pages.Account | ||
{ | ||
[AllowAnonymous] | ||
public class LoginModel : PageModel | ||
{ | ||
private readonly SignInManager<IdentityUser> _signInManager; | ||
private readonly ILogger<LoginModel> _logger; | ||
|
||
public LoginModel(SignInManager<IdentityUser> signInManager, ILogger<LoginModel> logger) | ||
{ | ||
_signInManager = signInManager; | ||
_logger = logger; | ||
} | ||
|
||
[BindProperty] | ||
public InputModel Input { get; set; } | ||
|
||
public IList<AuthenticationScheme> ExternalLogins { get; set; } | ||
|
||
public string ReturnUrl { get; set; } | ||
|
||
[TempData] | ||
public string ErrorMessage { get; set; } | ||
|
||
public class InputModel | ||
{ | ||
[Required] | ||
[EmailAddress] | ||
public string Email { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.Password)] | ||
public string Password { get; set; } | ||
|
||
[Display(Name = "Remember me?")] | ||
public bool RememberMe { get; set; } | ||
} | ||
|
||
public async Task OnGetAsync(string returnUrl = null) | ||
{ | ||
if (!string.IsNullOrEmpty(ErrorMessage)) | ||
{ | ||
ModelState.AddModelError(string.Empty, ErrorMessage); | ||
} | ||
|
||
returnUrl = returnUrl ?? Url.Content("~/"); | ||
|
||
// Clear the existing external cookie to ensure a clean login process | ||
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); | ||
|
||
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); | ||
|
||
ReturnUrl = returnUrl; | ||
} | ||
|
||
public async Task<IActionResult> OnPostAsync(string returnUrl = null) | ||
{ | ||
returnUrl = returnUrl ?? Url.Content("~/"); | ||
|
||
if (ModelState.IsValid) | ||
{ | ||
// This doesn't count login failures towards account lockout | ||
// To enable password failures to trigger account lockout, set lockoutOnFailure: true | ||
var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true); | ||
if (result.Succeeded) | ||
{ | ||
_logger.LogInformation("User logged in."); | ||
return LocalRedirect(returnUrl); | ||
} | ||
if (result.RequiresTwoFactor) | ||
{ | ||
return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }); | ||
} | ||
if (result.IsLockedOut) | ||
{ | ||
_logger.LogWarning("User account locked out."); | ||
return RedirectToPage("./Lockout"); | ||
} | ||
else | ||
{ | ||
ModelState.AddModelError(string.Empty, "Invalid login attempt."); | ||
return Page(); | ||
} | ||
} | ||
|
||
// If we got this far, something failed, redisplay form | ||
return Page(); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
DriverTracker/Areas/Identity/Pages/Account/Logout.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace DriverTracker.Areas.Identity.Pages.Account | ||
{ | ||
[AllowAnonymous] | ||
public class LogoutModel : PageModel | ||
{ | ||
private readonly SignInManager<IdentityUser> _signInManager; | ||
private readonly ILogger<LogoutModel> _logger; | ||
|
||
public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger) | ||
{ | ||
_signInManager = signInManager; | ||
_logger = logger; | ||
} | ||
|
||
public void OnGet() | ||
{ | ||
} | ||
|
||
public async Task<IActionResult> OnPost(string returnUrl = null) | ||
{ | ||
await _signInManager.SignOutAsync(); | ||
_logger.LogInformation("User logged out."); | ||
if (returnUrl != null) | ||
{ | ||
return LocalRedirect(returnUrl); | ||
} | ||
else | ||
{ | ||
return Page(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Accord.Statistics.Models.Regression; | ||
|
||
using DriverTracker.Models; | ||
using DriverTracker.Domain; | ||
|
||
namespace DriverTracker.Controllers | ||
{ | ||
[Authorize(Roles = "Admin,Analyst")] | ||
[Route("api/[controller]")] | ||
public class AnalysisApiController : Controller | ||
{ | ||
private readonly MvcDriverContext _context; | ||
private readonly DriverStatistics _driverStatistics; | ||
|
||
public AnalysisApiController(MvcDriverContext context) { | ||
_context = context; | ||
_driverStatistics = new DriverStatistics(context); | ||
} | ||
|
||
// GET: api/analysisapi | ||
[HttpGet] | ||
public IEnumerable<DriverStatisticResults> Get() | ||
{ | ||
List<DriverStatisticResults> driverStatisticResults = new List<DriverStatisticResults>(); | ||
|
||
IEnumerable<Driver> drivers = _context.Drivers.AsEnumerable(); | ||
|
||
|
||
foreach (Driver driver in drivers) { | ||
_driverStatistics.ComputeDriverStatistics(driver.DriverID); | ||
driverStatisticResults.Add(_driverStatistics.GetDriverStatisticResults(driver.DriverID)); | ||
} | ||
|
||
return driverStatisticResults; | ||
} | ||
|
||
// GET api/analysisapi/5 | ||
[HttpGet("{id}")] | ||
public DriverStatisticResults Get(int id) | ||
{ | ||
_driverStatistics.ComputeDriverStatistics(id); | ||
return _driverStatistics.GetDriverStatisticResults(id); | ||
} | ||
|
||
// GET api/analysisapi/logistic/5 | ||
[HttpGet("logistic/{id}")] | ||
public LogisticFarePredictionResult GetLogistic(int id) | ||
{ | ||
FarePrediction farePrediction = new FarePrediction(_context, id); | ||
DateTime fromDateTime = DateTime.Now.AddMonths(-12); | ||
DateTime toDateTime = DateTime.Now; | ||
farePrediction.LearnFromDates(fromDateTime, toDateTime); | ||
|
||
LogisticFarePredictionResult result = new LogisticFarePredictionResult(); | ||
result.DriverID = id; | ||
result.FromDateTime = fromDateTime; | ||
result.ToDateTime = toDateTime; | ||
result.RegressionResult = farePrediction.GetRegressionModel(); | ||
|
||
return result; | ||
} | ||
|
||
// GET api/analysisapi/multipickupprob/5/6/12/13.7/ | ||
[HttpGet("multipickupprob/{id}/{delay}/{duration}/{fare}/")] | ||
public double[] GetMultiPickupProb(int id, double delay, double duration, double fare) { | ||
FarePrediction farePrediction = new FarePrediction(_context, id); | ||
DateTime fromDateTime = DateTime.Now.AddMonths(-12); | ||
DateTime toDateTime = DateTime.Now; | ||
farePrediction.LearnFromDates(fromDateTime, toDateTime); | ||
|
||
LogisticRegression regression = farePrediction.GetRegressionModel(); | ||
return regression?.Probabilities(new double[] { delay, duration, fare }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.