Skip to content

Commit

Permalink
added organization, project and application as metadata and optional …
Browse files Browse the repository at this point in the history
…search terms
  • Loading branch information
tomaslin committed Aug 27, 2014
1 parent 46e2c34 commit f80a70e
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@ class SearchController {
@RequestMapping(value = '/search/events/{start}', method = RequestMethod.GET)
Map eventsByDate(
@PathVariable(value = 'start') String start,
@RequestParam(value = 'end', required = false) String end,
@RequestParam(value = 'source', required = false) String source,
@RequestParam(value = 'type', required = false) String type,
@RequestParam(value = 'end', required = false) String end,
@RequestParam(value = 'organization', required = false) String organization,
@RequestParam(value = 'project', required = false) String project,
@RequestParam(value = 'application', required = false) String application,
@RequestParam(value = 'full', required = false) String full,
@RequestParam(value = 'from', required = false) String from,
@RequestParam(value = 'size', required = false) String size
) {
searchIndex.searchEvents(
start, end, source, type,
start,
end,
source,
type,
organization,
project,
application,
Boolean.parseBoolean(full),
Integer.parseInt(from ?: '0'),
Integer.parseInt(size ?: '10')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,25 @@ class SearchIndex {
result.getSourceAsObject(Map)
}

Map searchEvents(String startDate, String end, String source, String type, boolean full, int from, int size) {
Map searchEvents(String startDate,
String end,
String source,
String type,
String organization,
String project,
String application,
boolean full,
int from,
int size) {

String sizeQuery = """ "size": ${size}, """
String fromQuery = """ "from": ${from}, """

String sourceQuery = source ? """, { "term" : { "source" : "${source}" } }""" : ''
String typeQuery = type ? """, { "term" : { "type" : "${type}" } }""" : ''
String organizationQuery = organization ? """, { "term" : { "organization" : "${organization}" } }""" : ''
String projectQuery = project ? """, { "term" : { "project" : "${project}" } }""" : ''
String applicationQuery = application ? """, { "term" : { "application" : "${application}" } }""" : ''
String endDateQuery = end ? """, "lt": $end""" : ''

SearchResult result = search(METADATA_KEY,
Expand All @@ -93,6 +106,9 @@ class SearchIndex {
}
${typeQuery}
${sourceQuery}
${organizationQuery}
${projectQuery}
${applicationQuery}
]
}
}
Expand All @@ -111,10 +127,10 @@ class SearchIndex {
fields.get('type').asString,
fields.get('_content_id').asString
) : [
source : fields.get('source').asString,
type : fields.get('type').asString,
id : fields.get('_content_id').asString,
created : fields.get('created').asString
source : fields.get('source').asString,
type : fields.get('type').asString,
id : fields.get('_content_id').asString,
created: fields.get('created').asString
]
},
paginationFrom: from,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,22 @@ class SearchControllerSpec extends Specification {
ResultActions resultActions = mockMvc.perform(get(queryString))

then:
1 * controller.searchIndex.searchEvents(start, end, source, type, full, from, size)
1 * controller.searchIndex.searchEvents(start, end, source, type, organization, project, application, full, from, size)
resultActions.andExpect MockMvcResultMatchers.status().isOk()

where:
queryString | end | source | type | full | from | size
'/search/events/12345' | null | null | null | false | 0 | 10
'/search/events/12345?end=25' | '25' | null | null | false | 0 | 10
'/search/events/12345?source=igor' | null | 'igor' | null | false | 0 | 10
'/search/events/12345?type=build' | null | null | 'build' | false | 0 | 10
'/search/events/12345?full=true' | null | null | null | true | 0 | 10
'/search/events/12345?from=20' | null | null | null | false | 20 | 10
'/search/events/12345?size=50' | null | null | null | false | 0 | 50
'/search/events/12345?type=build&source=igor&end=25&full=true&from=20&size=50' | '25' | 'igor' | 'build' | true | 20 | 50
queryString | end | source | type | organization | project | application | full | from | size
'/search/events/12345' | null | null | null | null | null | null | false | 0 | 10
'/search/events/12345?end=25' | '25' | null | null | null | null | null | false | 0 | 10
'/search/events/12345?source=igor' | null | 'igor' | null | null | null | null | false | 0 | 10
'/search/events/12345?type=build' | null | null | 'build' | null | null | null | false | 0 | 10
'/search/events/12345?organization=delivery' | null | null | null | 'delivery' | null | null | false | 0 | 10
'/search/events/12345?project=spinnaker' | null | null | null | null | 'spinnaker' | null | false | 0 | 10
'/search/events/12345?application=kato' | null | null | null | null | null | 'kato' | false | 0 | 10
'/search/events/12345?full=true' | null | null | null | null | null | null | true | 0 | 10
'/search/events/12345?from=20' | null | null | null | null | null | null | false | 20 | 10
'/search/events/12345?size=50' | null | null | null | null | null | null | false | 0 | 50
'/search/events/12345?type=build&source=igor&end=25&full=true&from=20&size=50&organization=delivery&project=spinnaker&application=kato' | '25' | 'igor' | 'build' | 'delivery' | 'spinnaker' | 'kato' | true | 20 | 50

start = '12345'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class SearchIndexSpec extends Specification {

void 'events are inserted to a metadata table and events table'() {
when:
String key = addEvent('test', 'build', ['key1': 'value1', 'key2': 'value2']).metadata_key
String key = addEvent('test', 'build', null, null, null, ['key1': 'value1', 'key2': 'value2']).metadata_key

and:
JsonObject event = searchIndex.client.execute(
Expand All @@ -86,7 +86,7 @@ class SearchIndexSpec extends Specification {

void 'retrieve an event by id'() {
when:
String key = addEvent('test', 'build', ['key': 'value1', 'key2': 'value2']).content_key
String key = addEvent('test', 'build', null, null, null, ['key': 'value1', 'key2': 'value2']).content_key

Map event = searchIndex.get('test', 'build', key)

Expand All @@ -96,31 +96,31 @@ class SearchIndexSpec extends Specification {
}

void 'search events by start date'() {
3.times { addEvent('test', 'build', [:]) }
3.times { addEvent('test', 'build', null, null, null, [:]) }
long now = new Date().time
List expectedKeys = []
3.times { expectedKeys << addEvent('test', 'build', [:]).content_key }
3.times { expectedKeys << addEvent('test', 'build', null, null, null, [:]).content_key }

when:
refreshSearchIndex()
Map searchResults = searchIndex.searchEvents(now as String, null, null, null, false, 0, 10)
Map searchResults = searchIndex.searchEvents(now as String, null, null, null, null, null, null, false, 0, 10)

then:
searchResults.total == 3
searchResults.hits*.id.containsAll expectedKeys
}

void 'search events by end date'() {
5.times { addEvent('test', 'build', [:]) }
5.times { addEvent('test', 'build', null, null, null, [:]) }
long now = new Date().time
List expectedKeys = []
5.times { expectedKeys << addEvent('test', 'build', [:]).content_key }
5.times { expectedKeys << addEvent('test', 'build', null, null, null, [:]).content_key }
long stop = new Date().time
5.times { addEvent('test', 'build', [:]) }
5.times { addEvent('test', 'build', null, null, null, [:]) }

when:
refreshSearchIndex()
Map searchResults = searchIndex.searchEvents(now as String, stop as String, null, null, false, 0, 10)
Map searchResults = searchIndex.searchEvents(now as String, stop as String, null, null, null, null, null, false, 0, 10)

then:
searchResults.total == 5
Expand All @@ -131,14 +131,14 @@ class SearchIndexSpec extends Specification {
long now = new Date().time
List expectedKeys = []
2.times {
expectedKeys << addEvent('test', 'yes', [:]).content_key
expectedKeys << addEvent('otherSource', 'yes', [:]).content_key
expectedKeys << addEvent('test', 'yes', null, null, null, [:]).content_key
expectedKeys << addEvent('otherSource', 'yes', null, null, null, [:]).content_key
}
3.times { addEvent('test', 'no', [:]) }
3.times { addEvent('test', 'no', null, null, null, [:]) }

when:
refreshSearchIndex()
Map searchResults = searchIndex.searchEvents(now as String, null, null, 'yes', false, 0, 10)
Map searchResults = searchIndex.searchEvents(now as String, null, null, 'yes', null, null, null, false, 0, 10)

then:
searchResults.total == 4
Expand All @@ -149,29 +149,83 @@ class SearchIndexSpec extends Specification {
long now = new Date().time
List expectedKeys = []
2.times {
expectedKeys << addEvent('maria', 'yes', [:]).content_key
expectedKeys << addEvent('maria', 'no', [:]).content_key
expectedKeys << addEvent('maria', 'yes', null, null, null, [:]).content_key
expectedKeys << addEvent('maria', 'no', null, null, null, [:]).content_key
}
3.times { addEvent('bob', 'yes', [:]) }
3.times { addEvent('steve', 'no', [:]) }
3.times { addEvent('bob', 'yes', null, null, null, [:]) }
3.times { addEvent('steve', 'no', null, null, null, [:]) }

when:
refreshSearchIndex()
Map searchResults = searchIndex.searchEvents(now as String, null, 'maria', null, false, 0, 10)
Map searchResults = searchIndex.searchEvents(now as String, null, 'maria', null, null, null, null, false, 0, 10)

then:
searchResults.total == 4
searchResults.hits*.id.containsAll expectedKeys
}

void 'filter event search by organization'() {
long now = new Date().time
List expectedKeys = []
2.times {
expectedKeys << addEvent('maria', 'yes', 'spinnaker', null, null, [:]).content_key
}
3.times { addEvent('bob', 'yes', null, null, null, [:]) }
3.times { addEvent('steve', 'no', 'bluespar', null, null, [:]) }

when:
refreshSearchIndex()
Map searchResults = searchIndex.searchEvents(now as String, null, null, null, 'spinnaker', null, null, false, 0, 10)

then:
searchResults.total == 2
searchResults.hits*.id.containsAll expectedKeys
}

void 'filter event search by project'() {
long now = new Date().time
List expectedKeys = []
2.times {
expectedKeys << addEvent('maria', 'yes', 'spinnaker', 'echo', null, [:]).content_key
}
3.times { addEvent('bob', 'yes', 'spinnaker', 'notecho', null, [:]) }
3.times { addEvent('steve', 'no', null, 'notecho', null, [:]) }

when:
refreshSearchIndex()
Map searchResults = searchIndex.searchEvents(now as String, null, null, null, null, 'echo', null, false, 0, 10)

then:
searchResults.total == 2
searchResults.hits*.id.containsAll expectedKeys
}

void 'filter event search by application'() {
long now = new Date().time
List expectedKeys = []
2.times {
expectedKeys << addEvent('maria', 'yes', 'spinnaker', 'spinnaker', 'kato', [:]).content_key
}
3.times { addEvent('bob', 'yes', 'spinnaker', 'spinnaker', 'igor', [:]) }
3.times { addEvent('steve', 'no', null, 'notecho', 'igor', [:]) }

when:
refreshSearchIndex()
Map searchResults = searchIndex.searchEvents(now as String, null, null, null, null, null, 'kato', false, 0, 10)

then:
searchResults.total == 2
searchResults.hits*.id.containsAll expectedKeys
}

void 'retrieve full results from search'() {
long now = new Date().time
addEvent('test', 'fe', ['key1': 'value1'])
addEvent('test', 'fe', ['key1': 'value2'])
addEvent('test', 'fe', null, null, null, ['key1': 'value1'])
addEvent('test', 'fe', null, null, null, ['key1': 'value2'])

when:
refreshSearchIndex()
Map searchResults = searchIndex.searchEvents(now as String, null, 'test', 'fe', true, 0, 10)
Map searchResults = searchIndex.searchEvents(now as String, null, 'test', 'fe', null, null, null, true, 0, 10)

then:
searchResults.total == 2
Expand All @@ -180,12 +234,12 @@ class SearchIndexSpec extends Specification {

void 'control number of results returned'() {
20.times {
addEvent('test', 'type', [:])
addEvent('test', 'type', null, null, null, [:])
}

when:
refreshSearchIndex()
Map searchResults = searchIndex.searchEvents('0', null, 'test', 'type', false, 0, number)
Map searchResults = searchIndex.searchEvents('0', null, 'test', 'type', null, null, null, false, 0, number)

then:
searchResults.total == 20
Expand All @@ -198,15 +252,15 @@ class SearchIndexSpec extends Specification {

void 'pagination works'() {
10.times {
addEvent('test', 'type', ['key': 'first'])
addEvent('test', 'type', null, null, null, ['key': 'first'])
}
10.times {
addEvent('test', 'type', ['key': 'second'])
addEvent('test', 'type', null, null, null, ['key': 'second'])
}

when:
refreshSearchIndex()
Map searchResults = searchIndex.searchEvents('0', null, 'test', 'type', true, 0, 10)
Map searchResults = searchIndex.searchEvents('0', null, 'test', 'type', null, null, null, true, 0, 10)

then:
searchResults.total == 20
Expand All @@ -215,7 +269,7 @@ class SearchIndexSpec extends Specification {
searchResults.paginationFrom == 0

when:
searchResults = searchIndex.searchEvents('0', null, 'test', 'type', true, 11, 10)
searchResults = searchIndex.searchEvents('0', null, 'test', 'type', null, null, null, true, 11, 10)

then:
searchResults.hits*.key.unique() == ['second']
Expand All @@ -234,10 +288,16 @@ class SearchIndexSpec extends Specification {
config.client.admin().indices().prepareRefresh().execute().actionGet()
}

private Map addEvent(source, type, content) {
private Map addEvent(source, type, organization, project, application, content) {
searchIndex.addToIndex(
new Event(
details: new Metadata(source: source, type: type),
details: new Metadata(
source: source,
type: type,
organization: organization,
project: project,
application: application
),
content: content
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ class Metadata {
String source
String type
String created = new Date().time
String organization
String project
String application
String _content_id
}

0 comments on commit f80a70e

Please sign in to comment.