Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce log messages when no model found #869

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/Command/Predict.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace OCA\SuspiciousLogin\Command;

use OCA\SuspiciousLogin\Exception\ModelNotFoundException;
use OCA\SuspiciousLogin\Exception\ServiceException;
use OCA\SuspiciousLogin\Service\EstimatorService;
use OCA\SuspiciousLogin\Service\Ipv4Strategy;
use OCA\SuspiciousLogin\Service\IpV6Strategy;
Expand Down Expand Up @@ -55,14 +57,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$uid = $input->getArgument('uid');
$ip = $input->getArgument('ip');
$modelId = $input->getArgument('model');
if (!$this->estimatorService->predict(
$uid,
$ip,
$input->getOption('v6') ? new IpV6Strategy() : new Ipv4Strategy(),
$modelId ? (int)$modelId : null)) {
$output->writeln("WARN: IP $ip is suspicious");
return 1;

try {
if (!$this->estimatorService->predict(
$uid,
$ip,
$input->getOption('v6') ? new IpV6Strategy() : new Ipv4Strategy(),
$modelId ? (int)$modelId : null)) {
$output->writeln("WARN: IP $ip is suspicious");
return 1;
}
} catch (ModelNotFoundException $ex) {
$output->writeln('<error>Could not predict suspiciousness: ' . $ex->getMessage() . '</error>');
return 2;
} catch (ServiceException $ex) {
$output->writeln('<error>Could not predict suspiciousness: ' . $ex->getMessage() . '</error>');
return 3;
}

$output->writeln("OK: IP $ip is not suspicious");
return 0;
}
Expand Down
29 changes: 29 additions & 0 deletions lib/Exception/ModelNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/**
* @copyright 2024 Victor Lap <victorlap@outlook.com>
*
* @author 2024 Victor Lap <victorlap@outlook.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\SuspiciousLogin\Exception;

class ModelNotFoundException extends ServiceException {
}
2 changes: 2 additions & 0 deletions lib/Service/EstimatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\SuspiciousLogin\Service;

use OCA\SuspiciousLogin\Exception\ModelNotFoundException;
use OCA\SuspiciousLogin\Exception\ServiceException;
use OCP\ILogger;
use Rubix\ML\Datasets\Unlabeled;
Expand All @@ -34,6 +35,7 @@ public function __construct(ModelStore $modelStore,
* @param int|null $modelId
*
* @return bool
* @throws ModelNotFoundException
* @throws ServiceException
*/
public function predict(string $uid, string $ip, AClassificationStrategy $strategy, ?int $modelId = null): bool {
Expand Down
7 changes: 6 additions & 1 deletion lib/Service/LoginClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\SuspiciousLogin\Db\SuspiciousLogin;
use OCA\SuspiciousLogin\Db\SuspiciousLoginMapper;
use OCA\SuspiciousLogin\Event\SuspiciousLoginEvent;
use OCA\SuspiciousLogin\Exception\ModelNotFoundException;
use OCA\SuspiciousLogin\Exception\ServiceException;
use OCA\SuspiciousLogin\Util\AddressClassifier;
use OCP\AppFramework\Utility\ITimeFactory;
Expand Down Expand Up @@ -97,9 +98,13 @@ public function process(string $uid, string $ip) {
// All good, carry on!
return;
}
} catch (ModelNotFoundException $ex) {
$this->logger->debug('Could not predict suspiciousness: ' . $ex->getMessage());
// This most likely means there is no trained model yet, so we return early here
return;
} catch (ServiceException $ex) {
$this->logger->warning("Could not predict suspiciousness: " . $ex->getMessage());
// This most likely means there is no trained model yet, so we return early here
// There was an error loading the model, so we return early here
return;
}

Expand Down
13 changes: 9 additions & 4 deletions lib/Service/ModelStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use OCA\SuspiciousLogin\AppInfo\Application;
use OCA\SuspiciousLogin\Db\Model;
use OCA\SuspiciousLogin\Db\ModelMapper;
use OCA\SuspiciousLogin\Exception\ServiceException;
use OCA\SuspiciousLogin\Exception\ModelNotFoundException;
use OCP\App\IAppManager;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Files\IAppData;
Expand Down Expand Up @@ -67,14 +67,14 @@ public function __construct(ModelMapper $modelMapper,
/**
* @return Estimator
* @throws RuntimeException
* @throws ServiceException
* @throws ModelNotFoundException
*/
public function loadLatest(AClassificationStrategy $strategy): Estimator {
try {
$latestModel = $this->modelMapper->findLatest($strategy::getTypeName());
} catch (DoesNotExistException $e) {
$this->logger->debug("No models found. Can't load latest");
throw new ServiceException("No models found", 0, $e);
throw new ModelNotFoundException("No models found", 0, $e);
}
return $this->load($latestModel->getId());
}
Expand Down Expand Up @@ -102,6 +102,11 @@ private function cache(int $id, string $serialized): void {
$cache->set($this->getCacheKey($id), $serialized);
}

/**
* @return Estimator
* @throws RuntimeException
* @throws ModelNotFoundException
*/
public function load(int $id): Estimator {
$cached = $this->getCached($id);
if ($cached !== null) {
Expand All @@ -116,7 +121,7 @@ public function load(int $id): Estimator {
$modelFile = $modelsFolder->getFile((string)$id);
} catch (NotFoundException $e) {
$this->logger->error("Could not load classifier model $id: " . $e->getMessage());
throw new ServiceException("Could not load model $id", 0, $e);
throw new ModelNotFoundException("Could not load model $id", 0, $e);
}

$serialized = $modelFile->getContent();
Expand Down
Loading