class WeighBridgeEmulator:
    user = # 
    pwd = # 
    server = # 

    def __init__(self):
       
        # fill out dbname host etc in psycopg2.connect 
        self.conn = psycopg2.connect("dbname=<database_name> host=<database_host> user=<database_user> password=<database_password>")
        self.cur = self.conn.cursor()
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)
        self.agbase.set_logging_on(True)
        self.user = self.agbase.connect(self.user, self.pwd, self.server)
        self.pp = pprint.PrettyPrinter(indent=4)
        if self.user is None:
            print("login failed")
        farms = self.farm.get_farms(self.user)
        for farm in farms:
            if farm.name == "Demo Farm":
                self.frm = farm

        cats = self.measurement_category.get_measurement_categories()
        for cat in cats:
            if cat.name == "Weight":
                self.cat = cat

        algs = self.algorithm.get_algorithms(self.cat)
        for alg in algs:
            if alg.name == "bayesy_andy_constrained_prior":
                self.alg = alg

    def get_animal_list(self):
        query = "select a.id, a.eid from animal as a join animalfarm as af on(a.id = af.animal_id) where a.vid IS NULL;" #limit(500)
        self.cur.execute(query)
        return self.cur.fetchall()

    def get_Day(self, date, animalid):
        query = 'select * from weights where weights.animal_id = {animalid} and date <@ tsrange(\'{date}\', \'{date}\'::date + 1, \'[)\') and method_id = 70 and weighbridge_id = 8 order by DATE;'.format(animalid=animalid, date=date)
        self.cur.execute(query)
        return self.cur.fetchall()

    def clean_up(self):
        self.cur.close()
        self.conn.close()

    def create_measurements(self, animal, measurements):
        for measurement in measurements:
            if(not math.isnan(measurement[5]) or not math.isnan(measurement[6]) or not math.isnan(measurement[7])):
                self.measurement.create_measurement(animal, self.alg, self.user, str(measurement[4]), measurement[5], None, measurement[6], None, measurement[7], None)

    def upload_animal_measurements_by_day(self):
        date = datetime.date(2015, 01, 01) # sets date to first day of the year
        animals = self.get_animal_list() # get animal list from the local database
        animal_max = len(animals)
        self.pp.pprint(animals)        
        for i in range(0, 100):
            print "day: {}/100".format(i)
            animal_count = 0
            for animalID in animals:
                animal_count += 1
                print "animal {}/{}".format(animal_count, animal_max)
                animal = self.animal.get_animal_by_eid(self.frm, animalID[1])
                if(animal == None):
                    animal = self.animal.create_animal(self.frm, animalID[1]) # creates an animal if it dosnt exist
                measurements = self.get_Day(date, animalID[0]) # gets the days measurements for the above animals on the above day
                if(len(measurements) > 0): # check to see if there are any measurements on that day
                    self.create_measurements(animal, measurements)

            date += datetime.timedelta(days=1)
