File: SystemStatsGeneric.pm

package info (click to toggle)
otrs2 2.2.7-2lenny3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 13,444 kB
  • ctags: 5,808
  • sloc: perl: 129,825; xml: 16,139; sql: 11,400; sh: 1,198; makefile: 31; php: 16
file content (173 lines) | stat: -rw-r--r-- 5,612 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# --
# Kernel/Modules/SystemStatsGeneric.pm - generic pure SQL stats module
# Copyright (C) 2001-2007 OTRS GmbH, http://otrs.org/
# --
# $Id: SystemStatsGeneric.pm,v 1.10 2007/02/07 11:37:26 tr Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --
#
# Description: copy this file into Kernel/Modules/ and change the
# config options ($Title, $DetailText, $SQL, ...) below.
#
# Add a html link to your OTRS agent frontend, e. g.
# <a href="$Env{"Baselink"}Action=SystemStatsGeneric">Your Stats</a>
# and you will get your printable pure SQL stats.
#
# If you want a CSV file add the param CSV=1 to your html link e. g.
# <a href="$Env{"Baselink"}Action=SystemStatsGeneric&CSV=1">Your CVS Stats File</a>
#
# --

package Kernel::Modules::SystemStatsGeneric;

use strict;

use vars qw($VERSION);
$VERSION = '$Revision: 1.10 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;

sub new {
    my $Type = shift;
    my %Param = @_;
    # allocate new hash for object
    my $Self = {};
    bless ($Self, $Type);
    # get common opjects
    foreach (keys %Param) {
        $Self->{$_} = $Param{$_};
    }
    # check all needed objects
    foreach (qw(ParamObject DBObject QueueObject LayoutObject ConfigObject LogObject TimeObject)) {
        die "Got no $_" if (!$Self->{$_});
    }
    $Self->{CSV} = $Self->{ParamObject}->GetParam(Param => 'CSV') || 0;
    return $Self;
}

sub Run {
    my $Self = shift;
    my %Param = @_;
    # ------------------------------------------------------------------------------ #
    # Config options!
    # ------------------------------------------------------------------------------ #
    my $Title = 'Name of Stats';
    my $DetailText = 'Some text about the content! $Data{"Records"} database records - ($Text{"Stand"}: $Env{"Time"})';
    my $CSVFile = 'csv-file';
    my $SQLLimit = 5000;
    my $SQL = qq|SELECT * FROM system_user|;
    my $Group = 'stats';
    # ------------------------------------------------------------------------------ #

    # permission check (user need to be rw in group stats)
    if (!$Self->{"UserIsGroup[$Group]"} || $Self->{"UserIsGroup[$Group]"} ne 'Yes') {
        return $Self->{LayoutObject}->NoPermission(
            Message => "You have to be in the $Group group!"
        );
    }

    # starting with page ...
    my $CSVBody = '';
    my $OutputBody = '';
    my $Records = 0;
    my @HeadData = ();
    my @GlobalData = ();

    # get table columns names
    my $sth = $Self->{DBObject}->{dbh}->prepare($SQL);
    $sth->execute;
    my $names = $sth->{NAME};
    @HeadData = @{$names};

    # get table columns data
    $Self->{DBObject}->Prepare(SQL => $SQL, Limit => $SQLLimit);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        push (@GlobalData, \@Row);
    }

    # fillup colomn names
    if (!$Self->{CSV}) {
        $OutputBody .= '<table border="0" width="100%" cellspacing="0" cellpadding="3">';
        $OutputBody .= "<tr>\n";
    }
    foreach my $col (@HeadData) {
        if ($Self->{CSV}) {
            $CSVBody .= "$col;";
        }
        else {
            $OutputBody .= "<th class=\"item\">$col</th>\n";
        }
    }
    if ($Self->{CSV}) {
        $CSVBody .= "\n";
    }
    else {
        $OutputBody .= "</tr>\n";
    }

    # fillup columns data
    foreach my $RowTmp (@GlobalData) {
        $Records++;
        my @Row = @{$RowTmp};
        if (!$Self->{CSV}) {
            $OutputBody .= "<tr>\n";
        }
        foreach (@Row) {
            if ($Self->{CSV}) {
                $CSVBody .= "$_;";
            }
            else {
                $OutputBody .= "<td class=\"small\">$_</td>\n";
            }
        }
        if ($Self->{CSV}) {
            $CSVBody .= "\n";
        }
        else {
            $OutputBody .= "</tr>\n";
        }
    }

    # return HTML or CSV page
    if ($Self->{CSV}) {
        my $CSV = $Self->{LayoutObject}->Output(
            Data => { Title => $Title, DetailText => $DetailText, Records => $Records},
            Template => '$Data{"Title"};$Text{"generated by"} $Env{"UserFirstname"} $Env{"UserLastname"} ($Env{"UserEmail"}) - $Env{"Time"}').";\n";
        $CSV .= $Self->{LayoutObject}->Output(
            Data => { Title => $Title, DetailText => $DetailText, Records => $Records},
            Template => $DetailText,
        ).";\n";
        # return csv to download
        my ($s,$m,$h, $D,$M,$Y) = $Self->{TimeObject}->SystemTime2Date(
            SystemTime => $Self->{TimeObject}->SystemTime(),
        );
        return $Self->{LayoutObject}->Attachment(
            Filename => "$CSVFile"."_"."$Y-$M-$D"."_"."$h-$m.csv",
            ContentType => "text/csv",
            Content => "\n".$CSV.$CSVBody,
        );
    }
    else {
        my $Output = $Self->{LayoutObject}->PrintHeader(Title => $Title, Width => 900);
        $Output .= $Self->{LayoutObject}->Output(
            Data => { Title => $Title, DetailText => $DetailText, Records => $Records},
            Template => '
        <table border="0" cellspacing="1" cellpadding="0" width="100%">
            <tr>
                <td><font size="+1"><b>$Data{"Title"}</font></b></td>
            </tr>
        </table>
        <br>
        <p>
            $Data{"DetailText"}
        </p>
        ');
        # return html
        $Output .= $OutputBody.$Self->{LayoutObject}->Output(Template => '</table><br>');
        $Output .= $Self->{LayoutObject}->PrintFooter();
        return $Output;
    }
}
1;