Skip to content

Commit

Permalink
#67 Refactor the UI code to be compliant to the backend APIs after da…
Browse files Browse the repository at this point in the history
…ta Migration version 8
  • Loading branch information
nadouani committed Dec 30, 2016
1 parent 594d7a0 commit 884e456
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 61 deletions.
4 changes: 2 additions & 2 deletions thehive-backend/app/models/ReportTemplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import org.elastic4play.models.{ AttributeDef, AttributeFormat ⇒ F, EntityDef,

trait ReportTemplateAttributes { _: AttributeDef
val content = attribute("content", F.textFmt, "Content of the template")
val reportType = attribute("reportType", F.stringFmt, "Flavor of the report (short or long)")
val reportType = attribute("reportType", F.stringFmt, "Type of the report (short or long)")
val analyzerId = multiAttribute("analyzerId", F.stringFmt, "Id of analyzers")
}

@Singleton
class ReportTemplateModel @Inject() extends ModelDef[ReportTemplateModel, ReportTemplate]("reportTemplate") with ReportTemplateAttributes
class ReportTemplate(model: ReportTemplateModel, attributes: JsObject) extends EntityDef[ReportTemplateModel, ReportTemplate](model, attributes) with ReportTemplateAttributes
class ReportTemplate(model: ReportTemplateModel, attributes: JsObject) extends EntityDef[ReportTemplateModel, ReportTemplate](model, attributes) with ReportTemplateAttributes
12 changes: 6 additions & 6 deletions thehive-cortex/app/connectors/cortex/models/ReportTemplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ object ReportType extends Enumeration with HiveEnumeration {
trait ReportTemplateAttributes { _: AttributeDef
val reportTemplateId = attribute("_id", F.stringFmt, "Report template id", O.model)
val content = attribute("content", F.textFmt, "Content of the template")
val flavor = attribute("flavor", F.enumFmt(ReportType), "Flavor of the report (short or long)")
val analyzers = attribute("analyzers", F.stringFmt, "Id of analyzers")
val reportType = attribute("reportType", F.enumFmt(ReportType), "Type of the report (short or long)")
val analyzerId = attribute("analyzerId", F.stringFmt, "Id of analyzers")
}

@Singleton
class ReportTemplateModel @Inject() extends ModelDef[ReportTemplateModel, ReportTemplate]("reportTemplate") with ReportTemplateAttributes {
override def creationHook(parent: Option[BaseEntity], attrs: JsObject) = {
val maybeId = for {
analyzers (attrs \ "analyzers").asOpt[String]
flavor (attrs \ "flavor").asOpt[String]
} yield analyzers + "_" + flavor
analyzerId (attrs \ "analyzerId").asOpt[String]
reportType (attrs \ "reportType").asOpt[String]
} yield analyzerId + "_" + reportType
Future.successful(maybeId.fold(attrs)(id attrs + ("_id" JsString(id))))
}
}
class ReportTemplate(model: ReportTemplateModel, attributes: JsObject) extends EntityDef[ReportTemplateModel, ReportTemplate](model, attributes) with ReportTemplateAttributes
class ReportTemplate(model: ReportTemplateModel, attributes: JsObject) extends EntityDef[ReportTemplateModel, ReportTemplate](model, attributes) with ReportTemplateAttributes
4 changes: 2 additions & 2 deletions thehive-cortex/app/controllers/CortextCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CortextCtrl @Inject() (
case GET(p"/report/template/$caseTemplateId<[^/]*>") reportTemplateCtrl.get(caseTemplateId)
case PATCH(p"/report/template/$caseTemplateId<[^/]*>") reportTemplateCtrl.update(caseTemplateId)
case DELETE(p"/report/template/$caseTemplateId<[^/]*>") reportTemplateCtrl.delete(caseTemplateId)
case GET(p"/report/template/content/$analyzerId<[^/]*>/$flavor<[^/]*>") reportTemplateCtrl.getContent(analyzerId, flavor)
case GET(p"/report/template/content/$analyzerId<[^/]*>/$reportType<[^/]*>") reportTemplateCtrl.getContent(analyzerId, reportType)
case POST(p"/report/template/_import") reportTemplateCtrl.importTemplatePackage
case r throw NotFoundError(s"${r.uri} not found")
}
Expand Down Expand Up @@ -97,4 +97,4 @@ class CortextCtrl @Inject() (
renderer.toOutput(OK, analyzers)
}
}
}
}
22 changes: 11 additions & 11 deletions thehive-cortex/app/controllers/ReportTemplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ class ReportTemplateCtrl @Inject() (
}

