Skip to content

Commit

Permalink
Add a new listing of gilded comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
spladug committed Dec 14, 2012
1 parent c91374a commit e028c35
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 7 deletions.
6 changes: 6 additions & 0 deletions r2/r2/config/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
from routes import Mapper
from pylons import config

def not_in_sr(environ, results):
return 'subreddit' not in environ and 'sub_domain' not in environ

def make_map():
map = Mapper()
mc = map.connect
Expand Down Expand Up @@ -118,6 +121,9 @@ def make_map():
mc('/info/:article/:dest/:comment', controller='front',
action='oldinfo', type='old', dest='comments', comment=None)

mc("/comments/gilded", action="listing", controller="gilded",
conditions={"function": not_in_sr})

mc('/related/:article/:title', controller='front',
action='related', title=None)
mc('/details/:article/:title', controller='front',
Expand Down
1 change: 1 addition & 0 deletions r2/r2/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def load_controllers():
from listingcontroller import RandomrisingController
from listingcontroller import UserController
from listingcontroller import CommentsController
from listingcontroller import GildedController

from listingcontroller import MyredditsController

Expand Down
6 changes: 6 additions & 0 deletions r2/r2/controllers/listingcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,3 +1049,9 @@ def GET_listing(self, **env):
c.profilepage = True
return ListingController.GET_listing(self, **env)


class GildedController(ListingController):
title_text = _("gilded comments")

def query(self):
return queries.get_gilded_comments()
22 changes: 17 additions & 5 deletions r2/r2/lib/db/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@
from r2.lib import amqp, sup, filters
from r2.lib.comment_tree import add_comments, update_comment_votes
from r2.models.promo import PROMOTE_STATUS, get_promote_srid
from r2.models.query_cache import (cached_query, merged_cached_query,
CachedQuery, CachedQueryMutator,
MergedCachedQuery)
from r2.models.query_cache import UserQueryCache, SubredditQueryCache
from r2.models.query_cache import ThingTupleComparator
from r2.models.query_cache import (
cached_query,
CachedQuery,
CachedQueryMutator,
filter_thing,
merged_cached_query,
MergedCachedQuery,
SubredditQueryCache,
ThingTupleComparator,
UserQueryCache,
)
from r2.models.last_modified import LastModified
from r2.lib.utils import SimpleSillyStub

Expand Down Expand Up @@ -749,6 +755,12 @@ def get_all_promoted_links():
get_all_accepted_links()]
return queries


@cached_query(SubredditQueryCache, sort=[desc("date")], filter_fn=filter_thing)
def get_gilded_comments():
return


def add_queries(queries, insert_items=None, delete_items=None, foreground=False):
"""Adds multiple queries to the query queue. If insert_items or
delete_items is specified, the query may not need to be
Expand Down
19 changes: 17 additions & 2 deletions r2/r2/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from pylons.i18n import ungettext, _
from datetime import datetime, timedelta
from hashlib import md5
from pycassa.util import convert_uuid_to_time

import random, re
import json
Expand Down Expand Up @@ -796,9 +797,17 @@ def make_permalink_slow(self, context=None, anchor=False):
context=context, anchor=anchor)

def _gild(self, user):
now = datetime.now(g.tz)

self._incr("gildings")

GildedCommentsByAccount.gild_comment(user, self)

from r2.lib.db import queries
with CachedQueryMutator() as m:
gilding = utils.Storage(thing=self, date=now)
m.insert(queries.get_gilded_comments(), [gilding])

def _fill_in_parents(self):
if not self.parent_id:
self.parents = ''
Expand Down Expand Up @@ -1438,9 +1447,15 @@ def _rowkey(date):
def get_gildings(cls, date):
key = cls._rowkey(date)
columns = cls.get_time_sorted_columns(key)
gildings = [json.loads(v) for v in columns.itervalues()]
for gilding in gildings:
gildings = []
for name, json_blob in columns.iteritems():
timestamp = convert_uuid_to_time(name)
date = datetime.utcfromtimestamp(timestamp).replace(tzinfo=g.tz)

gilding = json.loads(json_blob)
gilding["date"] = date
gilding["user"] = int(gilding["user"], 36)
gildings.append(gilding)
return gildings

@classmethod
Expand Down
10 changes: 10 additions & 0 deletions r2/r2/models/query_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@ def filter_thing2(x):
return x._thing2


def filter_thing(x):
"""Return "thing" from a proxy object.
Use this as the filter_fn when some object that's not a Thing or Relation
is used as the basis of a cached query.
"""
return x.thing


def _is_query_precomputed(query):
"""Return if this query must be updated offline in a batch job.
Expand Down
61 changes: 61 additions & 0 deletions scripts/migrate/backfill/gilded_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# The contents of this file are subject to the Common Public Attribution
# License Version 1.0. (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
# License Version 1.1, but Sections 14 and 15 have been added to cover use of
# software over a computer network and provide for limited attribution for the
# Original Developer. In addition, Exhibit A has been modified to be consistent
# with Exhibit B.
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is reddit.
#
# The Original Developer is the Initial Developer. The Initial Developer of
# the Original Code is reddit Inc.
#
# All portions of the code written by reddit are Copyright (c) 2006-2012 reddit
# Inc. All Rights Reserved.
###############################################################################
"""Fill in the gilded comment listing.
This listing is stored in get_gilded_comments and seen on /comments/gilded.
"""

import datetime

from pylons import g

from r2.lib.db.queries import get_gilded_comments
from r2.lib.utils import Storage
from r2.models import GildingsByDay, Thing, Comment
from r2.models.query_cache import MAX_CACHED_ITEMS, CachedQueryMutator


date = datetime.datetime.now(g.tz)
earliest_date = datetime.datetime(2012, 10, 01, tzinfo=g.tz)

already_seen = set()
to_insert = []

while date > earliest_date and len(to_insert) < MAX_CACHED_ITEMS:
gildings = GildingsByDay.get_gildings(date)
fullnames = [x["thing"] for x in gildings]
things = Thing._by_fullname(fullnames, data=True, return_dict=False)
comments = {t._fullname: t for t in things if isinstance(t, Comment)}

counter = 0
for gilding in gildings:
fullname = gilding["thing"]
if fullname in comments and fullname not in already_seen:
gilding["thing"] = comments[fullname]
to_insert.append(Storage(gilding))
already_seen.add(fullname)
counter += 1
date -= datetime.timedelta(days=1)

with CachedQueryMutator() as m:
m.insert(get_gilded_comments(), to_insert)

0 comments on commit e028c35

Please sign in to comment.