Example #2
0
class WeighBridgeEmulator:
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--user', required=True, help="AgBase Username")
        parser.add_argument('--passwd', required=True, help="AgBase Password")
        parser.add_argument('--date', required=True, help="Date of the measurements you wish to upload (YYYY:MM:DD)")
        parser.add_argument('--server', default="<website_address>", help="AgBase Server")
        
        args = parser.parse_args()        
        self.conn = psycopg2.connect("dbname=<database_name> host=<database_address> user=<database_user> password=<database_password>")
        self.cur = self.conn.cursor()
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)
        self.agbase.set_logging_on(True)
        self.user = self.agbase.connect(args.user, args.passwd , args.server)
        self.pp = pprint.PrettyPrinter(indent=4)
        datearr = args.date.split(':',2)
        print datearr[0], datearr[1], datearr[2]
        self.date = datetime.date(int(datearr[0]), int(datearr[1]), int(datearr[2]))
        if self.user is None:
            print("login failed")
        farms = self.farm.get_farms(self.user)
        for farm in farms:
            if farm.name == "Demo Farm":
                self.frm = farm

        cats = self.measurement_category.get_measurement_categories()
        for cat in cats:
            if cat.name == "Weight":
                self.cat = cat

        algs = self.algorithm.get_algorithms(self.cat)
        for alg in algs:
            if alg.name == "bayesy_andy_constrained_prior": # need to change alg id in get_Day function
                self.alg = alg

    def get_animal_list(self):
        query = "select a.id, a.eid from animal as a join animalfarm as af on(a.id = af.animal_id) where a.vid IS NULL;" #limit(500)
        self.cur.execute(query)
        return self.cur.fetchall()

    def get_Day(self, date, animalid):
        query = 'select * from weights where weights.animal_id = {animalid} and date <@ tsrange(\'{date}\'::date - 1, \'{date}\', \'[)\') and method_id = 70 and weighbridge_id = 8 order by DATE;'.format(animalid=animalid, date=date)
        self.cur.execute(query)
        return self.cur.fetchall()

    def clean_up(self):
        self.cur.close()
        self.conn.close()

    def upload_animal_measurements_by_day(self):
        animals = self.get_animal_list() # get animal list from the local database
        animal_max = len(animals)
        self.pp.pprint(animals)
        listIndex = 0
        self.meas_list = []
        self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))

        animal_count = 0
        for animalID in animals:
            animal_count += 1
            print "animal {}/{}".format(animal_count, animal_max)
            measurements = self.get_Day(self.date, animalID[0]) # gets the days measurements for the above animals on the above day
            if(len(measurements) > 0): # check to see if there are any measurements on that day
                for measurement in measurements:
                    if(self.meas_list[listIndex].get_measurement_count() >= 500):
                        self.agbase.log("meas_list Dump >>> " + json.dumps(self.meas_list[listIndex].get_json()))
                        listIndex += 1
                        self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))
                        
                    if(not math.isnan(measurement[5]) or not math.isnan(measurement[6]) or not math.isnan(measurement[7])):
                        self.meas_list[listIndex].add_measurement(str(measurement[4]), measurement[5], None, measurement[6], None, measurement[7], animalID[1])

        for measurementList in self.meas_list:
            self.measurement.upload_measurement_list(measurementList)

        self.meas_list = []
        listIndex = 0
        self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))