@Timed
def getContent(analyzerId: String, flavor: String) = authenticated(Role.read).async { implicit request
def getContent(analyzerId: String, reportType: String) = authenticated(Role.read).async { implicit request
import QueryDSL._
val (reportTemplates, total) = reportTemplateSrv.find(and("analyzers" ~= analyzerId, "flavor" ~= flavor), Some("0-1"), Nil)
val (reportTemplates, total) = reportTemplateSrv.find(and("analyzerId" ~= analyzerId, "reportType" ~= reportType), Some("0-1"), Nil)
total.foreach { t
if (t > 1) logger.warn(s"Multiple report templates match for analyzer $analyzerId with flavor $flavor")
if (t > 1) logger.warn(s"Multiple report templates match for analyzer $analyzerId with type $reportType")
}
reportTemplates
.runWith(Sink.headOption)
Expand Down Expand Up @@ -98,28 +98,28 @@ class ReportTemplateCtrl @Inject() (
}
val importedReportTemplates: Seq[Future[(String, JsBoolean)]] = zipFile.getFileHeaders.toSeq.filter(_ != null).collect {
case fileHeader: FileHeader if !fileHeader.isDirectory
val Array(analyzerId, flavorHtml, _*) = (fileHeader.getFileName + "/").split("/", 3)
val Array(analyzerId, reportTypeHtml, _*) = (fileHeader.getFileName + "/").split("/", 3)
val inputStream = zipFile.getInputStream(fileHeader)
val content = Source.fromInputStream(inputStream).mkString
inputStream.close()

val flavor = if (flavorHtml.endsWith(".html")) flavorHtml.dropRight(5) else flavorHtml
val reportType = if (reportTypeHtml.endsWith(".html")) reportTypeHtml.dropRight(5) else reportTypeHtml

val reportTemplateFields = Fields.empty
.set("flavor", flavor)
.set("analyzers", analyzerId)
.set("reportType", reportType)
.set("analyzerId", analyzerId)
.set("content", content)
reportTemplateSrv.create(reportTemplateFields)
.recoverWith { // if creation fails, try to update
case NonFatal(_)
val reportTemplateId = analyzerId + "_" + flavor
val reportTemplateId = analyzerId + "_" + reportType
reportTemplateSrv.update(reportTemplateId, Fields.empty.set("content", content))
}
.map(_.id JsBoolean(true))
.recoverWith {
case NonFatal(e)
logger.error(s"The import of the report template $analyzerId ($flavor) has failed", e)
val reportTemplateId = analyzerId + "_" + flavor
logger.error(s"The import of the report template $analyzerId ($reportType) has failed", e)
val reportTemplateId = analyzerId + "_" + reportType
Future.successful(reportTemplateId JsBoolean(false))
}
}
Expand All @@ -128,4 +128,4 @@ class ReportTemplateCtrl @Inject() (
renderer.toOutput(OK, JsObject(result))
}
}
}
}
22 changes: 11 additions & 11 deletions ui/app/scripts/controllers/admin/AdminReportTemplatesCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
]).then(function (response) {
self.templates = response[0].data;
self.analyzers = response[1];

return $q.resolve(self.analyzers);
}).then(function (analyzersMap) {
if(_.isEmpty(analyzersMap)) {
Expand All @@ -35,8 +35,8 @@
}

_.each(self.templates, function (tpl) {
analyzersMap[tpl.analyzers][tpl.flavor + 'Report'] = tpl;
});
analyzersMap[tpl.analyzerId][tpl.reportType + 'Report'] = tpl;
});

