Example #1
0
    def test_run_exists(self):
        class R(object):
            def __init__(self,
                         id=None,
                         flow=None,
                         contact=None,
                         responded=None,
                         path=None,
                         values=None):

                self.id = id
                self.flow = flow
                self.contact = contact
                self.responded = responded
                self.path = path
                self.values = values

                #created_on = models.DatetimeField()
                #modified_on = models.DatetimeField()
                #exited_on = models.DatetimeField()
                #exit_type = SimpleField()

        rapidpro_mock_run = R(id='001A',
                              flow='Axx',
                              contact='Allan',
                              path=None,
                              values=None)
        self.assertEquals(Run.group_exists(rapidpro_mock_run), False)
        Run.objects.create(id='001A',
                           flow='Axx',
                           contact='Allan',
                           path=None,
                           values=None)
        self.assertEquals(Run.group_exists(rapidpro_mock_run), True)
Example #2
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")
Example #3
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
Example #4
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
Example #5
0
def run_pep8(git_url, path, name, rev):
    """Check out the git project, run pep8, store the results"""

    # Do not allow duplicates of the same project/rev
    old_run = Run.objects.filter(project_name=name, git_revision=rev)
    if not old_run:
        run = Run(project_name=name, project_url=git_url, git_revision=rev)
        run.save()
        pep8_pipe = os.popen("pep8 -r %s" % path)
        parse_pep8(run, path, pep8_pipe)
        pep8_pipe.close()

    os.system("rm -rf %s" % os.path.dirname(path))
Example #6
0
File: tests.py Project: hwine/elmo
 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)
Example #7
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)
    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])))
Example #9
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()
Example #10
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')
Example #11
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
Example #12
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)
Example #13
0
    def test_run_exists(self):
        class S(object):
            def __init__(self,
                         run_id=None,
                         flow=None,
                         contact=None,
                         responded=None,
                         path=None,
                         values=None,
                         created_on=None,
                         modified_on=None,
                         exited_on=None,
                         exit_type=None):
                self.id = run_id
                self.flow = flow
                self.contact = contact
                self.responded = responded
                self.path = path
                self.values = values
                self.created_on = created_on
                self.modified_on = modified_on
                self.exited_on = exited_on
                self.exit_type = exit_type

        rapidpro_mock_run = S(run_id=1,
                              flow='Test flow',
                              contact='Test contact',
                              responded=1,
                              path='Test path',
                              values='Test Values',
                              created_on=timezone.now(),
                              modified_on=None,
                              exited_on=None,
                              exit_type='Test exit_type')
        self.assertEquals(Run.run_exists(rapidpro_mock_run), False)
        Run.objects.create(id=1,
                           flow='Test flow',
                           contact='Test contact',
                           responded=1,
                           path='Test path',
                           values='Test Values',
                           created_on=timezone.now(),
                           modified_on=None,
                           exited_on=None,
                           exit_type='Test exit_type')
        self.assertEquals(Run.run_exists(rapidpro_mock_run), True)
Example #14
0
    def __init__(self, site, name=None):

        self.site = site
        name = name or str( localtime( now() ) )
        self.run = Run( name=name, site = site )
        self.run.save()

        self.br = M.Browser()
        self.br.set_handle_robots(False)
Example #15
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)
Example #16
0
def search():
    get_response = []
    for run in Run.select():
        print(run.date, run.fitness)
        response_obj = {
            "date": run.date,
            "fitness": run.fitness,
            "model": run.modelName
        }
        get_response.append(response_obj)
    return get_response
Example #17
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')
Example #18
0
    def existing(self):
        '''Go through all existing iterations'''
        log.info('Main existing started')

        runs = Run.query(Run.is_finished == False).fetch()
        log.info('{0} runs found'.format(len(runs)))

        if len(runs):
            profit_table = self.binary.getProfitTable()

            # continue every run
            for run in runs:
                log.info('Run: finding profit for {0}'.format(run.binary_ref))

                # time frame ending?
                if run.ended_at > dt.datetime.utcnow() + dt.timedelta(seconds=60):
                    log.info('Run: skipping till {0}'.format(run.ended_at))
                    continue

                # wait for result
                profit_table_update_delay = dt.timedelta(seconds=15)
                to_sleep = max(0, int((run.ended_at - dt.datetime.utcnow() + profit_table_update_delay).total_seconds()))
                if to_sleep > 0:
                    log.info('Run: waiting for {0} seconds'.format(to_sleep))
                    time.sleep(to_sleep + 5)
                    log.info('Run: refreshing profit table...')
                    profit_table = self.binary.getProfitTable()

                # get result
                if run.binary_ref in profit_table:
                    parent_stake = run.stake_parent if run.stake_parent else 0.
                    run.profit = profit_table[run.binary_ref]
                    run.profit_net = run.profit + run.profit_parent
                    run.stake_net = run.stake + parent_stake
                    run.is_win = True if run.profit > 0 else False
                    run.is_finished = True
                    run.put()
                    log.info('Run: finished with profit {0:.2f}'.format(run.profit))

                    # continue to cancel loss?
                    if not run.is_win:
                        run_child = self.martingale(run)
                    else:
                        # update q
                        self.q = self.rl.updateQ(self.q, run)
                else:
                    log.error('{0} has no profit/loss in table'.format(run.binary_ref))

        log.info('Main existing ended')
