glidernet / ogn-python

A gateway with built-in database for the Open Glider Network (OGN)
http://glidernet.org
GNU Affero General Public License v3.0
17 stars 4 forks source link

flights relation (and model) missing #158

Open matburnham opened 2 years ago

matburnham commented 2 years ago

Trying to extract flights for today causes an exception:

(ogn-python) mat@devbox:~/dev/ogn-python$ flask flights create 2022-07-03 2022-07-03 0
2022-07-03:   0%|                                                                     | 0/1 [00:14<?, ?it/s]
Traceback (most recent call last):
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1819, in _execute_context
    self.dialect.do_execute(
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.UndefinedTable: relation "flights" does not exist
LINE 2:         INSERT INTO flights(date, sender_id, flight_type, mu...
                            ^

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/mat/.virtualenvs/ogn-python/bin/flask", line 8, in <module>
    sys.exit(main())
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/flask/cli.py", line 990, in main
    cli.main(args=sys.argv[1:])
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/flask/cli.py", line 596, in main
    return super().main(*args, **kwargs)
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/flask/cli.py", line 440, in decorator
    return __ctx.invoke(f, *args, **kwargs)
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/mat/dev/ogn-python/app/commands/flights.py", line 28, in create
    result = compute_flights(date=single_date, flight_type=flight_type)
  File "/home/mat/dev/ogn-python/app/collect/flights.py", line 67, in compute_flights
    db.session.execute(query)
  File "<string>", line 2, in execute
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1712, in execute
    result = conn._execute_20(statement, params or {}, execution_options)
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1631, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 332, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1498, in _execute_clauseelement
    ret = self._execute_context(
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1862, in _execute_context
    self._handle_dbapi_exception(
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2043, in _handle_dbapi_exception
    util.raise_(
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
    raise exception
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1819, in _execute_context
    self.dialect.do_execute(
  File "/home/mat/.virtualenvs/ogn-python/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "flights" does not exist
LINE 2:         INSERT INTO flights(date, sender_id, flight_type, mu...
                            ^

So flights relation is missing. There's no flights.py in app/model. It looks like `flights2d.py' was removed in a18e6aeab3b2c62ac967b3bb6f319556cda02577. I could try and re-create an appropriate table, but is this simply a file not committed to git?

matburnham commented 2 years ago

Looks like something the following should work, based on INSERT INTO flights(date, sender_id, flight_type, multilinestring, simple_multilinestring)

from geoalchemy2.types import Geometry

from app import db

class Flight(db.Model):
    __tablename__ = "flights"

    date = db.Column(db.Date, primary_key=True)

    flight_type = db.Column(db.SmallInteger, primary_key=True)

    multilinestring = db.Column("multilinestring", Geometry("MULTILINESTRING", srid=4326))
    simple_multilinestring = db.Column("simple_multilinestring", Geometry("MULTILINESTRING", srid=4326))  # this is the path simplified with ST_Simplify(path, 0.0001)

    # Relations
    sender_id = db.Column(db.Integer, db.ForeignKey("senders.id", ondelete="CASCADE"), index=True)
    sender = db.relationship("Sender", foreign_keys=[sender_id], backref=db.backref("flights"))

    def __repr__(self):
        return "<Flight %s: %s,%s>" % (self.date, self.path_wkt, self.path_simple_wkt)

db.Index("ix_flights_date_sender_id", Flight.date, Flight.sender_id)