self.analyzerCount = _.keys(analyzersMap).length;
});
Expand Down Expand Up @@ -65,7 +65,7 @@
};

this.deleteTemplate = function(template) {
var modalInstance = $modal.open({
var modalInstance = $modal.open({
templateUrl: 'views/partials/admin/report-template-delete.html',
controller: 'AdminReportTemplateDeleteCtrl',
controllerAs: 'vm',
Expand All @@ -82,7 +82,7 @@
});
};

this.import = function (analyzer, dataType) {
this.import = function () {
var modalInstance = $modal.open({
animation: true,
templateUrl: 'views/partials/admin/report-template-import.html',
Expand All @@ -97,7 +97,7 @@
};

this.load();
};
}

function AdminReportTemplateDialogCtrl($modalInstance, reportTemplate, ReportTemplateSrv, AlertSrv, analyzer) {
this.reportTemplate = reportTemplate;
Expand All @@ -108,10 +108,10 @@
showGutter: true,
theme: 'default',
mode: 'xml'
};
};

this.formData = _.pick(reportTemplate, 'id', 'flavor', 'content');
this.formData.analyzers = this.analyzer.id;
this.formData = _.pick(reportTemplate, 'id', 'reportType', 'content');
this.formData.analyzerId = this.analyzer.id;

this.cancel = function () {
$modalInstance.dismiss();
Expand All @@ -124,7 +124,7 @@
}, function(response) {
AlertSrv.error('AdminReportTemplateDialogCtrl', response.data, response.status);
});
};
};
}

function AdminReportTemplateDeleteCtrl($modalInstance, ReportTemplateSrv, AlertSrv, template) {
Expand All @@ -141,7 +141,7 @@
this.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
}

function AdminReportTemplateImportCtrl($modalInstance, ReportTemplateSrv, AlertSrv) {
this.formData = {};
Expand Down
18 changes: 9 additions & 9 deletions ui/app/scripts/directives/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@
return;
}

var reportUrl = '/api/connector/cortex/report/template/content/' + scope.name + '/' + scope.flavor;
var reportUrl = '/api/connector/cortex/report/template/content/' + scope.name + '/' + scope.reportType;