Example #19
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]
    statuses = Status.query.all()
    form.statusid.choices = [(s.id, s.description) for s in statuses]
    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()#.strftime("%Y/%m/%d %H:%M:%S")
        form.deadline.data = sydney_timezone_now()
        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.deadline = form.data["deadline"]
        run.cafeid = form.data["cafeid"]
        run.pickup = form.data["pickup"]
        run.statusid = form.data["statusid"]
        #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)
Example #20
0
    def loadQ(self):
        log.info('Q loading...')

        q_key = ndb.Key(Q, 'main')
        self.q = q_key.get()
        if not self.q:
            self.q = Q(key=q_key)

        # validate properties
        if not self.q.data:
            self.q.data = {}
        if not self.q.visits:
            self.q.visits = {}
            runs = Run.query().fetch()
            for run in runs:
                if run.getState() not in self.q.visits:
                    self.q.visits[run.getState()] = 0
                self.q.visits[run.getState()] += 1

        log.info('Q loaded {0}'.format(len(self.q.data)))
Example #21
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')
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
Example #23
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()
Example #24
0
def main():

    n_experiments, n_samples, n_runs = 0, 0, 0

    db = get_database()

    # clear pre-existing data
    print("--- dropping existing data")
    db.drop_tables([Sample, Experiment, Run])
    print("--- migrating up!")
    db.create_tables([Experiment, Sample, Run])

    batch_size = 1000

    with tarfile.open(TAR_PATH, "r:gz") as tar:
        for n, member in enumerate(tar):
            print(f"--- loading {member.name}")
            h = tar.extractfile(member)

            experiments_batch = []
            samples_batch = []
            runs_batch = []

            if h is not None:
                if "experiment" in member.name:
                    # experiment alias -> biosample identifier
                    with tar.extractfile(member) as handle:
                        root = xml.etree.ElementTree.parse(handle)
                        for _id, experiment_data in load_experiment_data(root):
                            experiments_batch.append({
                                "id": _id,
                                **experiment_data
                            })
                            n_experiments += 1

                            if len(experiments_batch) >= batch_size:
                                print("--- inserting batch of experiments")
                                print(len(experiments_batch))
                                Experiment.insert_many(
                                    experiments_batch).execute()
                                print(
                                    f"--- n_experiments={n_experiments}; row count={Experiment.select().count()}"
                                )
                                experiments_batch = []

                    if len(experiments_batch) > 0:
                        try:
                            Experiment.insert_many(experiments_batch).execute()
                        except peewee.DataError:
                            pprint(experiments_batch)
                            quit(-1)

                if "sample" in member.name:
                    with tar.extractfile(member) as handle:
                        root = xml.etree.ElementTree.parse(handle)
                        for _id, sample_data in load_sample_data(root):
                            row = {"id": _id, **sample_data}
                            samples_batch.append(row)
                            n_samples += 1
                            if len(samples_batch) >= batch_size:
                                print(len(samples_batch))
                                print("--- inserting batch of samples")
                                Sample.insert_many(samples_batch).execute()
                                print(
                                    f"--- n_samples={n_samples}; row count={Sample.select().count()}"
                                )
                                samples_batch = []

                    if len(samples_batch) > 0:
                        Sample.insert_many(samples_batch).execute()

                if "run" in member.name:
                    with tar.extractfile(member) as handle:
                        root = xml.etree.ElementTree.parse(handle)
                        for _id, run_data in load_run_data(root):
                            runs_batch.append({"id": _id, **run_data})
                            n_runs += 1

                            if len(runs_batch) >= batch_size:
                                print(len(runs_batch))
                                print("--- inserting batch of runs")
                                Run.insert_many(runs_batch).execute()
                                print(
                                    f"--- n_runs={n_runs}; row count={Run.select().count()}"
                                )
                                runs_batch = []
                    if len(runs_batch) > 0:
                        Run.insert_many(runs_batch).execute()

            else:
                pass
Example #25
0
 def test_add_runs(self):
     run_count = Run.objects.count()
     added_runs = Run.add_runs()
     self.assertEquals(Run.objects.count(), run_count + added_runs)
