-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery Starbot ⭐ refactored DucarrougeR/themis-1 #1
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,19 +19,22 @@ def test_db_connections(self): | |
db_errors = [] | ||
|
||
for connection in all_connections: | ||
if connection.name not in ('looker', 'looker__ilooker', 'looker__internal__analytics'): | ||
if connection.dialect: | ||
connection_tests = {'tests': connection.dialect.connection_tests} | ||
for test in connection_tests['tests']: | ||
try: | ||
db_validation = self.looker_client.test_connection(connection.name, test) | ||
for item in db_validation: | ||
if item.status != 'success': | ||
db_errors.append("Database Connection {} Test '{}' returned '{}'".format(connection.name, | ||
item.name, | ||
item.message)) | ||
except Exception as e: | ||
print("Database Connection test for {} failed due to {}".format(connection.name, e)) | ||
if ( | ||
connection.name | ||
not in ('looker', 'looker__ilooker', 'looker__internal__analytics') | ||
and connection.dialect | ||
): | ||
connection_tests = {'tests': connection.dialect.connection_tests} | ||
for test in connection_tests['tests']: | ||
try: | ||
db_validation = self.looker_client.test_connection(connection.name, test) | ||
for item in db_validation: | ||
if item.status != 'success': | ||
db_errors.append("Database Connection {} Test '{}' returned '{}'".format(connection.name, | ||
item.name, | ||
item.message)) | ||
except Exception as e: | ||
print("Database Connection test for {} failed due to {}".format(connection.name, e)) | ||
return db_errors | ||
|
||
def count_all_integrations(self): | ||
|
@@ -53,9 +56,6 @@ def test_integrations(self): | |
integration_errors.append("SUCCESS - Integration {} connectivity test".format(elem.label)) | ||
except Exception as e: | ||
integration_errors.append("Integration {} connectivity test could not run due to {}".format(elem.label, e)) | ||
else: | ||
# integration_errors.append("Integration {} not enabled.".format(elem.label)) | ||
pass | ||
Comment on lines
-56
to
-58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
return integration_errors | ||
|
||
def count_all_datagroups(self): | ||
|
@@ -65,10 +65,10 @@ def count_all_datagroups(self): | |
def test_datagroups(self): | ||
'''Tests the datagroups and returns failures''' | ||
all_datagroups = self.looker_client.all_datagroups() | ||
group_errors = [] | ||
for elem in all_datagroups: | ||
if elem.trigger_error: | ||
group_errors.append("Datagroup \"{}\" on model \"{}\" has this error:\t{}".format(elem.name, | ||
elem.model_name, | ||
elem.trigger_error)) | ||
return group_errors | ||
return [ | ||
"Datagroup \"{}\" on model \"{}\" has this error:\t{}".format( | ||
elem.name, elem.model_name, elem.trigger_error | ||
) | ||
for elem in all_datagroups | ||
if elem.trigger_error | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ def validate_content(self): | |
'''Runs the Content Validator and returns failures''' | ||
error = self.looker_client.content_validation().content_with_errors | ||
looks_error, dash_errors = [], [] | ||
for i in range(0, len(error)): | ||
for i in range(len(error)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
error_msg = re.sub(r'\[ContentValidationError\(message=', '', str(error[i].errors)) | ||
error_msg = re.sub(r'\, field_name=.*', '', error_msg) | ||
if error[i].look: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
def email_body(looker_url, looker_version, total_users, total_projects, total_erring_content, total_schedules, | ||
total_pdt_errors, pdt_less_30, pdt_30_60, pdt_over_60, unlimited_queries, total_connections, total_integrations, total_datagroups): | ||
'''Generates the summary body for the email''' | ||
body = ''' | ||
return ''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
Today's Report:<br><br> | ||
Looker instance: {}<br> | ||
Looker version: {} | ||
|
@@ -41,19 +41,22 @@ def email_body(looker_url, looker_version, total_users, total_projects, total_er | |
Find more information <a href="https://github.com/looker-open-source/Themis">go to the repo</a> | ||
<br> | ||
Something Wrong? <a href="https://github.com/looker-open-source/Themis">Tell us</a> | ||
</font>'''.format(looker_version, | ||
looker_url, | ||
total_users, | ||
total_projects, | ||
total_erring_content, | ||
total_schedules, | ||
total_pdt_errors, | ||
pdt_less_30, pdt_30_60, pdt_over_60, | ||
unlimited_queries, | ||
total_connections, | ||
total_integrations, | ||
total_datagroups) | ||
return body | ||
</font>'''.format( | ||
looker_version, | ||
looker_url, | ||
total_users, | ||
total_projects, | ||
total_erring_content, | ||
total_schedules, | ||
total_pdt_errors, | ||
pdt_less_30, | ||
pdt_30_60, | ||
pdt_over_60, | ||
unlimited_queries, | ||
total_connections, | ||
total_integrations, | ||
total_datagroups, | ||
) | ||
|
||
|
||
def email_attachment(looker_version, looker_url, total_users, user_details, total_projects, | ||
|
@@ -85,12 +88,12 @@ def email_attachment(looker_version, looker_url, total_users, user_details, tota | |
list_errors_schedules = list_errors_schedules, | ||
total_pdt_errors = total_pdt_errors, | ||
list_pdt_errors = list_pdt_errors, | ||
|
||
unlimited_queries = unlimited_queries, | ||
report_url = report_url, | ||
is_clustered = is_clustered, | ||
list_nodes = list_nodes, | ||
|
||
Comment on lines
-88
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
total_connections = total_connections, | ||
list_errors_connections = list_errors_connections, | ||
|
||
|
@@ -100,9 +103,8 @@ def email_attachment(looker_version, looker_url, total_users, user_details, tota | |
total_datagroups = total_datagroups, | ||
list_errors_datagroups = list_errors_datagroups | ||
) | ||
html_file = open('./modules/rendering/rendered_version.html', 'w') | ||
html_file.write(output_text) | ||
html_file.close() | ||
with open('./modules/rendering/rendered_version.html', 'w') as html_file: | ||
html_file.write(output_text) | ||
HTML(filename='./modules/rendering/rendered_version.html').write_pdf('./modules/rendering/final_attachment.pdf', | ||
stylesheets=[CSS(string='@page {{ font-family:arial, serif; font-size: 6; }}')]) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,25 +17,22 @@ def get_looker_instance(): | |
config = configparser.ConfigParser() | ||
config.read('looker.ini') | ||
config_details = dict(config['Looker']) | ||
instance_url = regex_base_url(config_details['base_url']) | ||
return instance_url | ||
return regex_base_url(config_details['base_url']) | ||
else: | ||
LOOKERSDK_BASE_URL = os.environ.get('LOOKERSDK_BASE_URL') | ||
instance_url = regex_base_url(LOOKERSDK_BASE_URL) | ||
return instance_url | ||
return regex_base_url(LOOKERSDK_BASE_URL) | ||
Comment on lines
-20
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def format_output(function_results): | ||
'''Formats list of errors in Looker to display first 20 elements''' | ||
if isinstance(function_results, (list)): | ||
if len(function_results) >=1: | ||
formatted_results = function_results[:20] | ||
formatted_results.append('...') | ||
return formatted_results | ||
else: | ||
if len(function_results) < 1: | ||
return ['No issues found.'] | ||
formatted_results = function_results[:20] | ||
formatted_results.append('...') | ||
return formatted_results | ||
elif isinstance(function_results, (tuple)): | ||
formatted_results = list(function_results)[:20] | ||
formatted_results.append('...') | ||
return formatted_results | ||
return formatted_results | ||
Comment on lines
-30
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
else: | ||
return function_results |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,25 +22,25 @@ def unlimited_downloads(self): | |
"query.limit" | ||
], | ||
filters = { | ||
"history.created_time": "24 hours", | ||
"history.source": "-regenerator,-suggest", | ||
"query.limit": ">5000" | ||
"history.created_time": "24 hours", | ||
"history.source": "-regenerator,-suggest", | ||
"query.limit": ">5000" | ||
}, | ||
sorts = ["history.created_time desc"], | ||
limit = "500" | ||
) | ||
unltd_downloads = self.looker_client.create_query(body) | ||
unlimited_downloads = self.looker_client.run_query(unltd_downloads.id, result_format='json') | ||
if unlimited_downloads: | ||
unltd_source, unltd_users = [], [] | ||
for unltd_query in json.loads(unlimited_downloads): | ||
unltd_source.append(unltd_query['history.source']) | ||
unltd_users.append(unltd_query['user.id']) | ||
results = "{} users have ran queries with more than 5000 rows from these sources: {}".format(len(list(set(unltd_users))), list(set(unltd_source))) | ||
return results, unltd_downloads.share_url | ||
else: | ||
if not unlimited_downloads: | ||
return None, unltd_downloads.share_url | ||
|
||
unltd_source, unltd_users = [], [] | ||
for unltd_query in json.loads(unlimited_downloads): | ||
unltd_source.append(unltd_query['history.source']) | ||
unltd_users.append(unltd_query['user.id']) | ||
results = "{} users have ran queries with more than 5000 rows from these sources: {}".format(len(list(set(unltd_users))), list(set(unltd_source))) | ||
return results, unltd_downloads.share_url | ||
Comment on lines
-25
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def check_if_clustered(self): | ||
'''Check is Looker is clustered settup''' | ||
body = models.WriteQuery( | ||
|
@@ -54,44 +54,42 @@ def check_if_clustered(self): | |
cluster_check = self.looker_client.create_query(body) | ||
check_clustered = self.looker_client.run_query(cluster_check.id, result_format='json') | ||
nodes_count = len(json.loads(check_clustered)) | ||
node_is_cluster = [] | ||
for node in json.loads(check_clustered): | ||
node_is_cluster.append(node['node.clustered']) | ||
is_clustered = nodes_count > 1 and list(set(node_is_cluster))[0] == "Yes" | ||
return is_clustered #, len(node_is_cluster) | ||
node_is_cluster = [ | ||
node['node.clustered'] for node in json.loads(check_clustered) | ||
] | ||
return nodes_count > 1 and list(set(node_is_cluster))[0] == "Yes" | ||
Comment on lines
-57
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
|
||
def nodes_matching(self): | ||
'''For clusters, checks nodes are on same version''' | ||
body = models.WriteQuery( | ||
model = "system__activity", | ||
view = "history", | ||
fields = [ | ||
"node.id", | ||
"node.version", | ||
"node.last_heartbeat_time", | ||
"node.last_heartbeat_time" | ||
], | ||
filters = { | ||
"node.last_heartbeat_date": "1 days" | ||
}, | ||
sorts = ["node.last_heartbeat_time desc"], | ||
limit = "500", | ||
vis_config = { | ||
"hidden_fields": ["node.id","node.version","node.last_heartbeat_time","most_recent_heartbeat","node.count"] | ||
}, | ||
dynamic_fields = "[{\"table_calculation\":\"most_recent_heartbeat\",\"label\":\"most_recent_heartbeat\",\"expression\":\"diff_minutes(${node.last_heartbeat_time}, now())\",\"value_format\":null,\"value_format_name\":null,\"_kind_hint\":\"dimension\",\"_type_hint\":\"number\"},{\"table_calculation\":\"node_version_at_last_beat\",\"label\":\"node_version_at_last_beat\",\"expression\":\"if(diff_minutes(${node.last_heartbeat_time}, now()) > ${most_recent_heartbeat}*1.10 OR diff_minutes(${node.last_heartbeat_time}, now()) < ${most_recent_heartbeat}*0.90, ${node.version}, null)\",\"value_format\":null,\"value_format_name\":null,\"_kind_hint\":\"dimension\",\"_type_hint\":\"string\"}]" ) | ||
model = "system__activity", | ||
view = "history", | ||
fields = [ | ||
"node.id", | ||
"node.version", | ||
"node.last_heartbeat_time", | ||
"node.last_heartbeat_time" | ||
], | ||
filters = { | ||
"node.last_heartbeat_date": "1 days" | ||
}, | ||
sorts = ["node.last_heartbeat_time desc"], | ||
limit = "500", | ||
vis_config = { | ||
"hidden_fields": ["node.id","node.version","node.last_heartbeat_time","most_recent_heartbeat","node.count"] | ||
}, | ||
dynamic_fields = "[{\"table_calculation\":\"most_recent_heartbeat\",\"label\":\"most_recent_heartbeat\",\"expression\":\"diff_minutes(${node.last_heartbeat_time}, now())\",\"value_format\":null,\"value_format_name\":null,\"_kind_hint\":\"dimension\",\"_type_hint\":\"number\"},{\"table_calculation\":\"node_version_at_last_beat\",\"label\":\"node_version_at_last_beat\",\"expression\":\"if(diff_minutes(${node.last_heartbeat_time}, now()) > ${most_recent_heartbeat}*1.10 OR diff_minutes(${node.last_heartbeat_time}, now()) < ${most_recent_heartbeat}*0.90, ${node.version}, null)\",\"value_format\":null,\"value_format_name\":null,\"_kind_hint\":\"dimension\",\"_type_hint\":\"string\"}]" ) | ||
node_check = self.looker_client.create_query(body) | ||
nodes_versions = self.looker_client.run_query(node_check.id, result_format='json') | ||
results = [] | ||
for version in json.loads(nodes_versions): | ||
if version['node_version_at_last_beat']: # to exclude older heartbeat checks with None values | ||
results.append(version['node_version_at_last_beat']) | ||
|
||
results = [ | ||
version['node_version_at_last_beat'] | ||
for version in json.loads(nodes_versions) | ||
if version['node_version_at_last_beat'] | ||
] | ||
diff_node_version = [] | ||
if len(list(set(results))) == 1: | ||
diff_node_version.append("All {} Nodes found on same Looker version".format(len(results))) | ||
return diff_node_version | ||
else: | ||
else: | ||
for k,v in Counter(results).items(): | ||
diff_node_version.append("{} nodes found on version {}".format(v,k)) | ||
return diff_node_version | ||
return diff_node_version |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,7 @@ def count_all_projects(self): | |
def all_projects(self): | ||
'''Returns the list of projects''' | ||
my_projects = self.looker_client.all_projects(fields='id,name') | ||
project_ids = [] | ||
for i in range(0, len(my_projects)): | ||
project_ids.append(my_projects[i].id) | ||
return project_ids | ||
return [my_projects[i].id for i in range(len(my_projects))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def validate_lookml(self, project_id): | ||
'''Returns LookML validation errors''' | ||
|
@@ -48,8 +45,6 @@ def run_git_test(self, project_id): | |
one_test = self.looker_client.run_git_connection_test(project_id, test_id.id) | ||
if one_test.status != "pass": | ||
git_tests.append("Test ID: {} failed on Project: {}".format(test_id.id, project_id)) | ||
else: | ||
pass | ||
Comment on lines
-51
to
-52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
# change session back to 'production' | ||
self.looker_client.update_session({"workspace_id": "production"}) | ||
return git_tests | ||
|
@@ -75,8 +70,5 @@ def get_lookml_test(self, project_id): | |
def run_lookml_test(self, project_id): | ||
'''Runs the LookML tests''' | ||
results = self.looker_client.run_lookml_test(project_id) | ||
if not results: | ||
pass | ||
# return "No test set up on Project: {}".format(project_id) | ||
else: | ||
if results: | ||
Comment on lines
-78
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
return results |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,9 +35,13 @@ def get_failed_schedules(self): | |
) | ||
schedules_query = self.looker_client.create_query(body) | ||
failed_schedules = self.looker_client.run_query(schedules_query.id, result_format='json') | ||
cleaned_errors = [] | ||
for elem in json.loads(failed_schedules): | ||
cleaned_errors.append("Schedule \'{}\' failed to send to {}".format(elem['scheduled_job.name'], elem['scheduled_plan_destination.type'])) | ||
cleaned_errors = [ | ||
"Schedule \'{}\' failed to send to {}".format( | ||
elem['scheduled_job.name'], elem['scheduled_plan_destination.type'] | ||
) | ||
for elem in json.loads(failed_schedules) | ||
] | ||
|
||
Comment on lines
-38
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if failed_schedules: | ||
cleaned_errors = list(set(cleaned_errors)) # set to remove duplicates | ||
return cleaned_errors, len(json.loads(failed_schedules)) | ||
|
@@ -56,10 +60,13 @@ def get_pdts_status(self): | |
) | ||
failed_pdts = self.looker_client.create_query(body) | ||
failed_pdts_list = self.looker_client.run_query(failed_pdts.id, result_format='json') | ||
cleaned_errors = [] | ||
for elem in json.loads(failed_pdts_list): | ||
# cleaned_errors.append("PDT \'{}\' failed with error: {}".format(elem['pdt_event_log.view_name'], elem['error_message'])) | ||
cleaned_errors.append("PDT \'{}\' failed on connection: {}".format(elem['pdt_event_log.view_name'], elem['pdt_event_log.connection'])) | ||
cleaned_errors = [ | ||
"PDT \'{}\' failed on connection: {}".format( | ||
elem['pdt_event_log.view_name'], elem['pdt_event_log.connection'] | ||
) | ||
for elem in json.loads(failed_pdts_list) | ||
] | ||
|
||
Comment on lines
-59
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
if failed_pdts_list: | ||
cleaned_errors = list(set(cleaned_errors)) # set to remove duplicates | ||
# len(json.loads(failed_pdts_list)) will return the number of failures (# of PDTs * build attempts) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,7 @@ def send_report_out(content): | |
try: | ||
# format env variable like: '[email protected],[email protected]' | ||
email_list = os.environ.get('THEMIS_EMAIL_RECIPIENTS') | ||
to_emails = [] | ||
for email in email_list.split(','): | ||
to_emails.append(email) | ||
to_emails = [email for email in email_list.split(',')] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
except Exception as e: | ||
print("Missing THEMIS_EMAIL_RECIPIENTS Variables {}".format(e)) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,9 @@ def count_all_users(self): | |
def get_users_issue(self): | ||
'''Returns locked out users for the Looker instance''' | ||
all_users = self.looker_client.all_users(fields='id, is_disabled') | ||
disabled_users = [] | ||
disabled_users = [i for i in all_users if i.is_disabled] | ||
|
||
for i in all_users: | ||
if i.is_disabled: | ||
disabled_users.append(i) | ||
user_results = [] | ||
user_results.append("Disabled Looker users: {}".format(len(disabled_users))) | ||
user_results = ["Disabled Looker users: {}".format(len(disabled_users))] | ||
Comment on lines
-25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
locked_out = self.looker_client.all_user_login_lockouts() | ||
user_results.append("Locked Out Looker users: {}".format(len(locked_out))) | ||
return user_results | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
Connectivity.test_db_connections
refactored with the following changes:merge-nested-ifs
)