def getTrafficStamp(): conn.openAll() sql = """ SELECT segment, speed FROM traffic.%(table)s """ % {'table': funcs.getAreaTable('track-view')} conn.execute(sql) return {segment[0]: segment[1] for segment in conn.fetchAll()}
def reset(area, isLog): def __reset(sql, message): conn.execute(sql) conn.commit() logger.logByCond(isLog, message) __reset( 'DELETE FROM traffic.%s' % (funcs.getAreaTable(area),), 'Reset: all segment speeds are deleted' ) if area == 'main': __reset('DELETE FROM traffic.segment_speeds_tmp', 'Reset: all tmp segment speeds are deleted') __reset('DELETE FROM traffic.segment_colors', 'Reset: all segment colors are deleted')
def getTrafficJam(params): conn = connection.Connection() conn.openAll() sql = """ SELECT s.segment, speed, ts, device_range FROM traffic.""" + funcs.getAreaTable('main') + """ AS s LEFT JOIN traffic.speed_ranges AS r ON s.segment = r.segment WHERE ts > %(ts)s AND NOT is_removed """ nowTs = str(cur_time.getTs(conn)) conn.execute(sql, {'ts': urllib.parse.unquote(params['ts'])}) speeds = conn.fetchAll() conn.execute('SELECT device_range FROM traffic.default_speed_range') defaultSpeedRange = conn.fetch()[0] conn.closeAll() # construct JSON response response = {'ts': nowTs, 'traffic-jam': []} for speed in speeds: speedRange = defaultSpeedRange if speed[3] is None else speed[3] if not 0 <= speed[1] < speedRange[0]: continue response['traffic-jam'].append({ 'id': speed[0], 'ts': str(speed[2]), }) return json.dumps(response)