Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated filter deleted zones using scala to sql query #1329

Merged
merged 8 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE SCHEMA IF NOT EXISTS ${dbName};

USE ${dbName};

ALTER TABLE zone_change
ADD COLUMN zone_name VARCHAR(256) NOT NULL;
CREATE INDEX zone_name_index ON zone_change(zone_name);


ALTER TABLE zone_change
ADD COLUMN zone_status CHAR(36) NOT NULL;
CREATE INDEX zone_status_index ON zone_change(zone_status);
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

private final val PUT_ZONE_CHANGE =
sql"""
|REPLACE INTO zone_change (change_id, zone_id, data, created_timestamp)
| VALUES ({change_id}, {zone_id}, {data}, {created_timestamp})
|REPLACE INTO zone_change (change_id, zone_id, data, created_timestamp, zone_name, zone_status)
| VALUES ({change_id}, {zone_id}, {data}, {created_timestamp},{zone_name}, {zone_status})
""".stripMargin

private final val BASE_ZONE_CHANGE_SEARCH_SQL =
Expand All @@ -49,9 +49,15 @@
| FROM zone_change zc
""".stripMargin

private final val BASE_GET_ZONES_SQL =
private final val BASE_ZONE_NAME_COUNT_SQL =
"""
|SELECT z.data
|SELECT COUNT(z.name)
| FROM zone z
""".stripMargin

private final val BASE_ZONE_NAME_SEARCH_SQL =
"""
|SELECT z.name
| FROM zone z
""".stripMargin

Expand Down Expand Up @@ -82,7 +88,9 @@
'change_id -> zoneChange.id,
'zone_id -> zoneChange.zoneId,
'data -> toPB(zoneChange).toByteArray,
'created_timestamp -> zoneChange.created.toEpochMilli
'created_timestamp -> zoneChange.created.toEpochMilli,
'zone_name -> zoneChange.zone.name,
'zone_status -> zoneChange.zone.status.toString
)
.update()
.apply()
Expand Down Expand Up @@ -144,7 +152,7 @@
s"""
| JOIN zone_access za ON zc.zone_id = za.zone_id
| AND za.accessor_id IN ($questionMarks)
""".stripMargin
""".stripMargin
(withAccessorCheck, accessors)
}

Expand Down Expand Up @@ -177,37 +185,46 @@
val sb = new StringBuilder
sb.append(withAccessorCheck)

val query = sb.toString
val zoneResults: Int =
SQL(BASE_ZONE_NAME_COUNT_SQL)
.map(_.int(1))
.single()
.apply()
.getOrElse(0)

val zoneChangeResults: List[ZoneChange] =
SQL(query)
.bind(accessors: _*)
.map(extractZoneChange(1))
.list()
.apply()
sb.append(s" WHERE ")

val zoneResults: List[Zone] =
SQL(BASE_GET_ZONES_SQL)
.map(extractZone(1))
.list()
.apply()
if (zoneResults != 0) sb.append(s" zc.zone_name NOT IN ($BASE_ZONE_NAME_SEARCH_SQL) AND ")

val zoneNotInZoneChange: List[ZoneChange] =
zoneChangeResults.filter(z=> !zoneResults.map(_.name).contains(z.zone.name) && z.zone.status != ZoneStatus.Active)
sb.append(s" zc.zone_status = 'Deleted' ")

val deletedZoneResults: List[ZoneChange] =
zoneNotInZoneChange.filter(_.zone.status.equals(ZoneStatus.Deleted)).distinct.sortBy(_.zone.updated).reverse
val filters = if (zoneNameFilter.isDefined && zoneNameFilter.get.contains("*"))
zoneNameFilter.map(flt => s"zc.zone_name LIKE '${flt.replace('*', '%')}'")

Check warning on line 202 in modules/mysql/src/main/scala/vinyldns/mysql/repository/MySqlZoneChangeRepository.scala

View check run for this annotation

Codecov / codecov/patch

modules/mysql/src/main/scala/vinyldns/mysql/repository/MySqlZoneChangeRepository.scala#L202

Added line #L202 was not covered by tests
else zoneNameFilter.map(flt => s"zc.zone_name LIKE '${flt.concat("%")}'")

if(zoneNameFilter.isDefined)
sb.append(s" AND ")

Check warning on line 206 in modules/mysql/src/main/scala/vinyldns/mysql/repository/MySqlZoneChangeRepository.scala

View check run for this annotation

Codecov / codecov/patch

modules/mysql/src/main/scala/vinyldns/mysql/repository/MySqlZoneChangeRepository.scala#L206

Added line #L206 was not covered by tests

val results: List[ZoneChange] =
if (zoneNameFilter.nonEmpty) {
deletedZoneResults.filter(r => r.zone.name.contains(zoneNameFilter.getOrElse("not found")))
} else {
deletedZoneResults
}
sb.append(filters.mkString)

val resultOrdering = s"""| GROUP BY zc.zone_name
| ORDER BY zc.created_timestamp DESC
""".stripMargin

sb.append(resultOrdering)

val query = sb.toString

val deletedZoneResults: List[ZoneChange] =
SQL(query)
.bind(accessors: _*)
.map(extractZoneChange(1))
.list()
.apply()

