Browse Source

Consolidating database calls in db.py

Change-Id: Ic50c3a91aab815056eab9d7c36a7748cacdf6e53
changes/23/246723/11
apoorvad 4 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
*~
.*.swp
.*sw?
.idea/

+ 5
- 27
ciwatch/api.py View File

@@ -17,14 +17,8 @@ from datetime import datetime
from datetime import timedelta

from flask import request
from sqlalchemy import and_
from sqlalchemy import desc

from ciwatch import db
from ciwatch.models import CiServer
from ciwatch.models import PatchSet
from ciwatch.models import Project


TIME_OPTIONS = OrderedDict([ # Map time options to hours
("24 hours", 24),
@@ -48,34 +42,18 @@ def _get_ci_info_for_patch_sets(ci, patch_sets):
return ci_info


def get_projects():
return db.Session().query(Project).order_by(Project.name).all()


def get_ci_servers():
return db.Session().query(CiServer).order_by(
desc(CiServer.trusted), CiServer.name).all()


def get_patch_sets(project, since):
return db.Session().query(PatchSet).filter(
and_(PatchSet.project == project, PatchSet.created >= since)
).order_by(PatchSet.created.desc()).all()


def get_time_options():
return TIME_OPTIONS.keys()


def get_context():
project = request.args.get('project', DEFAULT_PROJECT)
project_name = request.args.get('project', DEFAULT_PROJECT)
time = request.args.get('time', DEFAULT_TIME_OPTION)
since = datetime.now() - timedelta(hours=TIME_OPTIONS[time])
project = db.Session().query(Project).filter(
Project.name == project).one()
patch_sets = get_patch_sets(project=project, since=since)
project = db.get_project(project_name)
patch_sets = db.get_patch_sets(project, since)
results = OrderedDict()
for ci in get_ci_servers():
for ci in db.get_ci_servers():
ci_info = _get_ci_info_for_patch_sets(ci, patch_sets)
if any(result for result in ci_info["results"]):
results[ci.ci_owner] = results.get(ci.ci_owner, [])
@@ -86,5 +64,5 @@ def get_context():
"time_option": time,
"patch_sets": patch_sets,
"project": project,
"projects": get_projects(),
"projects": db.get_projects(),
"user_results": results}

+ 24
- 0
ciwatch/db.py View File

@@ -12,10 +12,13 @@
# License for the specific language governing permissions and limitations
# under the License.

from sqlalchemy import and_
from sqlalchemy import create_engine
from sqlalchemy import desc
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker


from ciwatch.config import Config
from ciwatch import models

@@ -58,3 +61,24 @@ def get_or_create(model, commit_=True, **kwargs):
if commit_:
session.commit()
return result


def get_projects():
return Session().query(models.Project).order_by(models.Project.name).all()


def get_ci_servers():
return Session().query(models.CiServer).order_by(
desc(models.CiServer.trusted), models.CiServer.name).all()


def get_patch_sets(project, since):
return Session().query(models.PatchSet).filter(
and_(models.PatchSet.project == project,
models.PatchSet.created >= since)
).order_by(models.PatchSet.created.desc()).all()


def get_project(project_name):
return Session().query(models.Project).filter(
models.Project.name == project_name).one()

+ 5
- 8
ciwatch/events.py View File

@@ -121,10 +121,8 @@ def parse_json_event(event, projects):


def add_event_to_db(event, commit_=True):
session = db.Session()
project = session.query(models.Project).filter(
models.Project.name == _process_project_name(
event["change"]["project"])).one()
project = db.get_project(_process_project_name(
event["change"]["project"]))
patch_set = db.get_or_create(
models.PatchSet,
commit_=False,
@@ -133,9 +131,6 @@ def add_event_to_db(event, commit_=True):
commit_message=event['change']['commitMessage'],
created=datetime.fromtimestamp(
int(event['patchSet']['createdOn'])))

owner_name = event["author"]["name"]
owner = db.get_or_create(models.CiOwner, name=owner_name)
trusted = (event["author"]["username"] == "jenkins")

if trusted and "approvals" in event:
@@ -144,6 +139,8 @@ def add_event_to_db(event, commit_=True):
elif event["approvals"][0]["value"] in ("-1", "-2"):
patch_set.verified = False

owner_name = event["author"]["name"]
owner = db.get_or_create(models.CiOwner, name=owner_name)
for ci, data in event['ci-status'].iteritems():
ci_server = db.get_or_create(models.CiServer,
commit_=False,
@@ -156,7 +153,7 @@ def add_event_to_db(event, commit_=True):
ci_server_id=ci_server.id,
patch_set_id=patch_set.id)
if commit_:
session.commit()
db.Session().commit()


def main():

+ 1
- 2
ciwatch/views.py View File

@@ -17,7 +17,6 @@ from sqlalchemy.orm.exc import NoResultFound
from werkzeug.exceptions import abort

from ciwatch.api import get_context
from ciwatch.api import get_projects
from ciwatch.cache import cached
from ciwatch import db
from ciwatch.server import app
@@ -29,7 +28,7 @@ from ciwatch.server import app
def home():
try:
return render_template(
'index.html.jinja', projects=get_projects())
'index.html.jinja', projects=db.get_projects())
finally:
db.Session.remove()


Loading…
Cancel
Save