Ejemplo n.º 1
0
 def test_equal(self):
     m1 = Monitor(self.url, self.http_status, self.response_time,
                  self.page_content, self.date_time)
     m2 = Monitor(self.url + "diff", self.http_status, self.response_time,
                  self.page_content, self.date_time)
     self.assertNotEqual(m1, m2)
     self.assertEqual(m1, m1)
     print(m1 == 1)
     self.assertNotEqual(m1, 1)
Ejemplo n.º 2
0
def main(name, address, client, proxy_client, server, proxy_server, output_dir,
         log_dir, tasks, size, timeout, stun_test, ipfs, dat, connect):

    assert (ipfs or
            dat) and not (ipfs and dat), "Please specify the IPFS or Dat flag"

    if client or proxy_client:

        if ipfs:
            cls = IPFSClientSession
        else:
            cls = DatClientSession

        if proxy_client:
            proxy_client = (address, proxy_client)

        logic = cls(name,
                    address,
                    output_dir,
                    log_dir,
                    int(tasks),
                    int(size),
                    proxy=proxy_client,
                    connect=connect)

    elif server or proxy_server:

        if ipfs:
            cls = IPFSServerSession
        else:
            cls = DatServerSession

        if proxy_server:
            proxy_server = (address, None)

        logic = cls(name,
                    address,
                    output_dir,
                    log_dir,
                    int(size),
                    proxy=proxy_server,
                    connect=connect)

    else:
        raise RuntimeError(
            "Neither (proxy) client or (proxy) server mode specified")

    if stun_test:
        perform_stun_test()

    session = Monitor(logic, timeout=int(timeout))
    session.start()
Ejemplo n.º 3
0
def main():
    # Ensure Python version
    if sys.version_info < (3, 0):
        print('Please use python 3 to run this program')

    # Get arguments from execution
    parser = ArgumentParser()
    parser.add_argument("-t",
                        "--target",
                        required=True,
                        help="Target website to monitor")
    parser.add_argument("-c",
                        "--config",
                        required=True,
                        help="Path to config file")
    parser.add_argument("-i",
                        "--interval",
                        required=False,
                        default=1,
                        help="Time interval in seconds to polling data")
    args = parser.parse_args()

    # Read configuration file
    with open(args.config) as fh:
        config_file = json.load(fh)

    kafka_config = config_file.get("kafka", {})

    # Initialise kafka producer
    producer = KafkaProducer(
        bootstrap_servers=kafka_config["kafka_url"],
        security_protocol="SSL",
        ssl_cafile=kafka_config['ssl_ca_file'],
        ssl_certfile=kafka_config['ssl_access_certificate_file'],
        ssl_keyfile=kafka_config['ssl_access_key_file'],
    )

    while True:
        monitor = Monitor(args.target)
        monitor.fetch()

        # Format monitoring data into JSON
        message = json.dumps(monitor.__dict__, indent=4)
        print("Sending: ", format(message))
        producer.send("monitoring", message.encode("utf-8"))

        # Force sending of all messages
        producer.flush()

        zzz = float(args.interval)
        print("sleeping for {} seconds Zzz...".format(zzz))
        time.sleep(zzz)
Ejemplo n.º 4
0
def main():
    # account like : 0x3121213232
    # tele like: '13012345678'
    monitor = Monitor('your-account')
    sender = SMSSender('your-api-key')
    tele = 'your-tele'
    while (1):
        offline_workders = monitor.get_offline_workers()

        if offline_workders:
            print('worker {0} are offline'.format(offline_workders))
            sender.send_warning(tele, offline_workders)
        else:
            print('no worker is offline')

        sleep(30)
Ejemplo n.º 5
0
    def test_to_string(self):
        expected = "<class 'monitor.monitor.Monitor'>: {'url': 'https://duckduckgo.com', 'http_status': 200, 'response_time': 0.100604, 'page_content': 'content', 'date_time': 1592379760.808488}"
        actual = str(
            Monitor(self.url, self.http_status, self.response_time,
                    self.page_content, self.date_time))

        self.assertEqual(expected, actual)
Ejemplo n.º 6
0
    def test_decode_json(self):
        expected = Monitor(self.url, self.http_status, self.response_time,
                           self.page_content, self.date_time)
        actual = self.monitor.decode_json(json.loads(self.sample))

        print("actual -", actual)
        print("expected", expected)
        self.assertEqual(expected, actual)
Ejemplo n.º 7
0
 def __init__(self, federated_clusters):
     redis_host = sys.argv[1]
     redis_port = sys.argv[2]
     self.redis = redis.Redis(host=redis_host, port=redis_port)
     self._monitors = []
     for cluster in federated_clusters:
         addr = CONFIG.get(cluster, "address")
         logging.info("Updated configuration for cluster: %s", cluster)
         self._monitors.append(Monitor(cluster, addr))
Ejemplo n.º 8
0
    def test_new_monitor_device_not_found(self):
        config = ConfigParser()
        config['system'] = {'key': 'testkey', 'secret': 'testsecret'}
        config['messaging'] = {'channel': '/test/channel'}
        with open(TEST_CONFIG, 'w') as f:
            config.write(f)

        with mock.patch('clearblade.ClearBladeCore.System') as m:
            with self.assertRaises(KeyError):
                monitor = Monitor(TEST_CONFIG)
Ejemplo n.º 9
0
    def test_new_monitor_happy_case(self):
        config = ConfigParser()
        config['system'] = {'key': 'testkey', 'secret': 'testsecret'}
        config['device'] = {'name': 'testname', 'active_key': 'testactivekey'}
        config['messaging'] = {'channel': '/test/channel'}
        with open(TEST_CONFIG, 'w') as f:
            config.write(f)

        with mock.patch('clearblade.ClearBladeCore.System.Device') as m:
            monitor = Monitor(TEST_CONFIG)
            self.assertEqual('/test/channel', monitor.channel)
Ejemplo n.º 10
0
    def test_new_monitor_messaging_not_found(self):
        config = ConfigParser()
        config['system'] = {'key': 'testkey', 'secret': 'testsecret'}
        config['device'] = {'name': 'testname', 'active_key': 'testactivekey'}
        with open(TEST_CONFIG, 'w') as f:
            config.write(f)

        with mock.patch('clearblade.ClearBladeCore.System.Device') as m:
            m.return_value = None
            with self.assertRaises(KeyError):
                monitor = Monitor(TEST_CONFIG)
Ejemplo n.º 11
0
def run_all():
    _name_ = 'main'
    print('\n【{}】【{}】 新一轮调度开始 at version {}'.format(datetime.now(), _name_,
                                                    flags.version))
    config_dict = None
    try:
        config_dict = read_db_config()
        print('【{}】【{}】 读取 config.text 文件成功'.format(datetime.now(), _name_))
        flags.version = 0
        flags.server_time_interval = config_dict['schedule_timestep_seconds']
    except Exception as e:
        print('【{}】【{}】 读取 config.txt 文件失败,原因:{}'.format(
            datetime.now(), _name_, repr(e)))
        flags.version = 1

    monitor = Monitor(config_dict)
    if flags.version == 0:
        monitor.run()
    server = Server(config_dict)
    server.run_real()
    a = 1
Ejemplo n.º 12
0
def auto_job():
    linux_dict = get_linux_config()
    content_list = []
    """ [(10.10.111.1,cd $(locate taskmngdomain1/nohuplogs) ; tail -50 *log*`date +%F`*),
         (10.10.111.2,cd $(locate taskmngdomain2/nohuplogs) ; tail -50 *log*`date +%F`*)]"""
    cmd_list = gol.get_value('cmd_list')
    for ip in linux_dict:
        linux_info = linux_dict[ip].split(
            ',')  # 根据ip获取user和pwd,通过逗号分隔成list(linux_info--> [user,pwd])
        user = linux_info[0]
        pwd = linux_info[1]
        monitor = Monitor(ip, user, pwd)
        # cmd的获取,添加根据ini不同ip实现动态对应linux指令
        for cmd_tuple in cmd_list:
            if cmd_tuple[0] == ip:
                cmd = cmd_tuple[1]  # 将指令赋予cmd
                break  # 若找到了,则不浪费循环资源直接跳出
        content = f'当前服务器ip:{ip},日志内容:\n' + monitor.link_server(cmd) + '\n\n'
        content_list.append(content)
    sendMail = SendMail()
    sendMail.send_mails(''.join(content_list))
    sendMail.mail_close()  #关闭连接
Ejemplo n.º 13
0
    def startCalibration(self, sensorName, port):
        """ Starts calibration and instantiates the EMT system

        :param sensorName: the uncalibrated sensor
        :param port: the port the uncalibrated sensor is connected to
        """
        if self.mode == MODE_IDLE:
            try:
                config = guiutils.import_default_config_settings()
                primaryChannels = config['system']['primary_channels']
                sensor = utils.get_sensor(sensorName)
                sensor.channel = utils.get_active_channel(sensor.dof, port, primaryChannels)
                calibration = EMCalibration(sensor, config)

                qtScheduler = QtScheduler(QtCore)
                newScheduler = NewThreadScheduler()
                self.systemMonitor = Monitor(calibration.anser)
                self.subscriptions.append(calibration.anser.sampleNotifications.sample(500, scheduler=newScheduler)\
                    .subscribe(self.systemMonitor.run_system_test))
                self.subscriptions.append(self.systemMonitor.systemNotifications.subscribe_on(scheduler=qtScheduler)\
                    .subscribe(self.SYS_EVENT_SYSTEM_STATUS_NOTIFICATION.emit))

                #remove this
                for i in range(3):
                    calibration.anser.sample_update()

                self.calibrationThread = CalibrationThread(calibration,
                                                           self.SYS_EVENT_POINT_CAPTURED,
                                                           self.SYS_EVENT_READY_TO_CALIBRATE,
                                                           self.SYS_EVENT_CALIBRATION_COMPLETED)
                self.calibrationThread.start()
                self.systemStatus = True
                self.SYS_EVENT_SYSTEM_STATUS.emit(self.systemStatus)
                self.SYS_EVENT_MODE_CALIBRATION.emit()
                self.SYS_EVENT_POINT_CAPTURED.emit(1)
                self.mode = MODE_CALIBRATING
                self.SYS_EVENT_MODE_CHANGED.emit(self.mode)

                logging.info('Started Calibration')
            except Exception as e:
                logging.info('Device cannot be accessed. Possible causes: '
                             '\n - Computer is not connected to DAQ port '
                             '\n - After plugging device into the USB Port, '
                             'wait a few moments  to let the driver install'
                             '\n - Ensure device specified is correct. '
                             '\n (Go to -> Developer Tab, in the configuration file under \'system\' change the \'device_name\' to your DevX indentifier) \n')
                print(str(e))
        elif self.mode == MODE_CALIBRATING:
            self.stopCalibration()
        else:
            logging.info('System is currently in use. Please stop tracking to continue')
Ejemplo n.º 14
0
def main():
    log.info('===============================>>>')
    log.info('启动服务器...')


    from database.mysqlConnPool import MysqlConn
    mysqlConner = MysqlConn(readconf('config.conf', 'mysql'))
    if not mysqlConner.testConnection():
        sys.exit(0)

    from collection.collection import Collection
    from communication.innerCommunication import InnerCommunication
    from monitor.monitor import Monitor


    innerCommun = InnerCommunication(readconf('config.conf','web-server'))
    innerCommun.createSingleProc()

    monitor = Monitor(readconf('config.conf','collection-server'))
    monitor.createProc()

    collection = Collection(readconf('config.conf','collection-server'))
    collection.createThreads()
Ejemplo n.º 15
0
 def __init__(self,
              model: nn.Module,
              criterion: nn.Module,
              dataset_name: str,
              accuracy_measure: Accuracy = None,
              env_suffix='',
              checkpoint_dir=CHECKPOINTS_DIR):
     if torch.cuda.is_available():
         model = model.cuda()
     self.model = model
     self.criterion = criterion
     self.dataset_name = dataset_name
     self.checkpoint_dir = Path(checkpoint_dir)
     self.train_loader = get_data_loader(dataset_name, train=True)
     self.timer = timer
     self.timer.init(batches_in_epoch=len(self.train_loader))
     self.env_name = f"{time.strftime('%Y.%m.%d')} {self.model.__class__.__name__}: " \
                     f"{self.dataset_name} {self.__class__.__name__} {self.criterion.__class__.__name__}"
     if env_suffix:
         self.env_name = self.env_name + f' {env_suffix}'
     if accuracy_measure is None:
         if isinstance(self.criterion, PairLoss):
             accuracy_measure = AccuracyEmbedding()
         else:
             # cross entropy loss
             accuracy_measure = AccuracyArgmax()
     self.accuracy_measure = accuracy_measure
     self.monitor = Monitor(test_loader=get_data_loader(self.dataset_name,
                                                        train=False),
                            accuracy_measure=self.accuracy_measure)
     for name, layer in find_named_layers(self.model,
                                          layer_class=self.watch_modules):
         self.monitor.register_layer(layer, prefix=name)
     images, labels = next(iter(self.train_loader))
     self.mask_trainer = MaskTrainer(accuracy_measure=self.accuracy_measure,
                                     image_shape=images[0].shape)
Ejemplo n.º 16
0
def monitor_factory(config_file):
    with open(config_file) as file_handle:
        configs = json.loads(file_handle.read())

    monitors = []
    for monitor_config in configs:
        validate_config(monitor_config)
        metrics = []
        for metric_config in monitor_config["metrics"]:
            metrics.append(metric_factory(metric_config))

        sinks = []
        for sink_config in monitor_config["sinks"]:
            sinks.append(sink_factory(sink_config))

        monitors.append(Monitor(metrics, sinks))
    return monitors
Ejemplo n.º 17
0
def test_monitorSizeMod(qtbot):
    '''
    Test the start of the PCV Mode, and then in PSV Mode
    '''

    assert qt_api.QApplication.instance() is not None

    esp32 = FakeESP32Serial(config)
    qtbot.addWidget(esp32)
    window = MainWindow(config, esp32)
    qtbot.addWidget(window)
    monitor = Monitor("Insp. Press.", config)
    qtbot.addWidget(monitor)

    assert monitor.configname == "Insp. Press."

    monitor.handle_resize(None)
    monitor.highlight()
    value = monitor.value
    monitor.update_value(10)

    assert monitor.value == 10
    monitor.value = value
Ejemplo n.º 18
0
def train_mask():
    """
    Train explainable mask for an image from ImageNet, using pretrained model.
    """
    model = torchvision.models.vgg19(pretrained=True)
    model.eval()
    for param in model.parameters():
        param.requires_grad_(False)
    normalize = torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                                 std=[0.229, 0.224, 0.225])
    transform = torchvision.transforms.Compose([
        torchvision.transforms.Resize(size=(224, 224)),
        torchvision.transforms.ToTensor(), normalize
    ])
    accuracy_measure = AccuracyArgmax()
    monitor = Monitor(test_loader=None, accuracy_measure=accuracy_measure)
    monitor.open(env_name='mask')
    monitor.normalize_inverse = NormalizeInverse(mean=normalize.mean,
                                                 std=normalize.std)
    image = Image.open(IMAGES_DIR / "flute.jpg")
    image = transform(image)
    mask_trainer = MaskTrainer(accuracy_measure=accuracy_measure,
                               image_shape=image.shape,
                               show_progress=True)
    monitor.log(repr(mask_trainer))
    if torch.cuda.is_available():
        model = model.cuda()
        image = image.cuda()
    outputs = model(image.unsqueeze(dim=0))
    proba = accuracy_measure.predict_proba(outputs)
    proba_max, label_true = proba[0].max(dim=0)
    print(f"True label: {label_true} (confidence {proba_max: .5f})")
    monitor.plot_mask(model=model,
                      mask_trainer=mask_trainer,
                      image=image,
                      label=label_true)
