Skip to content
Merged
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
23 changes: 0 additions & 23 deletions .github/workflows/lint.yml

This file was deleted.

10 changes: 10 additions & 0 deletions .github/workflows/linters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Linters

on:
push:
branches: ["master"]
pull_request:

jobs:
lint:
uses: RedHatInsights/processing-tools/.github/workflows/linters.yaml@v0.1.0
96 changes: 54 additions & 42 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,44 +1,56 @@
exclude: "doc|research|demos"
repos:
- repo: https://github.com/psf/black
rev: 26.3.1
hooks:
- id: black
args: [--safe, --quiet, --line-length, "100"]
language_version: python3
require_serial: true
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: trailing-whitespace
language_version: python3
- id: end-of-file-fixer
language_version: python3
- id: check-yaml
language_version: python3
- id: debug-statements
language_version: python3
- repo: https://github.com/asottile/pyupgrade
rev: v3.21.2
hooks:
- id: pyupgrade
language_version: python3
- repo: https://github.com/PyCQA/flake8
rev: 7.3.0
hooks:
- id: flake8
language_version: python3
args: [--extend-ignore=E501]
- repo: https://gitlab.com/pycqa/pydocstyle
rev: 6.1.1
hooks:
- id: pydocstyle
language_version: python3
# - repo: local
# hooks:
# - id: pylint
# name: pylint
# entry: pylint
# language: system
# types: [python]
# args: [--disable=fixme]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-json
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: mixed-line-ending
- id: check-ast
- id: check-merge-conflict
- id: check-added-large-files

- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.11.0.1
hooks:
- id: shellcheck
# Exclude checks that are commonly problematic in organization scripts
# SC1090: Can't follow non-constant source (dynamic sourcing)
# SC2086: Double quote to prevent globbing (intentional word splitting)
# SC2034: Variable appears unused (used by sourced scripts)
# SC1091: Not following included files (dynamic includes)
args: ['--exclude=SC1090,SC2086,SC2034,SC1091']

- repo: https://github.com/golangci/golangci-lint
rev: v2.11.3
hooks:
- id: golangci-lint-config-verify
- id: golangci-lint-full

- repo: https://github.com/RedHatInsights/processing-tools
rev: v0.1.0
hooks:
- id: abcgo
args: ['--threshold=64']

- id: go-version-consistency

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.15.5
hooks:
# Adding a set of rules to ruff check to match pyupgrade rules
- id: ruff-check
args:
- '--fix'
- '--line-length=100'
- '--select=UP,F632,E,W,F,I,UP,N,B,C4,SIM,D'
- id: ruff-format

# - repo: https://github.com/AleksaC/hadolint-py
# rev: v2.14.0
# hooks:
# - id: hadolint
6 changes: 5 additions & 1 deletion ccx_upgrades_inference/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
"severity": "critical",
}

EXAMPLE_FOC = {"name": "authentication", "condition": "Degraded", "reason": "AsExpected"}
EXAMPLE_FOC = {
"name": "authentication",
"condition": "Degraded",
"reason": "AsExpected",
}
19 changes: 10 additions & 9 deletions ccx_upgrades_inference/inference.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""
This module contains the definition of the Machine Learning/static predictors.
"""Module containing the definition of the Machine Learning/static predictors.

They are used for calculating the risk of an upgrade failure.
"""

from ccx_upgrades_inference.models import UpgradeRisksPredictors, Alert, FOC
from ccx_upgrades_inference.models import FOC, Alert, UpgradeRisksPredictors

