Skip to content

Commit a3ba831

Browse files
committed
updated
1 parent 7f53723 commit a3ba831

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

bin/stats.pl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
#!/usr/bin/env perl
2-
use Data::Printer;
32
use DBI;
3+
use Fcntl qw(:flock);
44
use HTTP::Request::Common qw(GET DELETE);
55
use JSON::XS;
66
use LWP::UserAgent;
77
use constant URL => q{https://rdap.org/stats};
88
use vars qw(@SERIES $DB);
99
use common::sense;
1010

11+
if (!flock(DATA, LOCK_EX | LOCK_NB)) {
12+
say STDERR q{waiting for existing process to finish...};
13+
flock(DATA, LOCK_EX);
14+
}
15+
1116
@SERIES = qw(status type user_agent network);
1217

18+
say STDERR q{opening database connection...};
1319
my $DB = DBI->connect(sprintf(
1420
q{dbi:SQLite:dbname=%s},
1521
$ARGV[0] || q{./stats.db}
1622
));
1723

1824
$DB->do(q{CREATE TABLE IF NOT EXISTS total_queries (
1925
id INTEGER PRIMARY KEY,
20-
timestamp INTEGER,
26+
timestamp INTEGER UNIQUE,
2127
count INTEGER
2228
)});
2329

@@ -28,6 +34,13 @@
2834
`%s` TEXT,
2935
`count` INTEGER
3036
)}, $column, $column));
37+
38+
$DB->do(sprintf(
39+
q{CREATE UNIQUE INDEX IF NOT EXISTS `queries_by_%s_index` ON `queries_by_%s`(`timestamp`, `%s`)},
40+
$column,
41+
$column,
42+
$column,
43+
));
3144
}
3245

3346
my $ua = LWP::UserAgent->new;
@@ -38,6 +51,9 @@
3851

3952
my $req1 = GET(URL);
4053
$req1->header(authorization => sprintf(q{Bearer %s}, $ENV{STATS_TOKEN}));
54+
55+
say STDERR q{getting stats...};
56+
4157
my $res1 = $ua->request($req1);
4258

4359
die($res1->status_line) unless ($res1->is_success);
@@ -48,6 +64,7 @@
4864
my $req2 = DELETE(URL);
4965
$req2->header(authorization => sprintf(q{Bearer %s}, $ENV{STATS_TOKEN}));
5066

67+
say STDERR q{purging stats...};
5168
my $res2 = $ua->request($req2);
5269

5370
die($res2->status_line) unless ($res2->is_success);
@@ -60,6 +77,8 @@
6077

6178
my $timestamp = delete($stats->{timestamp});
6279

80+
say STDERR q{updating database...};
81+
6382
$DB->prepare(q{
6483
INSERT INTO `total_queries`
6584
(`timestamp`, `count`)
@@ -90,3 +109,7 @@
90109
);
91110
}
92111
}
112+
113+
say STDERR q{done};
114+
115+
__DATA__

0 commit comments

Comments
 (0)