Skip to content

Commit

Permalink
Merge pull request #164 from deNBI/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dweinholz authored Jun 24, 2022
2 parents aec6d3c + 8c44e53 commit 664c123
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
20 changes: 10 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ psycopg2 = "2.9.3"
aiohttp-devtools = "1.0.post0"
pre-commit = "2.18.1"
black = {version = "22.3.0",allow-prereleases = true}
sphinx-autodoc-typehints = "1.17.0"
sphinx = "5.0.1"
sphinx-autodoc-typehints = "1.18.3"
sphinx = "5.0.2"
sphinxcontrib-trio = "1.1.2"
lxml = "4.8.0"
sphinxcontrib-programoutput = "0.17"
Expand Down
29 changes: 19 additions & 10 deletions src/os_credits/db_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from os_credits.settings import config
from os_credits.db_client.model import Base, MetricCredits, Project, Credits, PromCatalogReflected, PromMetricReflected, \
make_measurement_class, Metric, Label, \
BaseMeasurement
BaseMeasurement, LimitType
from os_credits.log import timescaledb_logger


Expand Down Expand Up @@ -258,6 +258,9 @@ async def get_granted_credits_for_project(self, project: Project, session):
half_limit = project.granted_credits / 2.0
if project.used_credits < half_limit:
project.half_limit_reached_send = False
if project.full_limit_reached_send:
if project.used_credits < project.granted_credits:
project.full_limit_reached_send = False
await session.flush()
else:
timescaledb_logger.warning(f"Could not get granted credits for {project}")
Expand All @@ -266,31 +269,37 @@ async def get_granted_credits_for_project(self, project: Project, session):
except Exception as e:
timescaledb_logger.exception(e)

async def inform_half_limit_reached(self, last_credits_entry, project, session):
async def inform_some_limit_reached(self, last_credits_entry, project, session, limit_type: LimitType):
timeout = aiohttp.ClientTimeout(total=10, connect=5)
data = {
"project_name": project.project_name,
"granted_credits": project.granted_credits,
"used_credits": last_credits_entry.used_credits,
"timestamp": datetime.timestamp(last_credits_entry.time)
"timestamp": datetime.timestamp(last_credits_entry.time),
"limit_type": limit_type
}
headers = {"X-Api-Key": config["API_CONTACT_KEY"]}
try:
timescaledb_logger.info(f"Sending information about half limit reached for {project} with {last_credits_entry}.")
timescaledb_logger.info(f"Sending information about {limit_type} for {project} with {last_credits_entry}.")
async with self.client_session.post(
f"{config['MAIL_CONTACT_URL']}",
f"{config['MAIL_CONTACT_URL']}/credits-limit/",
timeout=timeout,
data=data,
headers=headers
) as response:
if response.status == 200:
project.half_limit_reached_send = True
await session.flush()
timescaledb_logger.info(f"Information about half limit reached send for {project} with {last_credits_entry}.")
if limit_type is LimitType.HALF_LIMIT_REACHED:
project.half_limit_reached_send = True
await session.flush()
timescaledb_logger.info(f"Information about {limit_type} send for {project} with {last_credits_entry}.")
elif limit_type is LimitType.FULL_LIMIT_REACHED:
project.full_limit_reached_send = True
await session.flush()
timescaledb_logger.info(f"Information about {limit_type} send for {project} with {last_credits_entry}.")
else:
timescaledb_logger.warning(f"Could not send half limit reached mail for {project} with {last_credits_entry}")
timescaledb_logger.warning(f"Could not send {limit_type} mail for {project} with {last_credits_entry}")
except ClientConnectorError:
timescaledb_logger.debug(f"No connection possible to send half limit reached mail for {project} with {last_credits_entry}.")
timescaledb_logger.debug(f"No connection possible to send {limit_type} mail for {project} with {last_credits_entry}.")
except Exception as e:
timescaledb_logger.exception(e)

Expand Down
7 changes: 7 additions & 0 deletions src/os_credits/db_client/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

from sqlalchemy import Column, String, Boolean, event, DDL, func, BigInteger, ForeignKey, Float, text
from sqlalchemy.dialects.postgresql import TIMESTAMP
from sqlalchemy.ext.declarative import DeferredReflection
Expand Down Expand Up @@ -147,3 +149,8 @@ def _type(cls):
return BaseMeasurement.__class__.__name__

return Measurement


class LimitType(str, Enum):
HALF_LIMIT_REACHED = "half limit reached"
FULL_LIMIT_REACHED = "full limit reached"
5 changes: 4 additions & 1 deletion src/os_credits/db_client/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sqlalchemy.ext.asyncio import AsyncSession

from os_credits.db_client.client import TimescaleDBManager
from os_credits.db_client.model import LimitType
from os_credits.log import producer_logger, task_logger, TASK_ID
from os_credits.settings import config

Expand Down Expand Up @@ -172,7 +173,9 @@ async def compute_credits_by_label(
credits_value=(last_credits_entry.used_credits + credits_value)
)
if project.granted_credits > 0 and last_credits_entry.used_credits >= half_credits and not project.half_limit_reached_send:
await db_client.inform_half_limit_reached(last_credits_entry, project, session)
await db_client.inform_some_limit_reached(last_credits_entry, project, session, LimitType.HALF_LIMIT_REACHED)
if 0 < project.granted_credits <= last_credits_entry.used_credits and not project.full_limit_reached_send:
await db_client.inform_some_limit_reached(last_credits_entry, project, session, LimitType.FULL_LIMIT_REACHED)
task_logger.debug("Last metric now: {0}".format(last_metric_credits))
task_logger.debug("Last credits now: {0}".format(last_credits_entry))
metric_num += 1
Expand Down
1 change: 0 additions & 1 deletion src/os_credits/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import json
from collections import ChainMap
from collections import UserDict
from decimal import Decimal
from os import environ
from typing import Any, TypedDict
from typing import Dict
Expand Down

0 comments on commit 664c123

Please sign in to comment.