Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to maintenance.py to fix errors and the script bailing #448

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Update to maintenance.py to fix errors and the script bailing #448

wants to merge 2 commits into from

Conversation

target-drone
Copy link
Contributor

After my Sqlite3 db got huge, I checked the logs and found that the maint script was generating errors.

  • changed 'purge_flights = row' to 'purge_flights = int(row[0])'
    this explicitly gets the first result in the array and casts it to an int as the value returned from the DB (column type is TEXT') is unicode

  • added missed datetime.

Have NOT tested on MySQL

After my Sqlite3 db got huge, I checked the logs and found that the maint script was generating errors.

- changed 'purge_flights = row' to 'purge_flights = int(row[0])'
this explicitly gets the first result in the array and casts it to an int as the value returned from the DB (column type is TEXT') is unicode

Have NOT tested on MySQL
@target-drone target-drone changed the title Update to maintenance.py Update to maintenance.py to fix errors and the script bailing Jul 2, 2018
@dotwaffle
Copy link

In both MySQL and SQLite, fetchone() either returns a single row, or returns None when there is no more data available. I don't understand why you need to select the first column and cast it to an integer. What error are you seeing in the maintenance script log?

@target-drone
Copy link
Contributor Author

From what I understand fetchone() is not that simple. row = fetchone() gives a value of row of "u'60'," for a purgeDaysOld db value of 60 for example. Perhaps a better fix would be to replace the row = cursor.fetchone() lines withrow = cursor.fetchone()[0]?

FYI these are the errors I get with the script:, using purge_days_old as an example.
unmodified:

# python /usr/src/adsb-receiver/build/portal/python/maintenance.py 
Traceback (most recent call last):
  File "/usr/src/adsb-receiver/build/portal/python/maintenance.py", line 87, in <module>
    purge_datetime = datetime.datetime.utcnow() - datetime.timedelta(days=purge_days_old)
TypeError: unsupported type for timedelta days component: tuple

casting to int - purge_days_old = int(row)
TypeError: int() argument must be a string or a number, not 'tuple'

explicitly selecting element 0 with purge_days_old = row[0]
TypeError: unsupported type for timedelta days component: unicode

@target-drone
Copy link
Contributor Author

using row = cursor.fetchone()[0] still requires a cast to int in one place as feeding unicode to datetime causes a bail. The SQL calls are probably(?) ok with a unicode value, so perhaps use row = cursor.fetchone()[0] in setting the purge_x values, and add an int() cast in the purge_date construction, datetime.timedelta(days=int(purge_days_old))?

@dotwaffle
Copy link

Out of interest, what are you getting if you run python --version?

row is a tuple, yes, it contains the entire row returned -- we just happen to be only SELECTing a single column, so there will only be one returned value. The purge_positions and purge_flights statements do not need to be altered because the data is only used to signal whether or not actions need to take place.

You're right that timedelta should more properly be datetime.timedelta, though when I look at the table schema, I can see that the adsb_settings table's value column is a VARCHAR(100), so you're going to be getting a string back, and datetime.timedelta only accepts numbers, not strings as you rightly point out... Which is weird because maintenance.py works for me.

I'm thinking that there must be a timedelta package installed for me that isn't for you, and that it accepts strings. I ask which version of python you're using because I'm interested in whether this is a python2/python3 problem.

@target-drone
Copy link
Contributor Author

using python2 to run these scripts - Python 2.7.15
also have Python 3.6.6rc1 installed.

checked with pip2 list |grep -i time to see if I had another time/datetime module installed, but it didn't return anything.

reverted changes to purgeAircraft, purgeFlights, purgePositions as these are used as simple TRUE/FALSE tests.
split the changes to purge_days_old to to using the more popular seeming fetchone()[0] and a seperate cast to int
@dotwaffle
Copy link

This doesn't work now with mysql:

Traceback (most recent call last):
  File "maintenance.py", line 79, in <module>
    row = cursor.fetchone()[0]
TypeError: 'NoneType' object has no attribute '__getitem__'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants