forked from dropbox/changes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_data.py
executable file
·164 lines (131 loc) · 4.38 KB
/
stream_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/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.models import (
Change, Job, JobStep, LogSource, TestResultManager, ProjectPlan,
ItemStat
)
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) == 5)
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)
if random.randint(0, 1) == 1:
patch = mock.patch(project)
else:
patch = None
source = mock.source(
project.repository, revision_sha=revision.sha, patch=patch)
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,
)
db.session.add(ItemStat(item_id=build.id, name='lines_covered', value='5'))
db.session.add(ItemStat(item_id=build.id, name='lines_uncovered', value='5'))
db.session.add(ItemStat(item_id=build.id, name='diff_lines_covered', value='5'))
db.session.add(ItemStat(item_id=build.id, name='diff_lines_uncovered', value='5'))
db.session.commit()
for x in xrange(0, random.randint(1, 3)):
job = mock.job(
build=build,
change=change,
status=Status.in_progress,
)
db.session.commit()
if patch:
mock.file_coverage(project, job, patch)
for step in JobStep.query.filter(JobStep.job == job):
logsource = LogSource(
job=job,
project=job.project,
step=step,
name=step.label,
)
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.date_modified = datetime.utcnow()
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)
jobstep = JobStep.query.filter(JobStep.job == job).first()
if jobstep:
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(jobstep, result=result))
TestResultManager(jobstep).save(test_results)
if job.status == Status.finished:
job.build.status = job.status
job.build.result = job.result
job.build.date_finished = job.date_finished
job.build.date_modified = job.date_finished
db.session.add(job.build)
return job
def gen(project):
if random.randint(0, 5) == 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(0.1)
if __name__ == '__main__':
loop()