def experiment(args):
    """
    Note: 
    - data type is np.float16
    """
    paths = load_json(args.paths_config)

    for k, v in paths.items():
        if "PYTHONPATH" in k:
            sys.path.insert(0, v)

    from monitor.monitor import Monitor
    from repartition_experiments.scripts_exp.exp_utils import create_empty_dir, verify_results
    from repartition_experiments.algorithms.baseline_algorithm import baseline_rechunk
    from repartition_experiments.algorithms.keep_algorithm import keep_algorithm, get_input_aggregate
    from repartition_experiments.algorithms.utils import get_file_manager
    from repartition_experiments.algorithms.clustered_reads import clustered_reads

    # setting
    paths = load_json(args.paths_config)
    cases = load_json(args.cases_config)
    bpv = 2
    indir_path, outdir_path = os.path.join(paths["ssd_path"],
                                           'indir'), os.path.join(
                                               paths["ssd_path"], 'outdir')

    # creating empty output directories
    create_empty_dir(outdir_path)
    if args.distributed:
        print(f"Distributed mode -> creating the output directories")
        for i in range(6):
            dirpath = '/disk' + str(i) + '/gtimothee'
            create_empty_dir(os.path.join(dirpath, 'output'))
    fm = get_file_manager(args.file_format)
    if args.overwrite:
        fm.remove_all(paths["ssd_path"])

    # transform cases into tuples + perform sanity check
    case = cases[args.case_name]
    for run in case:
        R, O, I, B, volumestokeep = tuple(run["R"]), tuple(run["O"]), tuple(
            run["I"]), tuple(run["B"]), run["volumestokeep"]
        if args.case_name.split('_')[0] == "case 1":
            lambd = get_input_aggregate(O, I)
            B, volumestokeep = (lambd[0], lambd[1],
                                lambd[2]), list(range(1, 8))
            run["volumestokeep"] = volumestokeep

        run["R"] = R
        run["O"] = O
        run["I"] = I
        run["B"] = B

        for shape_to_test in [O, I, B]:
            for dim in range(3):
                try:
                    assert R[dim] % shape_to_test[dim] == 0
                except Exception as e:
                    print(R, shape_to_test)
                    print(e)

    random.shuffle(case)
    results = list()
    R_prev, I_prev = (0, 0, 0), (0, 0, 0)
    for run in case:
        R, O, I, B, volumestokeep = run["R"], run["O"], run["I"], run[
            "B"], run["volumestokeep"]
        ref = run["ref"]
        print(
            f'Case being processed: (ref: {ref}) {R}, {I}, {O}, {B}, {volumestokeep}'
        )
        filename = f'{R[0]}_{R[1]}_{R[2]}_original.hdf5'
        origarr_filepath = os.path.join(paths["ssd_path"], filename)

        # resplit
        print("processing...")

        flush_cache()
        print(f"cache flushed")

        if args.model == "baseline":
            _monitor = Monitor(enable_print=False,
                               enable_log=False,
                               save_data=True)
            _monitor.disable_clearconsole()
            _monitor.set_delay(15)
            _monitor.start()
            t = time.time()
            tread, twrite, seeks_data = baseline_rechunk(
                indir_path, outdir_path, O, I, R, args.file_format,
                args.addition, args.distributed)
            t = time.time() - t
            _monitor.stop()
            piles = _monitor.get_mem_piles()
            max_voxels = 0
            print(f"Processing time: {t}")
            print(f"Read time: {tread}")
            print(f"Write time: {twrite}")
            tpp = 0
            voxel_tracker = None
        elif args.model == "keep":
            print(f"Running keep...")
            t = time.time()
            tpp, tread, twrite, seeks_data, voxel_tracker, piles = keep_algorithm(
                R, O, I, B, volumestokeep, args.file_format, outdir_path,
                indir_path, args.addition, args.distributed)
            t = time.time() - t - tpp
            max_voxels = voxel_tracker.get_max()
            print(f"Processing time: {t}")
            print(f"Read time: {tread}")
            print(f"Write time: {twrite}")
        elif args.model == "clustered":
            tpp = 0
            m = args.clustered_mem * 1000000000  # one GIG

            _monitor = Monitor(enable_print=False,
                               enable_log=False,
                               save_data=True)
            _monitor.disable_clearconsole()
            _monitor.set_delay(15)
            _monitor.start()
            t = time.time()
            tread, twrite, seeks_data = clustered_reads(
                outdir_path, R, I, bpv, m, args.file_format, indir_path)
            t = time.time() - t - tpp
            _monitor.stop()
            piles = _monitor.get_mem_piles()

            voxel_tracker = None
            max_voxels = 0

            print(f"Processing time: {t}")
            print(f"Read time: {tread}")
            print(f"Write time: {twrite}")
        else:
            raise ValueError("Bad model name")

        # verify and clean output
        print("verifying results....")
        if args.verify:
            split_merge = False
            if args.case_name == "case 3":
                split_merge = True
            success = verify_results(outdir_path, origarr_filepath, R, O,
                                     args.file_format, args.addition,
                                     split_merge)
        else:
            success = True
        print("successful run: ", success)

        results.append([
            args.case_name, run["ref"], args.model, t, tpp, tread, twrite,
            seeks_data[0], seeks_data[1], seeks_data[2], seeks_data[3],
            max_voxels, success
        ])
        create_empty_dir(outdir_path)
        R_prev, I_prev = R, I

        write_memory_pile(piles[0], piles[1], run["ref"], args)
        if voxel_tracker != None:
            write_voxel_history(voxel_tracker, run["ref"], args)

    return results
Ejemplo n.º 20
0
 def setUp(self) -> None:
     self._monitor = Monitor("ws://test.com", "eth9", "user", "pass", True)
     self._monitor._update_output = Mock()