Example #3
0
class TestAgBase(TestCommon):

    def setUp(self):
        print('TestAgBase.setUp')
        super(self.__class__, self).setUp()
        self.pp = pprint.PrettyPrinter(indent=4)
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)

        self.agbase.set_logging_on(True)

        self.user = self.agbase.connect(self.testUser, self.testPwd, self.serverIp)

        if self.user is None:
            self.fail()

        print('connected to mooogle with user: {} with id: {}'.format(self.user.email, self.user.id))


    def test_farms(self):

        test_farm = self.farm.create_farm("Python Test Farm")

        if test_farm is None:
            self.fail()

        print('created farm: {} with id: {}'.format(test_farm.name, test_farm.id))

        farms = self.farm.get_farms()

        if farms is None:
            self.fail()

        for farm in farms:
            print('found farm: {} with id: {}'.format(farm.name, farm.id))

        single_query_farm = self.farm.get_farm(test_farm.id)

        if single_query_farm.id != test_farm.id:
            self.fail()

        farms = self.farm.get_farms(self.user)

        if farms is None:
            self.fail()

        for farm in farms:
            print('The current user can access farm: {}'.format(farm.name))

        deleted = self.farm.remove_farm(test_farm)

        if not deleted:
            self.fail()


    def test_roles(self):

        roles = self.agbase.get_roles()

        if roles is None:
            self.fail()

        for role in roles:
            print('Found role named {}'.format(role.name))


    def test_users(self):

        roles = self.agbase.get_roles()

        admin_role = None

        for role in roles:
            if role.name == "Viewer":
                admin_role = role
                break

        test_user = self.agbase.create_user("Test", "Testor", "*****@*****.**", "testpass", admin_role)

        if test_user is None:
            self.fail()

        print('created user: {} with id: {}'.format(test_user.email, test_user.id))

        users = self.agbase.get_users()

        if users is None:
            self.fail()

        for user in users:
            print('found user: {} {} with email: {}'.format(user.first_name, user.last_name, user.email))

        deleted = self.agbase.remove_user(test_user)

        if not deleted:
            self.fail()


    def test_measurement_categories(self):

        test_category = self.measurement_category.create_measurement_category('Test Category')

        if test_category is None:
            self.fail()

        print('created measurement category: {} with id: {}'.format(test_category.name, test_category.id))

        single_query_category = self.measurement_category.get_measurement_category(test_category.id)

        if test_category.id != single_query_category.id:
            self.fail()

        categories = self.measurement_category.get_measurement_categories()

        if categories is None:
            self.fail()

        for category in categories:
            print('found category: {}'.format(category.name))

        deleted = self.measurement_category.remove_measurement_category(test_category)

        if not deleted:
            self.fail()


    def test_algorithms(self):

        test_category = self.measurement_category.create_measurement_category('Algorithm Test Category')
        test_algorithm = self.algorithm.create_algorithm('Test Algorithm', test_category)
        print "test_algorithm", test_algorithm.to_json()
        if test_algorithm is None:
            self.fail()

        print('created algorithm {} with id: {}'.format(test_algorithm.name, test_algorithm.id))

        single_query_algorithm = self.algorithm.get_algorithm(test_algorithm.id)
        print "single_query_algorithm", single_query_algorithm.to_json()
        if test_algorithm.id != single_query_algorithm.id:
            self.fail()

        algorithms = self.algorithm.get_algorithms()

        if algorithms is None:
            self.fail()

        for algorithm in algorithms:
            print('found algorithm: {}'.format(algorithm.name))

        deleted = self.algorithm.remove_algorithm(test_algorithm)

        if not deleted:
            self.fail()

        self.measurement_category.remove_measurement_category(test_category)


    def test_animals(self):

        test_farm = self.farm.create_farm('Animal Test Farm')
        test_herd = self.herd.create_herd(test_farm, 'Animal Test Herd')

        test_eid = "AN-EID-FOR_TESTING"

        test_animal = self.animal.create_animal(test_farm, test_eid)

        if test_animal is None:
            self.fail()

        print('created animal {} with id: {}'.format(test_animal.eid, test_animal.id))

        result = self.animal.set_animal_herd(test_animal, test_herd)

        if result is None:
            self.fail()
        print "============farm==========",test_farm.to_json()
        print "============herd==========",test_herd.to_json()
        animals = self.animal.get_animals(test_farm, test_herd)

        if animals is None:
            self.fail()

        for animal in animals:
            print('found animal: {}'.format(animal.eid))

        updated = self.animal.update_animal_vid(test_animal, "My Pet Cow")

        if not updated:
            self.fail()

        expected_animal = self.animal.get_animal_by_eid(test_farm, test_eid)

        if expected_animal.id != test_animal.id:
            self.fail()

        deleted = self.animal.remove_animal(test_animal)

        if not deleted:
            self.fail()

        self.herd.remove_herd(test_herd)
        self.farm.remove_farm(test_farm)


    def test_measurements(self):

        test_farm = self.farm.create_farm('Animal Test Farm')
        test_eid = "AN-EID-FOR_TESTING"
        test_animal = self.animal.create_animal(test_farm, test_eid)
        test_category = self.measurement_category.create_measurement_category('Algorithm Test Category')
        test_algorithm = self.algorithm.create_algorithm('Test Algorithm', test_category)

        measurement = self.measurement.create_measurement(test_animal, test_algorithm, self.user, time.strftime("%c"), None, None, 0.3344,None,None,None)

        if measurement is None:
            self.fail()

        print('created measurement with id {}'.format(measurement.id))

        animal_measurements = self.measurement.get_measurements_for_animal(test_animal)

        if animal_measurements[0].id != measurement.id:
            self.fail()

        deleted = self.measurement.remove_measurement(measurement)

        if not deleted:
            self.fail()

        eid_measurement = self.measurement.create_measurement_for_eid(test_animal.eid,test_farm, test_algorithm, self.user, time.strftime("%c"), None, None, 0.3344,None,None,None)

        if eid_measurement is None:
            self.fail()

        if eid_measurement.animal_id != test_animal.id:
            self.fail()
        #self.pp.pprint(repr(eid_measurement))
        for keys,values in eid_measurement.__repr__().items():
            print(keys)
            print(values)
        self.measurement.remove_measurement(eid_measurement)

        self.animal.remove_animal(test_animal)
        self.farm.remove_farm(test_farm)
        self.algorithm.remove_algorithm(test_algorithm)
        self.measurement_category.remove_measurement_category(test_category)


    def test_measurements_bulk_upload(self):

        test_farm = self.farm.create_farm('Animal Test Farm')
        test_eid = "AN-EID-FOR_TESTING"
        test_animal = self.animal.create_animal(test_farm, test_eid)
        test_category = self.measurement_category.create_measurement_category('Algorithm Test Category')
        test_algorithm = self.algorithm.create_algorithm('Test Algorithm', test_category)

        measurement_list = self.measurement.create_bulk_measurement_upload_list(test_animal, test_algorithm, self.user)

        measurement_list.add_measurement(time.strftime("%c"), 0.3344)
        measurement_list.add_measurement(time.strftime("%c"), 0.4455)
        measurement_list.add_measurement(time.strftime("%c"), 0.5566)

        success = self.measurement.upload_measurement_list(measurement_list)

        if success is not True:
            self.fail()

        print('created bulk measurements')

        animal_measurements = self.measurement.get_measurements_for_animal(test_animal)

        if len(animal_measurements) != 3:
            self.fail()

        self.animal.remove_animal(test_animal)
        self.farm.remove_farm(test_farm)
        self.algorithm.remove_algorithm(test_algorithm)
        self.measurement_category.remove_measurement_category(test_category)
