forked from Normation/rudder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleCompliance.scala
317 lines (293 loc) · 12.8 KB
/
RuleCompliance.scala
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
*************************************************************************************
* Copyright 2014 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************************
*/
package com.normation.rudder.web.components
import com.normation.rudder.domain.policies._
import com.normation.rudder.repository.FullActiveTechniqueCategory
import com.normation.inventory.domain.NodeId
import com.normation.rudder.domain.nodes.NodeInfo
import scala.xml._
import net.liftweb.http._
import net.liftweb.common._
import com.normation.rudder.domain.reports._
import net.liftweb.util.Helpers._
import net.liftweb.util.Helpers
import net.liftweb.http.js.JsCmds._
import net.liftweb.http.js.JE._
import net.liftweb.http.js.JsCmd
import bootstrap.liftweb.RudderConfig
import com.normation.rudder.web.model.WBTextField
import com.normation.rudder.web.model.WBTextAreaField
import com.normation.rudder.web.model.WBSelectField
import com.normation.rudder.web.services.ComplianceData
import com.normation.rudder.rule.category.RuleCategory
import org.joda.time.DateTime
import org.joda.time.format.PeriodFormatterBuilder
import org.joda.time.Interval
import com.normation.rudder.web.services.ReportLine
import com.normation.rudder.web.services.ChangeLine
import com.normation.rudder.services.reports.NodeChanges
import net.liftweb.http.js.JsExp
import net.liftweb.http.js.JsObj
object RuleCompliance {
private def details =
(for {
xml <- Templates("templates-hidden" :: "components" :: "ComponentRuleEditForm" :: Nil)
} yield {
chooseTemplate("component", "details", xml)
}) openOr Nil
}
/**
* This component display the compliance of a Rule by showing compliance of every Directive
* It generates all Data and put them in a DataTable
*/
class RuleCompliance (
rule : Rule
, rootRuleCategory: RuleCategory
) extends Loggable {
private[this] val reportingService = RudderConfig.reportingService
private[this] val recentChangesService = RudderConfig.recentChangesService
private[this] val categoryService = RudderConfig.ruleCategoryService
private[this] val configService = RudderConfig.configService
//fresh value when refresh
private[this] val roRuleRepository = RudderConfig.roRuleRepository
private[this] val getFullDirectiveLib = RudderConfig.roDirectiveRepository.getFullDirectiveLibrary _
private[this] val getAllNodeInfos = RudderConfig.nodeInfoService.getAll _
import RuleCompliance._
def display : NodeSeq = {
(
"#ruleName" #> rule.name &
"#ruleCategory" #> categoryService.shortFqdn(rootRuleCategory, rule.categoryId) &
"#rudderID" #> rule.id.value &
"#ruleShortDescription" #> rule.shortDescription &
"#ruleLongDescription" #> rule.longDescription &
"#compliancedetails" #> showCompliance
)(details)
}
/*
* For each table : the subtable is contained in td : details
* when + is clicked: it gets the content of td details then process it
* as a datatable
*
* The table are empty when the page is displayed, then there is a JS call
* to refresh() that fill them.
*/
def showCompliance : NodeSeq = {
<div id="directiveComplianceSection" class="unfoldedSection" onclick="$('#directiveCompliance').toggle(400); $('#directiveComplianceSection').toggleClass('foldedSection');$('#directiveComplianceSection').toggleClass('unfoldedSection');">
<div class="section-title">Compliance by Directive</div>
</div>
<div id="directiveCompliance">
<table id="reportsGrid" cellspacing="0"> </table>
</div>
<div id="nodeComplianceSection" class="unfoldedSection" onclick="$('#nodeCompliance').toggle(400); $('#nodeComplianceSection').toggleClass('foldedSection');$('#nodeComplianceSection').toggleClass('unfoldedSection');">
<div class="section-title">Compliance by Node</div>
</div>
<div id="nodeCompliance">
<table id="nodeReportsGrid" cellspacing="0"> </table>
</div>
<div id="recentChangesSection" class="unfoldedSection" onclick="$('#recentChanges').toggle(400); $('#recentChangesSection').toggleClass('foldedSection');$('#recentChangesSection').toggleClass('unfoldedSection');">
<div class="section-title">Recent changes</div>
</div>
<div id="recentChanges" class="tw-bs">
<div class="alert alert-info " style="font-size: 14px">
<div style="width:90%;display:inline-block;">
<span class="glyphicon glyphicon-info-sign"></span>
Details of changes for each period are displayed below the graph. Click to change the selected period.
</div>
<div class="recentChange_refresh">
{SHtml.ajaxButton(<img src='/images/icRefresh.png'/>, () => refresh() , ("class","recentChangeGraph refreshButton") , ("title","Refresh"))}
</div>
</div>
<div id="changesChart"> </div>
</div>
<hr class="spacer" />
<span >Changes during period <b id="selectedPeriod"> --- </b> (selected in graph above)</span>
<table id="changesGrid" cellspacing="0"> </table> ++
Script(After(0,JsRaw(s"""
function refresh() {${refresh().toJsCmd}};
createDirectiveTable(true, false, "${S.contextPath}")("reportsGrid",[],refresh);
createNodeComplianceTable("nodeReportsGrid",[],"${S.contextPath}", refresh);
createChangesTable("changesGrid",[],"${S.contextPath}", refresh);
correctButtons();
refresh();
""")))
}
def refresh() = {
//we want to be able to see at least one if the other fails
SHtml.ajaxInvoke(() => refreshCompliance()) &
SHtml.ajaxInvoke(() => refreshGraphChanges()) &
SHtml.ajaxInvoke(() => refreshTableChanges(None))
}
def refreshGraphChanges() : JsCmd = {
try {
( for {
changesOnRule <- recentChangesService.countChangesByRuleByInterval().map( _.getOrElse(rule.id, Map()))
} yield {
JsRaw(s"""
var recentChanges = ${NodeChanges.json(changesOnRule, recentChangesService.getCurrentValidIntervals(None)).toJsCmd};
var data = recentChanges.y
data.splice(0,0,'Recent changes')
var x = recentChanges.x
x.splice(0,0,'x')
var selectedIndex = x.length-2;
//recentChart variable has to be global because we need it to refresh the graph clicking on compliance tab.
recentChart = c3.generate({
data: {
x: 'x'
, columns: [ x , data ]
, type: 'bar'
, onclick: function (d, element) {
selectedIndex = d.index;
${SHtml.ajaxCall(JsRaw("recentChanges.t[selectedIndex]"), s => refreshTableChanges(Some(s.toLong)))}
selectInterval(x[selectedIndex+1],element);
}
, onmouseover : function (element) {
changeCursor(element.value);
}
, onmouseout : function (element) {
changeCursor(element.value);
}
}
, legend : {
show : false
}
, bindto : '#changesChart'
, bar: {
width: {
ratio: 1 // this makes bar width 50% of length between ticks
}
}
, axis: {
x: {
type: 'categories'
}
}
, grid: {
x: { show: true }
, y: { show: true }
}
, onrendered: function () {
var element = document.getElementsByClassName('c3-bar-'+(selectedIndex).toString())[0];
selectInterval(x[selectedIndex+1],element);
}
} );
createTooltip();
""")
}) match {
case Full(cmd) => cmd
case eb:EmptyBox =>
val fail = eb ?~! "Could not refresh recent changes"
logger.error(fail.messageChain)
Noop
}
} catch {
case oom: OutOfMemoryError =>
val msg = "NodeChanges can not be retrieved du to OutOfMemory error. That mean that either your installation is missing " +
"RAM (see: http://www.rudder-project.org/doc-3.2/_performance_tuning.html#_java_out_of_memory_error) or that the number of recent changes is " +
"overwhelming, and you hit: http://www.rudder-project.org/redmine/issues/7735. Look here for workaround"
logger.error(msg)
Noop
}
}
/*
* Refresh the tables with details on events.
* The argument is the starting timestamp of the interval
* to check. If None is provided, the last current interval
* is used.
* We set a hard limit of 10 000 events by interval of 6 hours.
*/
def refreshTableChanges(intervalStartTimestamp: Option[Long]) : JsCmd = {
val intervals = recentChangesService.getCurrentValidIntervals(None).sortBy(_.getStartMillis)
val failure = Failure("No interval defined. It's likelly a bug, please contact report it to rudder-project.org/redmine")
val int = intervalStartTimestamp.orElse(intervals.lastOption.map(_.getStartMillis)) match {
case Some(t) => intervals.find { i => t == i.getStartMillis } match {
case Some(i) => Full(i)
case None => failure
}
case None => failure
}
try {
( for {
currentInterval <- int
changesOnRule <- recentChangesService.getChangesForInterval(rule.id, currentInterval, Some(10000))
directiveLib <- getFullDirectiveLib()
allNodeInfos <- getAllNodeInfos()
} yield {
val changesLine = ChangeLine.jsonByInterval(Map((currentInterval, changesOnRule)), Some(rule.name), directiveLib, allNodeInfos)
val changesArray = changesLine.in.toList.flatMap{case a:JsArray => a.in.toList; case _ => Nil}
JsRaw(s"""
refreshTable("changesGrid", ${JsArray(changesArray).toJsCmd});
""")
}) match {
case Full(cmd) => cmd
case eb:EmptyBox =>
val fail = eb ?~! "Could not refresh recent changes"
logger.error(fail.messageChain)
Noop
}
} catch {
case oom: OutOfMemoryError =>
val msg = "NodeChanges can not be retrieved du to OutOfMemory error. That mean that either your installation is missing " +
"RAM (see: http://www.rudder-project.org/doc-3.2/_performance_tuning.html#_java_out_of_memory_error) or that the number of recent changes is " +
"overwhelming, and you hit: http://www.rudder-project.org/redmine/issues/7735. Look here for workaround"
logger.error(msg)
Noop
}
}
def refreshCompliance() : JsCmd = {
( for {
reports <- reportingService.findDirectiveRuleStatusReportsByRule(rule.id)
updatedRule <- roRuleRepository.get(rule.id)
directiveLib <- getFullDirectiveLib()
allNodeInfos <- getAllNodeInfos()
globalMode <- configService.rudder_global_policy_mode()
} yield {
val directiveData = ComplianceData.getRuleByDirectivesComplianceDetails(reports, updatedRule, allNodeInfos, directiveLib, globalMode).json.toJsCmd
val nodeData = ComplianceData.getRuleByNodeComplianceDetails(directiveLib, reports, allNodeInfos, globalMode).json.toJsCmd
JsRaw(s"""
refreshTable("reportsGrid", ${directiveData});
refreshTable("nodeReportsGrid", ${nodeData});
createTooltip();
""")
}
) match {
case Full(cmd) => cmd
case eb : EmptyBox =>
val fail = eb ?~! s"Error while computing Rule ${rule.name} (${rule.id.value})"
logger.error(fail.messageChain)
Noop
}
}
}