Skip to content

Instantly share code, notes, and snippets.

@PreedhiVivek
Last active March 18, 2024 16:36
Show Gist options
  • Save PreedhiVivek/beb272bda042e94a4758255e86da03d0 to your computer and use it in GitHub Desktop.
Save PreedhiVivek/beb272bda042e94a4758255e86da03d0 to your computer and use it in GitHub Desktop.
[Insights app] Service to determine the most influential employee for a given duration
"""
Script to fetch the most influential employee from the current week's Survey data
Use-case : Most help given and taken employee (Monday to Friday)
Script to be run at 4pm IST every Friday
"""
from datetime import datetime
import json
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import common_functions
def get_most_help_taken_query():
"Returns the Cypher query string, for the most help taken employee"
query = '''
MATCH (m:Employees)-[r]->(n:Employees)
UNWIND r.helptaken as helpdate
WITH DISTINCT(helpdate) as help_taken_date, m.fullName as emp_name, n.fullName as help_taken_from
WHERE date(help_taken_date)>=date($start_date)
AND date(help_taken_date)<=date($end_date)
AND NOT emp_name IN $excluded_employees
WITH emp_name, collect(help_taken_from) as help_taken_from, count(help_taken_from) as help_taken_count
ORDER BY help_taken_count DESC
RETURN emp_name as most_help_taken_by, help_taken_from, help_taken_count
'''
return query
def get_most_help_given_query():
"Returns the Cypher query string, for the most help given employee"
query = '''
MATCH (m:Employees)-[r]->(n:Employees)
UNWIND r.helpgiven as helpdate
WITH DISTINCT(helpdate) as help_given_date, m.fullName as emp_name, n.fullName as help_given_to
WHERE date(help_given_date)>=date($start_date)
AND date(help_given_date)<=date($end_date)
AND NOT emp_name IN $excluded_employees
WITH emp_name, collect(help_given_to) as help_given_to, count(help_given_to) as help_given_count
ORDER BY help_given_count DESC
RETURN emp_name as most_help_given_by, help_given_to, help_given_count
'''
return query
def get_most_help_taken_employee(tx, start_date, end_date, excluded_employees):
"Returns the most help taken employee, for the set date range, from Survey DB"
help_taken_data = []
query_string = get_most_help_taken_query()
try:
neo4j_response = tx.run(query_string, start_date = start_date, end_date = end_date,
excluded_employees = excluded_employees)
query_results = [record.data() for record in neo4j_response]
if query_results:
max_count = query_results[0]["help_taken_count"]
help_taken_data = [record for record in query_results if record["help_taken_count"] == max_count]
except Exception as error:
print(f"Exception : {error}" )
raise Exception("Unable to fetch query results!") from error
return help_taken_data
def get_most_help_given_employee(tx, start_date, end_date, excluded_employees):
"Returns the most help given employee, for the set date range, from Survey DB"
help_given_data = []
query_string = get_most_help_given_query()
try:
neo4j_response = tx.run(query_string, start_date = start_date, end_date = end_date,
excluded_employees = excluded_employees)
query_results = [record.data() for record in neo4j_response]
if query_results:
max_count = query_results[0]["help_given_count"]
help_given_data = [record for record in query_results if record["help_given_count"] == max_count]
except Exception as error:
print(f"Exception : {error}" )
raise Exception("Unable to fetch query results!") from error
return help_given_data
def collate_results(help_taken_data, help_given_data, start_date, end_date):
"Collates insights to report"
taken_data = ""
given_data = ""
if help_taken_data:
for data in help_taken_data:
taken_data = taken_data + f' <i>{data["most_help_taken_by"]}</i> \n' +\
' Taken from: ' +\
f' {data["help_taken_from"]} \n'
else:
taken_data = "<b>No help taken data found for the given duration!</b>"
if help_given_data:
for data in help_given_data:
given_data = given_data + f' <i>{data["most_help_given_by"]}</i> \n' +\
' Given to: ' +\
f' {data["help_given_to"]} \n'
else:
given_data ="<b>No help given data found for the given duration!</b>"
results = '<b>Qxf2 Insights</b> \n' + \
f'<i>Most influential ({start_date} to {end_date})</i>: \n' +\
'\nMost help taken by: \n' +\
f'{taken_data}' +\
'\n' +\
'Most help given by: \n' +\
f'{given_data}'
common_functions.report_results(results)
if __name__ == '__main__':
NEO4J_DRIVER = common_functions.initialize_db_driver()
starting_date, ending_date = common_functions.get_dates()
try:
with NEO4J_DRIVER.session(database=os.environ["DB_NAME"]) as session:
help_taken_details = session.execute_read(get_most_help_taken_employee, starting_date,
ending_date, json.loads(os.environ.get("EXCLUDED_NAMES", None)))
help_given_details = session.execute_read(get_most_help_given_employee, starting_date,
ending_date, json.loads(os.environ.get("EXCLUDED_NAMES", None)))
NEO4J_DRIVER.close()
collate_results(help_taken_details, help_given_details, starting_date, ending_date)
except Exception as err:
print(f"Exception: {err}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment