Esempio n. 1
0
 def testTaskCreate(self):
     # Test that a Task can be created.
     task = model.Task(key_name='mytask')
     task.tag = 'faketag'
     task.put()
     self.assertEqual(1, len(model.Task().all().fetch(2)))
     result = model.Task().get_by_key_name('mytask')
     self.assertEqual(result.tag, 'faketag')
Esempio n. 2
0
    def testTaskExpando(self):
        # Test that Task.GetParam() and Task.Params() work as expected.
        task = model.Task(key_name='mytask')
        task.mparam_foo = 'somevalue1'
        task.mparam_bar = 'somevalue2'
        task.put()

        self.assertEqual(task.GetParam('foo'), 'somevalue1')
        self.assertEqual(task.GetParam('bar'), 'somevalue2')
        params = task.Params()
        self.assertEqual(params['foo'], 'somevalue1')
        self.assertEqual(params['bar'], 'somevalue2')
        self.assertEqual(len(params), 2)
Esempio n. 3
0
 def testMeasurementCreateWithTask(self):
     # Test that a Measurement can be created with a corresponding Task.
     task = model.Task()
     task.tag = 'faketag'
     task.put()
     task_id = task.key().id()
     measurement = model.Measurement(key_name='mymeasurement')
     measurement.type = 'sometype'
     measurement.task = task
     measurement.put()
     self.assertEqual(1, len(model.Measurement().all().fetch(2)))
     result = model.Measurement().get_by_key_name('mymeasurement')
     self.assertEqual(result.type, 'sometype')
     self.assertEqual(result.task.tag, 'faketag')
     self.assertEqual(result.GetTaskID(), task_id)
Esempio n. 4
0
 def testDeviceTaskCreate(self):
     # Test that a DeviceTask can be created.
     device = model.DeviceInfo(key_name='mydevice')
     device.put()
     task = model.Task()
     task.tag = 'mytag'
     task.put()
     device_task = model.DeviceTask(key_name='mydevicetask')
     device_task.device_info = device
     device_task.task = task
     device_task.put()
     self.assertEqual(1, len(model.DeviceTask().all().fetch(2)))
     result = model.DeviceTask().get_by_key_name('mydevicetask')
     self.assertEqual(result.device_info.key().name(), 'mydevice')
     self.assertEqual(result.task.tag, 'mytag')
Esempio n. 5
0
def schedule(task, start, end, count):
    group = model.TaskGroup()
    group.start_time = datetime.strptime(start, "%m-%d-%Y %H:%M")
    group.end_time = datetime.strptime(end, "%m-%d-%Y %H:%M")
    group.deviceCount = count
    group.doneCount = 0
    group.put()
    task.group = group
    for i in range(0, count):
        t = model.Task()
        t.group = task.group
        t.count = task.count
        t.created = task.created
        t.filter = task.filter
        t.tag = task.tag
        t.interval_sec = task.interval_sec
        t.priority = task.priority
        t.start_time = task.start_time
        t.end_time = task.end_time
        t.user = task.user
        t.type = task.type
        for name, value in task.Params().items():
            setattr(t, 'mparam_' + name, value)
        t.put()
Esempio n. 6
0
    def Add(self, **unused_args):
        """Add a task to the schedule."""
        if not acl.UserIsScheduleAdmin():
            self.error(404)
            return

        if not self.request.POST:
            add_to_schedule_form = AddToScheduleForm()
        else:
            add_to_schedule_form = AddToScheduleForm(self.request.POST)
            add_to_schedule_form.full_clean()
            if add_to_schedule_form.is_valid():
                thetype = add_to_schedule_form.cleaned_data['type']
                param1 = add_to_schedule_form.cleaned_data['param1']
                param2 = add_to_schedule_form.cleaned_data['param2']
                param3 = add_to_schedule_form.cleaned_data['param3']
                tag = add_to_schedule_form.cleaned_data['tag']
                thefilter = add_to_schedule_form.cleaned_data['filter']
                count = add_to_schedule_form.cleaned_data['count'] or -1
                interval = add_to_schedule_form.cleaned_data['interval']

                logging.info('Got TYPE: ' + thetype)

                task = model.Task()
                task.created = datetime.datetime.utcnow()
                task.user = users.get_current_user()
                task.type = thetype
                if tag:
                    task.tag = tag
                if thefilter:
                    task.filter = thefilter
                task.count = count
                task.interval_sec = float(interval)

                # Set up correct type-specific measurement parameters
                if task.type == 'ping':
                    task.mparam_target = param1
                    task.mparam_ping_timeout_sec = param2
                    task.mparam_packet_size_byte = param3
                elif task.type == 'traceroute':
                    task.mparam_target = param1
                    task.mparam_pings_per_hop = param2
                    task.mparam_max_hop_count = param3
                elif task.type == 'http':
                    task.mparam_url = param1
                    task.mparam_method = param2
                    task.mparam_headers = param3
                elif task.type == 'dns_lookup':
                    task.mparam_target = param1
                    task.mparam_server = param2
                task.put()

        schedule = model.Task.all()
        schedule.order('-created')

        template_args = {
            'user_schedule_admin': acl.UserIsScheduleAdmin(),
            'add_form': add_to_schedule_form,
            'schedule': schedule,
            'user': users.get_current_user().email(),
            'logout_link': users.create_logout_url('/')
        }
        self.response.out.write(
            template.render('templates/schedule.html', template_args))