Ejemplo n.º 21
0
class MonitorTest(TestCase):
    def setUp(self) -> None:
        self._monitor = Monitor("ws://test.com", "eth9", "user", "pass", True)
        self._monitor._update_output = Mock()

    def test_single_frame_message(self):
        self._monitor.on_ws_message(
            self._read_sample("single_frame_message.txt"))
        self._monitor._update_output.assert_called_once()
        self._monitor._update_output.assert_called_with(
            27296, 27832, "13.123.123.123")

    def test_single_frame_multi_message(self):
        self._monitor.on_ws_message(
            self._read_sample("single_frame_multi_message.txt"))
        self._monitor._update_output.assert_has_calls([
            call(27296, 27832, "13.123.123.123"),
            call(27304, 27760, "13.123.123.123")
        ])

    def test_multiple_frame_message(self):
        self._monitor.on_ws_message(
            self._read_sample("multi_frame_message_1.txt"))
        self._monitor.on_ws_message(
            self._read_sample("multi_frame_message_2.txt"))
        self._monitor._update_output.assert_called_once()
        self._monitor._update_output.assert_called_with(
            1978872, 208192, "13.123.123.123")

    def test_multiple_frame_with_naked_header(self):
        self._monitor.on_ws_message(
            self._read_sample("multi_frame_message_naked_1.txt"))
        self._monitor.on_ws_message(
            self._read_sample("multi_frame_message_naked_2.txt"))
        self._monitor._update_output.assert_called_once()
        self._monitor._update_output.assert_called_with(
            570848, 51624, "13.123.123.123")

    def test_human_rates(self):
        self.assertEqual(self._monitor._human_bps(123), "123 bps")
        self.assertEqual(self._monitor._human_bps(1250), "1.2 Kbps")
        self.assertEqual(self._monitor._human_bps(12500), "12.5 Kbps")
        self.assertEqual(self._monitor._human_bps(125000), "125.0 Kbps")
        self.assertEqual(self._monitor._human_bps(1250000), "1.2 Mbps")
        self.assertEqual(self._monitor._human_bps(12500000), "12.5 Mbps")
        self.assertEqual(self._monitor._human_bps(125000000), "125.0 Mbps")
        self.assertEqual(self._monitor._human_bps(1250000000), "1.2 Gbps")

    def _read_sample(self, file_name: str) -> str:
        abs_path = f"{dirname(abspath(__file__))}/{file_name}"
        with open(abs_path, "r") as file:
            return file.read()
Ejemplo n.º 22
0
class TestMonitor(TestCase):
    monitor = Monitor()

    def setUp(self):
        self.url = "https://duckduckgo.com"
        self.http_status = 200
        self.response_time = 0.100604
        self.page_content = "content"
        self.date_time = 1592379760.808488

        self.sample = '''
        {{
            "url": "{}",
            "http_status": {},
            "response_time": {},
            "page_content": "{}",
            "date_time": {}
        }}
        '''.format(self.url, self.http_status, self.response_time,
                   self.page_content, self.date_time)

    def test_equal(self):
        m1 = Monitor(self.url, self.http_status, self.response_time,
                     self.page_content, self.date_time)
        m2 = Monitor(self.url + "diff", self.http_status, self.response_time,
                     self.page_content, self.date_time)
        self.assertNotEqual(m1, m2)
        self.assertEqual(m1, m1)
        print(m1 == 1)
        self.assertNotEqual(m1, 1)

    def test_to_string(self):
        expected = "<class 'monitor.monitor.Monitor'>: {'url': 'https://duckduckgo.com', 'http_status': 200, 'response_time': 0.100604, 'page_content': 'content', 'date_time': 1592379760.808488}"
        actual = str(
            Monitor(self.url, self.http_status, self.response_time,
                    self.page_content, self.date_time))

        self.assertEqual(expected, actual)

    def test_class_representation(self):
        expected = "Monitor()"
        actual = repr(Monitor())
        self.assertEqual(expected, actual)

    def test_decode_json(self):
        expected = Monitor(self.url, self.http_status, self.response_time,
                           self.page_content, self.date_time)
        actual = self.monitor.decode_json(json.loads(self.sample))

        print("actual -", actual)
        print("expected", expected)
        self.assertEqual(expected, actual)

    # We patch 'requests.get' with our own method. The mock object is passed in to our test case method.

    @mock.patch('requests.get', side_effect=mocked_requests_get)
    def test_fetch(self, mock_get):
        # Assert requests.get calls
        url = 'http://up.com'
        expected = Monitor(url, 200, 1, "html text", 1592379760.808488)
        actual = Monitor(url)
        actual.fetch()
        actual.date_time = 1592379760.808488  # setting of date_time is not part of the

        self.assertEqual(expected, actual)

        url = 'http://exceotion.com'
        expected = Monitor(url, None, None, None)

        actual = Monitor(url)
        actual.fetch()

        self.assertEqual(expected, actual)
