forked from reddit-archive/reddit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_time_listings
executable file
·108 lines (94 loc) · 3.48 KB
/
compute_time_listings
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
#!/bin/bash
# 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-2014 reddit
# Inc. All Rights Reserved.
###############################################################################
set -e
# expects two environment variables
# REDDIT_ROOT = path to the root of the reddit public code; the directory with the Makefile
# REDDIT_INI = path to the ini file to use
# which should be supplied via:
source /etc/default/reddit
# additionally, some configuration can be overridden in the environment
export TMPDIR=${TMPDIR:-/tmp}
export PGUSER=${PGUSER:-reddit}
export PGHOST=${PGHOST:-localhost}
## command line args
# one of "link" or "comment"
export THING_CLS="$1"
# period of data to extract from postgres: e.g. "hour", "week", "year", "all"
export INTERVAL="$2"
# which period listings to update.
# formatted as python tuple of strings: e.g. '("hour",)' or ("week", "all",) etc
export TIMES="$3"
THING_DUMP=$TMPDIR/$THING_CLS-$INTERVAL-thing.dump
DATA_DUMP=$TMPDIR/$THING_CLS-$INTERVAL-data.dump
function clean_up {
rm -f $THING_DUMP $DATA_DUMP
}
trap clean_up EXIT
if [ -e $THING_DUMP ]; then
echo cannot start because $THING_DUMP exists
ls -l $THING_DUMP
exit 1
fi
touch $THING_DUMP
function run_query {
psql -F"\t" -A -t -c "$1"
}
function mrsort {
sort -S200m
}
function reddit {
paster --plugin=r2 run $REDDIT_INI $REDDIT_ROOT/r2/lib/mr_top.py -c "$1"
}
# Hack to let pg fetch all things with intervals
if [ $INTERVAL = "all" ]; then
export INTERVAL="century"
fi
MINID=$(run_query "SELECT thing_id
FROM reddit_thing_$THING_CLS
WHERE
date > now() - interval '1 $INTERVAL' AND
date < now()
ORDER BY date
LIMIT 1")
if [ -z $MINID ]; then
echo \$MINID is empty. Replication is likely behind.
exit 1
fi
run_query "\\copy (SELECT thing_id, 'thing', '$THING_CLS', ups, downs, deleted, spam, extract(epoch from date)
FROM reddit_thing_$THING_CLS
WHERE
not deleted AND
thing_id >= $MINID
) to $THING_DUMP"
run_query "\\copy (SELECT thing_id, 'data', '$THING_CLS', key, value
FROM reddit_data_$THING_CLS
WHERE
key IN ('url', 'sr_id', 'author_id') AND
thing_id >= $MINID
) to $DATA_DUMP"
cat $THING_DUMP $DATA_DUMP |
mrsort |
reddit "join_things()" |
reddit "time_listings($TIMES)" |
mrsort |
reddit "write_permacache()"