diff options
Diffstat (limited to 'backend')
-rw-r--r-- | backend/api/api/Controllers/DatasetController.cs | 6 | ||||
-rw-r--r-- | backend/api/api/Controllers/FileController.cs | 6 | ||||
-rw-r--r-- | backend/api/api/Controllers/ModelController.cs | 18 | ||||
-rw-r--r-- | backend/api/api/Controllers/PredictorController.cs | 9 | ||||
-rw-r--r-- | backend/api/api/Controllers/UserController.cs | 6 | ||||
-rw-r--r-- | backend/api/api/Models/IJwtToken.cs | 12 | ||||
-rw-r--r-- | backend/api/api/Models/JwtToken.cs | 19 | ||||
-rw-r--r-- | backend/api/api/Program.cs | 2 | ||||
-rw-r--r-- | backend/api/api/Services/AuthService.cs | 6 | ||||
-rw-r--r-- | backend/api/api/Services/IModelService.cs | 1 | ||||
-rw-r--r-- | backend/api/api/Services/IUserService.cs | 2 | ||||
-rw-r--r-- | backend/api/api/Services/ModelService.cs | 5 | ||||
-rw-r--r-- | backend/api/api/Services/UserService.cs | 8 | ||||
-rw-r--r-- | backend/microservice/api/ml_service.py | 19 |
14 files changed, 83 insertions, 36 deletions
diff --git a/backend/api/api/Controllers/DatasetController.cs b/backend/api/api/Controllers/DatasetController.cs index d9803744..8a622138 100644 --- a/backend/api/api/Controllers/DatasetController.cs +++ b/backend/api/api/Controllers/DatasetController.cs @@ -14,12 +14,12 @@ namespace api.Controllers public class DatasetController : ControllerBase { private readonly IDatasetService _datasetService; - private JwtToken jwtToken; + private IJwtToken jwtToken; - public DatasetController(IDatasetService datasetService, IConfiguration configuration) + public DatasetController(IDatasetService datasetService, IConfiguration configuration,IJwtToken Token) { _datasetService = datasetService; - jwtToken = new JwtToken(configuration); + jwtToken = Token; } // GET: api/<DatasetController>/mydatasets diff --git a/backend/api/api/Controllers/FileController.cs b/backend/api/api/Controllers/FileController.cs index a6bab373..89b4e473 100644 --- a/backend/api/api/Controllers/FileController.cs +++ b/backend/api/api/Controllers/FileController.cs @@ -12,12 +12,12 @@ namespace api.Controllers { private string[] permittedExtensions = { ".csv" }; private readonly IConfiguration _configuration; - private JwtToken _token; + private IJwtToken _token; private IFileService _fileservice; - public FileController(IConfiguration configuration,IFileService fileService) + public FileController(IConfiguration configuration,IFileService fileService,IJwtToken token) { _configuration = configuration; - _token = new JwtToken(configuration); + _token = token; _fileservice = fileService; } diff --git a/backend/api/api/Controllers/ModelController.cs b/backend/api/api/Controllers/ModelController.cs index 355eb9f4..d8bc1515 100644 --- a/backend/api/api/Controllers/ModelController.cs +++ b/backend/api/api/Controllers/ModelController.cs @@ -17,16 +17,16 @@ namespace api.Controllers private readonly IDatasetService _datasetService; private readonly IFileService _fileService; private readonly IModelService _modelService; - private JwtToken jwtToken; + private IJwtToken jwtToken; - public ModelController(IMlConnectionService mlService, IModelService modelService, IDatasetService datasetService, IFileService fileService, IConfiguration configuration) + public ModelController(IMlConnectionService mlService, IModelService modelService, IDatasetService datasetService, IFileService fileService, IConfiguration configuration,IJwtToken token) { _mlService = mlService; _modelService = modelService; _datasetService = datasetService; _fileService = fileService; - jwtToken = new JwtToken(configuration); + jwtToken = token; } [HttpPost("sendModel")] @@ -129,8 +129,9 @@ namespace api.Controllers // POST api/<ModelController>/add [HttpPost("add")] [Authorize(Roles = "User,Guest")] - public ActionResult<Model> Post([FromBody] Model model) + public ActionResult<Model> Post([FromBody] Model model)//, bool overwrite) { + bool overwrite = false; //username="" ako je GUEST model.inputNeurons = model.inputColumns.Length; if (_modelService.CheckHyperparameters(model.inputNeurons, model.hiddenLayerNeurons, model.hiddenLayers, model.outputNeurons) == false) @@ -138,11 +139,16 @@ namespace api.Controllers var existingModel = _modelService.GetOneModel(model.username, model.name); - if (existingModel != null) + if (existingModel != null && !overwrite) return NotFound($"Model with name = {model.name} exisits"); else { - _modelService.Create(model); + if (existingModel == null) + _modelService.Create(model); + else + { + _modelService.Replace(model); + } return CreatedAtAction(nameof(Get), new { id = model._id }, model); } diff --git a/backend/api/api/Controllers/PredictorController.cs b/backend/api/api/Controllers/PredictorController.cs index 63c5d2bf..98e2695e 100644 --- a/backend/api/api/Controllers/PredictorController.cs +++ b/backend/api/api/Controllers/PredictorController.cs @@ -13,12 +13,12 @@ namespace api.Controllers public class PredictorController : Controller { private readonly IPredictorService _predictorService; - private JwtToken jwtToken; + private IJwtToken jwtToken; - public PredictorController(IPredictorService predictorService, IConfiguration configuration) + public PredictorController(IPredictorService predictorService, IConfiguration configuration, IJwtToken Token) { _predictorService = predictorService; - jwtToken = new JwtToken(configuration); + jwtToken = Token; } // GET: api/<PredictorController>/mypredictors @@ -74,8 +74,7 @@ namespace api.Controllers return _predictorService.SearchPredictors(name, username); } - //SEARCH za predictore (public ili private sa ovim imenom ) - // GET api/<PredictorController>/search/{name} + // GET api/<PredictorController>/{name} [HttpGet("{id}")] [Authorize(Roles = "User")] public ActionResult<Predictor> GetPredictor(string id) diff --git a/backend/api/api/Controllers/UserController.cs b/backend/api/api/Controllers/UserController.cs index 741382b8..782a02cf 100644 --- a/backend/api/api/Controllers/UserController.cs +++ b/backend/api/api/Controllers/UserController.cs @@ -15,12 +15,12 @@ namespace api.Controllers public class UserController : ControllerBase { private readonly IUserService userService; - private JwtToken jwtToken; + private IJwtToken jwtToken; - public UserController(IUserService userService, IConfiguration configuration) + public UserController(IUserService userService, IConfiguration configuration,IJwtToken token) { this.userService = userService; - jwtToken = new JwtToken(configuration); + jwtToken = token; } // GET: api/<UserController> diff --git a/backend/api/api/Models/IJwtToken.cs b/backend/api/api/Models/IJwtToken.cs new file mode 100644 index 00000000..da71f7ec --- /dev/null +++ b/backend/api/api/Models/IJwtToken.cs @@ -0,0 +1,12 @@ +using api.Models.Users; + +namespace api.Models +{ + public interface IJwtToken + { + string GenGuestToken(); + string GenToken(AuthRequest user); + string RenewToken(string existingToken); + string TokenToUsername(string token); + } +}
\ No newline at end of file diff --git a/backend/api/api/Models/JwtToken.cs b/backend/api/api/Models/JwtToken.cs index f262fd23..29f4bafc 100644 --- a/backend/api/api/Models/JwtToken.cs +++ b/backend/api/api/Models/JwtToken.cs @@ -2,27 +2,33 @@ using System.Security.Claims; using System.Text; using api.Models.Users; +using api.Services; using Microsoft.IdentityModel.Tokens; namespace api.Models { - public class JwtToken + public class JwtToken : IJwtToken { private readonly IConfiguration _configuration; + private readonly IUserService _userService; - public JwtToken(IConfiguration configuration) + public JwtToken(IConfiguration configuration, IUserService userService) { _configuration = configuration; + _userService = userService; + } - + public string GenToken(AuthRequest user) { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); + var fullUser = _userService.GetUserByUsername(user.UserName); var tokenDescriptor = new SecurityTokenDescriptor { - Subject = new ClaimsIdentity(new[] { new Claim("name", user.UserName), - new Claim("role", "User")}), + Subject = new ClaimsIdentity(new[] { new Claim("name", fullUser.Username), + new Claim("role", "User"), + new Claim("id",fullUser._id)}), Expires = DateTime.UtcNow.AddMinutes(20), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; @@ -76,7 +82,8 @@ namespace api.Models var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new[] { new Claim("name",""), - new Claim("role", "Guest")}), + new Claim("role", "Guest"), + new Claim("id","")}), Expires = DateTime.UtcNow.AddMinutes(20), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; diff --git a/backend/api/api/Program.cs b/backend/api/api/Program.cs index 5913c2d3..2bb97e45 100644 --- a/backend/api/api/Program.cs +++ b/backend/api/api/Program.cs @@ -1,6 +1,7 @@ using System.Text; using api.Data; using api.Interfaces; +using api.Models; using api.Services; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.Options; @@ -32,6 +33,7 @@ builder.Services.AddScoped<IMlConnectionService, MlConnectionService>(); builder.Services.AddScoped<IModelService, ModelService>(); builder.Services.AddScoped<IPredictorService, PredictorService>(); builder.Services.AddScoped<IFileService, FileService>(); +builder.Services.AddScoped<IJwtToken, JwtToken>(); var mlwss = new MLWebSocketService(); diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs index a646cc9d..c7161dee 100644 --- a/backend/api/api/Services/AuthService.cs +++ b/backend/api/api/Services/AuthService.cs @@ -8,13 +8,13 @@ namespace api.Services { public class AuthService : IAuthService { - private JwtToken _jwt; + private IJwtToken _jwt; private readonly IConfiguration _configuration; private readonly IMongoCollection<User> _users; - public AuthService(IConfiguration configuration, IUserStoreDatabaseSettings settings, IMongoClient mongoClient) + public AuthService(IConfiguration configuration, IUserStoreDatabaseSettings settings, IMongoClient mongoClient,IJwtToken jwt) { _configuration = configuration; - _jwt = new JwtToken(_configuration); + _jwt = jwt; var database = mongoClient.GetDatabase(settings.DatabaseName); _users = database.GetCollection<User>(settings.CollectionName); } diff --git a/backend/api/api/Services/IModelService.cs b/backend/api/api/Services/IModelService.cs index 637d09a3..3e70a1c3 100644 --- a/backend/api/api/Services/IModelService.cs +++ b/backend/api/api/Services/IModelService.cs @@ -11,6 +11,7 @@ namespace api.Services List<Model> GetLatestModels(string username); //List<Model> GetPublicModels(); Model Create(Model model); + Model Replace(Model model); void Update(string username, string name, Model model); void Delete(string username, string name); bool CheckHyperparameters(int inputNeurons, int hiddenLayerNeurons, int hiddenLayers, int outputNeurons); diff --git a/backend/api/api/Services/IUserService.cs b/backend/api/api/Services/IUserService.cs index e4a23213..d34d410a 100644 --- a/backend/api/api/Services/IUserService.cs +++ b/backend/api/api/Services/IUserService.cs @@ -10,5 +10,7 @@ namespace api.Services User Create(User user); // kreira korisnika bool Update(string username, User user); //apdejtuje korisnika po idu void Delete(string username);//brise korisnika + public User GetUserByUsername(string username);//Uzima jednog korisnika po username-u + public User GetUserById(string id);//Uzima jednog korisnika po id-u } } diff --git a/backend/api/api/Services/ModelService.cs b/backend/api/api/Services/ModelService.cs index eae8c78b..c2b4e692 100644 --- a/backend/api/api/Services/ModelService.cs +++ b/backend/api/api/Services/ModelService.cs @@ -20,6 +20,11 @@ namespace api.Services _model.InsertOne(model); return model; } + public Model Replace(Model model) + { + _model.ReplaceOne(m => m._id == model._id, model); + return model; + } public void Delete(string username, string name) { diff --git a/backend/api/api/Services/UserService.cs b/backend/api/api/Services/UserService.cs index 607bb04b..39b3a8d3 100644 --- a/backend/api/api/Services/UserService.cs +++ b/backend/api/api/Services/UserService.cs @@ -33,6 +33,14 @@ namespace api.Services { return _users.Find(user => true).ToList(); } + public User GetUserByUsername(string username) + { + return _users.Find(user=>user.Username == username).FirstOrDefault(); + } + public User GetUserById(string id) + { + return _users.Find(user => user._id == id).FirstOrDefault(); + } public User GetUserUsername(string username) { return _users.Find(user => user.Username == username).FirstOrDefault(); diff --git a/backend/microservice/api/ml_service.py b/backend/microservice/api/ml_service.py index efd24fdc..68595a89 100644 --- a/backend/microservice/api/ml_service.py +++ b/backend/microservice/api/ml_service.py @@ -34,6 +34,7 @@ class TrainingResult: tpr: float def train(dataset, params, callback): + problem_type = params["type"] data = pd.DataFrame() for col in params["inputColumns"]: data[col]=dataset[col] @@ -123,13 +124,17 @@ def train(dataset, params, callback): # # Test # - y_pred=classifier.predict(x_test) - y_pred=(y_pred>=0.5).astype('int') - #y_pred=(y_pred * 100).astype('int') - y_pred=y_pred.flatten() - result=pd.DataFrame({"Actual":y_test,"Predicted":y_pred}) - model_name = params['_id'] - classifier.save("temp/"+model_name, save_format='h5') + if(problem_type == "regresioni"): + classifier.evaluate(x_test, y_test) + classifier.save("temp/"+model_name, save_format='h5') + elif(problem_type == "binarni-klasifikacioni"): + y_pred=classifier.predict(x_test) + y_pred=(y_pred>=0.5).astype('int') + #y_pred=(y_pred * 100).astype('int') + y_pred=y_pred.flatten() + result=pd.DataFrame({"Actual":y_test,"Predicted":y_pred}) + model_name = params['_id'] + classifier.save("temp/"+model_name, save_format='h5') # # Metrike # |