Ejemplo n.º 23
0
    def __init__(self, config, esp32, *args, **kwargs):
        #pylint: disable=too-many-statements
        """
        Initializes the main window for the MVM GUI. See below for subfunction setup description.
        """

        super(MainWindow, self).__init__(*args, **kwargs)
        uifile = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                              "mainwindow.ui")

        uic.loadUi(uifile, self)

        self.config = config
        self.esp32 = esp32
        settings_file = SettingsFile(self.config["settings_file_path"])
        self.user_settings = settings_file.load()
        '''
        Start the alarm handler, which will check for ESP alarms
        '''
        # Instantiate the critical alarm handler meant for severe communications and hardware error
        self.critical_alarm_handler = CriticalAlarmHandler(self, esp32)
        self.alarm_h = AlarmHandler(
            self.config, self.esp32, self.alarmbar,
            self.critical_alarm_handler.call_system_failure)
        '''
        Get the toppane and child pages
        '''
        self.toppane = self.findChild(QtWidgets.QStackedWidget, "toppane")
        self.main = self.findChild(QtWidgets.QWidget, "main")
        self.initial = self.findChild(QtWidgets.QWidget, "initial")
        self.startup = self.findChild(QtWidgets.QWidget, "startup")
        self.criticalerrorpage = self.findChild(QtWidgets.QWidget,
                                                "criticalerrorpage")
        '''
        Get the center pane (plots) widgets
        '''
        self.centerpane = self.findChild(QtWidgets.QStackedWidget,
                                         "centerpane")
        self.plots_all = self.findChild(QtWidgets.QWidget, "plots_all")
        self.alarms_settings = self.findChild(QtWidgets.QWidget,
                                              "alarms_settings")
        '''
        Get the bottombar and child pages
        '''
        self.bottombar = self.findChild(QtWidgets.QStackedWidget, "bottombar")
        self.toolbar = self.findChild(QtWidgets.QWidget, "toolbar")
        self.menu = self.findChild(QtWidgets.QWidget, "menu")
        self.frozen_bot = self.findChild(QtWidgets.QWidget,
                                         "frozenplots_bottom")
        self.settingsbar = self.findChild(QtWidgets.QWidget, "settingsbar")
        self.specialbar = self.findChild(QtWidgets.QWidget, "specialbar")
        self.blank = self.findChild(QtWidgets.QWidget, "blank")
        self.settingsfork = self.findChild(QtWidgets.QWidget,
                                           "settingsforkbar")
        self.alarmsbar = self.findChild(QtWidgets.QWidget, "alarmsbar")
        self.numpadbar = self.findChild(QtWidgets.QWidget, "numpadbar")
        self.criticalerrorbar = self.findChild(QtWidgets.QWidget,
                                               "criticalerrorbar")
        '''
        Get the stackable bits on the right
        '''
        self.rightbar = self.main.findChild(QtWidgets.QStackedWidget,
                                            "rightbar")
        self.monitors_bar = self.main.findChild(QtWidgets.QWidget,
                                                "monitors_bar")
        self.frozen_right = self.main.findChild(QtWidgets.QWidget,
                                                "frozenplots_right")
        '''
        Get initial and startup buttons
        '''
        self.button_new_patient = self.initial.findChild(
            QtWidgets.QPushButton, "button_new_patient")
        self.button_resume_patient = self.initial.findChild(
            QtWidgets.QPushButton, "button_resume_patient")
        self.button_resume_patient.setEnabled(self.user_settings != {})

        self.button_start_settings = self.startup.findChild(
            QtWidgets.QPushButton, "button_start_settings")
        self.button_start_vent = self.startup.findChild(
            QtWidgets.QPushButton, "button_start_vent")
        self.button_start_test = self.startup.findChild(
            QtWidgets.QPushButton, "button_start_test")
        '''
        Get toolbar widgets
        '''
        self.button_menu = self.toolbar.findChild(QtWidgets.QPushButton,
                                                  "button_menu")
        self.button_unlockscreen = self.toolbar.findChild(
            QtWidgets.QPushButton, "button_unlockscreen")
        self.home_button = self.toolbar.findChild(QtWidgets.QWidget,
                                                  "home_button")
        self.goto_menu = self.toolbar.findChild(QtWidgets.QWidget, "goto_menu")
        self.goto_unlock = self.toolbar.findChild(QtWidgets.QWidget,
                                                  "goto_unlock")
        self.label_status = self.toolbar.findChild(QtWidgets.QLabel,
                                                   "label_status")

        # Get menu widgets and connect settings for the menu widget
        self.button_back = self.menu.findChild(QtWidgets.QPushButton,
                                               "button_back")
        self.button_settingsfork = self.menu.findChild(QtWidgets.QPushButton,
                                                       "button_settingsfork")
        self.button_startstop = self.menu.findChild(QtWidgets.QPushButton,
                                                    "button_startstop")
        self.button_autoassist = self.menu.findChild(QtWidgets.QPushButton,
                                                     "button_autoassist")
        self.button_specialops = self.menu.findChild(QtWidgets.QPushButton,
                                                     "button_specialops")

        self.button_alarms = self.settingsfork.findChild(
            QtWidgets.QPushButton, "button_alarms")
        self.button_settings = self.settingsfork.findChild(
            QtWidgets.QPushButton, "button_settings")
        self.button_lockscreen = self.settingsfork.findChild(
            QtWidgets.QPushButton, "button_lockscreen")
        self.button_backsettings = self.settingsfork.findChild(
            QtWidgets.QPushButton, "button_backsettings")

        self.button_backalarms = self.alarmsbar.findChild(
            QtWidgets.QPushButton, "button_backalarms")
        self.button_applyalarm = self.alarmsbar.findChild(
            QtWidgets.QPushButton, "button_applyalarm")
        self.button_resetalarm = self.alarmsbar.findChild(
            QtWidgets.QPushButton, "button_resetalarm")
        self.button_upalarm = self.alarmsbar.findChild(QtWidgets.QPushButton,
                                                       "button_upalarm")
        self.button_downalarm = self.alarmsbar.findChild(
            QtWidgets.QPushButton, "button_downalarm")
        self.button_offalarm = self.alarmsbar.findChild(
            QtWidgets.QPushButton, "button_offalarm")

        self.button_freeze = self.specialbar.findChild(QtWidgets.QPushButton,
                                                       "button_freeze")
        self.button_backspecial = self.specialbar.findChild(
            QtWidgets.QPushButton, "button_backspecial")

        # Get frozen plots bottom bar widgets and connect
        self.button_unfreeze = self.frozen_bot.findChild(
            QtWidgets.QPushButton, "button_unfreeze")

        # Connect initial startup buttons
        self.button_resume_patient.pressed.connect(self.goto_resume_patient)
        self.button_new_patient.pressed.connect(self.goto_new_patient)
        self.button_start_vent.pressed.connect(self.goto_main)
        self.button_start_test.pressed.connect(self.goto_selftest)
        self.button_spiro_calib.pressed.connect(self.goto_spiro_calibration)
        self.button_start_settings.pressed.connect(self.goto_settings)

        # Connect back and menu buttons to toolbar and menu
        # This effectively defines navigation from the bottombar.

        # Toolbar
        self.button_menu.pressed.connect(self.show_menu)

        # Menu
        self.button_back.pressed.connect(self.show_toolbar)
        self.button_alarms.pressed.connect(self.goto_alarms)
        self.button_settingsfork.pressed.connect(self.show_settingsfork)
        self.button_specialops.pressed.connect(self.show_specialbar)

        # Settings
        self.button_settings.pressed.connect(self.goto_settings)
        self.button_lockscreen.pressed.connect(self.lock_screen)
        self.button_backsettings.pressed.connect(self.show_menu)

        # Special
        self.button_freeze.pressed.connect(self.freeze_plots)
        self.button_unfreeze.pressed.connect(self.unfreeze_plots)
        self.button_backspecial.pressed.connect(self.show_menu)

        # Confirmation bar
        self.messagebar = MessageBar(self)
        self.bottombar.insertWidget(self.bottombar.count(), self.messagebar)
        # Spirometer Calibration
        self.spiro_calib.connect_mainwindow_esp32(self, self.esp32)
        # Self Test
        self.self_test.connect_mainwindow_esp32_selftestbar(
            self, self.esp32, self.selftestbar)

        # Assign unlock screen button and setup state
        self.unlockscreen_interval = self.config['unlockscreen_interval']
        self.button_unlockscreen._state = 0
        self.button_unlockscreen.setAutoRepeat(True)
        self.button_unlockscreen.setAutoRepeatInterval(
            self.unlockscreen_interval)
        self.button_unlockscreen.clicked.connect(self.handle_unlock)

        self.numpad = NumPad(self)
        self.numpad.assign_code(self.config['unlockscreen_code'],
                                self.unlock_screen)

        self.numpad.button_back.pressed.connect(self.lock_screen)
        self.button_backalarms.pressed.connect(self.exit_alarms)

        #Instantiate the DataFiller, which takes
        #care of filling plots data
        self.data_filler = DataFiller(config)

        #Set up tool settings (bottom bar)

        #self.toolsettings[..] are the objects that hold min, max values for a given setting as
        #as the current value (displayed as a slider and as a number).
        toolsettings_names = {
            "toolsettings_1", "toolsettings_2", "toolsettings_3"
        }
        self.toolsettings = {}

        for name in toolsettings_names:
            toolsettings = self.toolbar.findChild(QtWidgets.QWidget, name)
            toolsettings.connect_config(config)
            self.toolsettings[name] = toolsettings

        # Set up data monitor/alarms (side bar) and plots

        #self.monitors[..] are the objects that hold monitor values and
        #thresholds for alarm min and max. The current value and
        #optional stats for the monitored value (mean, max) are set
        #here.
        # plot slot widget names
        self.plots = {}
        for name in config['plots']:
            plot = self.main.findChild(QtWidgets.QWidget, name)
            plot.setFixedHeight(130)
            self.data_filler.connect_plot(name, plot)
            self.plots[name] = plot

        # The monitored fields from the default_settings.yaml config file
        self.monitors = {}
        for name in config['monitors']:
            monitor = Monitor(name, config)
            self.monitors[name] = monitor
            self.data_filler.connect_monitor(monitor)

        # The alarms are from the default_settings.yaml config file
        # self.alarms = {}
        # for name in config['alarms']:
        #     alarm = GuiAlarm(name, config, self.monitors, self.alarm_h)
        #     self.alarms[name] = alarm
        self.gui_alarm = GuiAlarms(config, self.esp32, self.monitors)
        for monitor in self.monitors.values():
            monitor.connect_gui_alarm(self.gui_alarm)

        # Get displayed monitors
        self.monitors_slots = self.main.findChild(QtWidgets.QVBoxLayout,
                                                  "monitors_slots")
        self.alarms_settings.connect_monitors(self)
        self.alarms_settings.populate_monitors()
        self.button_applyalarm.pressed.connect(
            self.alarms_settings.apply_selected)
        self.button_resetalarm.pressed.connect(
            self.alarms_settings.reset_selected)
        self.button_offalarm.pressed.connect(
            self.alarms_settings.move_selected_off)
        self.button_upalarm.pressed.connect(
            self.alarms_settings.move_selected_up)
        self.button_downalarm.pressed.connect(
            self.alarms_settings.move_selected_down)

        # Connect the frozen plots
        # Requires building of an ordered array to associate the correct
        # controls with the plot.
        active_plots = []
        for slotname in self.plots:
            active_plots.append(self.plots[slotname])
        self.cursor = Cursor(active_plots)
        self.frozen_bot.connect_workers(self.data_filler, active_plots,
                                        self.cursor)
        self.frozen_right.connect_workers(active_plots, self.cursor)

        #Instantiate DataHandler, which will start a new
        #thread to read data from the ESP32. We also connect
        #the DataFiller to it, so the thread will pass the
        #data directly to the DataFiller, which will
        #then display them.
        self._data_h = DataHandler(config, self.esp32, self.data_filler,
                                   self.gui_alarm)

        self.specialbar.connect_datahandler_config_esp32(
            self._data_h, self.config, self.esp32, self.messagebar)

        #Connect settings button to Settings overlay.
        self.settings = Settings(self)
        self.toppane.insertWidget(self.toppane.count(), self.settings)

        #Set up start/stop auto/min mode buttons.

        #Connect each to their respective mode toggle functions.
        #The StartStopWorker class takes care of starting and stopping a run

        self._start_stop_worker = StartStopWorker(self, self.config,
                                                  self.esp32,
                                                  self.button_startstop,
                                                  self.button_autoassist,
                                                  self.toolbar, self.settings)

        if self._start_stop_worker.is_running():
            self.goto_main()

        self.button_startstop.released.connect(
            self._start_stop_worker.toggle_start_stop)
        self.button_autoassist.released.connect(
            self._start_stop_worker.toggle_mode)
        self.gui_alarm.connect_workers(self._start_stop_worker)
