Skip to content
Permalink
Browse files
Better grouping/sorting of commits, improvements to daemon
----------------------------------------------------------

* copious logging in `daemon/base.py`
* HACK to calculate repo name in `Commits` table
* `atexit` in `db.py` to "disconnect" from database
* `views.get_grouped_commits` to calculate, group the commits: repos ordered by "most recent commit"
* add missing (django-generated) `wsgi.py`
  • Loading branch information
stnbu committed Sep 5, 2018
1 parent e76c736 commit b00cd2c056b1e5d418c37d2db3aa2daca4b63dbf
Showing with 83 additions and 16 deletions.
  1. +16 −1 gitbored/daemon/base.py
  2. +12 −0 gitbored/db.py
  3. +1 −1 gitbored/templates/gitbored/index.html
  4. +38 −14 gitbored/views.py
  5. +16 −0 gitbored/wsgi.py
@@ -35,17 +35,22 @@ def __init__(self, dir_path, api_auth):
def update_repos(self):
updates = []
url = self.gapi.get_url('/users/{username}/repos'.format(username=self.gapi.username))
for repo in self.gapi.get(url):
repos = self.gapi.get(url)
logger.debug('considering {} repos...'.format(len(repos)))
for repo in repos:
existing_repo = db.Repos.objects.filter(name=repo['name'])
if existing_repo:
logger.debug(' ...already have {}, skipping'.format(repo['name']))
continue
repo_data = {}
repo_data['name'] = repo['name']
repo_data['description'] = repo['description']
repo_data['owner_login'] = repo['owner']['login']
repo_data['updated_at'] = repo['updated_at']
repo_data['html_url'] = repo['html_url']
logger.debug(' ...updating {}'.format(repo['name']))
updates.append(db.Repos(**repo_data))
logger.debug('bulk-updating {} records into Repos'.format(len(updates)))
db.Repos.objects.bulk_create(updates)

def update_commits(self):
@@ -56,18 +61,27 @@ def update_commits(self):
if event['type'] != 'PushEvent':
continue
commits = event['payload'].get('commits', [])
logger.debug('examining {} commits'.format(len(commits)))
for commit in commits:
commit = flatten(commit)
if db.Commits.objects.filter(sha=commit['sha']):
logger.debug('we already have {}, skipping'.format(commit['sha']))
continue
detail = self.gapi.get(commit['url'])
commit.update(flatten(detail))
fields = db.Commits._meta.get_fields()
kwargs = {}
logger.debug('calculating values for Commits fields...')
for field in fields:
if field.name in commit:
logger.debug(' ...setting {}'.format(field.name))
kwargs[field.name] = commit[field.name]
# FIXME -- unfortunate hack
logger.info(' ...setting {} (with hack)'.format(field.name))
repo = kwargs['url'].split('/')[5]
kwargs['repo'] = repo
updates.append(db.Commits(**kwargs))
logger.debug('bulk-updating {} records into Commits'.format(len(updates)))
db.Commits.objects.bulk_create(updates)

def worker(self):
@@ -89,6 +103,7 @@ def exception_handler(type_, value, tb):
logger.addHandler(fh)

while True:
logger.debug('updating GitHub info...')
self.update_repos()
self.update_commits()
logger.debug('sleeping 600s')
@@ -12,8 +12,20 @@
import django
from .settings import *
from django.conf import settings
from django.db import connections
import atexit
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

settings.configure(DATABASES=DATABASES)
django.setup()

from .models import *

def cleanup():
logger.info('closing all django database connections for this process')
connections.close_all()

atexit.register(cleanup)
@@ -25,7 +25,7 @@ <h1>We're not done here...</h1>
<p><a href="https://github.com/stnbu/gitbored">This is a little django app</a> that <a href="https://github.com/stnbu/">I've</a> created to list out my most recent github commits. (I'm trying not to say "under construction" here...)</p>
<p>Please enjoy!</p>
<ul class="repo-list">
{% for repo, commits, blurb in repos_list %}
{% for repo, blurb, commits in repos_list %}
<li class="repo">

<h2 class="repo-title">Repository Name: <a href="{{ repo.html_url }}">{{ repo.name }}</a></h2>
@@ -2,22 +2,46 @@
from django.http import HttpResponseRedirect
from .models import Repos, Commits, Blurbs

def get_grouped_commits():
"""Return a list of the format
def index(request):
repos = Repos.objects.order_by('-updated_at')
repos_ = []
for repo in repos:
commits = Commits.objects.filter(repo=repo.name)
commits = list(reversed(commits))[:10]
try:
blurb = Blurbs.objects.get(repo_id=repo.id).blurb
except Blurbs.DoesNotExist: # what is the right way...?
blurb = ''
repos_.append(
(repo, commits, blurb)
)
`[(repo, blurb, [commit, commit, ...], ...]`
Each list of commits belongs to the corresponding `repo` (and `blurb`). The tuples are then sorted by
the date of the first commit in the list of commits.
"""
# a temporary dict used to group commits by repo
by_repo = {}
for commit in Commits.objects.all():
by_repo.setdefault(commit.repo, [])
by_repo[commit.repo].append(commit)
# the list that we will return
grouped_commits = []
for commits in by_repo.values():
if not commits:
continue
repo = Repos.objects.filter(name=commits[0].repo).first()
if repo:
blurb = Blurbs.objects.filter(repo_id=repo.id).first()
else:
blurb = None
# sort the commits by date
commits = sorted(commits,
key=lambda c: c.author_date,
reverse=True)
grouped_commits.append((repo, blurb, commits))

# sort the tuples by the date of the first commit
grouped_commits = sorted(grouped_commits,
key=lambda x: x[2][0].author_date,
reverse=True)
print(len(grouped_commits))

return grouped_commits


def index(request):
context = {
'repos_list': repos_,
'repos_list': get_grouped_commits(),
}
return render(request, 'gitbored/index.html', context)
@@ -0,0 +1,16 @@
"""
WSGI config for gitbored project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'gitbored.settings')

application = get_wsgi_application()

0 comments on commit b00cd2c

Please sign in to comment.