Skip to content

Commit 16f562e

Browse files
committed
added
1 parent 992ede5 commit 16f562e

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

bin/stats.pl

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env perl
2+
use Data::Printer;
3+
use DBI;
4+
use HTTP::Request::Common qw(GET DELETE);
5+
use JSON::XS;
6+
use LWP::UserAgent;
7+
use constant URL => q{https://rdap.org/stats};
8+
use vars qw(@SERIES $DB);
9+
use common::sense;
10+
11+
@SERIES = qw(status type user_agent network);
12+
13+
my $DB = DBI->connect(sprintf(
14+
q{dbi:SQLite:dbname=%s},
15+
$ARGV[0] || q{./stats.db}
16+
));
17+
18+
$DB->do(q{CREATE TABLE IF NOT EXISTS total_queries (
19+
id INTEGER PRIMARY KEY,
20+
timestamp INTEGER,
21+
count INTEGER
22+
)});
23+
24+
foreach my $column (@SERIES) {
25+
$DB->do(sprintf(q{CREATE TABLE IF NOT EXISTS `queries_by_%s` (
26+
`id` INTEGER PRIMARY KEY,
27+
`timestamp` INTEGER,
28+
`%s` TEXT,
29+
`count` INTEGER
30+
)}, $column, $column));
31+
}
32+
33+
my $ua = LWP::UserAgent->new;
34+
35+
#
36+
# 1. get current stats
37+
#
38+
39+
my $req1 = GET(URL);
40+
$req1->header(authorization => sprintf(q{Bearer %s}, $ENV{STATS_TOKEN}));
41+
my $res1 = $ua->request($req1);
42+
43+
die($res1->status_line) unless ($res1->is_success);
44+
45+
#
46+
# 2. clear stats
47+
#
48+
my $req2 = DELETE(URL);
49+
$req2->header(authorization => sprintf(q{Bearer %s}, $ENV{STATS_TOKEN}));
50+
51+
my $res2 = $ua->request($req2);
52+
53+
die($res2->status_line) unless ($res2->is_success);
54+
55+
#
56+
# store stats in DB
57+
#
58+
59+
my $stats = JSON::XS->new->utf8->decode($res1->decoded_content);
60+
61+
my $timestamp = delete($stats->{timestamp});
62+
63+
$DB->prepare(q{
64+
INSERT INTO `total_queries`
65+
(`timestamp`, `count`)
66+
VALUES (?, ?)
67+
})->execute(
68+
$timestamp,
69+
delete($stats->{total_queries}),
70+
);
71+
72+
foreach my $column (@SERIES) {
73+
my $key = sprintf(q{queries_by_%s}, $column);
74+
75+
my $sth = $DB->prepare(sprintf(
76+
q{
77+
INSERT INTO `%s`
78+
(`timestamp`, `%s`, `count`)
79+
VALUES (?, ?, ?)
80+
},
81+
$key,
82+
$column,
83+
));
84+
85+
foreach my $value (keys(%{$stats->{$key}})) {
86+
$sth->execute(
87+
$timestamp,
88+
$value,
89+
$stats->{$key}->{$value},
90+
);
91+
}
92+
}

0 commit comments

Comments
 (0)