forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.py
More file actions
178 lines (151 loc) · 5.06 KB
/
Copy pathresolver.py
File metadata and controls
178 lines (151 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from decimal import Decimal
from random import randint
from graphql import GraphQLError
from app import logger
from db import db_session
from functions.auth_functions import is_user_read
from functions.auth_wrappers import require_token
from models import Summaries, Organizations
from schemas.shared_structures.categorized_summary import (
CategorizedSummary,
SummaryCategory,
)
@require_token
def resolve_email_summary(self, info, **kwargs):
"""
This function is used to resolve the emailSummary query. It does this by querying
the database for the latest summary data.
:param self: None
:param info: Request information
:param kwargs: Various arguments passed in
:return: CatagorizedSummary object with data
"""
user_id = kwargs.get("user_id")
user_roles = kwargs.get("user_roles")
# Generate user Org ID list
org_ids = []
for role in user_roles:
org_ids.append(role["org_id"])
# Set user read check
user_read = False
# Check to see if user belongs to at least one org
for org_id in org_ids:
if is_user_read(user_roles=user_roles, org_id=org_id):
user_read = True
if user_read is True:
# Grab latest three email results
summaries = (
db_session.query(Summaries)
.filter(Summaries.type == "email")
.order_by(Summaries.id.desc())
.limit(3)
.all()
)
# Check to ensure that there is data returned from the db
if not summaries:
logger.warning(
f"User: {user_id} tried to access email summary query but no email summaries could be found."
)
raise GraphQLError("Error, email summary could not be found.")
# Generate return data
total = 0
summary_catagories = []
# Loop through each summary returned by the db
for summary in summaries:
# Create list of SummaryCategory objects for the categories field
# In the CategorizedSummary Object
summary_catagories.append(
SummaryCategory(
name=summary.name,
count=summary.count,
percentage=summary.percentage,
)
)
# Calculate total domains that were retrieved
total += summary.count
# Create CategorizedSummary Object to return
rtr_data = CategorizedSummary(categories=summary_catagories, total=total,)
logger.info(
f"User: {user_id} successfully retrieved email summary information."
)
return rtr_data
else:
logger.warning(
f"User: {user_id} tried to access email summary query but does not have any user read or higher access."
)
raise GraphQLError("Error, email summary could not be found.")
def resolve_demo_email_summary(self, info, **kwargs):
"""
This function is used to resolve the demoEmailSummary query. It does this by
generating random data for demo purposes
:param self: None
:param info: Request information
:param kwargs: Various arguments passed in
:return: CatagorizedSummary object with data
"""
summaries = generate_demo_data()
# Generate return data
total = 0
summary_catagories = []
for summary in summaries:
summary_catagories.append(
SummaryCategory(
name=summary.get("name"),
count=summary.get("count"),
percentage=summary.get("percentage"),
)
)
total += summary.get("count")
rtr_data = CategorizedSummary(categories=summary_catagories, total=total,)
return rtr_data
def generate_demo_data(
full_pass_count=None, full_fail_count=None, partial_pass_count=None
):
if not full_pass_count:
full_pass_count = randint(100, 10000)
if not full_fail_count:
full_fail_count = randint(100, 10000)
if not partial_pass_count:
partial_pass_count = randint(100, 10000)
full_pass_percentage = round(
Decimal(
(full_pass_count / (full_pass_count + full_fail_count + partial_pass_count))
* 100
),
1,
)
full_fail_percentage = round(
Decimal(
(full_fail_count / (full_pass_count + full_fail_count + partial_pass_count))
* 100
),
1,
)
partial_pass_percentage = round(
Decimal(
(
partial_pass_count
/ (full_pass_count + full_fail_count + partial_pass_count)
)
* 100
),
1,
)
summaries = [
{
"name": "full-pass",
"count": full_pass_count,
"percentage": full_pass_percentage,
},
{
"name": "full-fail",
"count": full_fail_count,
"percentage": full_fail_percentage,
},
{
"name": "partial-pass",
"count": partial_pass_count,
"percentage": partial_pass_percentage,
},
]
return summaries