def run(self):
        """
        Periodically runs to gather new data and populate the databases.
        """
        while self.running:
            all_data = gather_data()
            for inst in self.inst_list:
                if inst["isScheduled"]:
                    name, host = correct_name(inst["name"]), inst["hostName"]
                    instrument_list = filter_instrument_data(all_data, name)
                    if not instrument_list:
                        logging.error(
                            f"Unable to update {name}, no data found. Expired data will still be cleared."
                        )
                        data_to_populate = None
                    else:
                        data_to_populate = reformat_data(instrument_list)
                    try:
                        update(name, host, self.db_lock, data_to_populate,
                               self.run_continuous)
                    except Exception as e:
                        logging.error("Unable to connect to {}: {}".format(
                            name, e))

            if self.run_continuous:
                for i in range(POLLING_TIME):
                    sleep(1)
                    if not self.running:
                        return
            else:
                break
예제 #2
0
    def test_GIVEN_contact_and_user_and_pi_and_corresponding_date_WHEN_data_formatted_THEN_experiment_teams_populated(
            self):
        data = [
            create_web_data_with_experimenters([TEST_USER_1, TEST_USER_PI])
        ]
        experiments, experiment_teams = reformat_data(data)

        expected_user_1 = UserData(TEST_PI_NAME, TEST_PI_ORG)
        expected_user_data_1 = ExperimentTeamData(expected_user_1,
                                                  TEST_PI_ROLE, TEST_RBNUMBER,
                                                  TEST_DATE)

        expected_user_2 = UserData(TEST_USER_1_NAME, TEST_USER_1_ORG)
        expected_user_data_2 = ExperimentTeamData(expected_user_2,
                                                  TEST_USER_1_ROLE,
                                                  TEST_RBNUMBER, TEST_DATE)

        expected_user_3 = UserData(
            TEST_CONTACT_NAME, "Science and Technology Facilities Council")
        expected_user_data_3 = ExperimentTeamData(expected_user_3, "Contact",
                                                  TEST_RBNUMBER, TEST_DATE)

        expected_user_data = [
            expected_user_data_3, expected_user_data_2, expected_user_data_1
        ]

        self.assertEqual(len(experiment_teams), 3)
        self.assertTrue(expected_user_data == experiment_teams)
예제 #3
0
    def test_GIVEN_data_WHEN_data_formatted_THEN_experiment_list_populated(
            self):
        experiments, experiment_teams = reformat_data(TEST_DATA)

        self.assertEqual(len(experiments), 1)
        exp_entry = experiments[0]
        self.assertEqual(exp_entry[Experiment.experimentid], TEST_RBNUMBER)
        self.assertEqual(exp_entry[Experiment.startdate], TEST_DATE)
        self.assertEqual(exp_entry[Experiment.duration], TEST_TIMEALLOCATED)
예제 #4
0
    def test_GIVEN_data_with_local_contacts_and_corresponding_date_WHEN_data_formatted_THEN_experiment_teams_populated(
            self):
        experiments, experiment_teams = reformat_data(TEST_DATA)

        expected_user = UserData(TEST_CONTACT_NAME, LOCAL_ORG)
        expected_user_data = ExperimentTeamData(expected_user, LOCAL_ROLE,
                                                TEST_RBNUMBER, TEST_DATE)

        self.assertEqual(len(experiment_teams), 1)
        self.assertTrue(expected_user_data == experiment_teams[0])
예제 #5
0
    def test_GIVEN_rb_with_multiple_start_dates_WHEN_data_formatted_THEN_two_experiments_added(
            self):
        data = TEST_DATA + [create_data(TEST_RBNUMBER, datetime.now(), 3)]
        experiments, experiment_teams = reformat_data(data)

        self.assertEqual(len(experiments), 2)
        first_exp, second_exp = experiments[0], experiments[1]
        self.assertEqual(first_exp[Experiment.experimentid],
                         second_exp[Experiment.experimentid])
        self.assertEqual(first_exp[Experiment.experimentid], TEST_RBNUMBER)
        self.assertNotEqual(first_exp[Experiment.startdate],
                            second_exp[Experiment.startdate])
