Skip to content

Commit

Permalink
feat: add lineage column on search view with filter
Browse files Browse the repository at this point in the history
  • Loading branch information
hbollon committed Jun 30, 2021
1 parent ff46b2f commit e8f45b0
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 86 deletions.
12 changes: 9 additions & 3 deletions db/db.go
Expand Up @@ -306,7 +306,8 @@ func (db *Database) SearchAttribute(query url.Values) (results []types.SearchRes

sqlQuery += " JOIN modules ON states.id = modules.state_id" +
" JOIN resources ON modules.id = resources.module_id" +
" JOIN attributes ON resources.id = attributes.resource_id"
" JOIN attributes ON resources.id = attributes.resource_id" +
" JOIN lineages ON lineages.id = states.lineage_id"

var where []string
var params []interface{}
Expand Down Expand Up @@ -340,6 +341,10 @@ func (db *Database) SearchAttribute(query url.Values) (results []types.SearchRes
where = append(where, fmt.Sprintf("states.tf_version LIKE '%s'", fmt.Sprintf("%%%s%%", v)))
}

if v := query.Get("lineage_value"); string(v) != "" {
where = append(where, fmt.Sprintf("lineages.value LIKE '%s'", fmt.Sprintf("%%%s%%", v)))
}

if len(where) > 0 {
sqlQuery += " WHERE " + strings.Join(where, " AND ")
}
Expand All @@ -352,11 +357,12 @@ func (db *Database) SearchAttribute(query url.Values) (results []types.SearchRes

// Now get results
// gorm doesn't support subqueries...
sql := "SELECT states.path, states.version_id, states.tf_version, states.serial, modules.path as module_path, resources.type, resources.name, resources.index, attributes.key, attributes.value" +
sql := "SELECT states.path, states.version_id, states.tf_version, states.serial, lineages.value as lineage_value, modules.path as module_path, resources.type, resources.name, resources.index, attributes.key, attributes.value" +
sqlQuery +
" ORDER BY states.path, states.serial, modules.path, resources.type, resources.name, resources.index, attributes.key" +
" ORDER BY states.path, states.serial, lineage_value, modules.path, resources.type, resources.name, resources.index, attributes.key" +
" LIMIT ?"

log.Info(sql)
params = append(params, pageSize)

if v := string(query.Get("page")); v != "" {
Expand Down
174 changes: 91 additions & 83 deletions static/search.html
@@ -1,89 +1,93 @@
<div class="row">
<div class="col-xs-2 col-sm-2 col-md-2">
<label for="resource_type">Terraform Version</label>
</div>
<div class="col-xs-2 col-sm-2 col-md-2">
<label for="resource_type">Resource Type</label>
</div>
<div class="col-xs-2 col-sm-2 col-md-2">
<label for="resource_id">Resource ID</label>
</div>
<div class="col-xs-2 col-sm-2 col-md-2">
<label for="attribute_key">Attribute Key</label>
</div>
<div class="col-xs-2 col-sm-2 col-md-2">
<label for="attribute_value">Attribute Value</label>
</div>
</div>
<div class="row">
<div class="row justify-content-around">
<form class="form-inline" role="form">
<div class="form-group col-xs-2 col-sm-2 col-md-2">
<ui-select id="tf_version"
ng-model="$parent.tfVersion"
theme="bootstrap"
ng-disabled="disabled"
ng-change="doSearch()"
style="min-width: 200px;"
reset-search-input="false"
title="Terraform Version">
<ui-select-match placeholder="Terraform Version" allow-clear="true">{{$parent.tfVersion}}</ui-select-match>
<ui-select-choices repeat="k in tf_versions | filter: $select.search | orderBy">
{{k}}
</ui-select-choices>
</ui-select>
</div>
<div class="form-group col-xs-2 col-sm-2 col-md-2">
<ui-select id="resource_type"
ng-model="$parent.resType"
theme="bootstrap"
ng-disabled="disabled"
ng-change="doSearch()"
style="min-width: 200px;"
reset-search-input="false"
title="Resource Type">
<ui-select-match placeholder="Resource Type" allow-clear="true">{{$parent.resType}}</ui-select-match>
<ui-select-choices repeat="k in resource_keys | filter: $select.search | orderBy">
{{k}}
</ui-select-choices>
</ui-select>
</div>
<div class="form-group col-xs-2 col-sm-2 col-md-2">
<ui-select id="resource_id"
ng-model="$parent.resID"
theme="bootstrap"
ng-disabled="disabled"
ng-change="doSearch()"
style="min-width: 200px;"
reset-search-input="false"
title="Resource ID">
<ui-select-match placeholder="Resource ID" allow-clear="true">{{$parent.resID}}</ui-select-match>
<ui-select-choices repeat="k in resource_names | filter: $parent.resType | filter: $select.search | orderBy">
{{k}}
</ui-select-choices>
</ui-select>
<div class="row" style="display: flex; justify-content: center; margin-bottom: 15px;">
<div class="form-group col-xs-3 col-sm-3 col-md-2">
<ui-select id="tf_version"
ng-model="$parent.tfVersion"
theme="bootstrap"
ng-disabled="disabled"
ng-change="doSearch()"
style="min-width: 200px; max-width: 300px;"
reset-search-input="false"
title="Terraform Version">
<ui-select-match placeholder="Terraform Version" allow-clear="true">{{$parent.tfVersion}}</ui-select-match>
<ui-select-choices repeat="k in tf_versions | filter: $select.search | orderBy">
{{k}}
</ui-select-choices>
</ui-select>
</div>
<div class="form-group col-xs-3 col-sm-3 col-md-2">
<ui-select id="lineage_value"
ng-model="$parent.lineage"
theme="bootstrap"
ng-disabled="disabled"
ng-change="doSearch()"
style="min-width: 200px; max-width: 300px;"
reset-search-input="false"
title="Lineage">
<ui-select-match placeholder="Lineage" allow-clear="true">{{$parent.lineage}}</ui-select-match>
<ui-select-choices repeat="k in lineages | filter: $select.search | orderBy">
{{k}}
</ui-select-choices>
</ui-select>
</div>
<div class="form-group col-xs-3 col-sm-3 col-md-2">
<ui-select id="resource_type"
ng-model="$parent.resType"
theme="bootstrap"
ng-disabled="disabled"
ng-change="doSearch()"
style="min-width: 200px; max-width: 300px;"
reset-search-input="false"
title="Resource Type">
<ui-select-match placeholder="Resource Type" allow-clear="true">{{$parent.resType}}</ui-select-match>
<ui-select-choices repeat="k in resource_keys | filter: $select.search | orderBy">
{{k}}
</ui-select-choices>
</ui-select>
</div>
<div class="form-group col-xs-3 col-sm-3 col-md-2">
<ui-select id="resource_id"
ng-model="$parent.resID"
theme="bootstrap"
ng-disabled="disabled"
ng-change="doSearch()"
style="min-width: 200px; max-width: 300px;"
reset-search-input="false"
title="Resource ID">
<ui-select-match placeholder="Resource ID" allow-clear="true">{{$parent.resID}}</ui-select-match>
<ui-select-choices repeat="k in resource_names | filter: $parent.resType | filter: $select.search | orderBy">
{{k}}
</ui-select-choices>
</ui-select>
</div>
<div class="form-group col-xs-3 col-sm-3 col-md-2">
<ui-select id="attribute_key"
ng-model="$parent.attrKey"
theme="bootstrap"
ng-disabled="disabled"
ng-change="doSearch()"
style="min-width: 200px; max-width: 300px;"
reset-search-input="false"
title="Attribute Key">
<ui-select-match placeholder="Attribute Key" allow-clear="true">{{$parent.attrKey}}</ui-select-match>
<ui-select-choices
repeat="k in attribute_keys | filter: $select.search | orderBy"
refresh="refreshAttrKeys()"
refresh-delay="0">
{{k}}
</ui-select-choices>
</ui-select>
</div>
</div>
<div class="form-group col-xs-2 col-sm-2 col-md-2">
<ui-select id="attribute_key"
ng-model="$parent.attrKey"
theme="bootstrap"
ng-disabled="disabled"
ng-change="doSearch()"
style="min-width: 200px;"
reset-search-input="false"
title="Attribute Key">
<ui-select-match placeholder="Attribute Key" allow-clear="true">{{$parent.attrKey}}</ui-select-match>
<ui-select-choices
repeat="k in attribute_keys | filter: $select.search | orderBy"
refresh="refreshAttrKeys()"
refresh-delay="0">
{{k}}
</ui-select-choices>
</ui-select>
<div class="row" style="display: flex; justify-content: center;">
<div class="form-group col-xs-2 col-sm-2 col-md-2">
<input id="attribute_value" type="text" class="form-control" placeholder="Attribute Value" ng-model="attrVal" ng-change="doSearch()" />
</div>
<div class="col-xs-3 col-sm-2 col-md-1">
<button class="clear btn btn-warning" ng-click="clearForm()">Reset</button>
</div>
</div>
<div class="form-group col-xs-2 col-sm-2 col-md-2">
<input id="attribute_value" type="text" class="form-control" placeholder="Attribute Value" ng-model="attrVal" ng-change="doSearch()" />
</div>
<button class="clear btn btn-warning" ng-click="clearForm()">Reset</button>
</form>
</div>
<hr />
Expand All @@ -105,6 +109,9 @@
<th>
Serial
</th>
<th>
Lineage
</th>
<th>
Module Path
</th>
Expand All @@ -124,6 +131,7 @@
<td>{{r.path}}</td>
<td>{{r.tf_version}}</td>
<td>{{r.serial}}</td>
<td>{{r.lineage_value}}</td>
<td>{{r.module_path}}</td>
<td>{{r.resource_type}}.{{r.resource_name}}{{r.resource_index}}</td>
<td>{{r.attribute_key}}</td>
Expand Down
1 change: 1 addition & 0 deletions types/search.go
Expand Up @@ -14,6 +14,7 @@ type SearchResult struct {
VersionID string `gorm:"column:version_id" json:"version_id"`
TFVersion string `gorm:"column:tf_version" json:"tf_version"`
Serial int64 `gorm:"column:serial" json:"serial"`
LineageValue string `json:"lineage_value"`
ModulePath string `gorm:"column:module_path" json:"module_path"`
ResourceType string `gorm:"column:type" json:"resource_type"`
ResourceName string `gorm:"column:name" json:"resource_name"`
Expand Down

0 comments on commit e8f45b0

Please sign in to comment.