Example #26
0
def afficher_challenge(request, id):
    error=False
    if request.method == 'POST':  # S'il s'agit d'une requête POST
        form = AddRunForm(request.POST)  # Nous reprenons les données

        if form.is_valid(): # Nous vérifions que les données envoyées sont valides

            # Ici nous pouvons traiter les données du formulaire
            date = form.cleaned_data['date']
	    score=form.cleaned_data['score']
	    user=request.user
	    challenge = get_object_or_404(Challenge, id=id)
	    run=Run(date=date,score=score,challenge=challenge,user=user)
	    run.save()
	    print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!CREATION RUN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")

    else: # Si ce n'est pas du POST, c'est probablement une requête GET
        form = AddRunForm()  # Nous créons un formulaire vide

    challenge = get_object_or_404(Challenge, id=id)  
    member=challenge.team.filter(id=request.user.id).exists()


    #Creation du graphique
    #Create the data

    #Voir highcharts pour les options
 
    series=[]
    series_options_terms={}
    for joueur in challenge.team.all():
	serie_joueur=	{'options': {'source': Run.objects.filter(user=joueur,challenge=challenge)}}
	serie_joueur['terms']=[{''.join(["score_",joueur.username]):'score'},{''.join(["date_",joueur.username]):'date'}]
	series.append(serie_joueur)
	series_options_terms[''.join(["date_",joueur.username])]=[''.join(["score_",joueur.username])]

    #Ajouter l'objectif
    serie_obj=	{'options': {'source': Challenge.objects.filter(id=challenge.id),'colors': 'red'},'terms':['goal_date','goal']}
    series_options_terms['goal_date']=['goal']
    series.append(serie_obj)

    

    challengeData =DataPool(series)

    print(series_options_terms)
    #Step 2: Create the Chart object

    series_options0 =[{'options':{
                  'type': 'spline',
                  'stacking': False},
                'terms':series_options_terms
		}]

    cht = Chart(
            datasource = challengeData,
            series_options=series_options0,
            chart_options =
              {'title': {'text': 'Performances'},
               'xAxis': {'title': {'text': 'Date'}},
	       'yAxis': {'title': {'text': 'Scores'}}
	})
 

    return render(request, 'challenges/challenge_page.html', locals())
Example #27
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)
Example #28
0
class Crawler(object):

    def __init__(self, site, name=None):

        self.site = site
        name = name or str( localtime( now() ) )
        self.run = Run( name=name, site = site )
        self.run.save()

        self.br = M.Browser()
        self.br.set_handle_robots(False)

    def crawl(self,result ,force=False):

        if result.link.available == False or result.status != None:
            if not force:
                return []

        #: get page
        res=self.br.open(result.link.url,timeout=2.0) 
        result.status = res.code  
        result.content_type = res.info()['Content-Type']

        if not result.content_type or result.content_type.find('text/') <0:
            #: PDF ... 
            result.save()
            return []

        result.set_output(res.get_data())

        result.save()

        #: page links
        next_links=result.children()

        #: page has cases
        for c in result.link.case_set.all():
            case_result, case_result_created= LinkResult.objects.get_or_create(
                                                run=self.run,
                                                link=result.link,
                                                case = c
                                            )
            #: TOOD: this implementation expect only 1 depth
            if case_result.status == None:
                if c.form_index >= 0 : 
                    self.br.select_form( nr = c.form_index )
                    for k,v in c.form_params.items(): 
                        self.br[ k ] = v
                    res = self.br.submit()
                    case_result.status = res.code
                    case_result.content_type=res.info()['Content-Type']
                    if result.content_type or result.content_type.find('text/') >=0:
                        case_result.set_output(res.get_data() )
                    case_result.save()

            self.br.open(result.link.url,timeout=2.0,)           #: access original page

        return next_links

    def start(self,url=None,parent=None,follow=True ,force=False):
        #: parent : parent Link instance
        self.br.clear_history()
        time.sleep(0.01 )        
        url = url or self.site.start_url

        result =None
        try:
            result = self.run.provide_result( url, parent )      
            if result == None:
                return 
            next_links = self.crawl( result,force=force)
        except:
            if result:
                result.errors = traceback.format_exc()
                result.save()
            return

        if not follow:
            return 

        for path in next_links:
            self.start(  path ,result.link)
Example #29
0
 def __init__(self):
     log.info('Stat init started')
     self.loadQ()
     self.runs = Run.query().order(Run.ended_at).fetch()
     log.info('Main init ended')
