def upload(): if request.method == 'POST': if 'file' not in request.files: flash('Nothing to upload') return redirect(request.url) file = request.files['file'] if file.filename == '': flash('Please select a GPX file to upload') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) name, date, time = metadata_from_gpx(filename) run = Run.query.filter_by(name=name, date=date, time=time, \ user_id=current_user.id) if run.first() is not None: flash('This run already exists in the database') return redirect(request.url) run = Run(name=name, date=date, time=time, user_id=current_user.id) distance, elevation_gain = gpx_to_csv_to_msm(run, filename) # Distance and elevation_gain do not yet work run.distance = distance run.elevation_gain = elevation_gain db.session.add(run) db.session.commit() flash(name + ' successfully uploaded and processed.') return redirect(url_for('index')) return render_template('upload.html', title='Upload')
def test_add_run(self): with self.app.app_context(): u = User(username="******", email="*****@*****.**") db.session.add(u) db.session.commit() run_1 = Run(user_id=u.id, distances="1000", times="600") run_2 = Run(user_id=u.id, distances="2000", times="1200") run_3 = Run(user_id=u.id, distances="3000", times="1800") db.session.add(run_1) db.session.add(run_2) db.session.add(run_3) db.session.commit() self.assertEqual(len(u.runs.all()), 3) self.assertEqual(u.runs.first().distances, "1000") self.assertEqual(u.runs.first().times, "600")
def experiment(experiment_id): experiment = Experiment.query.filter_by(id=experiment_id).first_or_404() form = AddRunForm() if form.validate_on_submit(): columns = '' if experiment.column_extract_code is not None: # extract columns from output # TODO This part should be done once when code is submitted. source_code = experiment.column_extract_code try: byte_code = compile(source_code, filename='<inline code>', mode='exec') loc = {} exec(byte_code, globals(), loc) columns = loc['parse'](form.run_result.data) if columns is None: columns = '' if validat_csv(columns) is False: form.run_result.errors.append( 'exctracted columns wrongs format: ' + columns) return render_experiment(experiment, form) except Exception as e: form.run_result.errors.append('Error while parsing:' + str(e)) return render_experiment(experiment, form) columns = clean_columns(columns) if validat_csv(form.columns.data) is False: form.columns.errors.append('columns wrongs format') return render_experiment(experiment, form) # if columns == '': # columns = clean_columns(form.columns.data) # else: # columns = columns + ', ' + clean_columns(form.columns.data) run = Run(description=form.description.data, run_result=form.run_result.data, owner=current_user, result_inffered_columns=columns, columns=clean_columns(form.columns.data), experiment_id=int(experiment_id)) db.session.add(run) db.session.commit() if form.upload_file.data is not None: f = form.upload_file.data fn = images.save(f, str(run.id)) new_file = FileContent(run_id=run.id, file_name=fn) db.session.add(new_file) db.session.commit() return redirect(url_for('main.experiment', experiment_id=experiment_id)) return render_experiment(experiment, form)
def terminate(self, operation=None, err=None): if not hasattr(self, 'run'): return if err is not None: self.run['status'] = 'error' self.run['error_msg'] = str(err) self.run['operation'] = operation self.run['end_time'] = datetime.now() self.run['new_records'] = self.record_counter run = Run(**self.run) db_session.add(run) db_session.commit()
def add_run(): form = AddRunForm() if form.validate_on_submit(): duration = int(form.duration_h.data) * 3600 + int( form.duration_m.data) * 60 + int(form.duration_s.data) timestamp = form.timestamp.data distances = '{:.0f}'.format(float(form.distance.data) * 1000) run = Run(user_id=current_user.id, distances=distances, times=str(duration), timestamp=timestamp) db.session.add(run) db.session.commit() flash("run added") return redirect(url_for('main.index')) return render_template('main/add_run.html', form=form)
def sample_data(app): with app.app_context(): u1 = User(username="******", email="*****@*****.**") u1.set_password('12345') u2 = User(username="******", email="*****@*****.**") u2.set_password('12345') db.session.add(u1) db.session.add(u2) db.session.commit() distances = [8000, 11400, 5000, 15100, 4200, 10000, 12000, 14000, 11000, 15600, 12200, 13300, 14000, 12000, 10000, 15600, 12200, 13300, 14000, 12000, 10000, 15600, 12200, 13300] times = [1400, 1800, 2100, 2000, 2250, 3300, 1300, 1000, 4300, 2900, 2100, 2300, 2300, 2000, 1800, 2900, 2100, 2300, 2300, 2000, 1800, 2900, 2100, 2300] timestamp = [datetime(2019, 5, 6), datetime(2019, 5, 10), datetime(2019, 5, 12), datetime(2019, 5, 16), datetime(2019, 5, 19), datetime(2019, 5, 21), datetime(2019, 5, 25), datetime(2019, 5, 27), datetime(2019, 6, 1), datetime(2019, 6, 4), datetime(2019, 6, 8), datetime(2019, 6, 10),datetime(2019, 6, 16), datetime(2019, 6, 20), datetime(2019, 6, 23), datetime(2019, 6, 28), datetime(2019, 7, 2), datetime(2019, 7, 6),datetime(2019, 7, 13), datetime(2019, 7, 16), datetime(2019, 7, 19), datetime(2019, 7, 21), datetime(2019, 7, 24), datetime(2019, 7, 26)] for i in range(len(distances)): run = Run(user_id=u1.id, distances=str(distances[i]), times=str(times[i]), timestamp=timestamp[i]) db.session.add(run) db.session.commit()
def save_run(project_id): run = Run(project_id=project_id, date=datetime(1900, 1, 1)) db.session.add(run) db.session.commit() return run
def __apply_command(self, patch: Patch, step: str): print(patch, step) if step == 'quickcheck_command': timeout = patch.project.quickcheck_timeout command = patch.project.quickcheck_command elif step == 'test_command': timeout = patch.project.test_timeout command = patch.project.test_command elif step == 'build_command': timeout = None command = patch.project.build_command elif step == 'clean_command': timeout = None command = patch.project.clean_command else: raise NotImplementedError # if no command is provided, return without creating a run; True means: next command must be executed if not command: return True run = Run() run.command = step run.patch_id = patch.id run.project_id = patch.project_id run.timestamp_start = datetime.datetime.now() # execute command try: output = self.__execute_command_timeout(command, cwd=patch.project.workdir, timeout=timeout) timeout = False success = True nochange = False except subprocess.CalledProcessError as e: output = e.output timeout = False success = False nochange = e.returncode == 77 except subprocess.TimeoutExpired as e: output = e.output timeout = True success = False nochange = False run.output = str(output, encoding='utf-8', errors='ignore') run.timestamp_end = datetime.datetime.now() run.duration = (run.timestamp_end - run.timestamp_start).total_seconds() # determine log message if success: log = 'success' elif timeout: log = 'timeout' elif nochange: log = 'nochange' else: log = 'failure' run.log = log run.success = success db.session.add(run) if not success: patch.state = 'killed' db.session.commit() return success