<commit_before>#!/usr/bin/env python

import random
import time

from datetime import datetime

from changes import mock
from changes.config import db, create_app
from changes.constants import Result, Status
from changes.db.utils import get_or_create
from changes.events import publish_build_update, publish_job_update
from changes.models import Change, Job, LogSource, TestResultManager, ProjectPlan

app = create_app()
app_context = app.app_context()
app_context.push()


def create_new_change(project, **kwargs):
    return mock.change(project=project, **kwargs)


def create_new_entry(project):
    new_change = (random.randint(0, 2) == 1)
    if not new_change:
        try:
            change = Change.query.all()[0]
        except IndexError:
            new_change = True

    if new_change:
        author = mock.author()
        revision = mock.revision(project.repository, author)
        change = create_new_change(
            project=project,
            author=author,
            message=revision.message,
        )
    else:
        change.date_modified = datetime.utcnow()
        db.session.add(change)
        revision = mock.revision(project.repository, change.author)

    source = mock.source(project.repository, revision_sha=revision.sha)

    date_started = datetime.utcnow()

    build = mock.build(
        author=change.author,
        project=project,
        source=source,
        message=change.message,
        result=Result.unknown,
        status=Status.in_progress,
        date_started=date_started,
    )
    publish_build_update(build)

    for x in xrange(3):
        job = mock.job(
            build=build,
            change=change,
            status=Status.in_progress,
        )
        publish_job_update(job)

        logsource = LogSource(
            job=job,
            project=job.project,
            name='console',
        )
        db.session.add(logsource)
        db.session.commit()

        offset = 0
        for x in xrange(30):
            lc = mock.logchunk(source=logsource, offset=offset)
            db.session.commit()
            offset += lc.size

    return build


def update_existing_entry(project):
    try:
        job = Job.query.filter(
            Job.status == Status.in_progress,
        )[0]
    except IndexError:
        return create_new_entry(project)

    job.status = Status.finished
    job.result = Result.failed if random.randint(0, 3) == 1 else Result.passed
    job.date_finished = datetime.utcnow()
    db.session.add(job)
    publish_job_update(job)

    test_results = []
    for _ in xrange(50):
        if job.result == Result.failed:
            result = Result.failed if random.randint(0, 3) == 1 else Result.passed
        else:
            result = Result.passed
        test_results.append(mock.test_result(job, result=result))
    TestResultManager(job).save(test_results)

    return job


def gen(project):
    if random.randint(0, 3) == 1:
        build = create_new_entry(project)
    else:
        build = update_existing_entry(project)

    db.session.commit()

    return build


def loop():
    repository = mock.repository()
    project = mock.project(repository)

    while True:
        plan = mock.plan()
        get_or_create(ProjectPlan, where={
            'plan': plan,
            'project': project,
        })

        build = gen(project)
        print 'Pushed build {0} on {1}'.format(build.id, project.slug)
        time.sleep(1)


if __name__ == '__main__':
    loop()<commit_msg>Only mock a single plan per run<commit_after>#!/usr/bin/env python

import random
import time

from datetime import datetime

from changes import mock
from changes.config import db, create_app
from changes.constants import Result, Status
from changes.db.utils import get_or_create
from changes.events import publish_build_update, publish_job_update
from changes.models import Change, Job, LogSource, TestResultManager, ProjectPlan

app = create_app()
app_context = app.app_context()
app_context.push()


def create_new_change(project, **kwargs):
    return mock.change(project=project, **kwargs)


def create_new_entry(project):
    new_change = (random.randint(0, 2) == 1)
    if not new_change:
        try:
            change = Change.query.all()[0]
        except IndexError:
            new_change = True

    if new_change:
        author = mock.author()
        revision = mock.revision(project.repository, author)
        change = create_new_change(
            project=project,
            author=author,
            message=revision.message,
        )
    else:
        change.date_modified = datetime.utcnow()
        db.session.add(change)
        revision = mock.revision(project.repository, change.author)

    source = mock.source(project.repository, revision_sha=revision.sha)

    date_started = datetime.utcnow()

    build = mock.build(
        author=change.author,
        project=project,
        source=source,
        message=change.message,
        result=Result.unknown,
        status=Status.in_progress,
        date_started=date_started,
    )
    publish_build_update(build)

    for x in xrange(3):
        job = mock.job(
            build=build,
            change=change,
            status=Status.in_progress,
        )
        publish_job_update(job)

        logsource = LogSource(
            job=job,
            project=job.project,
            name='console',
        )
        db.session.add(logsource)
        db.session.commit()

        offset = 0
        for x in xrange(30):
            lc = mock.logchunk(source=logsource, offset=offset)
            db.session.commit()
            offset += lc.size

    return build


def update_existing_entry(project):
    try:
        job = Job.query.filter(
            Job.status == Status.in_progress,
        )[0]
    except IndexError:
        return create_new_entry(project)

    job.status = Status.finished
    job.result = Result.failed if random.randint(0, 3) == 1 else Result.passed
    job.date_finished = datetime.utcnow()
    db.session.add(job)
    publish_job_update(job)

    test_results = []
    for _ in xrange(50):
        if job.result == Result.failed:
            result = Result.failed if random.randint(0, 3) == 1 else Result.passed
        else:
            result = Result.passed
        test_results.append(mock.test_result(job, result=result))
    TestResultManager(job).save(test_results)

    return job


def gen(project):
    if random.randint(0, 3) == 1:
        build = create_new_entry(project)
    else:
        build = update_existing_entry(project)

    db.session.commit()

    return build


def loop():
    repository = mock.repository()
    project = mock.project(repository)
    plan = mock.plan()
    get_or_create(ProjectPlan, where={
        'plan': plan,
        'project': project,
    })

    while True:
        build = gen(project)
        print 'Pushed build {0} on {1}'.format(build.id, project.slug)
        time.sleep(1)


if __name__ == '__main__':
    loop()
{'fraction_non_alphanumeric': 0.09984310369419484, 'fraction_numerical': 0.006561118242761375, 'mean_word_length': 2.3809064609450337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '295649', 'n_tokens_mistral': 2112, 'n_tokens_neox': 2038, 'n_words': 457}