def _respond_csv(self, ty, id, info_only=False): out = tempfile.TemporaryFile() writer = UnicodeWriter(out) empty_row = [] p = project_repo.get(id) if p is not None: project_section = ['Project Statistics'] project_header = ['Id', 'Name', 'Short Name', 'Total Tasks', 'First Task Submission', 'Last Task Submission', 'Average Time Spend Per Task', 'Task Redundancy'] writer.writerow(project_section) writer.writerow(project_header) project_data = get_project_report_projectdata(id) writer.writerow(project_data) writer.writerow(empty_row) user_section = ['User Statistics'] user_header = ['Id', 'Name', 'Fullname', 'Email', 'Admin', 'Subadmin', 'Enabled', 'Languages', 'Locations', 'Start Time', 'End Time', 'Timezone', 'Type of User', 'Additional Comments', 'Total Tasks Completed', 'Percent Tasks Completed', 'First Task Submission', 'Last Task Submission', 'Average Time Per Task'] writer.writerow(user_section) users_project_data = get_project_report_userdata(id) if users_project_data: writer.writerow(user_header) for user_data in users_project_data: writer.writerow(user_data) else: writer.writerow(['No user data']) return self._get_csv(out, writer)
def test_project_report_date_range_data(self): created = "2019-10-11T10:00:00" date_now = "2019-12-11T10:00:00" two_days_ago = "2019-12-09T10:00:00" four_days_ago = "2019-12-07T10:00:00" six_days_ago = "2019-12-05T10:00:00" exp_avg_time = 83808 # 58 days 04:48:00 exp_first_task_submission = "2019-12-07T10:00:00" exp_last_task_submission = "2019-12-09T10:00:00" proj = ProjectFactory.create() task = TaskFactory.create(project=proj) TaskRunFactory.create_batch(2, project=proj, created=created, finish_time=six_days_ago, task=task) TaskRunFactory.create_batch(4, project=proj, created=created, finish_time=four_days_ago, task=task) TaskRunFactory.create_batch(6, project=proj, created=created, finish_time=two_days_ago, task=task) TaskRunFactory.create_batch(2, project=proj, created=created, finish_time=date_now, task=task) report_data = get_project_report_projectdata(proj.id, start_date=four_days_ago, end_date=two_days_ago) assert report_data[4] == exp_first_task_submission and report_data[5] == exp_last_task_submission assert report_data[6] == exp_avg_time
def _respond_csv(self, ty, id, info_only=False): out = tempfile.TemporaryFile() writer = UnicodeWriter(out) empty_row = [] p = project_repo.get(id) if p is not None: project_section = ['Project Statistics'] project_header = ['Id', 'Name', 'Short Name', 'Total Tasks', 'First Task Submission', 'Last Task Submission', 'Average Time Spend Per Task', 'Task Redundancy'] writer.writerow(project_section) writer.writerow(project_header) project_data = get_project_report_projectdata(id) writer.writerow(project_data) writer.writerow(empty_row) user_section = ['User Statistics'] user_header = ['Id', 'Name', 'Fullname', 'Email', 'Admin', 'Subadmin', 'Enabled', 'Languages', 'Locations', 'Start Time', 'End Time', 'Timezone', 'Type of User', 'Additional Comments', 'Total Tasks Completed', 'Percent Tasks Completed', 'First Task Submission', 'Last Task Submission', 'Average Time Per Task'] writer.writerow(user_section) try: users_project_data = get_project_report_userdata(id) if users_project_data: writer.writerow(user_header) else: writer.writerow(['No user data']) except Exception: current_app.logger.exception('Error in get_project_report_userdata. project_id: {}'.format(id)) raise BadRequest("Failed to obtain user Statistics") for user_data in users_project_data: writer.writerow(user_data) return self._get_csv(out, writer) else: def empty_csv(out): yield out.read() return empty_csv(out)
def _respond_csv(self, ty, id, info_only=False): out = tempfile.TemporaryFile() empty_row = [] p = project_repo.get(id) if p is not None: project_section = ['Project Statistics'] project_header = ['Id', 'Name', 'Short Name', 'Total Tasks', 'First Task Submission', 'Last Task Submission', 'Average Time Spend Per Task', 'Task Redundancy'] project_data = get_project_report_projectdata(id) project_csv = pd.DataFrame([project_data], columns=project_header).to_csv(index=False) user_section = ['User Statistics'] user_header = ['Id', 'Name', 'Fullname', 'Total Tasks Completed', 'Percent Tasks Completed', 'First Task Submission', 'Last Task Submission', 'Average Time Per Task'] users_project_data = get_project_report_userdata(id) users_csv = 'No user data\n' if users_project_data: users_csv = pd.DataFrame([users_project_data], columns=user_header).to_csv(index=False) csv_txt = 'Project Statistics\n{}\n{}'.format(project_csv, users_csv) return csv_txt