preparation commit for henni

This commit includes:
* sql conversion script
* fixes for the column type for lat and lng fields
* removes prints / console logs
* unneccessary comments
* if no home data is available, we center to germany
* hide non working brand logos
* on closed stations the word "closed" is printed instead of outdated
price
* updates on filling stations is done in chunks of max 10
* configuration separates between development and testing
This commit is contained in:
Joachim Lusiardi 2017-11-24 09:08:04 +01:00
parent 543d3e1658
commit f7cb273254
9 changed files with 98 additions and 76 deletions

View File

@ -39,8 +39,8 @@ class User(db.Model, UserMixin):
password = db.Column(db.String(255)) password = db.Column(db.String(255))
active = db.Column(db.Boolean()) active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime()) confirmed_at = db.Column(db.DateTime())
home_lat = db.Column(db.Numeric(2, 5), default=0) home_lat = db.Column(db.Numeric(8, 5), default=0)
home_long = db.Column(db.Numeric(2, 5), default=0) home_long = db.Column(db.Numeric(8, 5), default=0)
home_zoom = db.Column(db.Integer(), default=0) home_zoom = db.Column(db.Integer(), default=0)
vehicles = db.relationship( vehicles = db.relationship(
@ -188,8 +188,8 @@ class FillingStation(db.Model):
houseNumber = db.Column(db.Text()) houseNumber = db.Column(db.Text())
postCode = db.Column(db.Integer(), nullable=False) postCode = db.Column(db.Integer(), nullable=False)
brand = db.Column(db.Text(), nullable=False) brand = db.Column(db.Text(), nullable=False)
lat = db.Column(db.Numeric(2, 5), nullable=False) lat = db.Column(db.Numeric(8, 5), nullable=False)
lng = db.Column(db.Numeric(2, 5), nullable=False) lng = db.Column(db.Numeric(8, 5), nullable=False)
last_update = db.Column(db.DateTime) last_update = db.Column(db.DateTime)
diesel = db.Column(db.Numeric(10, 3), default=0) diesel = db.Column(db.Numeric(10, 3), default=0)
e5 = db.Column(db.Numeric(10, 3), default=0) e5 = db.Column(db.Numeric(10, 3), default=0)

View File

@ -93,9 +93,7 @@ def edit_consumable(cid):
if form.validate_on_submit(): if form.validate_on_submit():
consumable.name = form.name.data consumable.name = form.name.data
consumable.unit = form.unit.data consumable.unit = form.unit.data
print(form.ext_id.data)
consumable.ext_id = choices[form.ext_id.data][1] consumable.ext_id = choices[form.ext_id.data][1]
print(consumable.ext_id)
try: try:
db.session.commit() db.session.commit()
db_log_update(consumable) db_log_update(consumable)

View File

@ -7,16 +7,9 @@ from ..entities import FillingStation
from .. import app, db, limiter from .. import app, db, limiter
@app.route('/filling_stations/update')
def update_filling_stations():
print(FillingStation.query.all())
return jsonify({})
@app.route('/filling_stations/favourites/toggle/<fsid>') @app.route('/filling_stations/favourites/toggle/<fsid>')
def add_favourite_filling_stations(fsid): def add_favourite_filling_stations(fsid):
favourite_ids = {x.id: x for x in current_user.favourite_filling_stations} favourite_ids = {x.id: x for x in current_user.favourite_filling_stations}
print(favourite_ids)
if fsid in favourite_ids: if fsid in favourite_ids:
current_user.favourite_filling_stations.remove(favourite_ids[fsid]) current_user.favourite_filling_stations.remove(favourite_ids[fsid])
@ -38,12 +31,12 @@ def query_filling_stations():
latitude = request.args.get('latitude') latitude = request.args.get('latitude')
longitude = request.args.get('longitude') longitude = request.args.get('longitude')
radius = request.args.get('radius', default=1.5) radius = request.args.get('radius', default=1.5)
type = request.args.get('type', default='all') gas_type = request.args.get('type', default='all')
sort = request.args.get('sort', default='dist') sort = request.args.get('sort', default='dist')
url = 'https://creativecommons.tankerkoenig.de/json/list.php' url = 'https://creativecommons.tankerkoenig.de/json/list.php'
params = { params = {
'lat': latitude, 'lng': longitude, 'rad': radius, 'apikey': api_key, 'type': type, 'sort': sort 'lat': latitude, 'lng': longitude, 'rad': radius, 'apikey': api_key, 'type': gas_type, 'sort': sort
} }
response = requests.get(url, params=params) response = requests.get(url, params=params)
data = response.json() data = response.json()
@ -67,24 +60,3 @@ def query_filling_stations():
station['state'] = 'normal' station['state'] = 'normal'
db.session.commit() db.session.commit()
return jsonify(data) return jsonify(data)
@app.route('/filling_stations/<fsid>', methods=['GET'])
@login_required
@limiter.limit('1 per second')
def query_filling_station_details(fsid):
api_key = app.config['TANKERKOENIG_API_KEY']
if ',' in fsid:
# more than one id, redirect to method 2 (preisabfrage)
url = 'https://creativecommons.tankerkoenig.de/json/prices.php'
params = {
'apikey': api_key, 'ids': fsid
}
else:
url = 'https://creativecommons.tankerkoenig.de/json/detail.php'
params = {
'apikey': api_key, 'id': fsid
}
response = requests.get(url, params=params)
return jsonify(response.json())

View File

@ -236,7 +236,6 @@ def plan_pit_stop_form(vid, cid):
offers = [] offers = []
for fs in current_user.favourite_filling_stations: for fs in current_user.favourite_filling_stations:
# if fs.open:
offers.append((fs, getattr(fs, consumable.ext_id),)) offers.append((fs, getattr(fs, consumable.ext_id),))
return render_template('planPitStopForm.html', vehicle=vehicle, consumable=consumable, offers=offers) return render_template('planPitStopForm.html', vehicle=vehicle, consumable=consumable, offers=offers)

View File

@ -1,7 +1,7 @@
// initially go to Brandenburger Tor // initially display germany
var lat = 52.516275, var lat = 50.75653081787912,
lon = 13.377704, lon = 9.262980794432847,
zoom = 13; zoom = 5;
var map; var map;
@ -13,6 +13,7 @@ query_location = function(updater) {
navigator.geolocation.getCurrentPosition(function(position) { navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude; lat = position.coords.latitude;
lon = position.coords.longitude; lon = position.coords.longitude;
zoom = 11;
if(updater){ if(updater){
updater(lat, lon); updater(lat, lon);
} }
@ -48,15 +49,12 @@ load_filling_stations = function() {
clicked_on_filling_station_marker = function(station, marker) { clicked_on_filling_station_marker = function(station, marker) {
return function(data) { return function(data) {
console.log(station);
console.log(marker);
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
url: '/filling_stations/favourites/toggle/'+station.id, url: '/filling_stations/favourites/toggle/'+station.id,
dataType: 'json', dataType: 'json',
timeout: 1000, timeout: 1000,
success: function(data) { success: function(data) {
console.log(data);
if (data.state == 'favourite') { if (data.state == 'favourite') {
marker.setUrl('/static/img/filling_station_favourite_marker.png'); marker.setUrl('/static/img/filling_station_favourite_marker.png');
} else { } else {
@ -70,8 +68,6 @@ clicked_on_filling_station_marker = function(station, marker) {
display_station_information = function(station) { display_station_information = function(station) {
return function(event) { return function(event) {
console.log(station);
var info = $('#station_info'); var info = $('#station_info');
info.empty(); info.empty();
info.addClass('filling_station_info'); info.addClass('filling_station_info');
@ -80,6 +76,10 @@ display_station_information = function(station) {
var cell2 = $('<div>', {'class':'col-md-4'}) var cell2 = $('<div>', {'class':'col-md-4'})
var img = $('<img>', {'src': '/static/logos/'+station.brand.toLowerCase()+'.png'}); var img = $('<img>', {'src': '/static/logos/'+station.brand.toLowerCase()+'.png'});
// hide if the brand icon loads with error
img.error(function(){
$(this).hide();
});
var name = $('<div>').text(station.name); var name = $('<div>').text(station.name);
@ -94,7 +94,6 @@ display_station_information = function(station) {
.append(cell2 .append(cell2
.append(img)); .append(img));
console.log(cell1.height())
cell2.height(cell1.height()); cell2.height(cell1.height());
}; };
} }
@ -103,7 +102,6 @@ update_filling_station_markers = function() {
for(id in filling_stations) { for(id in filling_stations) {
var station = filling_stations[id]; var station = filling_stations[id];
if(!station.marker) { if(!station.marker) {
console.log(station.id);
var lonLat = new OpenLayers.LonLat(station.lng, station.lat) var lonLat = new OpenLayers.LonLat(station.lng, station.lat)
.transform(new OpenLayers.Projection('EPSG:4326'), map.getProjectionObject()); .transform(new OpenLayers.Projection('EPSG:4326'), map.getProjectionObject());
if (station.state == 'favourite') { if (station.state == 'favourite') {
@ -114,7 +112,6 @@ update_filling_station_markers = function() {
var marker = new OpenLayers.Marker(lonLat, icon); var marker = new OpenLayers.Marker(lonLat, icon);
marker.events.register('click', marker, clicked_on_filling_station_marker(station, marker)); marker.events.register('click', marker, clicked_on_filling_station_marker(station, marker));
marker.events.register('mouseover', null, display_station_information(station)); marker.events.register('mouseover', null, display_station_information(station));
marker.events.register('mouseout', null, function(e){console.log('out');});
filling_station_markers.addMarker(marker); filling_station_markers.addMarker(marker);
station.marker = true; station.marker = true;
} }
@ -150,18 +147,15 @@ activate_map = function(map_div_id, button_ids, home_lat, home_long, home_zoom,
lat = home_lat; lat = home_lat;
lon = home_long; lon = home_long;
zoom = home_zoom; zoom = home_zoom;
console.log(lat, long);
update_map(); update_map();
} }
// get button // get button
$('#'+button_ids[0]).click(function(e){ $('#'+button_ids[0]).click(function(e){
console.log('clicked get button');
load_filling_stations(); load_filling_stations();
}); });
// set home button // set home button
$('#'+button_ids[1]).click(function(e){ $('#'+button_ids[1]).click(function(e){
console.log('clicked set home');
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: '/account/home', url: '/account/home',
@ -173,14 +167,12 @@ activate_map = function(map_div_id, button_ids, home_lat, home_long, home_zoom,
}); });
// go home button // go home button
$('#'+button_ids[2]).click(function(e){ $('#'+button_ids[2]).click(function(e){
console.log('clicked the go home button');
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
url: '/account/home', url: '/account/home',
dataType: 'json', dataType: 'json',
timeout: 1000, timeout: 1000,
success: function(data) { success: function(data) {
console.log(data);
lat = data.lat; lat = data.lat;
lon = data.long; lon = data.long;
zoom = data.zoom; zoom = data.zoom;

View File

@ -82,9 +82,9 @@
</div> </div>
</div> </div>
<script> <script>
var lat = {{ map_pos[0] }}; var lat = {{ map_pos[0] or 0 }};
var long = {{ map_pos[1] }}; var long = {{ map_pos[1] or 0 }};
var zoom = {{ map_pos[2] }}; var zoom = {{ map_pos[2] or 0 }};
var init_filling_station = JSON.parse({{ fs|tojson }}); var init_filling_station = JSON.parse({{ fs|tojson }});
activate_map('mapdiv', ['get_button', 'set_home_button', 'go_home_button'], lat, long, zoom, init_filling_station); activate_map('mapdiv', ['get_button', 'set_home_button', 'go_home_button'], lat, long, zoom, init_filling_station);
</script> </script>

View File

@ -30,8 +30,12 @@
</div> </div>
</div> </div>
</td> </td>
<td class="filling_station_{{ 'open' if offer[0].open else 'closed'}}"> <td>
{% if offer[0].open %}
{{ offer[1] }} €/{{ consumable.unit }} {{ offer[1] }} €/{{ consumable.unit }}
{% else %}
Closed
{% endif %}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
@ -44,6 +48,9 @@
<script> <script>
$(document).ready(function() { $(document).ready(function() {
$("#compare").tablesorter({sortList: [[1,0]]}); $("#compare").tablesorter({sortList: [[1,0]]});
$("img").error(function(){
$(this).hide();
});
}); });
</script> </script>
<div class="col-md-2" ></div> <div class="col-md-2" ></div>

View File

@ -167,33 +167,48 @@ def get_event_line_for_vehicle(vehicle):
return data return data
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
def update_filling_station_prices(ids): def update_filling_station_prices(ids):
max_age = (datetime.now() - timedelta(minutes=15)).strftime('%Y-%m-%d %H:%M') max_age = (datetime.now() - timedelta(minutes=15)).strftime('%Y-%m-%d %H:%M')
res = db.session. \ res = db.session. \
query(FillingStation). \ query(FillingStation). \
filter(FillingStation.id.in_(ids)). \ filter(FillingStation.id.in_(ids)). \
filter(or_(FillingStation.last_update==None, FillingStation.last_update < max_age)).\ filter(or_(FillingStation.last_update == None, FillingStation.last_update < max_age)). \
all() all()
if len(res) > 0: if len(res) > 0:
map = {x.id:x for x in res} id_map = {x.id: x for x in res}
query_ids = [x.id for x in res] query_ids = [x.id for x in res]
api_key = app.config['TANKERKOENIG_API_KEY'] api_key = app.config['TANKERKOENIG_API_KEY']
url = 'https://creativecommons.tankerkoenig.de/json/prices.php' url = 'https://creativecommons.tankerkoenig.de/json/prices.php'
# documentation tells us to query max 10 filling stations at a time...
for c in chunks(query_ids, 10):
params = { params = {
'apikey': api_key, 'ids': ','.join(query_ids) 'apikey': api_key, 'ids': ','.join(c)
} }
response = requests.get(url, params=params) response = requests.get(url, params=params)
prices = response.json()['prices'] response_json = response.json()
if response_json['ok']:
print(response_json)
prices = response_json['prices']
for price in prices: for price in prices:
id = price id = price
station_status = prices[id] station_status = prices[id]
print(id, station_status) id_map[id].open = station_status['status'] == 'open'
map[id].open = station_status['status'] == 'open' if id_map[id].open:
if map[id].open: id_map[id].diesel = station_status['diesel']
map[id].diesel = station_status['diesel'] id_map[id].e10 = station_status['e10']
map[id].e10 = station_status['e10'] id_map[id].e5 = station_status['e5']
map[id].e5 = station_status['e5'] id_map[id].last_update = datetime.now()
map[id].last_update = datetime.now() else:
logging.error(
'could not update filling stations because of {r} on URL {u}.'.format(r=str(response_json),
u=response.url))
db.session.commit() db.session.commit()

View File

@ -0,0 +1,39 @@
CREATE TABLE `filling_station` (
`int_id` int(11) NOT NULL AUTO_INCREMENT,
`id` varchar(40) NOT NULL,
`name` text NOT NULL,
`street` text NOT NULL,
`place` text NOT NULL,
`houseNumber` text,
`postCode` int(11) NOT NULL,
`brand` text NOT NULL,
`lat` decimal(8,5) NOT NULL,
`lng` decimal(8,5) NOT NULL,
`last_update` datetime DEFAULT NULL,
`diesel` decimal(10,3) DEFAULT NULL,
`e5` decimal(10,3) DEFAULT NULL,
`e10` decimal(10,3) DEFAULT NULL,
`open` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`int_id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=latin1;
CREATE TABLE `users_fillingstations` (
`user_id` int(11) DEFAULT NULL,
`fillingstation_id` int(11) DEFAULT NULL,
KEY `user_id` (`user_id`),
KEY `fillingstation_id` (`fillingstation_id`),
CONSTRAINT `users_fillingstations_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
CONSTRAINT `users_fillingstations_ibfk_2` FOREIGN KEY (`fillingstation_id`) REFERENCES `filling_station` (`int_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `consumable` ADD COLUMN `ext_id` varchar(255) DEFAULT NULL;
update consumable set ext_id = 'e5' where id = 1;
update consumable set ext_id = 'e10' where id = 34;
update consumable set ext_id = 'diesel' where id = 32;
ALTER TABLE `user` ADD COLUMN `home_lat` decimal(8,5) DEFAULT NULL;
ALTER TABLE `user` ADD COLUMN `home_long` decimal(8,5) DEFAULT NULL;
ALTER TABLE `user` ADD COLUMN `home_zoom` int(11) DEFAULT NULL;