Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.20 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # use strict;
  3. # use warnings;
  4. # use DBI;
  5. # use DBI qw(:sql_types);
  6. # use Getopt::Long;
  7. # use Config::Simple;
  8. # use Carp qw(croak);
  9. # use DateTime;
  10. # use Time::HiRes qw{gettimeofday};
  11. # use POSIX qw{ceil floor strftime};
  12. # use Log::Log4perl qw{:easy};
  13. # use MIME::Lite;
  14.  
  15. # my $dbName = 'reivernet';
  16. # my $month = -1;
  17. # my $year = -1;
  18. # my $dayofmonth = -1;
  19. # my $debug;
  20. # my $all;
  21. # my $today;
  22. # my $yesterday;
  23. # my $thismonth;
  24. # my $background;
  25. # my $block_type;
  26. # my $previous_hours;
  27.  
  28. # GetOptions(
  29. # "db|database=s" => \$dbName,
  30. # "day=i" => \$dayofmonth,
  31. # "month=i" => \$month,
  32. # "year=i" => \$year,
  33. # "d|debug" => \$debug,
  34. # "all" => \$all,
  35. # "today" => \$today,
  36. # "yesterday" => \$yesterday,
  37. # "thismonth" => \$thismonth,
  38. # "background" => \$background,
  39. # "block-type=s" => \$block_type,
  40. # "hours=i" => \$previous_hours,
  41. # );
  42.  
  43. if (defined($background) and $background) {
  44. use Proc::Daemon;
  45. Proc::Daemon::Init;
  46. }
  47.  
  48. use Proc::PID::File;
  49. croak("Already running!") if Proc::PID::File->running();
  50.  
  51. my $layout = Log::Log4perl::Layout::PatternLayout->new('%d [%p] %m%n');
  52.  
  53. my $file_appender = Log::Log4perl::Appender->new(
  54. 'Log::Dispatch::File',
  55. filename => '/var/log/reivernet/crunch_postpaid_stats.log',
  56. mode => 'append',
  57. );
  58. $file_appender->layout($layout);
  59.  
  60. # my $logger = get_logger('crunch_postpaid_stats');
  61. # if (!$logger) {
  62. # print "ERROR: Can't get logger\n";
  63. # exit;
  64. # }
  65. # $logger->add_appender($file_appender);
  66. # $logger->level($INFO);
  67.  
  68. # if ($debug) {
  69. # $logger->level($DEBUG);
  70. # my $screen_appender =
  71. # Log::Log4perl::Appender->new("Log::Dispatch::Screen", stderr => "0" );
  72. # $screen_appender->layout($layout);
  73. # $logger->add_appender($screen_appender);
  74. # }
  75.  
  76. # my $localtz = DateTime::TimeZone->new( name => 'local' );
  77.  
  78. # my %config;
  79. # my $config_file = '/etc/reivernet.conf';
  80. # Config::Simple->import_from( $config_file, \%config );
  81.  
  82. # # Database Parameters
  83. # if (!defined($dbName)) {
  84. # $dbName = $config{'dbName'};
  85. # if ( (!defined($dbName)) or ($dbName eq '') ) { $dbName = 'reivernet'; }
  86. # }
  87. # my $dbUsername = $config{'dbuser'};
  88. # if ( (!defined($dbUsername)) or ($dbUsername eq '') ) { $dbUsername = 'root'; }
  89. # my $dbPassword = $config{'dbpassword'};
  90. # if ( (!defined($dbPassword)) or ($dbPassword eq '') ) { $dbPassword = 'hmm'; }
  91.  
  92. # # Connect to the database
  93. # our $dbh = DBI->connect( 'dbi:mysql:' . $dbName, $dbUsername, $dbPassword,
  94. # { AutoCommit => 1, 'mysql_auto_reconnect' => 1, RaiseError => 0 } )
  95. # or croak "ERROR: Database connection not made: $DBI::errstr";
  96.  
  97. # my $start_dt;
  98. # my $finish_dt;
  99. # my $now_dt = DateTime->now(
  100. # time_zone => $localtz
  101. # );
  102.  
  103.  
  104.  
  105. if (defined($all) and $all == 1) {
  106. my $sql_firstLast = $dbh->prepare('SELECT DATE(MIN(calculation_time)), DATE(MAX(calculation_time)) FROM BillingSession')
  107. or $logger->critical("Unable to execute min and max times from pool_readings: $DBI::errstr");
  108.  
  109. die if (!$sql_firstLast);
  110. $sql_firstLast->execute
  111. or $logger->critical("Unable to execute min and max times from pool_readings: $DBI::errstr");
  112.  
  113. die if (!$sql_firstLast);
  114. if ($sql_firstLast->rows == 0) {
  115. print "There are no pool_readings available\n";
  116. exit 0;
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. my ($min_date,$max_date) = $sql_firstLast->fetchrow_array;
  125. my ($min_year,$min_month,$min_day) = split /-/x, $min_date;
  126. my ($max_year,$max_month,$max_day) = split /-/x, $max_date;
  127.  
  128.  
  129.  
  130.  
  131. $start_dt = DateTime->new(
  132. year => $min_year,
  133. month => $min_month,
  134. day => $min_day,
  135. time_zone => $localtz
  136. );
  137. $finish_dt = DateTime->new(
  138. year => $max_year,
  139. month => $max_month,
  140. day => $max_day,
  141. time_zone => $localtz
  142. );
  143. }
  144. elsif ($dayofmonth > 0 and $month >= 0 and $year > 0) {
  145. $start_dt = DateTime->new(
  146. year => $year,
  147. month => ($month == 0 ? 1 : $month),
  148. day => $dayofmonth,
  149. time_zone => $localtz
  150. );
  151. $finish_dt = $start_dt->clone();
  152. $finish_dt->add(new DateTime::Duration('days' => 1));
  153. }
  154. elsif ($month >= 0 and $year > 0) {
  155. $start_dt = DateTime->new(
  156. year => $year,
  157. month => ($month == 0 ? 1 : $month),
  158. day => 1,
  159. time_zone => $localtz
  160. );
  161. $finish_dt = $start_dt->clone();
  162. if ($month == 0) {
  163. $finish_dt->add(new DateTime::Duration('years' => 1));
  164. }
  165. else {
  166. $finish_dt->add(new DateTime::Duration('months' => 1));
  167. }
  168. }
  169. elsif (defined($thismonth)) {
  170. my $today = DateTime->today(
  171. time_zone => $localtz
  172. );
  173. $start_dt = $today->clone();
  174. $start_dt->set(day => 1);
  175. $finish_dt = $today->clone();
  176. }
  177. elsif (defined($today)) {
  178. my $today = DateTime->today(
  179. time_zone => $localtz
  180. );
  181. $start_dt = $today->clone();
  182. $finish_dt = $today->clone()->add(new DateTime::Duration('days' => 1));
  183. }
  184. else {
  185. if (defined($previous_hours)) {
  186. $start_dt = $now_dt->clone()->subtract(new DateTime::Duration('hours' => $previous_hours));
  187. $finish_dt = $now_dt->clone();
  188. }
  189. else {
  190. my $today = DateTime->today(
  191. time_zone => $localtz
  192. );
  193. $start_dt = $today->clone()->subtract(new DateTime::Duration('days' => 1));
  194. $finish_dt = $today->clone();
  195. }
  196. }
  197.  
  198. $start_dt->set( second => 0 );
  199. $finish_dt->set( second => 0 );
  200.  
  201. if (!defined($previous_hours)) {
  202. $start_dt->set( hour => 0, minute => 0 );
  203. $finish_dt->set( hour => 0, minute => 0 );
  204. }
  205.  
  206. my $total_time_start = gettimeofday();
  207.  
  208. my %billing_types;
  209. my $sql_getDataBillingType = $dbh->prepare("SELECT id, name FROM BillingType")
  210. or croak("Unable to prepare billing type select: $DBI::errstr");
  211. $sql_getDataBillingType->execute();
  212. if ($sql_getDataBillingType->rows > 0) {
  213. while (my ($id, $name) = $sql_getDataBillingType->fetchrow_array()) {
  214. $billing_types{$name} = $id;
  215. }
  216. }
  217.  
  218.  
  219. my $sql_deleteStats = $dbh->prepare("DELETE FROM PostpaidStatistic WHERE date = ?")
  220. or croak("Unable to prepare postpaid statistic delete: $DBI::errstr");
  221.  
  222. my $sql_insertStats = $dbh->prepare("
  223. INSERT INTO PostpaidStatistic
  224. (date, room_name, room_type, pricing_plan, total_data_used, total_revenue)
  225. SELECT
  226. calc_date, room_name, IF(room_type = 'Guest room', 'guest_room', IF(room_type = 'Guest room 2', 'guest_room_2', 'other')) AS room_type, pricing_plan,
  227. SUM(total_data_used), SUM(total_revenue)
  228. FROM (
  229. SELECT DATE(bd.calculation_time) AS calc_date,
  230. e.room_name AS room_name,
  231. e.room_type AS room_type,
  232. e.pricing_plan AS pricing_plan,
  233. SUM(bd.units) as total_data_used,
  234. SUM(bd.charge) AS total_revenue
  235. FROM Enrolment e, BillingSession bd
  236. WHERE e.id = bd.enrolment_id
  237. AND e.room_type IN ('Guest room', 'Guest room 2', 'Other')
  238. AND bd.billing_type_id = $billing_types{'data'}
  239. AND bd.calculation_time BETWEEN ? AND ?
  240. AND e.cc_trans_id IN ('0','')
  241. AND e.description != 'Master'
  242. AND (e.vip_code = '' OR e.vip_code IS NULL)
  243. GROUP BY calc_date, room_name, room_type, pricing_plan
  244. UNION
  245. SELECT DATE(bt.calculation_time) AS calc_date,
  246. e.room_name AS room_name,
  247. e.room_type AS room_type,
  248. e.pricing_plan AS pricing_plan,
  249. 0 as total_data_used,
  250. SUM(bt.charge) AS total_revenue
  251. FROM Enrolment e, BillingSession bt
  252. WHERE e.id = bt.enrolment_id
  253. AND e.room_type IN ('Guest room', 'Guest room 2', 'Other')
  254. AND bt.billing_type_id = $billing_types{'time'}
  255. AND bt.calculation_time BETWEEN ? AND ?
  256. AND e.cc_trans_id IN ('0','')
  257. AND e.description != 'Master'
  258. AND (e.vip_code = '' OR e.vip_code IS NULL)
  259. GROUP BY calc_date, room_name, room_type, pricing_plan
  260. ) AS inception_query
  261. GROUP BY calc_date, room_name, room_type, pricing_plan
  262.  
  263. ")
  264. or croak("Unable to prepare insert postpaid statistic: $DBI::errstr");
  265.  
  266. my $sql_selectPlanPurchases = $dbh->prepare("
  267. SELECT IF(e.room_type = 'Guest room', 'guest_room', IF(e.room_type = 'Guest room 2', 'guest_room_2', 'other')) AS room_type,
  268. room_name, pricing_plan, COUNT(DISTINCT(e.id))
  269. FROM Enrolment e
  270. WHERE e.room_type IN ('Guest room', 'Guest room 2', 'Other')
  271. AND e.start_time BETWEEN ? AND ?
  272. AND e.cc_trans_id IN ('0','')
  273. AND e.description != 'Master'
  274. AND (e.vip_code = '' OR e.vip_code IS NULL)
  275. GROUP BY room_type, room_name, pricing_plan
  276. ")
  277. or croak("Unable to prepare select postpaid statistic: $DBI::errstr");
  278.  
  279. my $sql_updatePlanPurchases = $dbh->prepare("
  280. UPDATE PostpaidStatistic
  281. SET enrolment_count = ?
  282. WHERE date = ?
  283. AND room_type = ?
  284. AND room_name = ?
  285. AND pricing_plan = ?
  286. ")
  287. or croak("Unable to prepare update postpaid statistic: $DBI::errstr");
  288.  
  289. while ($start_dt < $finish_dt && $start_dt < $now_dt) {
  290. my $start_time = $start_dt->ymd('-').' '.$start_dt->hms(':');
  291. my $finish_time = $start_dt->clone()->add(new DateTime::Duration('days' => 1))->ymd('-').' '.$finish_dt->hms(':');
  292. if (defined($previous_hours)) { $finish_time = $finish_dt->ymd('-').' '.$finish_dt->hms(':'); }
  293.  
  294. my $date_processing = $start_dt->ymd('-');
  295. $logger->info("Processing $start_time to $finish_time");
  296.  
  297. my $day_timer_start = gettimeofday();
  298.  
  299.  
  300. $sql_deleteStats->execute($start_dt->ymd('-'))
  301. or croak("Unable to delete postpaid statistic between '$start_time' AND '$finish_time': $DBI::errstr");
  302.  
  303. $logger->debug(".. .. deleted ".$sql_deleteStats->rows." rows");
  304.  
  305. $sql_insertStats->execute($start_time, $finish_time, $start_time, $finish_time)
  306. or croak("Unable to insert postpaid statistic between '$start_time' AND '$finish_time': $DBI::errstr");
  307.  
  308. $logger->debug(".. .. inserted ".$sql_insertStats->rows." rows");
  309.  
  310. $sql_selectPlanPurchases->execute($start_time, $finish_time)
  311. or croak("Unable to execute select purchases between '$start_time' AND '$finish_time': $DBI::errstr");
  312.  
  313. my $update_count = 0;
  314. my ($room_type, $room_name, $pricing_plan, $count);
  315. $sql_selectPlanPurchases->bind_columns(\$room_type, \$room_name, \$pricing_plan, \$count);
  316. while ($sql_selectPlanPurchases->fetch()) {
  317. $sql_updatePlanPurchases->execute($count, $start_dt->ymd('-'), $room_type, $room_name, $pricing_plan)
  318. or croak("Unable to update postpaid plan purchase statistic between '$start_time' AND '$finish_time': $DBI::errstr");
  319. $update_count++;
  320. }
  321.  
  322. $logger->debug(".. .. updated $update_count rows");
  323.  
  324. my $day_timer_end = gettimeofday();
  325. my $day_timer_diff = floor($day_timer_end - $day_timer_start);
  326. $logger->info(".. $day_timer_diff seconds to process $date_processing");
  327.  
  328. $start_dt->add(new DateTime::Duration('days' => 1));
  329. }
  330. my $total_time_end = gettimeofday();
  331. my $total_time_diff = floor($total_time_end - $total_time_start);
  332. $logger->info("Done. $total_time_diff seconds in total");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement