def data_create(point_id): point = Point.query.get_or_404(point_id) if point.type == "지중경사계": form = DataVerticalForm() elif point.type == "하중계": form = DataLoadcellForm() else: form = DataForm() if request.method == 'POST' and form.validate_on_submit(): if point.type == '지중경사계': data = Data(measuringdate=form.measuringdate.data, data09=form.data09.data) elif point.type == "하중계": data = Data(measuringdate=form.measuringdate.data, data01=form.data01.data, data02=form.data02.data, data03=form.data03.data) else: data = Data(measuringdate=form.measuringdate.data, data01=form.data01.data) point.data_set.append(data) db.session.add(data) db.session.commit() return redirect(url_for('point.point_detail', point_id=point_id)) if point.type == '지중경사계': return render_template('data/vertical_form.html', form=form) else: return render_template('data/data_form.html', form=form)
def delete_shoppinglist(_id): Data.delete_dictionary(_id, Data.shoppinglists) all_items = Data.get_the_data(_id, Data.items) if all_items is not None: for item in all_items: if item['_id'] in Data.items: Data.delete_dictionary(item['_id'], Data.items) flash('Shopping list deleted') return redirect(url_for('dashboard'))
def shoppinglist_items(_id): form = ShoppingitemForm() title = "Items" shopping_list = Data.get_the_data(_id, Data.shoppinglists) items = Data.get_the_data(_id, Data.items) notify = 'You have no items in this shopping list yet' return render_template('list_items.html', items=items, notify=notify, shopping_list=shopping_list, form=form, title=title)
def parseJsonData(response, datatype, user_id): objs = response.json() #objs = json.loads(response.text) for index in range(len(objs[datatype + '-intraday']['dataset'])): newdata = Data( user_id=user_id, date=objs[datatype][0]['dateTime'], time=objs[datatype + '-intraday']['dataset'][index]['time'], data_type=datatype, value=objs[datatype + '-intraday']['dataset'][index]['value']) newdata.save()
def read_data(self): threading.Timer(60.0, self.read_data).start() humidity, temperature = Adafruit_DHT.read_retry( self.DHT_SENSOR, self.DHT_PIN) if humidity is not None and temperature is not None\ and humidity < 101 and humidity > 0: temperature = '{0:.2f}'.format(temperature) humidity = '{0:.2f}'.format(humidity) with self.app.app_context(): if Data.query.count() > 10000: Data.query.delete() data = Data(temperature=temperature, humidity=humidity) db.session.add(data) db.session.commit() data = [] date = time.strftime('%Y-%m-%d %H:%M') data.append(date) data.append(temperature) data.append(humidity) #write to data file with open(self.record_file, "a") as f_data: wr = csv.writer(f_data) wr.writerow(data) self.write_html()
def login2(): form2 = RetrieveDBInfo(request.form) if request.method == 'POST' and form2.validate(): user_return = form2.username_ret.data passwd_return = form2.passwd_ret.data user = Data.query_by_name(user_return.encode('ascii', 'ignore'), passwd_return.encode('ascii', 'ignore')) db.session.close() if not isinstance(user, str): login_user(user) return redirect(url_for('submit')) else: if user == "no_user": string_response = 'The username {} does not exist in the ' \ 'database. Please sign up.'.format(user_return) elif user == "no_password": string_response = \ 'The password entered for username {} is incorrect.' \ ' Please try again.'.format(user_return) else: string_response = "Unknown error occurred." return render_template('thanks2.html', output=string_response) return render_template('login2.html', form=form2)
def get_data(u_id, d_id): if g.current_user.id != u_id: abort(403) page = request.args.get('page', 1, type=int) per_page = min(request.args.get('per_page', 10, type=int), 100) data = Data.to_collection_dict(Data.query.filter_by(device_id=d_id), page, per_page, 'api.get_users') return jsonify(data)
def post_data(): data = request.get_json() or {} if 'string' not in data: return bad_request("request must include 'string' field") Serial().write(data['string']) response = jsonify(Data(Serial().in_waiting(), data['string']).to_dict()) response.status_code = 201 return response
async def create_entry(payload: DataPayloadSchema) -> DataResponseSchema: entry = Data(parameter=payload.parameter, location=payload.location, date=payload.date, mean=payload.mean, upper=payload.upper, lower=payload.lower) await entry.save() return entry
def dataEntry(): form = DataEntry() if form.validate_on_submit(): sensor = Sensor.query.filter_by(name=form.sensorName.data).first() data = Data(temp=form.temperature.data, humidity=form.humidity.data,sensor_id=sensor.id) db.session.add(data) db.session.commit() return redirect(url_for('dataEntry')) return render_template('enter-data.html',form=form)
def success(): print("Hi") if (request.method == 'POST'): age_ = request.form['age'] country_ = request.form['country'] data = Data(age_, country_) db.session.add(data) db.session.commit() return render_template("success.html")
def index(): if request.method=="POST": print "got here" data = request.form.get("data") d = Data(data) db.session.add(d) db.session.commit() return render_template("index.html",entered_data=data) return render_template("index.html")
def get_all_data(): page = request.args.get('page', 1, type=int) per_page = min(request.args.get('per_page', 10, type=int), 100) return jsonify( Data.to_collection_dict(Data.query, page, per_page, 'main.get_all_data', order_by=Data.timestamp.desc())), 200
def save_data(filename: str, form_data: list): ''' get filename and list of categories saves data to a file and db ''' dict_data = {'categories': form_data, 'timestamp': time()} with open(filename, 'w') as f: json.dump(dict_data, f) d = Data(data=json.dumps(dict_data)) db.session.add(d)
def post(self): new_post = Data(timestamp=request.json['timestamp'], value1=request.json['value1'], value2=request.json['value2'], value3=request.json['value3'], creationDate=time.time(), lastModificationDate=time.time()) db.session.add(new_post) db.session.commit() return post_schema.dump(new_post)
def upload_data(current_user): if not current_user.admin: return jsonify({'message': 'Not allowed to perform that function!'}), 401 file = request.files["file"] new_data = Data(public_id=str(uuid.uuid4()), data=file.read()) db.session.add(new_data) db.session.commit() return jsonify({'message': 'New data added!'})
def create_data(): print('开始生成数据') import numpy as np nSampleNum = 5120 / 2 ncount = 1024.0 df = nSampleNum / ncount sampleTime = ncount / nSampleNum x = np.linspace(0, sampleTime, ncount) # 三个标准正弦波形 sinx = np.sin(2 * np.pi * 250 * x) sinx2 = 0.5 * np.sin(2 * np.pi * 500 * x) sinx3 = 0.3 * np.sin(2 * np.pi * 1000 * x) # 叠加时域 sinx += sinx2 sinx += sinx3 i = 0 equipments = Equipment.objects.all() time = timezone.now() while i < len(equipments): equipment = equipments[i] points = Point.objects.filter(equipment=equipment) j = 0 while j < len(points): point = points[j] k = 0 data_list = [] while k < len(x) * (60 * 60 * 8): time += datetime.timedelta(microseconds=976.5625) data = Data(equipment=equipment, point=point, amplitude=sinx[k % len(x)], add_time=time) data_list.append(data) if k % (1024 * 60 * 15) == 0: print('设备: %s, 测点: %s, 已生成: %d ' % (equipment.name, point.number, k)) Data.objects.bulk_create(data_list) data_list = [] print('设备: %s, 测点: %s , %d 小时数据已存储' % (equipment.name, point.number, k / (1024 * 60 * 60))) k += 1 j += 1 i += 1 print('生成数据完成')
def data(did, sid): print(type(request.json), request.json) device = g.current_user.devices.filter_by(id=did).first() if device is None: return jsonify({'status': 404, 'info': '找不到你的设备'}) sensor = device.sensors.filter_by(id=sid).first() if sensor is None: return jsonify({'status': 404, 'info': '找不到你的传感器'}) print(request.json) if request.json is None: return jsonify({'status': 404, 'info': '找不到你的数据'}) if 'data' not in request.json.keys(): return jsonify({'status': 404, 'info': '找不到你的数据'}) # 判断报警 data = Data() data.data = request.json.get('data') data.sensor_id = sid db.session.add(data) db.session.commit() if data.data > sensor.max: alert = Alert() alert.sensor_id = sensor.id alert.reason = '超出最大值' alert.data_id = data.id alert.current_max = sensor.max alert.current_min = sensor.min db.session.add(alert) db.session.commit() if data.data < sensor.max: alert = Alert() alert.sensor_id = sensor.id alert.reason = '低于最小值' alert.data_id = data.id alert.current_max = sensor.max alert.current_min = sensor.min db.session.add(alert) db.session.commit() return jsonify({'status': 200})
def dashboard(): form = Shoppinglist_nameForm() title = "Dashboard" shopping_lists = Data.get_the_data(session['id'], Data.shoppinglists) notify = 'You have no shopping lists yet!' return render_template('dashboard.html', shopping_lists=shopping_lists, notify=notify, username=session['username'], form=form, title=title)
def data(): ret = Data.select().order_by(Data.id.desc()).get() input = { 'temperature': ret.temperature, 'stirringRate': ret.stirringRate, 'pH': ret.ph } return jsonify(input)
def createDataEntry(): data = request.get_json() or {} print(data) if 'device_id' not in data or 'room_temp' not in data or 'room_humd' not in data or 'baby_temp' not in data: return bad_request('Corrupted data!') #Create data object dataEntry = Data() dataEntry.from_dict(data) print(dataEntry) #Update db db.session.add(dataEntry) db.session.commit() #Return the response response = jsonify() response.status_code = 201 return response
def edit_shoppinglist(_id): page_title = "Edit" index_ = Data.get_index(_id, Data.shoppinglists) form = Shoppinglist_nameForm(request.form) form.name.data = Data.shoppinglists[index_]['Name'] if form.validate_on_submit(): title = request.form['name'] Data.shoppinglists[index_]['title'] = title flash('Your Shopping list has been updated') return redirect(url_for('dashboard')) return render_template('dashboard.html', form=form, title=page_title)
def index(): """Основной контроллер, который обрабатывает запросы на фильтрацию и группировку информации из таблицы""" form = FilterForm() result_set = Data.query columns = [ elem.name for elem in Data.__table__.columns if elem.name != 'timestamp' ] filters = { Data.date: [request.args.get('date_from'), request.args.get('date_to')], Data.keyword: request.args.get('kw'), Data.domain: request.args.get('domain', type=int), Data.code_response: request.args.get('code'), Data.size_page: request.args.get('size', type=int) } groups = { Data.date: request.args.get('g_date'), Data.keyword: request.args.get('g_kw'), Data.domain: request.args.get('g_domain'), Data.code_response: request.args.get('g_code') } if any(groups.values()): result_set = result_set.with_entities( *[key for key in groups if groups[key]], func.sum(Data.size_page)) columns = [ desc['name'] for desc in result_set.column_descriptions if desc['name'] ] columns.append('sum_sizes') for key in filters: if filters[key]: result_set = Data.filter(result_set, key, filters[key]) if any(groups.values()): result_set = result_set.group_by( *[key for key in groups if groups[key]]) result_set = result_set.all() if not isinstance(result_set, list): result_set = [result_set] return render_template('index.html', form=form, query=result_set, columns=columns)
def handle(self, *args, **options): input = options["input"] f_obj = open(input) count = 0 recs = [] for line in f_obj: count += 1 line = line.strip() word, occur = line.split("\t") recs.append(Data(word=word, count=occur)) if count == 500: Data.objects.bulk_create(recs) recs = [] count = 0 f_obj.close()
def worldseatemp_temp(): logging.debug("worldseatemp_temp() started") source_name = "worldseatemp" source_id = DataSource.query.filter(DataSource.source_name == source_name) source_id = source_id.all() if not source_id: source_id = create_source(source_name) else: source_id = source_id[0].source_id temp = get_temp_worldseatemp() data = Data(indicator=temp, chart_id=chart_id, source_id=source_id) db_session.add(data) db_session.commit()
def login2(): form2 = RetrieveDBInfo(request.form) if request.method == 'POST' and form2.validate(): user_return = form2.username_ret.data passwd_return = form2.passwd_ret.data user_query=Data.query_by_name(user_return.encode('ascii','ignore'),passwd_return.encode('ascii','ignore')) if user_query[1]=='no_password': string_response='The password entered for username: '******' is incorrect. Please try again.' return render_template('thanks2.html', output=string_response) elif user_query=='no_user': string_response='The username: '******' does not exist in the database. Please sign up.' return render_template('thanks2.html', output=string_response) else: return redirect(url_for('submit')) db.session.close() return render_template('login2.html', form=form2)
def main(): first_names = [ "John", "Charlie", "Bob", "Rick", "Steven", "Eric", "Alex", "Rachel", "Sarah", "Margie", "Maggie", "Albert" ] last_names = [ "Johnson", "Harding", "Phillips", "Stevens", "Richardson", "Archer", "Black", "Brown", "Steele", "Powers" ] for i in xrange(50): first_name = first_names[randint(0, len(first_names) - 1)] last_name = last_names[randint(0, len(last_names) - 1)] name = first_name + " " + last_name email = first_name + last_name + "@gmail.com" data = Data( datetime(randint(2011, 2015), randint(1, 10), randint(1, 28), 1, 1), name, email) db.session.add(data) db.session.commit()
def UpdateDb(fpath, year, month): workbook = xlrd.open_workbook(fpath) if workbook == None: log_info('无法打开该文件') return False names = workbook.sheet_names() if not SALARY_SHEET_NAME in names: log_info('未找到数据表') return False sheet = workbook.sheet_by_name(SALARY_SHEET_NAME) label_pre = sheet.row_values(3) label = sheet.row_values(4) for i in range(0, len(label)): if label[i] == '': label[i] = label_pre[i] users = sheet.col_values(1) if len(users) < 6: log_info('无效数据表') return False dic = {} dic['月份'] = month for i in range(5, len(users) - 6): for j in range(1, sheet.ncols - 2): dic[label[j].strip()] = sheet.cell_value(i, j) obj = json.dumps(dic) username = users[i] user = User.query.filter_by(name=username).first() if user == None: user = NewUser(username) data = Data(year, month, obj) db.session.add(data) user.data.append(data) db.session.commit() return True
def script(): """Сбор данных для таблицы через поиск яндекса.""" keyword = request.args.get('keyword') if not keyword: return 'I need keyword', 400 r = requests.get(f'http://yandex.ru/?q={keyword}') soup = BeautifulSoup(r.content, 'lxml') headers = soup.find('body').find_all('h2') links = [ headers[i].find('a')['href'] for i in range(len(headers)) if headers[i].find('a') ] data = [] for link in links: if link.startswith('http://yabs'): r = requests.get(link) soup = BeautifulSoup(r.content, 'lxml') url = soup.find('noscript').find('meta')['content'][7:-1] start = time() r = requests.get(url) finish = time() - start else: try: url = link start = time() r = requests.get(link) finish = time() - start except: continue data.append({ 'keyword': keyword, 'code_response': r.status_code, 'size_page': len(r.content), 'url': url, 'domain': r.headers.get('domain') or url, 'timeload': int(finish) }) for elem in data: d = Data(**elem) db.session.add(d) db.session.commit() return 'OK', 200
def edit_shoppinglist_item(_id): page_title = "Edit" index_ = Data.get_index(_id, Data.items) form = ShoppingitemForm() # ### populating the form for user to edit ### form.name.data = Data.items[index_]['item_name'] form.quantity.data = Data.items[index_]['quantity'] if form.validate_on_submit(): title = request.form['title'] quantity = request.form['body'] Data.items[index_]['item_name'] = title Data.items[index_]['quantity'] = quantity flash('Your Item has been updated', 'success') return redirect( url_for('shoppinglist_items', _id=Data.items[index_]['owner_id'])) return render_template('list_items.html', form=form, title=page_title)
from app import db from app.models import Scraper, Data from super_scraper import crawl from datetime import datetime for scraper in Scraper.query.all(): result = crawl(scraper.url, scraper.get_params()) entry = Data(timestamp = datetime.now(), scraper = scraper.id) entry.set_data(result) db.session.add(entry) db.session.commit()
def load_user(username): user = Data.user_query(username) return user