Ejemplo n.º 24
0
def _run_keep(arrays_dict, buffers, buffers_to_infiles, buffer_to_outfiles):
    """
    Arguments: 
    ----------
        arrays_dict: dictionary mapping each output block index to its list of write blocks
        buffers: list of Volume objects (see utils.py) representing the read buffers. Each volume contains the coordinates of the buffer in the original image.  
        buffers_to_infiles: maps each read buffer index to the list of input blocks it crosses -> to know which input block to read
        buffer_to_outfiles: maps each read buffer index to the list of output blocks it crosses -> to only search for the write buffers linked to that read buffer
    """
    cache = dict()
    voxel_tracker = VoxelTracker()
    nb_infile_openings = 0
    nb_infile_inside_seeks = 0
    nb_buffers = len(buffers.keys())
    read_time = 0
    write_time = 0

    from monitor.monitor import Monitor
    _monitor = Monitor(enable_print=False, enable_log=False, save_data=True)
    _monitor.disable_clearconsole()
    _monitor.set_delay(5)
    _monitor.start()
    
    buffer_shape = buffers[0].get_shape()
    buffer_size = buffer_shape[0] * buffer_shape[1] * buffer_shape[2] * 2
    buffer_data = np.empty(copy.deepcopy(buffer_shape), dtype=np.float16)
    voxel_tracker.add_voxels(buffer_size)

    # for each read buffer
    for buffer_index in range(nb_buffers):
        print("\nBUFFER ", buffer_index, '/', nb_buffers)
        if DEBUG:
            print_mem_info() 
            
        buffer = buffers[buffer_index]
        nb_opening_seeks_tmp, nb_inside_seeks_tmp, t1, t2 = process_buffer(buffer_data, arrays_dict, buffers, buffer, voxel_tracker, buffers_to_infiles, buffer_to_outfiles, cache)
        
        read_time += t1
        write_time += t2
        nb_infile_openings += nb_opening_seeks_tmp
        nb_infile_inside_seeks += nb_inside_seeks_tmp

        if DEBUG:
            print("End of buffer - Memory info:")
            print_mem_info()

            if buffer_index == 1:
                sys.exit()

        buffer_data = np.empty(copy.deepcopy(buffer_shape), dtype=np.float16)

    file_manager.close_infiles()

    _monitor.stop()
    ram_pile, swap_pile = _monitor.get_mem_piles()
    return [read_time, write_time], ram_pile, swap_pile, nb_infile_openings, nb_infile_inside_seeks, voxel_tracker
