michael-lazar / astrobotany

🌱 A community garden over the Gemini protocol
gemini://astrobotany.mozz.us
Other
132 stars 11 forks source link

Added endpoints for plants that are Healthy, Wilting, or Dead. #73

Closed bjornwarmedal closed 3 years ago

bjornwarmedal commented 3 years ago

Python is still a relatively new language to me, and I'm not sure I've understood the code base fully yet. Would this work?

I'm specifically uncertain about dead plants. Are they ever removed from the database, or will that query return every plant that has ever died?

Is there a risk that dead plants will actually show up among healthy or wilting plants because nobody has visited in a while and therefore refresh() hasn't been called?

michael-lazar commented 3 years ago

Greetings! What's the motive for adding these endpoints? This looks kind of like a native version of the garden gnome society?

Python is still a relatively new language to me, and I'm not sure I've understood the code base fully yet. Would this work?

It will not work as currently written, there are a couple errors in the code. Have you tried getting an astrobotany server running locally in order to test?

I'm specifically uncertain about dead plants. Are they ever removed from the database, or will that query return every plant that has ever died?

Dead plants are never deleted from the database. A user's most recent plant will be linked via Plant.user_active, but you can still get all of their old dead plants too.

>>> Plant.select().count()  # All plants in the database 
1185
>>> Plant.all_active().count()  # All "active" plants in the database
486

>>> user = User.select().where(User.username=="mozz").get()
>>> for plant in user.plants:
...     print(plant.created_at, plant.dead, plant.description)
...
2020-04-05 04:29:46.010672 True common levitating gold seed-bearing hemp (deceased)
2020-05-08 04:55:39.224003 True common lithe red seed-bearing hemp (deceased)
2020-06-11 04:54:27.812808 True rare depressed rainbow seed-bearing pachypodium (deceased)
2020-07-08 02:59:16.125342 True common goth yellow seed-bearing venus flytrap (deceased)
2020-07-31 01:30:04.067105 True uncommon naive orange seed-bearing lithops (deceased)
2020-08-19 03:01:11.199169 True common scaly violet seed-bearing daffodil (deceased)
2020-09-15 13:32:06.853191 True uncommon gelatinous violet seed-bearing aloe (deceased)
2020-12-08 23:40:10.081296 True rare chaotic yellow seed-bearing baobab (deceased)
2021-01-20 02:30:52.328187 True common depressed orange seed-bearing sage (deceased)
2021-02-28 19:19:05.771087 True common aromatic black seed-bearing brugmansia (deceased)
2021-03-16 23:08:53.887279 True common vibrating blue seed-bearing jade plant (deceased)
2021-03-27 02:51:58.225616 True common flaming blue seed-bearing brugmansia (deceased)
2021-04-06 23:53:31.901577 True godly psychic white seed-bearing agave (deceased)
2021-04-29 21:42:18.257970 False flaming young lithops

Is there a risk that dead plants will actually show up among healthy or wilting plants because nobody has visited in a while and therefore refresh() hasn't been called?

Yes, there is a risk that plant.dead won't be up-to-date if refresh() hasn't been called recently. But the logic for determining if a plant is dead is quite simple (has it been 5 days since it has been watered?) which can be translated to database queries.

now = datetime.now()

healthy = Plant.all_active().filter(Plant.watered_at > now - timedelta(days=3))
wilting = Plant.all_active().filter(now - timedelta(days=3) >= Plant.watered_at > now - timedelta(days=5))
dead = Plant.all_active().filter(now - timedelta(days=5) >= Plant.watered_at)
bjornwarmedal commented 3 years ago

Greetings! What's the motive for adding these endpoints? This looks kind of like a native version of the garden gnome society?

I run the Garden Gnome Society :) The purpose is to reduce the number of calls to AstroBotany. It seems a little wasteful to make 225 calls to compile the list.

Python is still a relatively new language to me, and I'm not sure I've understood the code base fully yet. Would this work?

It will not work as currently written, there are a couple errors in the code. Have you tried getting an astrobotany server running locally in order to test?