// find report template
$templateRequest(reportUrl, true)
.then(function (tmpl) {
scope.element.html($compile(tmpl)(scope));
}, function (/*response*/) {
$templateRequest('/views/reports/' + scope.flavor + '.html', true)
$templateRequest('/views/reports/' + scope.reportType + '.html', true)
.then(function (tmpl) {
scope.element.html($compile(tmpl)(scope));
});
});
}
return {
restrict: 'E',
restrict: 'E',
scope: {
'name': '=',
'artifact': '=',
'flavor': '@',
'status': '=',
'content': '=',
'success': '='
name: '=',
artifact: '=',
reportType: '@',
status: '=',
content: '=',
success: '='
},
link: function (scope, element) {
scope.element = element;
Expand Down
2 changes: 1 addition & 1 deletion ui/app/views/partials/admin/report-template-delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h3 class="modal-title">Remove report template</h3>
</div>
<div class="modal-body">
<p>
Are you sure you want to delete the {{vm.template.flavor}} report template for analyzer <strong>{{vm.template.analyzers}}</strong>.
Are you sure you want to delete the {{vm.template.reportType}} report template for analyzer <strong>{{vm.template.analyzerId}}</strong>.
</p>
<p>
This action cannot be undone.
Expand Down
14 changes: 7 additions & 7 deletions ui/app/views/partials/admin/report-template-dialog.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
<div class="modal-header bg-primary">
<h3 class="modal-title">{{vm.reportTemplate.id ? 'Update' : 'Add'}} report template</h3>
</div>
<div class="modal-body">
<div class="modal-body">
<div class="form-group">
<label class="col-sm-2 control-label">
Report Type
<i class="fa fa-asterisk text-danger"></i>
</label>
<div class="col-sm-4">
<select class="form-control" ng-model="vm.formData.flavor" ng-options="t for t in vm.reportTypes" required>
<select class="form-control" ng-model="vm.formData.reportType" ng-options="t for t in vm.reportTypes" required>
<option value="">-- choose report type --</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Analyzer</label>
<div class="col-sm-10">
<p class="form-control-static">{{vm.formData.analyzers}}</p>
<p class="form-control-static">{{vm.formData.analyzerId}}</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">
Template
<i class="fa fa-asterisk text-danger"></i>
</label>
<div class="col-sm-10">
<div ui-ace="vm.editorOptions"
ng-model="vm.formData.content" style="height: 400px;"></div>
<div class="col-sm-10">
<div ui-ace="vm.editorOptions"
ng-model="vm.formData.content" style="height: 400px;"></div>
</div>
</div>

</div>
<div class="modal-footer text-left">
<button class="btn btn-default" ng-click="vm.cancel()" type="button">Cancel</button>
Expand Down
8 changes: 4 additions & 4 deletions ui/app/views/partials/admin/report-templates.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h2>Report template management</h2>
<div class="pull-left">
<!-- Single button -->
<div class="btn-group">
<button type="button" class="btn btn-primary" ng-click="vm.import()">
<button type="button" class="btn btn-primary" ng-click="vm.import()">
<span class="fa fa-upload"></span> Import templates
</button>
</div>
Expand Down Expand Up @@ -39,7 +39,7 @@ <h2>Report template management</h2>
<button type="button" class="btn btn-danger" ng-click="vm.deleteTemplate(analyzer.shortReport)"><i class="fa fa-times"></i></button>
</div>
<div class="btn-group btn-group-sm" ng-if="!analyzer.shortReport">
<button type="button" class="btn btn-dark" ng-click="vm.showTemplate({flavor: 'short'}, analyzer)">Default template</button>
<button type="button" class="btn btn-dark" ng-click="vm.showTemplate({reportType: 'short'}, analyzer)">Default template</button>
</div>
</td>
<td class="vmiddle">
Expand All @@ -48,8 +48,8 @@ <h2>Report template management</h2>
<button type="button" class="btn btn-danger" ng-click="vm.deleteTemplate(analyzer.longReport)"><i class="fa fa-times"></i></button>
</div>
<div class="btn-group btn-group-sm" ng-if="!analyzer.longReport">
<button type="button" class="btn btn-dark" ng-click="vm.showTemplate({flavor: 'long'}, analyzer)">Default template</button>
</div>
<button type="button" class="btn btn-dark" ng-click="vm.showTemplate({reportType: 'long'}, analyzer)">Default template</button>
</div>
</td>
</tr>
</tbody>
Expand Down
10 changes: 9 additions & 1 deletion ui/app/views/partials/case/case.observables.item.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ <h4>
</div>
<!-- Observable analysers short reports -->
<div class="col-md-11 col-md-offset-1">
<report class="short-report" ng-if="jobs[0]" ng-repeat="(analyzerId, jobs) in analyzerJobs" artifact="artifact" content="jobs[0].report.summary" flavor="short" name="analyzerId" status="jobs[0].status" success="jobs[0].success"></report>
<report ng-if="jobs[0]"
ng-repeat="(analyzerId, jobs) in analyzerJobs"
artifact="artifact"
content="jobs[0].report.summary"
report-type="short"
name="analyzerId"
status="jobs[0].status"
success="jobs[0].success"
class="short-report"></report>
</div>
</div>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h3 class="pad10 text-primary">
<report ng-if="!showRaw" artifact="artifact"
content="report.content.full || report.content"
default="jsonhuman"
flavor="long"
report-type="long"
name="report.template"
status="report.status"
success="report.content.success"
Expand Down
1 change: 0 additions & 1 deletion ui/test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ module.exports = function(config) {
'bower_components/ng-csv/build/ng-csv.min.js',
'bower_components/ng-tags-input/ng-tags-input.js',
'bower_components/underscore/underscore.js',
'bower_components/codemirror/lib/codemirror.js',
'bower_components/angular-ui-codemirror/ui-codemirror.js',
'bower_components/angular-ui-notification/dist/angular-ui-notification.js',
'bower_components/d3/d3.js',
Expand Down

0 comments on commit 884e456

Please sign in to comment.