LJSUP-17669: Login.bml form refactoring
[livejournal.git] / cgi-bin / supportstatslib.pl
blob2bd9c7dc94b7895ae2abf28371a0d749f3b1ebbc
1 #!/usr/bin/perl
3 # This library is used by Support Stats.
5 # In particular, it's used by the following pages:
6 # - htdocs/admin/support/dept.bml
7 # - htdocs/admin/support/individual.bml
9 # This library doesn't have any DB access routines.
10 # All DB access routines are in supportlib.pl
13 use strict;
14 package LJ::Support::Stats;
15 use vars qw($ALL_CATEGORIES_ID);
17 use Carp qw(croak);
18 use DateTime;
19 use LJ::TimeUtil;
21 # Constants
22 $ALL_CATEGORIES_ID = -1;
24 # <LJFUNC>
25 # name: LJ::Support::Stats::filter_support_by_category
26 # des: Filter Support by Category ID.
27 # args: support
28 # des-support: HashRef of Support Rows indexed by Support ID.
29 # info: Used by dept.bml and individual.bml under
30 # htdocs/admin/support/.
31 # All DB access routines are in supportlib.pl.
32 # Return: Filtered HashRef of Support Rows.
33 # </LJFUNC>
34 sub filter_support_by_category {
35 my($support_hashref, $category_id_parm) = @_;
37 return $support_hashref if $category_id_parm == $ALL_CATEGORIES_ID;
39 my %filtered_support = ();
40 while (my($support_id, $support) = each %{$support_hashref}) {
41 $filtered_support{$support_id} = $support
42 if $support->{spcatid} == $category_id_parm;
45 return \%filtered_support;
48 # <LJFUNC>
49 # name: LJ::Support::Stats::date_formatter
50 # des: Format a date
51 # args: year, month, day
52 # des-year: Four digit year (e.g. 2001)
53 # des-month: One-based numeric month: 1-12
54 # des-day: One-based numeric day: 1-31
55 # info: Used by dept.bml and individual.bml under
56 # htdocs/admin/support/.
57 # All DB access routines are in supportlib.pl.
58 # Return: Date formatted as follows: YYYY-MM-DD
59 # </LJFUNC>
60 sub date_formatter {
61 croak('Not enough parameters') if @_ < 3;
62 my($year, $month, $day) = @_;
63 my $date = sprintf("%04d-%02d-%02d", $year, $month, $day);
64 return $date;
67 # <LJFUNC>
68 # name: LJ::Support::Stats::comma_formatter
69 # des: Format a number with commas
70 # args: number
71 # des-number: number to commafy.
72 # info: Used by dept.bml and individual.bml under
73 # htdocs/admin/support/.
74 # All DB access routines are in supportlib.pl.
75 # Return: Number with commas inserted.
76 # </LJFUNC>
77 sub comma_formatter {
78 my $number = shift or croak('No parameter for comma_formatter');
79 1 while ($number =~ s/([-+]?\d+)(\d\d\d\.?)(?!\d)/$1,$2/);
80 return $number;
84 # <LJFUNC>
85 # name: LJ::Support::Stats::percent_formatter
86 # des: Format a percentage: Take integer portion and append percent sign.
87 # args: percent
88 # des-percent: Number to format as a percentage.
89 # info: Used by dept.bml and individual.bml under
90 # htdocs/admin/support/.
91 # All DB access routines are in supportlib.pl.
92 # Return: Formatted percentage.
93 # </LJFUNC>
94 sub percent_formatter {
95 my $percent = shift;
96 $percent = int($percent) . '%';
97 return $percent;
100 # <LJFUNC>
101 # name: LJ::Support::Stats::get_grains_from_seconds
102 # des: Determine the grains (day/week/month/year) of given a date
103 # args: seconds
104 # des-seconds: Seconds since Unix epoch.
105 # info: Used by dept.bml and individual.bml under
106 # htdocs/admin/support/.
107 # All DB access routines are in supportlib.pl.
108 # Return: HashRef of Grains.
109 # </LJFUNC>
110 sub get_grains_from_seconds {
111 my $seconds_since_epoch = shift or croak('No parameter specified');
113 my $date = LJ::TimeUtil->mysql_time($seconds_since_epoch);
115 my %grain;
116 $grain{grand} = 'Grand';
117 $grain{day} = substr($date, 0, 10);
118 $grain{month} = substr($date, 0, 7);
119 $grain{year} = substr($date, 0, 4);
121 # Get week of Support Ticket
122 my $dt = DateTime->from_epoch( epoch => $seconds_since_epoch );
123 my($week_year, $week_number) = $dt->week;
124 $grain{week} = $week_year . ' - Week #' . sprintf('%02d', $week_number);
126 return \%grain;