Ejemplo n.º 25
0
    def test_fetch(self, mock_get):
        # Assert requests.get calls
        url = 'http://up.com'
        expected = Monitor(url, 200, 1, "html text", 1592379760.808488)
        actual = Monitor(url)
        actual.fetch()
        actual.date_time = 1592379760.808488  # setting of date_time is not part of the

        self.assertEqual(expected, actual)

        url = 'http://exceotion.com'
        expected = Monitor(url, None, None, None)

        actual = Monitor(url)
        actual.fetch()

        self.assertEqual(expected, actual)
Ejemplo n.º 26
0
 def run_wrapper(self):
     Monitor('config_test.yaml').run()
Ejemplo n.º 27
0
from monitor.monitor import Monitor
import time

if __name__ == "__main__":
    _monitor = Monitor()
    _monitor.system_info()
    _monitor.start()
    try:
        for i in range(50):
            time.sleep(0.2)
    finally:
        _monitor.stop()
Ejemplo n.º 28
0
from time import sleep
from psutil import cpu_percent
from monitor.monitor import Monitor
from monitor.exiter import Exiter


def cpu_utilization():
    cpu_percent_strs = [str(percent) for percent in cpu_percent(percpu=True)]
    return ','.join(cpu_percent_strs)

if __name__ == '__main__':
    monitor = Monitor()

    monitor.connect()

    exiter = Exiter()
    while not exiter.should_exit:
        monitor.publish(cpu_utilization())
        sleep(5)

    monitor.disconnect()
Ejemplo n.º 29
0
from monitor.monitor import MonitorSocketThread, Monitor, db

if __name__ == '__main__':
    ips = db.get_all_monitor_ip()
    monitor = Monitor(ips)
    monitor_sock_thr = MonitorSocketThread(monitor)
    monitor_sock_thr.setDaemon(True)
    monitor_sock_thr.start()
    monitor.run()  

Ejemplo n.º 30
0
 def run_wrapper(self):
     Monitor('config_empty_config.yaml').run()
Ejemplo n.º 31
0
 def test_class_representation(self):
     expected = "Monitor()"
     actual = repr(Monitor())
     self.assertEqual(expected, actual)