Last active
December 31, 2015 10:29
-
-
Save mix3/7973095 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ "" == "$(ps aux | grep ScreenSaverEngine | grep -v grep)" ]; then | |
/usr/local/opt/mysql/bin/mysql -u root beacon_receiver -e "insert into receive values (now());" | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use 5.10.0; | |
use strict; | |
use warnings; | |
use utf8; | |
use Devel::KYTProf; | |
use Time::Piece; | |
use Time::Seconds; | |
use DBIx::Sunny; | |
use Getopt::Long qw/:config posix_default no_ignore_case bundling auto_help/; | |
GetOptions(\my %opt, qw/ | |
time|t=s | |
interval|i=i | |
help|h|? | |
/); | |
usage() if exists $opt{help}; | |
$opt{interval} ||= 300; | |
use constant CONF => { | |
dsn => 'dbi:mysql:beacon_receiver', | |
user => 'root', | |
pass => '', | |
}; | |
my $summary = summary(%opt); | |
my $s = get_tp(); | |
my $e = $s + ONE_DAY; | |
while ($s < $e) { | |
my $k = $s->strftime("%Y-%m-%d %H:%M:%S"); | |
print "[", $k, "]: ", "*"x($summary->{$k} || 0), "\n"; | |
$s = $s + $opt{interval}; | |
} | |
exit 0; | |
sub summary { | |
my (%opt) = @_; | |
my $tp = get_tp(%opt); | |
my $start_at = $tp->ymd('-')." 05:00:00"; | |
my $end_at = ($tp + ONE_DAY)->ymd('-')." 04:59:59"; | |
my $sql = <<"___"; | |
SELECT | |
FROM_UNIXTIME(TRUNCATE(UNIX_TIMESTAMP(time) / $opt{interval}, 0) * $opt{interval}) AS time, | |
COUNT(time) AS count | |
FROM receive | |
WHERE time BETWEEN '$start_at' AND '$end_at' | |
GROUP BY TRUNCATE(UNIX_TIMESTAMP(time) / $opt{interval}, 0); | |
___ | |
my $ret = dbh()->select_all($sql); | |
my $map = {}; | |
for my $r (@$ret) { | |
$map->{$r->{time}} = $r->{count}; | |
} | |
return $map; | |
} | |
sub get_tp { | |
my (%opt) = @_; | |
state $tp = do { | |
my $t; | |
if (exists $opt{time}) { | |
$t = Time::Piece->strptime($opt{time}, "%Y-%m-%d"); | |
} else { | |
$t = localtime; | |
$t = $t - (ONE_HOUR * 5); | |
} | |
Time::Piece->strptime($t->ymd('-')." 05:00:00", "%Y-%m-%d %H:%M:%S"); | |
}; | |
} | |
sub dbh { | |
state $dbh = DBIx::Sunny->connect( | |
CONF()->{dsn}, CONF()->{user}, CONF()->{pass} | |
); | |
} | |
sub usage { | |
print <<"___"; | |
Usage: perl $0 [--time "Y-m-d"] [--interval seconds] | |
___ | |
exit 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* * * * * /bin/bash ~/.beacon.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE receive ( | |
time datetime NOT NULL, | |
PRIMARY KEY (`time`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment