Ejemplo n.º 1
0
def add():
    if not g.user.is_authenticated():
        return render_template('add.html')
    if request.method == 'GET':
        return render_template('add.html')

    resource_type = request.form['resource_type']
    url = request.form['url'].strip()
    resource = Resource.query.filter_by(resource_type=resource_type,
                                        url=url).first()
    if resource is not None:
        flash('service already registered (%s, %s)' % (resource_type, url),
              'danger')
        if 'resource_type' in request.args:
            rtype = request.args.get('resource_type')
            return redirect(url_for('add', resource_type=rtype))
        return redirect(url_for('add'))

    [title, success, response_time, message,
     start_time] = run_test_resource(resource_type, url)

    resource_to_add = Resource(current_user, resource_type, title, url)
    run_to_add = Run(resource_to_add, success, response_time, message,
                     start_time)

    DB.session.add(resource_to_add)
    DB.session.add(run_to_add)
    try:
        DB.session.commit()
        flash('service registered (%s, %s)' % (resource_type, url), 'success')
    except Exception, err:
        DB.session.rollback()
        flash(str(err), 'danger')
Ejemplo n.º 2
0
    def martingale(self, run_parent):
        log.info('Martingale: loss for {0}'.format(run_parent.binary_ref))

        # a child is born
        run_child_key = ndb.Key('Run', str(dt.datetime.utcnow()))
        run_child = Run(
            key=run_child_key,
            currency=run_parent.currency,
            time_frame=run_parent.time_frame,
            trade_base=run_parent.trade_base,
            trade_aim=run_parent.trade_aim,

            parent_run=run_parent.key,
            profit_parent=run_parent.profit_net,
            stake_parent=run_parent.stake,

            step=run_parent.step + 1,
            payout=run_parent.payout * 2,
            ended_at=dt.datetime.utcnow() + dt.timedelta(minutes=int(run_parent.time_frame)),
        )

        # a child is registered
        if self.binary.createNew(run_child):
            run_child.put()
            log.info('New martingale run: {0}'.format(run_child))

        log.info('Martingale: created new run')
        return run_child
Ejemplo n.º 3
0
    def testRunResoures(self):
        # Do the whole healthcheck for all Resources for now
        resources = Resource.query.all()
        for resource in resources:
            result = run_test_resource(resource)
            print('resource: %s result=%s' % (resource.url, result.success))
            run = Run(resource, result)

            print('Adding Run: success=%s, response_time=%ss\n' %
                  (str(run.success), run.response_time))
            self.db.session.add(run)
            self.db.session.commit()

        self.db.session.close()

        # Verify
        resources = Resource.query.all()
        for resource in resources:
            # Each Resource should have one Run
            self.assertEqual(resource.runs.count(), 1,
                             'RunCount should be 1 for %s' % resource.url)
            self.assertEqual(
                resource.runs[0].success, True,
                'Run should be success for %s report=%s' %
                (resource.url, str(resource.runs[0])))
Ejemplo n.º 4
0
def run_resource(resourceid):
    resource = Resource.query.filter_by(identifier=resourceid).first()

    if not resource.active:
        # Exit test of resource if it's not active
        return

    # Get the status of the last run,
    # assume success if there is none
    last_run_success = True
    last_run = resource.last_run
    if last_run:
        last_run_success = last_run.success

    # Run test
    result = run_test_resource(resource)

    run1 = Run(resource, result, datetime.utcnow())

    DB.session.add(run1)

    # commit or rollback each run to avoid long-lived transactions
    # see https://github.com/geopython/GeoHealthCheck/issues/14
    db_commit()

    if APP.config['GHC_NOTIFICATIONS']:
        # Attempt notification
        try:
            notify(APP.config, resource, run1, last_run_success)
        except Exception as err:
            # Don't bail out on failure in order to commit the Run
            msg = str(err)
            logging.warn('error notifying: %s' % msg)
    if not __name__ == '__main__':
        DB.session.remove()
