def add_animal(): if request.method == 'POST': animal_dict = request.get_json(force=True) rd1.hset(rd1.dbsize(), animal_dict) return "Added new animal with ID = " + animal_dict['Animal_ID'] else: return """
def get_data(): animal_data = [] for i in range(rd1.dbsize() - 1): animal = {} animal['Animal_ID'] = str(rd1.hget(i, 'Animal_ID'))[1:] animal['Name'] = str(rd1.hget(i, 'Name'))[1:] animal['Date_of_Entry'] = str(rd1.hget(i, 'Date_of_Entry'))[1:] animal['Date_of_Birth'] = str(rd1.hget(i, 'Date_of_Birth'))[1:] animal['Outcome_Type'] = str(rd1.hget(i, 'Outcome_Type'))[1:] animal['Outcome_Subtype'] = str(rd1.hget(i, 'Outcome_Subtype'))[1:] animal['Animal_Type'] = str(rd1.hget(i, 'Animal_Type'))[1:] animal['Sex'] = str(rd1.hget(i, 'Sex'))[1:] animal['Age'] = str(rd1.hget(i, 'Age'))[1:] animal['Breed'] = str(rd1.hget(i, 'Breed'))[1:] animal['Color'] = str(rd1.hget(i, 'Color'))[1:] animal_data.append(animal) return animal_data
def get_data(): animal_data = [] #rd1 = redis.StrictRedis(host = redis_ip,port=6379,db=3) for i in range(rd1.dbsize() - 1): animal = {} animal['Animal_ID'] = str(rd1.hget(i, 'Animal_ID'))[1:] animal['Name'] = str(rd1.hget(i, 'Name'))[1:] animal['Date_of_Entry'] = str(rd1.hget(i, 'Date_of_Entry'))[1:] #datetime.datetime.strptime( animal['DateTime'],'%m/%d/%Y %H:%M') animal['Date_of_Birth'] = str(rd1.hget(i, 'Date_of_Birth'))[1:] #datetime.datetime.strptime( animal['Date of Birth'],'%m/%d/%Y') animal['Outcome_Type'] = str(rd1.hget(i, 'Outcome_Type'))[1:] animal['Outcome_Subtype'] = str(rd1.hget(i, 'Outcome_Subtype'))[1:] animal['Animal_Type'] = str(rd1.hget(i, 'Animal_Type'))[1:] animal['Sex'] = str(rd1.hget(i, 'Sex'))[1:] animal['Age'] = str(rd1.hget(i, 'Age'))[1:] animal['Breed'] = str(rd1.hget(i, 'Breed'))[1:] animal['Color'] = str(rd1.hget(i, 'Color'))[1:] animal_data.append(animal) return animal_data
def add_animal(): if method == 'POST' animal_dict = request.get_json(force=True) rd1.hset( rd1.dbsize(), animal_dict ) return f'Added new animal with ID = {animal_dict['Animal_ID']}' else: return """ Assemble and post a json structure like this: curl -X POST -H "Content-type: application/json" -d @file.json host-ip:5000/add_animal Where 'file.json' is a file containing: { "Animal_ID": "A781976", "Name": "*Fancy", "DateTime": "10/16/2018 14:25", "Date_of_Birth": "10/8/2017", "Outcome_Type": "Transfer", "Outcome_Subtype": "Partner", "Animal_Type": "Dog", "Sex_upon_Outcome": "Intact Female", "Age_upon_Outcome": "1 year", "Breed": "German Shepherd Mix", "Color": "White" } """ # this is a read route @app.route('/get_animal',methods=['GET']) def get_id_animal(): animalid = str(request.args.get('Animal_ID')) test = get_data() return json.dumps([x for x in test if x['Animal_ID'] == "'"+animalid+"'"]) # need an update route @app.route('/update_animal', methods=['GET', 'POST']) def update_animal(): if method == 'POST': animalid = str(request.args.get('Animal_ID')) field_to_update = request.args.keys() # joe look this up for key in rd1.keys(): if rd1.hget(key, 'Animal_ID') == animalid: rd1.hset(key, field_to_update, value_to_update) else: return """ Try a curl command like: curl localhost:5000/update_animal?Animal_ID=A643424&Animal_Type=Dog """ # need a delete route @app.route('/delete_animal', methods=['DELETE']) def delete_animal(): if method == 'DELETE': animal_to_delete = request.args.get('Animal_ID') for key in rd1.keys(): if rd1.hget(key, 'Animal_ID') == animal_to_delete: rd1.hdel(key) # or maybe rd1.delete() @app.route('/get_results',methods=['GET']) def get_result(): jid = str(request.args.get('Job_ID')) return json.dumps(jobs.get_result(jid)) @app.route('/jobs', methods=['POST']) def jobs_api(): try: job = request.get_json(force=True) except Exception as e: return True, json.dumps({'status': "Error", 'message': 'Invalid JSON: {}.'.format(e)}) return json.dumps(jobs.add_job(job['start'], job['end'])) # user should do a curl request like: # curl ip_address:5000/jobs -X POST -H "content-type: application/json" -d '{"start": "9/26/2018", "end": "9/26/2019"}' @app.route('/download/<jobid>', methods=['GET']) def download(jobid): path = f'/app/{jobid}.png' with open(path, 'wb') as f: f.write(rd.hget(jobid, 'image')) return send_file(path, mimetype='image/png', as_attachment=True) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
def execute_job(jid): data = jobs.get_job_data(jid) job_type = jobs.get_job_type(jid) #jobs.update_job_status(jid, job_type)#'in progress') #analysis: plot total number of outcomes for each day in date range #first job type: dates if (job_type == 'dates'): #jobs.update_job_status(jid, 'entered first if statement') start = jobs.get_job_start(jid) end = jobs.get_job_end(jid) #set inital outcomes count animal_outcomes_of_day = 0 #x values: list of dates x_values_to_plot = [] #y values: list of integer numbers of outcomes per date y_values_to_plot = [] start_date = datetime.datetime.strptime(start, "%m-%d-%Y") end_date = datetime.datetime.strptime(end, '%m-%d-%Y') #format to check for full day for key in rd1.keys(): #jobs.update_job_status(jid, 'you have entered first for loop') #decode time to a string without hours and minutes key_time_temp = rd1.hget( key, 'Date_of_Entry').decode('utf-8').split()[0] #string to datetime key_time = datetime.datetime.strptime(key_time_temp, '%m-%d-%Y') #jobs.update_job_status(jid, str(key_time)) #check for keys in date range if (start_date <= key_time and end_date >= key_time): #jobs.update_job_status(jid, 'this is entering 2nd if statement') #set specific date x = key_time_temp #jobs.update_job_status(jid, x) #check if date is alread in x_values_to_plot if x not in x_values_to_plot: #jobs.update_job_status(jid, 'this is entering 3rd if statement') #if new date: add to list of x_values_to_plot x_values_to_plot.append(x) #check through db for each animal with matching Date_of_Entry for i in range(rd1.dbsize()): #jobs.update_job_status(jid, 'this is entering 2nd for loop') #issue with formatting? if (x == rd1.hget( i, 'Date_of_Entry').decode('utf-8').split()[0]): #jobs.update_job_status(jid, 'this is entering 4th if statement') #increment animal_outcomes_of_day count animal_outcomes_of_day = animal_outcomes_of_day + 1 #finalize count for the day y = animal_outcomes_of_day #add total count to list of y_values_to_plot y_values_to_plot.append(y) #reset animal_outcomes_of_day count before moving to next day animal_outcomes_of_day = 0 jobs.update_job_status(jid, 'we have exited first loop') #plot scatter plot plt.scatter(x_values_to_plot, y_values_to_plot) plt.savefig('/outcomes_by_date.png') with open('/outcomes_by_date.png', 'rb') as f: img = f.read() rd.hset(f'job.{jid}', 'result', img) jobs.update_job_status(jid, 'complete') #analysis: plot total # of outcomes by type of animal in date range if (job_type == 'animal_type'): #jobs.update_job_status(jid, 'it has entered the for loop') animal_types = ['Bird', 'Cat', 'Dog', 'Livestock', 'Other'] animal_counts = [0, 0, 0, 0, 0] for key in rd1.keys(): #jobs.update_job_status(jid, str(key))#'it has entered the for loop') this_animal_type = str(rd1.hget(key, 'Animal_Type'))[1:] #jobs.update_job_status(jid, str(this_animal_type)) if this_animal_type == "'Bird'": animal_counts[0] += 1 elif this_animal_type == "'Cat'": animal_counts[1] += 1 elif this_animal_type == "'Dog'": animal_counts[2] += 1 elif this_animal_type == "'Livestock'": animal_counts[3] += 1 elif this_animal_type == "'Other'": animal_counts[4] += 1 #jobs.update_job_status(jid, str(animal_counts)) plt.clf() plt.bar(animal_types, animal_counts, color='green') plt.xlabel('Animal Type') plt.ylabel('Frequency') plt.title('Outcomes by Animal Type') plt.savefig('/outcomes_by_animal_type.png') with open('/outcomes_by_animal_type.png', 'rb') as f: img = f.read() rd.hset(f'job.{jid}', 'result', img) jobs.update_job_status(jid, 'complete')
def execute_job(jid): data = jobs.get_job_data(jid) job_type = jobs.get_job_type(jid) #jobs.update_job_status(jid, job_type)#'in progress') #analysis: plot total number of outcomes for each day in date range #first job type: dates if (job_type == 'dates'): #jobs.update_job_status(jid, str(data)) #jobs.update_job_status(jid, 'You have entered the if statement') #format of how dates come in??????????? should be a string start = jobs.get_job_start(jid) end = jobs.get_job_end(jid) #jobs.update_job_status(jid, str(start)) #set inital outcomes count animal_outcomes_of_day = 0 #x values: list of dates x_values_to_plot = [] #y values: list of integer numbers of outcomes per date y_values_to_plot = [] start_date = datetime.datetime.strptime(start, "%m-%d-%Y") end_date = datetime.datetime.strptime(end, '%m-%d-%Y') #format to check for full day, rather than specific time?????????? for key in rd1.keys(): #jobs.update_job_status(jid, 'you have entered the for loop') key_time_temp = str(rd1.hget(key, 'Date_of_Entry'))[1:] #jobs.update_job_status(jid, str(key_time_temp)) #key_time_temp = key_time_temp.replace("/","-") #jobs.update_job_status(jid, str(key_time_temp)) key_time = datetime.datetime.strptime(key_time_temp, "'%m-%d-%Y'") #jobs.update_job_status(jid, str(key_time_temp)) # ------------------- #check for keys in date range if (start_date <= key_time and end_date >= key_time): #jobs.update_job_status(jid, 'this is entering 2nd if statement') #set specific date x = str(key_time) jobs.update_job_status(jid, str(x)) # ----------- #check if date is alread in x_values_to_plot if x not in x_values_to_plot: jobs.update_job_status( jid, 'this is entering 3rd if statement') #if new date: add to list of x_values_to_plot x_values_to_plot.append(x) #check through db for each animal with matching Date_of_Entry for i in range(rd1.dbsize()): #will there be an issue with b formatting? should we use [1:]? if (x == rd1.hget(i, 'Date_of_Entry')): #increment animal_outcomes_of_day count animal_outcomes_of_day = animal_outcomes_of_day + 1 #finalize count for the day y = animal_outcomes_of_day #add total count to list of y_values_to_plot y_values_to_plot.append(y) #reset animal_outcomes_of_day count before moving to next day animal_outcomes_of_day = 0 #plot line graph plt.scatter(x_values_to_plot, y_values_to_plot) #plt.show() plt.savefig('/outcomes_by_date.png') with open('/outcomes_by_date.png', 'rb') as f: img = f.read() rd.hset(f'job.{jid}', 'result', img) #rd.hset(jobid, 'image', img) #jobs.update_job_status(jid, 'complete') #analysis: plot total # of outcomes by type of animal in date range if (job_type == 'animal_type'): #jobs.update_job_status(jid, 'it has entered the for loop') animal_types = ['Bird', 'Cat', 'Dog', 'Livestock', 'Other'] animal_counts = [0, 0, 0, 0, 0] for key in rd1.keys(): #jobs.update_job_status(jid, str(key))#'it has entered the for loop') this_animal_type = str(rd1.hget(key, 'Animal_Type'))[1:] #jobs.update_job_status(jid, str(this_animal_type)) if this_animal_type == "'Bird'": animal_counts[0] += 1 elif this_animal_type == "'Cat'": animal_counts[1] += 1 elif this_animal_type == "'Dog'": animal_counts[2] += 1 elif this_animal_type == "'Livestock'": animal_counts[3] += 1 elif this_animal_type == "'Other'": animal_counts[4] += 1 #jobs.update_job_status(jid, str(animal_counts)) plt.clf() plt.bar(animal_types, animal_counts, color='green') plt.xlabel('Animal Type') plt.ylabel('Frequency') plt.title('Outcomes by Animal Type') #plt.show() plt.savefig('/outcomes_by_animal_type.png') with open('/outcomes_by_animal_type.png', 'rb') as f: img = f.read() #rd.hset("job.{}".format(jid), 'image' img) rd.hset(f'job.{jid}', 'result', img) jobs.update_job_status(jid, 'complete')