EXCLUDE_NAMESPACES = [
"openshift-cnv",
Expand All @@ -25,7 +24,7 @@


class StaticPredictor:
"""This predictor uses basic filters to detect risks."""
"""Predictor that uses basic filters to detect risks."""

def filter_alert(self, alert: Alert) -> bool:
"""Return True if the alert matches any of the conditions."""
Expand All @@ -43,19 +42,21 @@ def filter_foc(self, foc: FOC) -> bool:
return foc.condition in ["Not Available", "Degraded"]

def predict(self, risks: UpgradeRisksPredictors) -> UpgradeRisksPredictors:
"""
Filter the `risks` with the alerts and FOCs queries.
"""Filter the `risks` with the alerts and FOCs queries.

Return those elements that are likely to make the upgrade fail.
"""
suspicious_alerts = [alert for alert in risks.alerts if self.filter_alert(alert)]
suspicious_alerts = [
alert for alert in risks.alerts if self.filter_alert(alert)
]
suspicious_operators_conditions = [
foc for foc in risks.operator_conditions if self.filter_foc(foc)
]

if len(suspicious_alerts) < 2:
suspicious_alerts = list()
suspicious_alerts = []

return UpgradeRisksPredictors(
alerts=suspicious_alerts, operator_conditions=suspicious_operators_conditions
alerts=suspicious_alerts,
operator_conditions=suspicious_operators_conditions,
)
12 changes: 6 additions & 6 deletions ccx_upgrades_inference/logging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Utility functions to redirect logs to cloudwatch.
"""Utility functions to redirect logs to cloudwatch."""

Copied from https://github.com/RedHatInsights/insights-ccx-messaging/blob/main/ccx_messaging/utils/logging.py # noqa: E501
"""
# Copied from https://github.com/RedHatInsights/insights-ccx-messaging/blob/main/ccx_messaging/utils/logging.py # noqa: E501

import os
import logging
import os

from boto3.session import Session
from watchtower import CloudWatchLogHandler
Expand All @@ -35,6 +33,7 @@ def __new__(self):
logging.NullHandler: if the hanlder couldn't be configured.
or
watchtower.CloudWatchLogHandler: if it could be configured.

"""
enabled = os.getenv("LOGGING_TO_CW_ENABLED", "False").lower()
if enabled not in ("true", "1", "t", "yes"):
Expand All @@ -51,7 +50,8 @@ def __new__(self):
)
missing_envs = list(
filter(
lambda key: os.environ.get(key, "").strip() == "", [key for key in aws_config_vars]
lambda key: os.environ.get(key, "").strip() == "",
aws_config_vars,
)
)

Expand Down
11 changes: 6 additions & 5 deletions ccx_upgrades_inference/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"""Definition of the REST API for the inference service."""

import os
from contextlib import asynccontextmanager

from fastapi import FastAPI
from contextlib import asynccontextmanager
from prometheus_fastapi_instrumentator import Instrumentator

from ccx_upgrades_inference.models import UpgradeApiResponse, UpgradeRisksPredictors
from ccx_upgrades_inference.inference import StaticPredictor
from ccx_upgrades_inference.models import UpgradeApiResponse, UpgradeRisksPredictors
from ccx_upgrades_inference.sentry import init_sentry

from prometheus_fastapi_instrumentator import Instrumentator

init_sentry(os.environ.get("SENTRY_DSN", None), None, os.environ.get("SENTRY_ENVIRONMENT", None))
init_sentry(
os.environ.get("SENTRY_DSN", None), None, os.environ.get("SENTRY_ENVIRONMENT", None)
)


def create_lifespan_handler(instrumentator: Instrumentator):
Expand Down
18 changes: 10 additions & 8 deletions ccx_upgrades_inference/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
"""Models to be used in the REST API."""

from typing import List, Optional
from pydantic import BaseModel # pylint: disable=no-name-in-module

from ccx_upgrades_inference.examples import EXAMPLE_ALERT, EXAMPLE_FOC, EXAMPLE_PREDICTORS
from ccx_upgrades_inference.examples import (
EXAMPLE_ALERT,
EXAMPLE_FOC,
EXAMPLE_PREDICTORS,
)


class Alert(BaseModel): # pylint: disable=too-few-public-methods
"""Alert containing name, namespace and severity."""

name: str
namespace: Optional[str] = None
namespace: str | None = None
severity: str

class Config: # pylint: disable=too-few-public-methods
Expand All @@ -24,7 +27,7 @@ class FOC(BaseModel): # pylint: disable=too-few-public-methods

name: str
condition: str
reason: Optional[str] = None
reason: str | None = None

class Config: # pylint: disable=too-few-public-methods
"""Update the configuration with an example."""
Expand All @@ -35,13 +38,12 @@ class Config: # pylint: disable=too-few-public-methods
class UpgradeRisksPredictors(BaseModel):
"""A dict containing list of alerts and FOCs."""

alerts: List[Alert]
operator_conditions: List[FOC]
alerts: list[Alert]
operator_conditions: list[FOC]


class UpgradeApiResponse(BaseModel): # pylint: disable=too-few-public-methods
"""
UpgradeApiResponse is the response for the upgrade-risks-prediction endpoint.
"""UpgradeApiResponse is the response for the upgrade-risks-prediction endpoint.

Contains the predictors that the model detected as actual risks.
"""
Expand Down
4 changes: 3 additions & 1 deletion ccx_upgrades_inference/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def init_sentry(dsn=None, transport=None, environment=None):
"""Configure and initialize sentry SDK for this project."""
if dsn:
logging.getLogger(__name__).info("Initializing sentry")
sentry_logging = LoggingIntegration(level=logging.INFO, event_level=get_event_level())
sentry_logging = LoggingIntegration(
level=logging.INFO, event_level=get_event_level()
)

sentry_sdk.init(
dsn=dsn,
Expand Down
Loading
Loading