Ejemplo n.º 5
0
def mobile_syncrun():
    if request.headers["Content-Type"] == "application/json":
        runjson = request.get_json()
    else:
        return redirect(url_for("home"))
    try:
        print "Run JSON Request", runjson
        if "id" in runjson:
            run = Run.query.filter_by(id=runjson["id"]).first()
            run.time = runjson["time"]
        else:
            run = Run(runjson["time"])
        person = get_person(runjson["person"])
        run.person = person.id
        run.fetcher = person
        run.time = runjson["time"]
        run.cafe = runjson["cafe"]
        run.pickup = runjson["pickup"]
        run.is_open = runjson["is_open"]
        run.statusobj = Status.query.filter_by(id=runjson["status"]).first()
        if "id" not in runjson:
            db.session.add(run)
        db.session.commit()
        return jsonify(msg="success",
                       id=run.id,
                       modified=run.jsondatetime("modified"))
    except:
        return jsonify(msg="error")
Ejemplo n.º 6
0
def CreateRuns(key):
	runs = []
	item = stocks[key][0]
	data = stocks[key][1]
	while item != None:
		if abs(data['Delta'].iloc[item.index])<runThreshold and data['Delta'].iloc[ item.index -1  ]*data['Delta'].iloc[item.index+1]>0:
			if item._next !=None:
				if item._prev !=None: 	
					item._prev._next = item._next._next
				if item._next._next!=None:
					item._next._next.prev = item._prev
				item._next = item._next._next
			elif item._prev !=None: 	
				item._prev._next = None
		item = item._next
	item = stocks[key][0]
	while item != None:
		inflections = []
		inflections.append(item)
		n=0
		start = item
		while n<runLength-1 and item!=None:
			n=n+1
			inflections.append(item._next)
			item = item._next
			finish = item
		if finish !=None:
			_run = Run(key, inflections)
			runs.append(_run)
		j=0
		item = start
		while j<deltaJ and item!=None:
			j=j+1
			item = item._next
	return runs
Ejemplo n.º 7
0
def post():

    print(request.json['fitness'])
    new_fit = request.json['fitness']
    new_date = request.json['date']
    new_model = request.json['model']

    new_run = Run(date=new_date, modelName=new_model, fitness=new_fit)
    new_run.save()

    return response
Ejemplo n.º 8
0
 def test_green(self):
     r = Run()
     r.id = 1
     rv = showrun(r)
     ok_(isinstance(rv, SafeString))
     frag = parseFragment(rv)
     eq_(len(frag.childNodes), 1)
     a = frag.childNodes[0]
     eq_(a.attributes, {'data-errors': '0',
                        'data-total': '0',
                        'data-missing': '0',
                        'href': '/dashboard/compare?run=1',
                        'data-warnings': '0'})
     text = a.childNodes[0].value
     ok_('green' in text)
Ejemplo n.º 9
0
def add_run(cafeid=None):
    form = RunForm(request.form)
    users = User.query.all()
    form.person.choices = [(user.id, user.name) for user in users]
    cafes = Cafe.query.all()
    if not cafes:
        flash(
            "There are no cafes currently configured. Please add one before creating a run",
            "warning")
        return redirect(url_for("home"))
    form.cafeid.choices = [(cafe.id, cafe.name) for cafe in cafes]
    if request.method == "GET":
        if cafeid:
            form.cafe.data = cafeid
        form.person.data = current_user.id
        form.time.data = sydney_timezone_now() + timedelta(
            minutes=30)  # strftime("%Y/%m/%d %H:%M:%S")
        return render_template("runform.html",
                               form=form,
                               formtype="Add",
                               current_user=current_user)
    if form.validate_on_submit():
        # Add run
        print form.data
        run = Run(form.data["time"])
        person = User.query.filter_by(id=form.data["person"]).first()
        run.person = person.id
        run.fetcher = person
        run.cafeid = form.data["cafeid"]
        run.pickup = form.data["pickup"]
        #run.modified = sydney_timezone_now()
        run.modified = sydney_timezone_now()
        db.session.add(run)
        db.session.commit()
        write_to_events("created", "run", run.id)
        flash("Run added", "success")
        return redirect(url_for("view_run", runid=run.id))
    else:
        for field, errors in form.errors.items():
            flash("Error in %s: %s" % (field, "; ".join(errors)), "danger")
        return render_template("runform.html",
                               form=form,
                               formtype="Add",
                               current_user=current_user)