Example #4
0
class WeighBridgeEmulator:
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--user', required=True, help="AgBase Username")
        parser.add_argument('--passwd', required=True, help="AgBase Password")
        parser.add_argument('--algorithm', required=True, help="The name of the data algorithm you wish "+
                            "to transfer from local moogle to agbase.(Must be an algorithm name in the moogle database)" + 
                            "and must also be added to the website.")

        parser.add_argument('--server', default="<website_address>", help="AgBase Server")
        
        args = parser.parse_args()        
        self.conn = psycopg2.connect("dbname=<database_name> host=<database_address> user=<database_user> password=<database_password>")
        self.cur = self.conn.cursor()
        self.agbase = AgBase()
        self.algorithm = AlgorithmAPI(self.agbase)
        self.animal = AnimalAPI(self.agbase)
        self.farm = FarmAPI(self.agbase)
        self.herd = HerdAPI(self.agbase)
        self.measurement = MeasurementAPI(self.agbase)
        self.measurement_category = MeasurementCategoryAPI(self.agbase)
        self.agbase.set_logging_on(True)
        self.user = self.agbase.connect(args.user, args.passwd , args.server)
        self.pp = piprint.PrettyPrinter(indent=4)
        if self.user is None:
            print("login failed")
        farms = self.farm.get_farms(self.user)
        for farm in farms:
            if farm.name == "Demo Farm":
                self.frm = farm

        cats = self.measurement_category.get_measurement_categories()
        for cat in cats:
            if cat.name == "Weight":
                self.cat = cat

        algs = self.algorithm.get_algorithms(self.cat)
        for alg in algs:
            if alg.name == args.algorithm:  # need to set meathod_id to local moogles method_id in get_day
                self.alg = alg
                self.moogle_alg_id = self.get_algorithm_id()
                
                
    def get_algorithm_id(self):
        query = "select id from method where name = '{}';".format(self.alg.name)
        self.cur.execute(query)
        return self.cur.fetchone()[0]

    def get_animal_list(self):
        query = "select a.id, a.eid from animal as a join animalfarm as af on(a.id = af.animal_id) where a.vid IS NULL;" #limit(500)
        self.cur.execute(query)
        return self.cur.fetchall()

    def get_day(self, date, animalid):
        query = 'select * from weights where weights.animal_id = {animalid} and date <@ tsrange(\'{date}\', \'{date}\'::date + 1, \'[)\') and method_id = {methodid} and weighbridge_id = 8 order by DATE;'.format(animalid=animalid, date=date, methodid=self.moogle_alg_id)
        self.cur.execute(query)
        return self.cur.fetchall()

    def clean_up(self):
        self.cur.close()
        self.conn.close()

    def upload_animal_measurements_by_day(self):
        now = datetime.datetime.now()
        date = datetime.date(2015, 01, 01) # sets date to first day of the year
        animals = self.get_animal_list() # get animal list from the local database
        animal_max = len(animals)
        self.pp.pprint(animals)
        listIndex = 0
        self.meas_list = []
        self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))
               
        for i in range(0, 100): #change this if you want it to go for more days
            print "day: {}/100".format(i)
            animal_count = 0
            for animalID in animals:
                animal_count += 1
                print "animal {}/{}".format(animal_count, animal_max)
                measurements = self.get_day(date, animalID[0]) # gets the days measurements for the above animals on the above day
                if(len(measurements) > 0): # check to see if there are any measurements on that day
                    for measurement in measurements:
                        if(self.meas_list[listIndex].get_measurement_count() >= 500):
                            self.agbase.log("meas_list Dump >>> " + json.dumps(self.meas_list[listIndex].get_json()))
                            listIndex += 1
                            self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))
                            
                        if(not math.isnan(measurement[5]) or not math.isnan(measurement[6]) or not math.isnan(measurement[7])):
                            self.meas_list[listIndex].add_measurement(str(measurement[4]), measurement[5], None, measurement[6], None, measurement[7], animalID[1])

            date += datetime.timedelta(days=1)
            for measurementList in self.meas_list:
                self.measurement.upload_measurement_list(measurementList)

            self.meas_list = []
            listIndex = 0
            self.meas_list.append(self.measurement.create_bulk_measurement_upload_list(self.alg,self.user,None,self.frm.id))