예제 #6
0
    def test_GIVEN_rb_with_multiple_start_dates_WHEN_data_formatted_THEN_two_local_contacts_added(
            self):
        data = TEST_DATA + [create_data(TEST_RBNUMBER, datetime.now(), 3)]
        experiments, experiment_teams = reformat_data(data)

        self.assertEqual(len(experiment_teams), 2)
        self.assertTrue(experiment_teams[0].user == experiment_teams[1].user)
        self.assertEqual(experiment_teams[0].role, experiment_teams[1].role)
        self.assertEqual(experiment_teams[0].rb_number,
                         experiment_teams[1].rb_number)
        self.assertNotEqual(experiment_teams[0].start_date,
                            experiment_teams[1].start_date)
예제 #7
0
    def test_GIVEN_data_with_different_dates_WHEN_data_formatted_THEN_experiment_list_contains_both(
            self):
        rb, date, duration = 2000, datetime(1992, 2, 7), 10
        data = TEST_DATA + [create_data(rb, date, duration)]
        experiments, experiment_teams = reformat_data(data)

        self.assertEqual(len(experiments), 2)
        for entry in experiments:
            if entry[Experiment.experimentid] == TEST_RBNUMBER:
                self.assertEqual(entry[Experiment.startdate], TEST_DATE)
                self.assertEqual(entry[Experiment.duration],
                                 TEST_TIMEALLOCATED)
            else:
                self.assertEqual(entry[Experiment.experimentid], rb)
                self.assertEqual(entry[Experiment.startdate], date)
                self.assertEqual(entry[Experiment.duration], duration)
예제 #8
0
    def test_GIVEN_rb_with_multiple_start_dates_WHEN_data_formatted_THEN_two_other_users_added(
            self):
        data = [
            create_web_data_with_experimenters([TEST_USER_1]),
            create_web_data_with_experimenters_and_other_date([TEST_USER_1],
                                                              datetime.now())
        ]
        experiments, experiment_teams = reformat_data(data)

        self.assertEqual(len(experiment_teams), 4)
        self.assertTrue(experiment_teams[1].user == experiment_teams[3].user)
        self.assertEqual(experiment_teams[1].role, experiment_teams[3].role)
        self.assertEqual(experiment_teams[1].rb_number,
                         experiment_teams[3].rb_number)
        self.assertNotEqual(experiment_teams[1].start_date,
                            experiment_teams[3].start_date)
    parser.add_argument('--as_instrument', type=str, default=None,
                        help="Puts the specified instruments data into the local database")
    parser.add_argument('--db_user', type=str, default=None,
                        help="The username to use for writing to the database")
    parser.add_argument('--db_pass', type=str, default=None,
                        help="The password to use for writing to the database")
    args = parser.parse_args()

    main = InstrumentPopulatorRunner(args.cont)
    if args.as_instrument:
        debug_inst_list = [{"name": args.as_instrument, "hostName": "localhost", "isScheduled": True}]
        main.prev_inst_list = debug_inst_list
        main.inst_list_changes(debug_inst_list)
    elif args.test_data:
        data = [create_web_data_with_experimenters_and_other_date([TEST_USER_1], datetime.now())]
        update("localhost", "localhost", threading.RLock(), reformat_data(data),
               credentials=(args.db_user, args.db_pass))
    else:
        main.start_inst_list_monitor()

        if args.cont:
            running = True
            menu_string = 'Enter U to force update from instrument list or Q to Quit\n '

            while running:
                menu_input = input(menu_string).upper()
                if menu_input and isinstance(menu_input, str):
                    logging.info("User entered {}".format(menu_input))
                    if menu_input == "Q":
                        main.remove_gatherer()
                        running = False
예제 #10
0
    def test_GIVEN_no_data_set_WHEN_data_formatted_THEN_no_data_set(self):
        experiments, experiment_teams = reformat_data([])

        self.assertEqual(experiments, [])
        self.assertEqual(experiment_teams, [])