Ejemplo n.º 10
0
 def test_obsolete(self):
     r = Run(obsolete=3)
     r.id = 1
     rv = showrun(r)
     ok_(isinstance(rv, SafeString))
     frag = parseFragment(rv)
     childNodes = list(frag)
     eq_(len(childNodes), 1)
     a = childNodes[0]
     eq_(
         a.attrib, {
             'data-errors': '0',
             'data-total': '0',
             'data-missing': '0',
             'href': '/dashboard/compare?run=1',
             'data-warnings': '0'
         })
     text = a.text
     ok_('3' in text and 'obsolete' in text)
Ejemplo n.º 11
0
def test(resource_identifier):
    resource = Resource.query.filter_by(identifier=resource_identifier).first()
    if resource is None:
        flash('resource not found', 'danger')
        return redirect(request.referrer)

    [title, success, response_time, message,
     start_time] = run_test_resource(resource.resource_type, resource.url)
    run_to_add = Run(resource, success, response_time, message, start_time)

    if message not in ['OK', None, 'None']:
        flash('ERROR: %s' % message, 'danger')
    else:
        flash('Resource tested successfully', 'success')

    DB.session.add(run_to_add)

    try:
        DB.session.commit()
    except Exception, err:
        DB.session.rollback()
        flash(str(err), 'danger')
Ejemplo n.º 12
0
    def new(self):
        '''Create new iteration'''
        log.info('Main new started')

        currency, time_frame, trade_base, trade_aim = self.rl.selectNew(self.q)
        run_key = ndb.Key('Run', str(dt.datetime.utcnow()))
        run = Run(
            key=run_key,
            currency=currency,
            time_frame=time_frame,
            trade_base=trade_base,
            trade_aim=trade_aim,
            step=1,
            payout=1.,
            ended_at=dt.datetime.utcnow() + dt.timedelta(minutes=int(time_frame)),
        )

        if self.binary.createNew(run):
            run.put()
            log.info('New run: {0}'.format(run))

        log.info('Main new ended')
Ejemplo n.º 13
0
def add():
    """add resource"""
    if not g.user.is_authenticated():
        return render_template('add.html')
    if request.method == 'GET':
        return render_template('add.html')

    tag_list = []

    resource_type = request.form['resource_type']
    tags = request.form.getlist('tags')
    url = request.form['url'].strip()
    resource = Resource.query.filter_by(resource_type=resource_type,
                                        url=url).first()
    if resource is not None:
        msg = gettext('Service already registered')
        flash('%s (%s, %s)' % (msg, resource_type, url), 'danger')
        if 'resource_type' in request.args:
            rtype = request.args.get('resource_type')
            return redirect(
                url_for('add', lang=g.current_lang, resource_type=rtype))
        return redirect(url_for('add', lang=g.current_lang))

    [title, success, response_time, message,
     start_time] = sniff_test_resource(APP.config, resource_type, url)

    if not success:
        flash(message, 'danger')
        return redirect(
            url_for('add', lang=g.current_lang, resource_type=resource_type))

    if tags:
        for tag in tags:
            tag_found = False
            for tag_obj in Tag.query.all():
                if tag == tag_obj.name:  # use existing
                    tag_found = True
                    tag_list.append(tag_obj)
            if not tag_found:  # add new
                tag_list.append(Tag(name=tag))

    resource_to_add = Resource(current_user,
                               resource_type,
                               title,
                               url,
                               tags=tag_list)

    probe_to_add = None
    checks_to_add = []

    # Always add a default Probe and Check(s)  from the GHC_PROBE_DEFAULTS conf
    if resource_type in APP.config['GHC_PROBE_DEFAULTS']:
        resource_settings = APP.config['GHC_PROBE_DEFAULTS'][resource_type]
        probe_class = resource_settings['probe_class']
        if probe_class:
            # Add the default Probe
            probe_obj = Factory.create_obj(probe_class)
            probe_to_add = ProbeVars(resource_to_add, probe_class,
                                     probe_obj.get_default_parameter_values())

            # Add optional default (parameterized) Checks to add to this Probe
            checks_info = probe_obj.get_checks_info()
            checks_param_info = probe_obj.get_plugin_vars()['CHECKS_AVAIL']
            for check_class in checks_info:
                check_param_info = checks_param_info[check_class]
                if 'default' in checks_info[check_class]:
                    if checks_info[check_class]['default']:
                        # Filter out params for Check with fixed values
                        param_defs = check_param_info['PARAM_DEFS']
                        param_vals = {}
                        for param in param_defs:
                            if param_defs[param]['value']:
                                param_vals[param] = param_defs[param]['value']
                        check_vars = CheckVars(probe_to_add, check_class,
                                               param_vals)
                        checks_to_add.append(check_vars)

    result = run_test_resource(resource_to_add)

    run_to_add = Run(resource_to_add, result)

    DB.session.add(resource_to_add)
    if probe_to_add:
        DB.session.add(probe_to_add)
    for check_to_add in checks_to_add:
        DB.session.add(check_to_add)
    DB.session.add(run_to_add)

    try:
        DB.session.commit()
        msg = gettext('Service registered')
        flash('%s (%s, %s)' % (msg, resource_type, url), 'success')
    except Exception as err:
        DB.session.rollback()
        flash(str(err), 'danger')
        return redirect(url_for('home', lang=g.current_lang))
    else:
        return edit_resource(resource_to_add.identifier)
Ejemplo n.º 14
0
def add():
    """add resource"""
    if not g.user.is_authenticated():
        return render_template('add.html')
    if request.method == 'GET':
        return render_template('add.html')
    resource_type = request.form['resource_type']
    tags = request.form.getlist('tags')
    url = request.form['url'].strip()
    resources_to_add = []

    from healthcheck import sniff_test_resource, run_test_resource
    sniffed_resources = sniff_test_resource(CONFIG, resource_type, url)

    if not sniffed_resources:
        msg = gettext("No resources detected")
        LOGGER.exception()
        flash(msg, 'danger')

    for (
            resource_type,
            resource_url,
            title,
            success,
            response_time,
            message,
            start_time,
            resource_tags,
    ) in sniffed_resources:

        # sniffed_resources may return list of resource
        # types different from initial one
        # so we need to test each row separately
        resource = Resource.query.filter_by(resource_type=resource_type,
                                            url=url).first()
        if resource is not None:
            msg = gettext('Service already registered')
            flash('%s (%s, %s)' % (msg, resource_type, url), 'danger')

            if len(sniffed_resources) == 1 and 'resource_type' in request.args:
                return redirect(url_for('add', lang=g.current_lang))

        tags_to_add = []
        for tag in chain(tags, resource_tags):
            tag_obj = tag
            if not isinstance(tag, Tag):
                tag_obj = Tag.query.filter_by(name=tag).first()
                if tag_obj is None:
                    tag_obj = Tag(name=tag)
            tags_to_add.append(tag_obj)

        resource_to_add = Resource(current_user,
                                   resource_type,
                                   title,
                                   resource_url,
                                   tags=tags_to_add)

        resources_to_add.append(resource_to_add)
        probe_to_add = None
        checks_to_add = []

        # Always add a default Probe and Check(s)
        # from the GHC_PROBE_DEFAULTS conf
        if resource_type in CONFIG['GHC_PROBE_DEFAULTS']:
            resource_settings = CONFIG['GHC_PROBE_DEFAULTS'][resource_type]
            probe_class = resource_settings['probe_class']
            if probe_class:
                # Add the default Probe
                probe_obj = Factory.create_obj(probe_class)
                probe_to_add = ProbeVars(
                    resource_to_add, probe_class,
                    probe_obj.get_default_parameter_values())

                # Add optional default (parameterized)
                # Checks to add to this Probe
                checks_info = probe_obj.get_checks_info()
                checks_param_info = probe_obj.get_plugin_vars()['CHECKS_AVAIL']
                for check_class in checks_info:
                    check_param_info = checks_param_info[check_class]
                    if 'default' in checks_info[check_class]:
                        if checks_info[check_class]['default']:
                            # Filter out params for Check with fixed values
                            param_defs = check_param_info['PARAM_DEFS']
                            param_vals = {}
                            for param in param_defs:
                                if param_defs[param]['value']:
                                    param_vals[param] = \
                                        param_defs[param]['value']
                            check_vars = CheckVars(probe_to_add, check_class,
                                                   param_vals)
                            checks_to_add.append(check_vars)

        result = run_test_resource(resource_to_add)

        run_to_add = Run(resource_to_add, result)

        DB.session.add(resource_to_add)
        # prepopulate notifications for current user
        resource_to_add.set_recipients('email', [g.user.email])

        if probe_to_add:
            DB.session.add(probe_to_add)
        for check_to_add in checks_to_add:
            DB.session.add(check_to_add)
            DB.session.add(run_to_add)

    try:
        DB.session.commit()
        msg = gettext('Services registered')
        flash('%s (%s, %s)' % (msg, resource_type, url), 'success')
    except Exception as err:
        DB.session.rollback()
        flash(str(err), 'danger')
        return redirect(url_for('home', lang=g.current_lang))

    if len(resources_to_add) == 1:
        return edit_resource(resources_to_add[0].identifier)
    return redirect(url_for('home', lang=g.current_lang))
