Browse Source

Consolidating database calls in db.py

Change-Id: Ic50c3a91aab815056eab9d7c36a7748cacdf6e53
changes/23/246723/11
apoorvad 3 years ago
parent
commit
5d66cc9fa0
5 changed files with 36 additions and 37 deletions
  1. 1
    0
      .gitignore
  2. 5
    27
      ciwatch/api.py
  3. 24
    0
      ciwatch/db.py
  4. 5
    8
      ciwatch/events.py
  5. 1
    2
      ciwatch/views.py

+ 1
- 0
.gitignore View File

@@ -52,3 +52,4 @@ ChangeLog
52 52
 *~
53 53
 .*.swp
54 54
 .*sw?
55
+.idea/

+ 5
- 27
ciwatch/api.py View File

@@ -17,14 +17,8 @@ from datetime import datetime
17 17
 from datetime import timedelta
18 18
 
19 19
 from flask import request
20
-from sqlalchemy import and_
21
-from sqlalchemy import desc
22 20
 
23 21
 from ciwatch import db
24
-from ciwatch.models import CiServer
25
-from ciwatch.models import PatchSet
26
-from ciwatch.models import Project
27
-
28 22
 
29 23
 TIME_OPTIONS = OrderedDict([  # Map time options to hours
30 24
     ("24 hours", 24),
@@ -48,34 +42,18 @@ def _get_ci_info_for_patch_sets(ci, patch_sets):
48 42
     return ci_info
49 43
 
50 44
 
51
-def get_projects():
52
-    return db.Session().query(Project).order_by(Project.name).all()
53
-
54
-
55
-def get_ci_servers():
56
-    return db.Session().query(CiServer).order_by(
57
-        desc(CiServer.trusted), CiServer.name).all()
58
-
59
-
60
-def get_patch_sets(project, since):
61
-    return db.Session().query(PatchSet).filter(
62
-        and_(PatchSet.project == project, PatchSet.created >= since)
63
-        ).order_by(PatchSet.created.desc()).all()
64
-
65
-
66 45
 def get_time_options():
67 46
     return TIME_OPTIONS.keys()
68 47
 
69 48
 
70 49
 def get_context():
71
-    project = request.args.get('project', DEFAULT_PROJECT)
50
+    project_name = request.args.get('project', DEFAULT_PROJECT)
72 51
     time = request.args.get('time', DEFAULT_TIME_OPTION)
73 52
     since = datetime.now() - timedelta(hours=TIME_OPTIONS[time])
74
-    project = db.Session().query(Project).filter(
75
-        Project.name == project).one()
76
-    patch_sets = get_patch_sets(project=project, since=since)
53
+    project = db.get_project(project_name)
54
+    patch_sets = db.get_patch_sets(project, since)
77 55
     results = OrderedDict()
78
-    for ci in get_ci_servers():
56
+    for ci in db.get_ci_servers():
79 57
         ci_info = _get_ci_info_for_patch_sets(ci, patch_sets)
80 58
         if any(result for result in ci_info["results"]):
81 59
             results[ci.ci_owner] = results.get(ci.ci_owner, [])
@@ -86,5 +64,5 @@ def get_context():
86 64
             "time_option": time,
87 65
             "patch_sets": patch_sets,
88 66
             "project": project,
89
-            "projects": get_projects(),
67
+            "projects": db.get_projects(),
90 68
             "user_results": results}

+ 24
- 0
ciwatch/db.py View File

@@ -12,10 +12,13 @@
12 12
 #    License for the specific language governing permissions and limitations
13 13
 #    under the License.
14 14
 
15
+from sqlalchemy import and_
15 16
 from sqlalchemy import create_engine
17
+from sqlalchemy import desc
16 18
 from sqlalchemy.orm import scoped_session
17 19
 from sqlalchemy.orm import sessionmaker
18 20
 
21
+
19 22
 from ciwatch.config import Config
20 23
 from ciwatch import models
21 24
 
@@ -58,3 +61,24 @@ def get_or_create(model, commit_=True, **kwargs):
58 61
         if commit_:
59 62
             session.commit()
60 63
     return result
64
+
65
+
66
+def get_projects():
67
+    return Session().query(models.Project).order_by(models.Project.name).all()
68
+
69
+
70
+def get_ci_servers():
71
+    return Session().query(models.CiServer).order_by(
72
+        desc(models.CiServer.trusted), models.CiServer.name).all()
73
+
74
+
75
+def get_patch_sets(project, since):
76
+    return Session().query(models.PatchSet).filter(
77
+        and_(models.PatchSet.project == project,
78
+             models.PatchSet.created >= since)
79
+        ).order_by(models.PatchSet.created.desc()).all()
80
+
81
+
82
+def get_project(project_name):
83
+    return Session().query(models.Project).filter(
84
+        models.Project.name == project_name).one()

+ 5
- 8
ciwatch/events.py View File

@@ -121,10 +121,8 @@ def parse_json_event(event, projects):
121 121
 
122 122
 
123 123
 def add_event_to_db(event, commit_=True):
124
-    session = db.Session()
125
-    project = session.query(models.Project).filter(
126
-        models.Project.name == _process_project_name(
127
-            event["change"]["project"])).one()
124
+    project = db.get_project(_process_project_name(
125
+        event["change"]["project"]))
128 126
     patch_set = db.get_or_create(
129 127
         models.PatchSet,
130 128
         commit_=False,
@@ -133,9 +131,6 @@ def add_event_to_db(event, commit_=True):
133 131
         commit_message=event['change']['commitMessage'],
134 132
         created=datetime.fromtimestamp(
135 133
             int(event['patchSet']['createdOn'])))
136
-
137
-    owner_name = event["author"]["name"]
138
-    owner = db.get_or_create(models.CiOwner, name=owner_name)
139 134
     trusted = (event["author"]["username"] == "jenkins")
140 135
 
141 136
     if trusted and "approvals" in event:
@@ -144,6 +139,8 @@ def add_event_to_db(event, commit_=True):
144 139
         elif event["approvals"][0]["value"] in ("-1", "-2"):
145 140
             patch_set.verified = False
146 141
 
142
+    owner_name = event["author"]["name"]
143
+    owner = db.get_or_create(models.CiOwner, name=owner_name)
147 144
     for ci, data in event['ci-status'].iteritems():
148 145
         ci_server = db.get_or_create(models.CiServer,
149 146
                                      commit_=False,
@@ -156,7 +153,7 @@ def add_event_to_db(event, commit_=True):
156 153
                                     ci_server_id=ci_server.id,
157 154
                                     patch_set_id=patch_set.id)
158 155
     if commit_:
159
-        session.commit()
156
+        db.Session().commit()
160 157
 
161 158
 
162 159
 def main():

+ 1
- 2
ciwatch/views.py View File

@@ -17,7 +17,6 @@ from sqlalchemy.orm.exc import NoResultFound
17 17
 from werkzeug.exceptions import abort
18 18
 
19 19
 from ciwatch.api import get_context
20
-from ciwatch.api import get_projects
21 20
 from ciwatch.cache import cached
22 21
 from ciwatch import db
23 22
 from ciwatch.server import app
@@ -29,7 +28,7 @@ from ciwatch.server import app
29 28
 def home():
30 29
     try:
31 30
         return render_template(
32
-            'index.html.jinja', projects=get_projects())
31
+            'index.html.jinja', projects=db.get_projects())
33 32
     finally:
34 33
         db.Session.remove()
35 34
 

Loading…
Cancel
Save