Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #192 from cisagov/DJ_add_trending
Browse files Browse the repository at this point in the history
1 - Add Trending to report
  • Loading branch information
cduhn17 committed May 10, 2022
2 parents c926100 + 3db59d0 commit ee68ece
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 149 deletions.
75 changes: 48 additions & 27 deletions src/pe_reports/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,22 @@ def stacked_bar(self):
name = self.name
color = ["#1357BE", "#D0342C"]
df.plot(kind="bar", stacked=True, zorder=3, color=color)
# Add title to chart
plt.title(title, pad=15, fontsize=10)
# Format chart's axis
plt.xlabel(x_label, labelpad=10, fontdict={"size": 8})
plt.ylabel(y_label, labelpad=10, fontdict={"size": 8})
plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
plt.rc("axes", axisbelow=True)
plt.grid(axis="y", zorder=0)
plt.xticks(rotation=0)
plt.ylim(ymin=0)
# Set sizing for image
plt.gcf().set_size_inches(
width / CM_CONVERSION_FACTOR, height / CM_CONVERSION_FACTOR
)
plt.tight_layout()
plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
plt.rc("axes", axisbelow=True)
plt.grid(axis="y", zorder=0)
plt.xticks(rotation=30, ha="right")
# Save chart to assets directory
plt.savefig(BASE_DIR + "/assets/" + name, transparent=True, dpi=500)
plt.clf()

Expand All @@ -113,19 +118,24 @@ def h_bar(self):
fig, ax = plt.subplots()
ax.spines.right.set_visible(False)
ax.spines.top.set_visible(False)
# Generate horizontal bar chart
plt.barh(df.index, value_column, bar_width, align="center", color="#466fc6")
# Specify axis atributes
plt.xticks(fontsize=7)
plt.yticks(fontsize=7)
plt.xlim(xmin=0)
plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
plt.gca().set_ylim(-1.0, len(category_column))
plt.gca().set_yticks(df.index)
plt.gca().set_yticklabels(category_column)
plt.gca().set_xlabel(x_label, fontdict={"size": 8})
plt.gca().set_ylabel(y_label)
# Set sizing for image
plt.gcf().set_size_inches(
width / CM_CONVERSION_FACTOR, height / CM_CONVERSION_FACTOR
)
plt.tight_layout()

# Add data labels to each bar if greater than 0
for i in range(len(df)):
if df.loc[i, value_name] > 0:
label = df.loc[i, value_name]
Expand All @@ -137,8 +147,7 @@ def h_bar(self):
ha="center", # horizontal alignment can be left, right or center
fontsize=8,
)

plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
# Save chart to assets directory
plt.savefig(
BASE_DIR + "/assets/" + name, transparent=True, dpi=500, bbox_inches="tight"
)
Expand All @@ -152,32 +161,44 @@ def line_chart(self):
width = self.width
height = self.height
name = self.name
value_column = df[df.columns[1]]
color = ["#1357BE", "#D0342C"]
fig, ax = plt.subplots()
ax.spines.right.set_visible(False)
ax.spines.top.set_visible(False)
plt.plot(df[df.columns[0]], value_column, label=x_label)
plt.legend(loc=9, ncol=2, framealpha=0, fontsize=8, bbox_to_anchor=(0.5, -0.5))
plt.gcf().set_size_inches(
width / CM_CONVERSION_FACTOR, height / CM_CONVERSION_FACTOR
)
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
plt.set_loglevel("WARNING")
# Generate lines for chart and add data labels
for col in range(len(df.columns)):
plt.plot(
df.index, df[df.columns[col]], color=color[col], label=df.columns[col]
)
for i, j in df[df.columns[col]].items():
if int(j):
plt.annotate(
str(int(j)),
xy=(i, j),
textcoords="offset points", # how to position the text
xytext=(
0,
5,
), # distance from text to points (x,y)
ha="center", # horizontal alignment can be left, right or center
)
# Specify axis attributes
plt.ylim(ymin=0, ymax=int(df[df.columns].max().max() * 1.15))
plt.xticks(fontsize=7)
plt.yticks(fontsize=7)
plt.gca().set_ylabel(y_label, labelpad=10, fontdict={"size": 8})
plt.xticks(rotation=30, ha="right")
plt.xlabel(x_label, labelpad=10, fontdict={"size": 8})
plt.xticks(rotation=0)
plt.grid(axis="y")
# Add legend
plt.legend(loc="upper right")
# Set sizing for image
plt.gcf().set_size_inches(
width / CM_CONVERSION_FACTOR, height / CM_CONVERSION_FACTOR
)
plt.tight_layout()

for i, j in df[df.columns[1]].items():
ax.annotate(
j,
xy=(i, j),
textcoords="offset points", # how to position the text
xytext=(0, 5), # distance from text to points (x,y)
ha="center", # horizontal alignment can be left, right or center
fontsize=7,
)

# Save chart to assets directory
plt.savefig(
BASE_DIR + "/assets/" + name, transparent=True, dpi=500, bbox_inches="tight"
)
Expand Down
6 changes: 3 additions & 3 deletions src/pe_reports/data/db_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def get_orgs(conn):
close(conn)


def query_hibp_view(org_uid, start_date, end_date):
"""Query 'Have I Been Pwned?' table."""
def query_creds_view(org_uid, start_date, end_date):
"""Query credentials view."""
conn = connect()
try:
sql = """SELECT * FROM vw_breach_complete
sql = """SELECT * FROM vw_breachcomp
WHERE organizations_uid = %(org_uid)s
AND modified_date BETWEEN %(start_date)s AND %(end_date)s"""
df = pd.read_sql(
Expand Down

0 comments on commit ee68ece

Please sign in to comment.