Not yet. Meaning to do it soon.

Dead plants are never deleted from the database. A user's most recent plant will be linked via Plant.user_active, but you can still get all of their old dead plants too.

>>> Plant.select().count()  # All plants in the database 
1185
>>> Plant.all_active().count()  # All "active" plants in the database
486

>>> user = User.select().where(User.username=="mozz").get()
>>> for plant in user.plants:
...     print(plant.created_at, plant.dead, plant.description)
...
2020-04-05 04:29:46.010672 True common levitating gold seed-bearing hemp (deceased)
2020-05-08 04:55:39.224003 True common lithe red seed-bearing hemp (deceased)
2020-06-11 04:54:27.812808 True rare depressed rainbow seed-bearing pachypodium (deceased)
2020-07-08 02:59:16.125342 True common goth yellow seed-bearing venus flytrap (deceased)
2020-07-31 01:30:04.067105 True uncommon naive orange seed-bearing lithops (deceased)
2020-08-19 03:01:11.199169 True common scaly violet seed-bearing daffodil (deceased)
2020-09-15 13:32:06.853191 True uncommon gelatinous violet seed-bearing aloe (deceased)
2020-12-08 23:40:10.081296 True rare chaotic yellow seed-bearing baobab (deceased)
2021-01-20 02:30:52.328187 True common depressed orange seed-bearing sage (deceased)
2021-02-28 19:19:05.771087 True common aromatic black seed-bearing brugmansia (deceased)
2021-03-16 23:08:53.887279 True common vibrating blue seed-bearing jade plant (deceased)
2021-03-27 02:51:58.225616 True common flaming blue seed-bearing brugmansia (deceased)
2021-04-06 23:53:31.901577 True godly psychic white seed-bearing agave (deceased)
2021-04-29 21:42:18.257970 False flaming young lithops

Nice! And good to know!

Is there a risk that dead plants will actually show up among healthy or wilting plants because nobody has visited in a while and therefore refresh() hasn't been called?

Yes, there is a risk that plant.dead won't be up-to-date if refresh() hasn't been called recently. But the logic for determining if a plant is dead is quite simple (has it been 5 days since it has been watered?) which can be translated to database queries.

now = datetime.now()

healthy = Plant.all_active().filter(Plant.watered_at > now - timedelta(days=3))
wilting = Plant.all_active().filter(now - timedelta(days=3) >= Plant.watered_at > now - timedelta(days=5))
dead = Plant.all_active().filter(now - timedelta(days=5) >= Plant.watered_at)

Super!

I'll get back with a better version of this when I've had time fiddle with it some more. Unless you feel that you don't want these endpoints at all.

michael-lazar commented 3 years ago

Hey ew0k! So I was playing around with this concept a bit, and I added an API endpoint to return all of the plants in the garden via JSON. https://github.com/michael-lazar/astrobotany/pull/91. The request looks like this (it's not live yet)

Request

gemini://astrobotany.mozz.us/api/plants

Response

20 application/json
{
    "response": [
        {
            "url": "gemini://astrobotany.mozz.us/app/visit/22fbfa82bc294d2eadb846d6df5ae638",
            "username": "vee",
            "description": "godly bonsai white seed-bearing sage",
            "health": "healthy"
        },
        {
            "url": "gemini://astrobotany.mozz.us/app/visit/8f67346004cc4fa08cfde71a66b46286",
            "username": "tomasino",
            "description": "common abraxan blue seed-bearing columbine",
            "health": "healthy"
        },
            "url": "gemini://astrobotany.mozz.us/app/visit/151a605d56cb4a459b083cac0caf3968",
            "username": "GCU Prosthetic Conscience",
            "description": "uncommon luminous indigo seed-bearing cactus",
            "health": "dry"
        },
        ...

Would parsing a JSON response like this work for you? I'm not too worried about pagination or "proper" REST API design because there aren't that many users at this point. Is there any other plant or user information that's desired for garden gnome society?