1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9package Kernel::Output::HTML::Dashboard::Stats;
10
11use strict;
12use warnings;
13
14use Kernel::System::VariableCheck qw(:all);
15
16use Kernel::System::Stats;
17use Kernel::Output::HTML::Statistics::View;
18
19our $ObjectManagerDisabled = 1;
20
21sub new {
22    my ( $Type, %Param ) = @_;
23
24    # allocate new hash for object
25    my $Self = {%Param};
26    bless( $Self, $Type );
27
28    # get needed parameters
29    for my $Needed (qw(Config Name UserID)) {
30        die "Got no $Needed!" if ( !$Self->{$Needed} );
31    }
32
33    # Settings
34    $Self->{PrefKeyStatsConfiguration} = 'UserDashboardStatsStatsConfiguration' . $Self->{Name};
35
36    return $Self;
37}
38
39sub Preferences {
40    my ( $Self, %Param ) = @_;
41
42    # get StatID
43    my $StatID = $Self->{Config}->{StatID};
44
45    my $Stat = $Kernel::OM->Get('Kernel::System::Stats')->StatsGet( StatID => $StatID );
46
47    # get the object name
48    if ( $Stat->{StatType} eq 'static' ) {
49        $Stat->{ObjectName} = $Stat->{File};
50    }
51
52    # if no object name is defined use an empty string
53    $Stat->{ObjectName} ||= '';
54
55    my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
56    my $UserObject   = $Kernel::OM->Get('Kernel::System::User');
57
58    # check if the user has preferences for this widget
59    my %Preferences = $UserObject->GetPreferences(
60        UserID => $Self->{UserID},
61    );
62    my $StatsSettings;
63    if ( $Preferences{ $Self->{PrefKeyStatsConfiguration} } ) {
64        $StatsSettings = $Kernel::OM->Get('Kernel::System::JSON')->Decode(
65            Data => $Preferences{ $Self->{PrefKeyStatsConfiguration} },
66        );
67    }
68
69    my %Format = %{ $Kernel::OM->Get('Kernel::Config')->Get('Stats::Format') || {} };
70
71    my %FilteredFormats;
72    for my $Key ( sort keys %Format ) {
73        $FilteredFormats{$Key} = $Format{$Key} if $Key =~ m{^D3}smx;
74    }
75
76    my @Errors;
77
78    my %GetParam = eval {
79        $Kernel::OM->Get('Kernel::Output::HTML::Statistics::View')->StatsParamsGet(
80            Stat         => $Stat,
81            UserGetParam => $StatsSettings,
82            UserID       => $Self->{UserID},
83        );
84    };
85
86    if ( $@ || ref $@ eq 'ARRAY' ) {
87        @Errors = @{$@};
88    }
89
90    # Fetch the stat again as StatsParamGet might have modified it in between.
91    $Stat = $Kernel::OM->Get('Kernel::System::Stats')->StatsGet(
92        StatID => $StatID,
93        UserID => $Self->{UserID},
94    );
95    my $StatsParamsWidget = $Kernel::OM->Get('Kernel::Output::HTML::Statistics::View')->StatsParamsWidget(
96        Stat         => $Stat,
97        UserGetParam => $StatsSettings,
98        Formats      => \%FilteredFormats,
99        UserID       => $Self->{UserID},
100    );
101
102    # This indicates that there are configuration errors in the statistic.
103    # In that case we show a warning in Run() and no configuration here.
104    if ( !$StatsParamsWidget ) {
105        return;
106    }
107
108    my $SettingsHTML = $LayoutObject->Output(
109        TemplateFile => 'AgentDashboardStatsSettings',
110        Data         => {
111            %{$Stat},
112            Errors            => \@Errors,
113            JSONFieldName     => $Self->{PrefKeyStatsConfiguration},
114            NamePref          => $Self->{Name},
115            StatsParamsWidget => $StatsParamsWidget,
116        },
117    );
118
119    my @Params = (
120        {
121            Desc  => 'Stats Configuration',
122            Name  => $Self->{PrefKeyStatsConfiguration},
123            Block => 'RawHTML',
124            HTML  => $SettingsHTML,
125        },
126    );
127
128    return @Params;
129}
130
131sub Config {
132    my ( $Self, %Param ) = @_;
133
134    return (
135        %{ $Self->{Config} }
136    );
137}
138
139sub Run {
140    my ( $Self, %Param ) = @_;
141
142    my $StatID = $Self->{Config}->{StatID};
143
144    my %Preferences = $Kernel::OM->Get('Kernel::System::User')->GetPreferences(
145        UserID => $Self->{UserID},
146    );
147    my $StatsSettings = {};
148
149    # get JSON object
150    my $JSONObject = $Kernel::OM->Get('Kernel::System::JSON');
151
152    if ( $Preferences{ $Self->{PrefKeyStatsConfiguration} } ) {
153        $StatsSettings = $JSONObject->Decode(
154            Data => $Preferences{ $Self->{PrefKeyStatsConfiguration} },
155        );
156    }
157
158    my $Stat = $Kernel::OM->Get('Kernel::System::Stats')->StatsGet( StatID => $StatID );
159
160    my $StatConfigurationValid = $Kernel::OM->Get('Kernel::Output::HTML::Statistics::View')->StatsConfigurationValidate(
161        Stat   => $Stat,
162        Errors => {},
163        UserID => $Self->{UserID},
164    );
165
166    my $StatParametersValid;
167    eval {
168        $Kernel::OM->Get('Kernel::Output::HTML::Statistics::View')->StatsParamsGet(
169            Stat         => $Stat,
170            UserGetParam => $StatsSettings,
171            UserID       => $Self->{UserID},
172        );
173        $StatParametersValid = 1;
174    };
175
176    my $CachedData;
177
178    if ( $StatConfigurationValid && $StatParametersValid ) {
179        $CachedData = $Kernel::OM->Get('Kernel::System::Stats')->StatsResultCacheGet(
180            StatID       => $StatID,
181            UserGetParam => $StatsSettings,
182            UserID       => $Self->{UserID},
183        );
184    }
185
186    my $Format = $StatsSettings->{Format};
187    if ( !$Format ) {
188        my $Stat = $Kernel::OM->Get('Kernel::System::Stats')->StatsGet(
189            StatID => $StatID,
190            UserID => $Self->{UserID},
191        );
192        STATFORMAT:
193        for my $StatFormat ( @{ $Stat->{Format} || [] } ) {
194            if ( $StatFormat =~ m{^D3}smx ) {
195                $Format = $StatFormat;
196                last STATFORMAT;
197            }
198        }
199    }
200
201    my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
202
203    # check permission for AgentStatistics
204    my $StatsReg = $Kernel::OM->Get('Kernel::Config')->Get('Frontend::Module')->{'AgentStatistics'};
205    my $AgentStatisticsFrontendPermission = 0;
206    if ( !$StatsReg->{GroupRo} && !$StatsReg->{Group} ) {
207        $AgentStatisticsFrontendPermission = 1;
208    }
209    else {
210        my $GroupObject = $Kernel::OM->Get('Kernel::System::Group');
211        TYPE:
212        for my $Type (qw(GroupRo Group)) {
213            my $StatsGroups = ref $StatsReg->{$Type} eq 'ARRAY' ? $StatsReg->{$Type} : [ $StatsReg->{$Type} ];
214            GROUP:
215            for my $StatsGroup ( @{$StatsGroups} ) {
216                next GROUP if !$StatsGroup;
217                next GROUP if !$GroupObject->PermissionCheck(
218                    UserID    => $Self->{UserID},
219                    GroupName => $StatsGroup,
220                    Type      => 'ro',
221                );
222
223                $AgentStatisticsFrontendPermission = 1;
224                last TYPE;
225            }
226        }
227    }
228
229    my $StatsResultDataJSON = $LayoutObject->JSONEncode(
230        Data     => $CachedData,
231        NoQuotes => 1,
232    );
233
234    my $StatsFormatJSON = $LayoutObject->JSONEncode(
235        Data     => $Format,
236        NoQuotes => 1,
237    );
238
239    # send data to JS
240    $LayoutObject->AddJSData(
241        Key   => 'StatsData' . $StatID,
242        Value => {
243            Name               => $Self->{Name},
244            Format             => $StatsFormatJSON,
245            StatResultData     => $StatsResultDataJSON,
246            Preferences        => $Preferences{ 'GraphWidget' . $Self->{Name} } || '{}',
247            MaxXaxisAttributes => $Kernel::OM->Get('Kernel::Config')->Get('Stats::MaxXaxisAttributes'),
248        },
249    );
250
251    if ( $Self->{UserRefreshTime} ) {
252        my $Refresh = 60 * $Self->{UserRefreshTime};
253
254        $LayoutObject->AddJSData(
255            Key   => 'WidgetRefreshStat' . $StatID,
256            Value => {
257                Name        => $Self->{Name},
258                NameHTML    => $Self->{Name},
259                RefreshTime => $Refresh,
260            },
261        );
262    }
263
264    my $Content = $LayoutObject->Output(
265        TemplateFile => 'AgentDashboardStats',
266        Data         => {
267            Name                              => $Self->{Name},
268            StatConfigurationValid            => $StatConfigurationValid,
269            StatParametersValid               => $StatParametersValid,
270            StatResultData                    => $CachedData,
271            Stat                              => $Stat,
272            Format                            => $Format,
273            AgentStatisticsFrontendPermission => $AgentStatisticsFrontendPermission,
274            Preferences                       => $Preferences{ 'GraphWidget' . $Self->{Name} } || '{}',
275        },
276        AJAX => $Param{AJAX},
277    );
278
279    return $Content;
280}
281
2821;
283