Skip to content

Commit

Permalink
For #234
Browse files Browse the repository at this point in the history
Disables pylint checkers: fixme,no-name-in-module,use-dict-literal and redefined-outer-name
- fixes the lint issues in test_date_utils
- removes lint issues for test_notion
- cleans out the easy from notion.py
- resolves most lint issues in supplementary_data
- cleans some lint in tempo_data
  • Loading branch information
drBosse committed Feb 3, 2024
1 parent 4fee502 commit 934c9b6
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 83 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ black:
## uses isort to check the import declarations
.PHONY: isort
isort:
$(PIP_INSTALL) isort > black-install.log
$(PIP_INSTALL) isort > isort-install.log
isort . -c --diff

## mypy
Expand All @@ -84,9 +84,15 @@ mypy:
## uses pylint to lint the files
##
.PHONY: pylint
LINT_DISABLE := --disable=fixme,no-name-in-module,redefined-outer-name,use-dict-literal
ifeq (true, $(CI))
EXIT_ZERO := --exit-zero
else
EXIT_ZERO :=
endif
pylint:
$(PIP_INSTALL) pylint > pylint-install.log
pylint --recursive=y --exit-zero .
pylint --recursive=y $(EXIT_ZERO) $(LINT_DISABLE) .

## lint:
## a PHONY rule to run all linters
Expand Down
4 changes: 4 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
The metrics dash app with data from tempo
"""

import os

from dash import Dash, html
Expand Down
44 changes: 23 additions & 21 deletions metrics/index.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""System module."""
"""The metrics module."""

import logging
import os
from datetime import date, datetime, timedelta
from datetime import date, datetime

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from dash import dcc, html

from metrics.date_utils import lastMonthDay, lookBack
from metrics.date_utils import lookBack
from metrics.notion import OKR, Allocations, Crew, Financials, WorkingHours
from metrics.supplementary_data import SupplementaryData
# fmt: off
Expand All @@ -33,7 +33,7 @@
if TEMPO_LOG_LEVEL in ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]:
logging.basicConfig(level=logging.getLevelName(TEMPO_LOG_LEVEL))
else:
logging.warning(f"{TEMPO_LOG_LEVEL} is not a valid log level, using default: WARNING")
logging.warning("%s {} is not a valid log level, using default: WARNING", TEMPO_LOG_LEVEL)

NOTION_KEY = os.environ.get("NOTION_KEY", "")
NOTION_OKR_DATABASE_ID = os.environ.get("NOTION_OKR_DATABASE_ID", "")
Expand Down Expand Up @@ -87,7 +87,7 @@ def normaliseUserRolling7(frame, working_hours):
if not working_hours.empty:
for _, row in working_hours.iterrows():
if row["Daily"] != float(TEMPO_DAILY_HOURS):
logging.debug(row["User"], row["Daily"])
logging.debug("%s %s", row["User"], row["Daily"])
if row["Start"] != "*":
first_date = row["Start"]
if row["Stop"] != "*":
Expand Down Expand Up @@ -127,12 +127,12 @@ def tableHeight(table, base_height=208):
# =========================================================

start = datetime.now()
logging.info(f"Starting {start}")
logging.info("Starting %s", start)


def delta(txt):
delta = datetime.now() - start
logging.info(f"{delta} : {txt}")
logging.info("%s: %s", delta, txt)


# ---------------------------------------------------------
Expand Down Expand Up @@ -205,7 +205,7 @@ def delta(txt):
table_working_hours = data.tableByUser(supplementary_data.working_hours, tableHeight, COLOR_HEAD, COLOR_ONE)
delta("Table Working Hours")
last_reported = pd.to_datetime(min(data.byUser(supplementary_data.working_hours)["Last"]))
logging.info(f"Last common day: {last_reported}")
logging.info("Last common day: %s", last_reported)

if not supplementary_data.working_hours.empty:
df_user_time_rolling = data.userRolling7(["Billable", "Internal"])
Expand Down Expand Up @@ -465,7 +465,7 @@ def figureFinancialTotal(year=None):
monthly_sum_cost = monthly_result.rolling(12, min_periods=1)["External_cost"].sum()
monthly_sum_in = monthly_result.rolling(12, min_periods=1)["Real_income"].sum()
monthly_result["Result"] = monthly_sum_in - monthly_sum_cost
logging.debug(monthly_result)
logging.debug("%s", monthly_result)