Ejemplo n.º 15
0
# (c) Facebook, Inc. and its affiliates. Confidential and proprietary.

import os


if __name__ == "__main__":
    try:
        os.remove("database.sqlite3")
    except IOError:
        pass

    from models import engine, session, Base, Run, Issue

    Base.metadata.create_all(bind=engine)

    session.add(Run(run_id=0))
    session.add(Run(run_id=1))

    session.add(
        Issue(issue_id=0, source="UserControlled", sink="RemoteCodeExecution", run=1)
    )
    session.add(Issue(issue_id=1, source="UserControlled", sink="Logging", run=1))
    session.add(Issue(issue_id=2, source="UserControlled", sink="SqlInjection", run=1))
    session.add(Issue(issue_id=3, source="Filesystem", sink="ReturnedToUser", run=0))

    session.commit()
Ejemplo n.º 16
0
    released_pile = simpy.Store(env)
    progress_pile = simpy.Store(env)

    # Create named piles for specific work types, i.e., work that needs to be done to get the case to the Done pile
    work_piles = {
        wt.WORK_ANALYSIS: simpy.Store(env, len(sizes)),
        wt.WORK_QA: simpy.Store(env, MAX_QA_PILE),
        wt.WORK_DEV: simpy.Store(env, MAX_DEV_PILE),
        wt.WORK_REVIEW: simpy.FilterStore(env, MAX_REVIEW_PILE),
        wt.WORK_MERGE: simpy.FilterStore(env)
    }

    # Create a Run to store params
    run = Run(
        params={
            'dev_strategy': dev_strategy,
            'ba_strategy': ba_strategy,
            'dev_review_choice_strategy': dev_review_choice_strategy
        })

    # Create a pile of all remaining source cases
    for case in standard_cases[NUM_INITIAL_CASES:len(standard_cases)]:
        _case = copy.deepcopy(case)
        _case.set_env(env)
        source_pile.put(_case)

    # Create a pile of cases to get the system to a steady state. We will start by putting the first workflow step
    # for each case on the dispatcher's pile and let them allocate work based on the workflow step properties.
    for case in standard_cases[:NUM_INITIAL_CASES]:
        _case = copy.deepcopy(case)
        _case.set_env(env)
        dispatch_new_case_work(_case)
Ejemplo n.º 17
0
def create_run(session, test):
    # Run(test=test) requires the same session as test for adding it
    run = Run(test_id=test.id)
    session.add(run)
    session.commit()
    return run