def check_for_overlaps(start_time, end_time): """This function checks if any of the previously saved time records overlaps with given time interval. In case of overlap, this method will raise an exception """ try: with open(completed_jobs_file) as inf: for line in inf: record_to_check = tkutil.parse_time_details(line) if is_overlapping(record_to_check, start_time, end_time): job_details = get_job_details(record_to_check["jobid"]) record_to_check["jobname"] = job_details[0] msg = ("The time records is overlapping with the following record, please check:\n%s\n%s" % (tkutil.get_print_header(), tkutil.get_pretty_print_record(record_to_check))) raise ValueError(msg) except IOError: pass # first time so no records at all
def get_time_summaries(start_time, end_time): """Returns the summary of the time records. These are the total times spent for job types. Returning element is a dictionary with following keys: JobType.WORK, JobType.NON_WORK """ total_time_spent = {} total_time_spent[JobType.WORK] = timedelta() total_time_spent[JobType.NON_WORK] = timedelta() with open(completed_jobs_file) as inf: for line in inf: time_details = tkutil.parse_time_details(line) if is_within_interval(time_details, start_time, end_time): job_details = get_job_details(time_details["jobid"]) time_spent = time_details["end_time"] - time_details["start_time"] total_time_spent[job_details[1]] += time_spent return total_time_spent
def get_all_records_with_type(job_types, start_time, end_time): """This function returns the time records for corresponding job types. Arguments job_types is a list of JobType elements Returning elements is a list of dictionary elements whose keys are: record_id, jobid, jobname, start_time, end_time """ all_records = [] with open(completed_jobs_file) as inf: for line in inf: time_details = tkutil.parse_time_details(line) if is_within_interval(time_details, start_time, end_time): job_details = get_job_details(time_details["jobid"]) time_details["jobname"] = job_details[0] if job_details[1] in job_types: all_records.append(time_details) return all_records
def delete_from_records(record_id): """Deletes the time records for given record id. If somehow it deletes more than one entry it will rollback and raise an exception """ elements_deleted = delete_elements(completed_jobs_file, record_id, 0) if len(elements_deleted) <= 0: # no element is deleted which means there is no corresponding record for given id msg = "There is no time record corresponding to this record_id %s" % record_id raise ValueError(msg) # find a better exception if len(elements_deleted) > 1: # something wrong, there should only be 1 record for given id, rollback rollback(completed_jobs_file, elements_deleted) msg = "Something wrong with the following id %s, please check the records" % record_id raise ValueError(msg) time_details = tkutil.parse_time_details(elements_deleted[0]) job_details = get_job_details(time_details["jobid"]) time_details["jobname"] = job_details[0] return time_details
def get_time_summaries(start_time, end_time): """Returns the summary of the time records. These are the total times spent for job types. Returning element is a dictionary with following keys: JobType.WORK, JobType.NON_WORK """ total_time_spent = {} total_time_spent[JobType.WORK] = timedelta() total_time_spent[JobType.NON_WORK] = timedelta() with open(completed_jobs_file) as inf: for line in inf: time_details = tkutil.parse_time_details(line) if is_within_interval(time_details, start_time, end_time): job_details = get_job_details(time_details["jobid"]) time_spent = time_details["end_time"] - time_details[ "start_time"] total_time_spent[job_details[1]] += time_spent return total_time_spent
def check_for_overlaps(start_time, end_time): """This function checks if any of the previously saved time records overlaps with given time interval. In case of overlap, this method will raise an exception """ try: with open(completed_jobs_file) as inf: for line in inf: record_to_check = tkutil.parse_time_details(line) if is_overlapping(record_to_check, start_time, end_time): job_details = get_job_details(record_to_check["jobid"]) record_to_check["jobname"] = job_details[0] msg = ( "The time records is overlapping with the following record, please check:\n%s\n%s" % (tkutil.get_print_header(), tkutil.get_pretty_print_record(record_to_check))) raise ValueError(msg) except IOError: pass # first time so no records at all
def delete_from_records(record_id): """Deletes the time records for given record id. If somehow it deletes more than one entry it will rollback and raise an exception """ elements_deleted = delete_elements(completed_jobs_file, record_id, 0) if len( elements_deleted ) <= 0: # no element is deleted which means there is no corresponding record for given id msg = "There is no time record corresponding to this record_id %s" % record_id raise ValueError(msg) # find a better exception if len( elements_deleted ) > 1: # something wrong, there should only be 1 record for given id, rollback rollback(completed_jobs_file, elements_deleted) msg = "Something wrong with the following id %s, please check the records" % record_id raise ValueError(msg) time_details = tkutil.parse_time_details(elements_deleted[0]) job_details = get_job_details(time_details["jobid"]) time_details["jobname"] = job_details[0] return time_details