Example #30
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))
Example #31
0
    def notifyMe(self):
        log.info('Main Notifying me')

        log.info('Main waiting for trading to finish')
        time.sleep(60)
        log.info('Main waiting finished')

        started_at = dt.datetime.utcnow() + dt.timedelta(hours=-1)
        ended_at = dt.datetime.utcnow()
        runs = Run.query(Run.ended_at >= started_at, Run.ended_at <= ended_at, Run.is_finished == True, Run.is_win == True).fetch()
        log.info('Fetched {0} runs from {1} till {2}'.format(len(runs), started_at, ended_at))

        # exit if nothing
        if not runs:
            log.warn('Exiting as there is no runs!')
            return

        net_profit = 0.
        stakes = 0.
        runs_size = len(runs) + 0.

        fields = ['binary_ref', 'time_frame', 'trade_base', 'trade_aim', 'step', 'profit_parent', 'stake_parent', 'stake', 'probability', 'payout', 'profit_net']
        # table header
        table = '<table width=100%" border="1"><thead><tr>'
        for field in fields:
            table += '<th>{0}</th>'.format(field)
        table += '</tr></thead>'
        # table body
        table += '<tbody>'
        for run in runs:
            # update results
            net_profit += run.profit_net
            stakes += run.stake

            row = '<tr>'
            for field in fields:
                row += '<td>{0}</td>'.format(getattr(run, field))
            row += '</tr>'
            log.info('Row: {0}'.format(row))
            table += row

            while run.step > 1:
                run = run.parent_run.get()
                stakes += run.stake

                row = '<tr><td>&nbsp;</td>'
                for field in fields[1:-1]:
                    row += '<td>{0}</td>'.format(getattr(run, field))
                row += '<td>&nbsp;</td></tr>'
                log.info('Row: {0}'.format(row))
                table += row

        table += '</tbody></table>'
        # pprint(table)

        subject = '[{0:.2f}] {1} runs totalling {2:.2f} profit with {3:.2f} stakes'.format(
            net_profit / stakes,
            runs_size,
            net_profit,
            stakes,
        )
        log.info('Subject: {0}'.format(subject))

        msg = mail.EmailMessage(
            sender='*****@*****.**',
            subject=subject,
            to='*****@*****.**',
        )

        msg.body = table

        msg.html = '<html><body>' + table + '</body></html>'

        msg.send()

        log.info('Main Me notified')
Example #32
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)
Example #33
0
class MyPDFView(View):
    template = 'qcreports/test.html'  # the template
    groups = Group.get_sms_maama_groups()
    contacts = Contact.get_sms_maama_weekly_contacts()
    sms_maama_contacts = Contact.get_sms_maama_contacts()
    sent_messages = Message.get_sms_maama_sent_messages()
    delivered_messages = Message.get_sms_maama_delivered_messages()
    failed_messages = Message.get_sms_maama_failed_messages()
    failed_messages_count = Message.get_sms_maama_failed_messages_count()
    contacts_count = Contact.get_sms_maama_contacts_count()
    weekly_contacts_count = Contact.get_sms_maama_weekly_contacts_count()
    messages_count = Message.get_sms_maama_sent_messages_count()
    read_messages_count = Message.get_sms_maama_read_messages_count()
    unread_messages = Message.get_sms_maama_unread_messages()
    flow_responses_count = Message.get_sms_maama_flow_responses_count()
    baby_responses = Message.get_sms_maama_flow_responses_baby()
    baby_responses_count = Message.get_sms_maama_flow_responses_baby_count()
    stops = Message.get_sms_maama_opted_out()
    stops_count = Message.get_sms_maama_opted_out_count()
    flows = Run.sms_maama_contact_flows()
    antenatal_responses = Run.sms_maama_contact_flows_antenatal()
    start_date = datetime.datetime.now() - datetime.timedelta(days=7)
    end_date = datetime.datetime.now()
    this_day = now()
    context = {
        'groups': groups,
        'contacts': contacts,
        'sms_maama_contacts': sms_maama_contacts,
        'sent_messages': sent_messages,
        'delivered_messages': delivered_messages,
        'failed_messages': failed_messages,
        'failed_messages_count': failed_messages_count,
        'contacts_count': contacts_count,
        'weekly_contacts_count': weekly_contacts_count,
        'messages_count': messages_count,
        'read_messages_count': read_messages_count,
        'unread_messages': unread_messages,
        'flow_responses_count': flow_responses_count,
        'baby_responses': baby_responses,
        'baby_responses_count': baby_responses_count,
        'stops': stops,
        'stops_count': stops_count,
        'flows': flows,
        'antenatal_responses': antenatal_responses,
        'start_date': start_date,
        'end_date': end_date,
        'this_day': this_day
    }  # data that has to be renderd to pdf templete

    def get(self, request):
        response = PDFTemplateResponse(
            request=request,
            template=self.template,
            filename="sms_maama_weekly_report.pdf",
            context=self.context,
            show_content_in_browser=False,
            # cmd_options={'margin-top': 10,
            #              "zoom": 1,
            #              "viewport-size": "1366 x 513",
            #              'javascript-delay': 1000,
            #              'footer-center': '[page]/[topage]',
            #              "no-stop-slow-scripts": True},
        )
        return response