figure_rolling_total.add_trace(
go.Scatter(
Expand Down Expand Up @@ -562,24 +562,26 @@ def figureAllocations(allocations_df):

# Define a function to determine color based on conditions
def determine_color(row):
result = ""
if row["Unconfirmed"]:
if row["Allocation"] < 0.4:
return "Unconfirmed (< 40%)"
result = "Unconfirmed (< 40%)"
elif row["Allocation"] > 0.8:
return "Unconfirmed (> 80%)"
return "Unconfirmed"
result = "Unconfirmed (> 80%)"
result = "Unconfirmed"
elif "?" in row["JiraID"]:
if row["Allocation"] < 0.4:
return "Missing Jira (< 40%)"
result = "Missing Jira (< 40%)"
elif row["Allocation"] > 0.8:
return "Missing Jira (> 80%)"
return "Missing Jira"
result = "Missing Jira (> 80%)"
result = "Missing Jira"
elif row["Allocation"] < 0.4:
return "Less than 40%"
result = "Less than 40%"
elif row["Allocation"] > 0.8:
return "More than 80%"
result = "More than 80%"
else:
return "OK"
result = "OK"
return result

color_mapping = {
"Unconfirmed": "red",
Expand Down Expand Up @@ -692,12 +694,12 @@ def figureRunway(
start = pd.Timestamp(row["Start"])
stop = pd.Timestamp(row["Stop"])
task_id = row["JiraID"]
if stop != None and stop < start_date:
if stop is not None and stop < start_date:
continue
if start == None or start < invoiced_cutoff:
if start is None or start < invoiced_cutoff:
start = invoiced_cutoff

if stop == None:
if stop is None:
stop = max_enddate

# I'm sure there's a nice clever one-liner to do this. I'm apparently not that clever.
Expand Down
34 changes: 12 additions & 22 deletions metrics/notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ def fetch_data(self, database_id) -> requests.Response:


class WorkingHours(Notion):
"The class for working hour handling"
data: pd.DataFrame

def __init__(self, token: Optional[str] = None, database_id: str = "") -> None:
super().__init__(token, database_id)

def get_workinghours(self) -> None:
result_dict = self.fetch_data(self.database_id).json()
data = pd.DataFrame(columns=["User", "Daily", "Delta", "Start", "Stop"])
Expand All @@ -58,11 +56,9 @@ def get_workinghours(self) -> None:


class Allocations(Notion):
"The class for allocations"
data: pd.DataFrame

def __init__(self, token: Optional[str] = None, database_id: str = "") -> None:
super().__init__(token, database_id)

def get_allocations(self) -> None:
result_dict = self.fetch_data(self.database_id).json()
data = pd.DataFrame(columns=["User", "Allocation", "Start", "Stop", "Unconfirmed", "JiraID"])
Expand All @@ -86,11 +82,9 @@ def get_allocations(self) -> None:


class Crew(Notion):
"The class for crew data"
data: pd.DataFrame

def __init__(self, token: Optional[str] = None, database_id: str = "") -> None:
super().__init__(token, database_id)

def get_crew(self) -> None:
result_dict = self.fetch_data(self.database_id).json()
data = pd.DataFrame(columns=["User", "Role", "Hours", "Total cost"])
Expand All @@ -109,11 +103,9 @@ def get_crew(self) -> None:


class Financials(Notion):
"The class for finance data"
data: pd.DataFrame

def __init__(self, token: Optional[str] = None, database_id: str = "") -> None:
super().__init__(token, database_id)

def get_financials(self) -> None:
result_dict = self.fetch_data(self.database_id).json()

Expand All @@ -127,15 +119,15 @@ def get_financials(self) -> None:
sekstart = item["properties"]["SEK Start"]["number"]
eurstart = item["properties"]["EUR Start"]["number"]
start = 0
if sekstart != None:
if sekstart is not None:
start += sekstart / EUR2SEK
if eurstart != None:
if eurstart is not None:
start += eurstart

abcost = item["properties"]["AB-Cost"]["number"]
oycost = item["properties"]["OY-Cost"]["number"]

if oycost != None and abcost != None:
if oycost is not None and abcost is not None:
data.loc[-1] = [month, extcost, income, start]
data.index = data.index + 1

Expand Down Expand Up @@ -163,15 +155,13 @@ def get_financials(self) -> None:
self.data.index = self.data.index + 1
current_finances = 0

logging.debug(f"Financial data\n{self.data}")
logging.debug("Financial data\n%s", self.data)


class OKR(Notion):
"The class for OKR's"
data: pd.DataFrame

def __init__(self, token: Optional[str] = None, database_id: str = "") -> None:
super().__init__(token, database_id)

def get_okr(self) -> None:
result_dict = self.fetch_data(self.database_id).json()

Expand All @@ -196,7 +186,7 @@ def get_okr(self) -> None:
data.loc[-1] = [title, current_value, target_value, objective, assignee, period, scope, notes]
data.index = data.index + 1
self.data = data.sort_index()
logging.debug(f"OKR data\n{self.data}")
logging.debug("OKR data\n%s", self.data)

def get_figure_key_result(self, search_period=None) -> px.bar:
keyresults = self.data[self.data["scope"] == "Company"]
Expand All @@ -206,7 +196,7 @@ def get_figure_key_result(self, search_period=None) -> px.bar:
fig_title = f"{fig_title} {search_period}"
keyresults = keyresults[keyresults["period"].isin([search_period])]

logging.debug(f"Key Results\n{keyresults}")
logging.debug("Key Results\n%s", keyresults)

keyresults["Progress (%)"] = keyresults["current_value"] / keyresults["target_value"] * 100

Expand Down Expand Up @@ -234,7 +224,7 @@ def get_figure_initiatives(
fig_title = f"{fig_title} {search_period}"
initiatives = initiatives[initiatives["period"].isin([search_period])]

logging.debug(f"Initiatives\n{initiatives}")
logging.debug("Initiatives\n%s", initiatives)

initiatives = initiatives.drop(
["current_value", "target_value", "objective", "scope", "period"], axis="columns"
Expand Down
9 changes: 5 additions & 4 deletions metrics/supplementary_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def load(self, users: pd.Series) -> None:
else:
self.costs = self.financials
self.raw_costs = splitMonthTable(self.costs)
logging.debug(f"Raw costs\n{self.raw_costs}")
logging.debug("Raw costs\n%s", self.raw_costs)
self.costs.index.name = "Month"
self.costs.index = self.costs["Month"]
self.costs.index = self.costs.index.map(str)
Expand All @@ -66,11 +66,12 @@ def load(self, users: pd.Series) -> None:
logging.info("Loaded financials")

if not os.path.exists(self.rates_path):
logging.warning("Rates file path does not exist: " + self.rates_path)
logging.warning("Rates file path does not exist: %s", self.rates_path)
else:
rates_data = json.load(open(self.rates_path))
with open(self.rates_path, "r", encoding="utf8") as rates_file:
rates_data = json.load(rates_file)
self.rates = pd.json_normalize(rates_data, record_path="Default")
logging.info("Loaded " + self.rates_path)
logging.info("Loaded %s", self.rates_path)

self.rates["User"] = [users.values.tolist() for _ in range(len(self.rates))]
self.rates = self.rates.explode("User")
Expand Down
6 changes: 3 additions & 3 deletions metrics/tempo_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def byUser(self, working_hours: pd.DataFrame) -> pd.DataFrame:
user_data["Trend"] = [
last_week - 5 * daily for daily, last_week in zip(user_data["Daily"], user_data["Last 7 days"])
]
logging.debug("\n" + user_data.to_string())
logging.debug("\n%s", user_data.to_string())
user_data = user_data.drop(["Daily", "Start", "Stop", "First", "Days", "Expected", "Total"], axis="columns")

else:
Expand Down Expand Up @@ -278,7 +278,7 @@ def padTheData(self, working_hours: pd.DataFrame) -> None:
"""
self.padded_data = self.data
if not working_hours.empty:
for index, row in working_hours.iterrows():
for _, row in working_hours.iterrows():
df_user = pd.DataFrame()
user = row["User"]
if row["Start"] == "*":
Expand Down Expand Up @@ -369,6 +369,6 @@ def zeroOutBillableTime(self, keys: pd.DataFrame) -> None:
"""
if not keys.empty:
for key in keys["Key"]:
logging.debug("Internal Key: " + key)
logging.debug("Internal Key: %s", key)
self.data.loc[self.data["Group"] == key, ("Billable")] = 0
self.data.loc[self.data["Group"] == key, ("Internal")] = self.data[self.data["Group"] == key]["Time"]
18 changes: 5 additions & 13 deletions tests/test_date_utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import importlib.util
import sys
import unittest


def load_module(file_name, module_name):
spec = importlib.util.spec_from_file_location(module_name, file_name)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
"""
Simple tests for the date_utils
"""

import unittest

load_module("metrics/date_utils.py", "date_utils")
from date_utils import *
from metrics.date_utils import lastMonthDay, leapYear, lookAhead, weekdays


class TestWeekdays(unittest.TestCase):
Expand Down
42 changes: 24 additions & 18 deletions tests/test_notion.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import os
import unittest

import metrics.notion as notion


class TestNotion(unittest.TestCase):
def test_fetch_data(self):
os.environ.pop("NOTION_KEY", None)

with self.assertRaises(SystemExit) as cm:
notion.OKR().get_okr()

self.assertEqual(cm.exception.code, "Notion token not provided or NOTION_KEY not set")


if __name__ == "__main__":
unittest.main()
"""
Unit tests for the notion class
"""

import os
import unittest

from metrics import notion


class TestNotion(unittest.TestCase):
"the unit tests"

def test_fetch_data(self):
os.environ.pop("NOTION_KEY", None)

with self.assertRaises(SystemExit) as cm:
notion.OKR().get_okr()

self.assertEqual(cm.exception.code, "Notion token not provided or NOTION_KEY not set")


if __name__ == "__main__":
unittest.main()

0 comments on commit 934c9b6

Please sign in to comment.