val deletedZonesWithStartFrom: List[ZoneChange] = startFrom match {
case Some(zoneId) => results.dropWhile(_.zone.id != zoneId)
case None => results
case Some(zoneId) => deletedZoneResults.dropWhile(_.zone.id != zoneId)
case None => deletedZoneResults
}

val deletedZonesWithMaxItems = deletedZonesWithStartFrom.take(maxItems + 1)
Expand Down Expand Up @@ -247,8 +264,4 @@
private def extractZoneChange(colIndex: Int): WrappedResultSet => ZoneChange = res => {
fromPB(VinylDNSProto.ZoneChange.parseFrom(res.bytes(colIndex)))
}

private def extractZone(columnIndex: Int): WrappedResultSet => Zone = res => {
fromPB(VinylDNSProto.Zone.parseFrom(res.bytes(columnIndex)))
}
}
10 changes: 5 additions & 5 deletions modules/portal/app/views/zones/zones.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ <h3><span class="fa fa-table"></span> Zones</h3>
<span class="fa fa-search"></span>
</button>
</span>
<input id="deleted-zones-search-text" ng-model="query" type="text" class="form-control" placeholder="Zone Name"/>
<input id="deleted-zones-search-text" ng-model="query" type="text" class="form-control" placeholder="Deleted Zone Name"/>
</div>
</form>
</div>
Expand All @@ -309,13 +309,13 @@ <h3><span class="fa fa-table"></span> Zones</h3>
<!-- DELETED ZONES TABS -->
<div class="panel panel-default panel-tabs">
<ul class="nav nav-tabs bar_tabs">
<li class="active"><a href="#myDeletedZones" data-toggle="tab">My Zones</a></li>
<li><a id="tab2-button" href="#allDeletedZones" data-toggle="tab">All Zones</a></li>
<li class="active"><a href="#myDeletedZones" data-toggle="tab" ng-click="myZonesAccess()" >My Zones</a></li>
<li><a id="tab2-button" href="#allDeletedZones" data-toggle="tab" ng-click="allZonesAccess()">All Zones</a></li>
</ul>
<div class="panel-body tab-content">
<div class="tab-pane active" id="myDeletedZones">
<div id="zone-list-table" class="panel-body">
<p ng-if="!myDeletedZonesLoaded">Loading zones...</p>
<p ng-if="!myDeletedZonesLoaded">Loading my deleted zones...</p>
<p ng-if="myDeletedZonesLoaded && !myDeletedZones.length">No zones match the search criteria.</p>

<!-- PAGINATION -->
Expand Down Expand Up @@ -394,7 +394,7 @@ <h3><span class="fa fa-table"></span> Zones</h3>
</div>
<div class="tab-pane" id="allDeletedZones">
<div id="zone-list-table" class="panel-body">
<p ng-if="!allDeletedZonesLoaded">Loading zones...</p>
<p ng-if="!allDeletedZonesLoaded">Loading all deleted zones...</p>
<p ng-if="allDeletedZonesLoaded && !allDeletedZones.length">No zones match the search criteria.</p>

<!-- PAGINATION -->
Expand Down
4 changes: 3 additions & 1 deletion modules/portal/public/lib/controllers/controller.zones.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ angular.module('controller.zones', [])
$scope.refreshZones = function () {
zonesPaging = pagingService.resetPaging(zonesPaging);
allZonesPaging = pagingService.resetPaging(allZonesPaging);
myDeleteZonesPaging = pagingService.resetPaging(myDeleteZonesPaging);
nspadaccino marked this conversation as resolved.
Show resolved Hide resolved
allDeleteZonesPaging = pagingService.resetPaging(allDeleteZonesPaging);

zonesService
.getZones(zonesPaging.maxItems, undefined, $scope.query, $scope.searchByAdminGroup, false, $scope.includeReverse)
Expand Down Expand Up @@ -467,7 +469,7 @@ angular.module('controller.zones', [])

$scope.nextPageAllDeletedZones = function () {
return zonesService
.getDeletedZones(allDeleteZonesPaging.maxItems, allDeleteZonesPaging.next, $scope.query, false)
.getDeletedZones(allDeleteZonesPaging.maxItems, allDeleteZonesPaging.next, $scope.query, true)
.then(function(response) {
var allDeletedZoneSets = response.data.zonesDeletedInfo;
allDeleteZonesPaging = pagingService.nextPageUpdate(allDeletedZoneSets, response.data.nextId, allDeleteZonesPaging);
Expand Down
14 changes: 13 additions & 1 deletion modules/portal/public/lib/services/zones/service.zones.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,20 @@ angular.module('service.zones', [])
"nameFilter": query,
"ignoreAccess": ignoreAccess
};

let loader = $("#loader");
loader.modal({
backdrop: "static", //remove ability to close modal with click
keyboard: false, //remove option to close with keyboard
show: true //Display loader!
})

var url = groupsService.urlBuilder("/api/zones/deleted/changes", params);
return $http.get(url);

let promis = $http.get(url);
// Hide loader when api gets response
promis.then(()=>loader.modal("hide"), ()=>loader.modal("hide"))
return promis;
};

this.getBackendIds = function() {
Expand Down