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))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
home_lat = db.Column(db.Numeric(2, 5), default=0)
home_long = db.Column(db.Numeric(2, 5), default=0)
home_lat = db.Column(db.Numeric(8, 5), default=0)
home_long = db.Column(db.Numeric(8, 5), default=0)
home_zoom = db.Column(db.Integer(), default=0)
vehicles = db.relationship(
@ -188,8 +188,8 @@ class FillingStation(db.Model):
houseNumber = db.Column(db.Text())
postCode = db.Column(db.Integer(), nullable=False)
brand = db.Column(db.Text(), nullable=False)
lat = db.Column(db.Numeric(2, 5), nullable=False)
lng = db.Column(db.Numeric(2, 5), nullable=False)
lat = db.Column(db.Numeric(8, 5), nullable=False)
lng = db.Column(db.Numeric(8, 5), nullable=False)
last_update = db.Column(db.DateTime)
diesel = 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():
consumable.name = form.name.data
consumable.unit = form.unit.data
print(form.ext_id.data)
consumable.ext_id = choices[form.ext_id.data][1]
print(consumable.ext_id)
try:
db.session.commit()
db_log_update(consumable)

View File

@ -7,16 +7,9 @@ from ..entities import FillingStation
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>')
def add_favourite_filling_stations(fsid):
favourite_ids = {x.id: x for x in current_user.favourite_filling_stations}
print(favourite_ids)
if fsid in favourite_ids:
current_user.favourite_filling_stations.remove(favourite_ids[fsid])
@ -38,12 +31,12 @@ def query_filling_stations():
latitude = request.args.get('latitude')
longitude = request.args.get('longitude')
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')
url = 'https://creativecommons.tankerkoenig.de/json/list.php'
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)
data = response.json()
@ -67,24 +60,3 @@ def query_filling_stations():
station['state'] = 'normal'
db.session.commit()
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 = []
for fs in current_user.favourite_filling_stations:
# if fs.open:
offers.append((fs, getattr(fs, consumable.ext_id),))
return render_template('planPitStopForm.html', vehicle=vehicle, consumable=consumable, offers=offers)

View File

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

View File

@ -82,9 +82,9 @@
</div>
</div>
<script>
var lat = {{ map_pos[0] }};
var long = {{ map_pos[1] }};
var zoom = {{ map_pos[2] }};
var lat = {{ map_pos[0] or 0 }};
var long = {{ map_pos[1] or 0 }};
var zoom = {{ map_pos[2] or 0 }};
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);
</script>

View File

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

View File

@ -167,33 +167,48 @@ def get_event_line_for_vehicle(vehicle):
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):
max_age = (datetime.now() - timedelta(minutes=15)).strftime('%Y-%m-%d %H:%M')
res = db.session. \
query(FillingStation). \
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()
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]
api_key = app.config['TANKERKOENIG_API_KEY']
url = 'https://creativecommons.tankerkoenig.de/json/prices.php'
params = {
'apikey': api_key, 'ids': ','.join(query_ids)
}
response = requests.get(url, params=params)
prices = response.json()['prices']
for price in prices:
id = price
station_status = prices[id]
print(id, station_status)
map[id].open = station_status['status'] == 'open'
if map[id].open:
map[id].diesel = station_status['diesel']
map[id].e10 = station_status['e10']
map[id].e5 = station_status['e5']
map[id].last_update = datetime.now()
# documentation tells us to query max 10 filling stations at a time...
for c in chunks(query_ids, 10):
params = {
'apikey': api_key, 'ids': ','.join(c)
}
response = requests.get(url, params=params)
response_json = response.json()
if response_json['ok']:
print(response_json)
prices = response_json['prices']
for price in prices:
id = price
station_status = prices[id]
id_map[id].open = station_status['status'] == 'open'
if id_map[id].open:
id_map[id].diesel = station_status['diesel']
id_map[id].e10 = station_status['e10']
id_map[id].e5 = station_status['e5']
id_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()

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;