Esempio n. 7
0
                    value = add_to_schedule_form.cleaned_data[field]
                    if value:
                        params[field] = value
                tag = add_to_schedule_form.cleaned_data['tag']
                thefilter = add_to_schedule_form.cleaned_data['filter']
                count = add_to_schedule_form.cleaned_data['count'] or -1
                interval = add_to_schedule_form.cleaned_data['interval']
                priority = add_to_schedule_form.cleaned_data['priority']
                p1 = add_to_schedule_form.cleaned_data['profile_1_freq']
                p2 = add_to_schedule_form.cleaned_data['profile_2_freq']
                p3 = add_to_schedule_form.cleaned_data['profile_3_freq']
                p4 = add_to_schedule_form.cleaned_data['profile_4_freq']

                logging.info('Got TYPE: ' + thetype)

                task = model.Task()
                task.created = datetime.datetime.utcnow()
                task.user = users.get_current_user()
                task.type = thetype
                if tag:
                    task.tag = tag
                if thefilter:
                    task.filter = thefilter
                task.count = count
                task.interval_sec = float(interval)
                task.priority = priority

                # Set up correct type-specific measurement parameters
                for name, value in params.items():
                    setattr(task, 'mparam_' + name, value)
                task.put()
Esempio n. 8
0
def GetDeviceSchedule(device_properties):
    """Return entries from the global schedule that match this device."""

    matched = set()
    if not device_properties.device_info:
        return matched

    schedule = model.Task.all()
    for task in schedule:
        if not task.filter:
            try:
                app_version = float(device_properties.app_version[1:])
                if task.type != "tcpthroughput" or app_version > 2.0:
                    matched.add(task)
            except:
                matched.add(task)
        else:
            # Does the filter match this device?
            devices = []
            # Match against DeviceProperties
            try:
                matching_device_properties = model.DeviceProperties.gql(
                    'WHERE ' + task.filter)
                devices += [
                    dp.device_info for dp in matching_device_properties
                ]
            except db.BadQueryError:
                logging.warn('Bad filter expression %s', task.filter)

            # Match against DeviceInfo
            try:
                matching_device_info = model.DeviceInfo.gql('WHERE ' +
                                                            task.filter)
                devices += matching_device_info
            except db.BadQueryError:
                logging.warn('Bad filter expression %s', task.filter)

            for dev in devices:
                if dev and dev.id == device_properties.device_info.id:
                    matched.add(task)

    # Un-assign all current tasks from this device
    for dt in device_properties.device_info.devicetask_set:
        dt.delete()

    # Assign matched tasks to this device
    for task in matched:
        device_task = model.DeviceTask()
        device_task.task = task
        device_task.device_info = device_properties.device_info
        device_task.put()

    ip_list = []
    q = model.CDNIpData.all()
    for record in q.run():
        delta = datetime.now() - record.timestamp
        if delta.days >= 1:
            continue
        ip_list.append(record.ip)

    random.shuffle(ip_list)

    for i in range(0, 10 if len(ip_list) >= 10 else len(ip_list)):
        ping_task = model.Task()
        ping_task.user = users.get_current_user()
        ping_task.count = -1
        ping_task.interval_sec = 3600.0
        ping_task.created = datetime.utcnow()
        ping_task.type = "ping"
        setattr(ping_task, 'mparam_target', ip_list[i])
        matched.add(ping_task)

    return matched