Skip to content

Instantly share code, notes, and snippets.

@paul121
Last active January 2, 2020 08:00
Show Gist options
  • Save paul121/463fdd02deec767ce5a1374c3e17c303 to your computer and use it in GitHub Desktop.
Save paul121/463fdd02deec767ce5a1374c3e17c303 to your computer and use it in GitHub Desktop.
Aggregator Tracker Brainstorm
from farmosaggregator import Tracker
# Define a tracker that inherits from the Aggregator Tracker base class
class SpinachStudy(Tracker):
def __init__(self):
self.name = "Spinach Study Tracker"
self.description = "Aggregate spinach plantings and field data for analysis."
def get_farm_data(self, farmos_client):
# this list represents one farm's data for the study
# one row in the db table
data = []
# Get farm planting assets
plantings = farmos_client.asset.get(filters={'type': 'planting'})
for planting in plantings:
if planting['crop'] == 'spinach':
crop_data = {}
crop_data['planting_asset'] = planting
asset_id = planting['id']
# Load logs referencing the planting
logs = farmos_client.log.get(filters={'asset': asset_id})
crop_data['logs'] = logs
# Load areas referenced by the planting
crop_data['fields'] = []
if len(planting['location']) > 0:
for area in planting['location']:
area = farmos_client.areas.get(planting['location'][0]['id'])
area_logs = farmos_client.logs.get(filters={'area': area['id']})
crop_data['fields'].append(
{
'area': area,
'logs': area_logs
}
)
data.append(crop_data)
# return data, save in DB
return data
class NumberCompostPiles(Tracker):
def __init__(self):
self.name = "Number of Compost Piles"
self.description = "Aggregate the number of compost piles farms have"
def get_farm_data(self, farmos_client):
compost_piles = farmos_client.asset.get(filters={'type': 'farm_compost'})
# Only return the number of compost piles. We don't want to track additional data.
return len(compost_piles)
class FirstPlantingTracker(Tracker):
def __init__(self):
self.name = "First Planting Tracker"
self.description = "Aggregate seeding logs to determine first planting dates."
def get_farm_data(self, farmos_client):
seeding_logs = farmos_client.log.get(filters={'type': 'farm_seeding', 'done': True})
# This would be one farms data for the Tracker
# One row in the db table
return seeding_logs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment