Skip to content

Commit

Permalink
pattern moved to MetricsServiceImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
munishchouhan committed Apr 25, 2024
1 parent e279c81 commit a3c9d79
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ class LocalCounterProvider implements CounterProvider {
@Override
Map<String, Long> getAllMatchingEntries(String key, String pattern) {
def keyStore = store.get(key)
def matchingPairs = keyStore.findAll { k, v ->
k.contains(pattern)
}
def matchingPairs = keyStore.findAll { k, v -> k =~pattern}
Map<String, Long> result = [:]
matchingPairs.each { k, v ->
result.put(k, v as Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class RedisCounterProvider implements CounterProvider {
Map<String, Long> getAllMatchingEntries(String key, String pattern) {
try(Jedis conn=pool.getResource() ) {
Map<String, Long> result = [:]
def scanResult = conn.hscan(key, "0", new ScanParams().match("$pattern*"))
def scanResult = conn.hscan(key, "0", new ScanParams().match(pattern))
for(String entry : scanResult.result) {
def parts = entry.split('=')
result.put(parts[0], parts[1] as Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ class MetricsServiceImpl implements MetricsService {
@Override
GetOrgCountResponse getOrgCount(String metrics){
GetOrgCountResponse response = new GetOrgCountResponse(metrics, 0, [:])
def orgCounts = metricsCounterStore.getAllMatchingEntries("$metrics/$MetricConstants.PREFIX_ORG")
log.info("Found ${orgCounts.size()} orgs for key: $metrics/$MetricConstants.PREFIX_ORG")
def orgCounts = metricsCounterStore.getAllMatchingEntries("$metrics/$MetricConstants.PREFIX_ORG/*")
for(def entry : orgCounts) {
log.info(entry.key+" -> "+entry.value)
if(!entry.key.contains("/$MetricConstants.PREFIX_DAY/")) {
response.count += entry.value
response.orgs.put(entry.key.split("/$MetricConstants.PREFIX_ORG/").last(), entry.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ class LocalCounterProviderTest extends Specification {

def 'should get correct org count' () {
when:
localCounterProvider.inc('metrics/v1', '/builds/o/foo', 1)
localCounterProvider.inc('metrics/v1', '/builds/o/bar', 1)
localCounterProvider.inc('metrics/v1', '/builds/o/abc', 2)
localCounterProvider.inc('metrics/v1', '/pulls/o/foo', 1)
localCounterProvider.inc('metrics/v1', '/pulls/o/bar', 2)
localCounterProvider.inc('metrics/v1', '/pulls/o/abc', 3)
localCounterProvider.inc('metrics/v1', '/pulls/o/abc/date/yyyy-mm-dd', 1)
localCounterProvider.inc('metrics/v1', 'builds/o/foo.com', 1)
localCounterProvider.inc('metrics/v1', 'builds/o/bar.io', 1)
localCounterProvider.inc('metrics/v1', 'builds/o/abc.org', 2)
localCounterProvider.inc('metrics/v1', 'pulls/o/foo.it', 1)
localCounterProvider.inc('metrics/v1', 'pulls/o/bar.es', 2)
localCounterProvider.inc('metrics/v1', 'pulls/o/abc.in', 3)
localCounterProvider.inc('metrics/v1', 'pulls/o/abc.com.au/date/yyyy-mm-dd', 1)

then:
localCounterProvider.getAllMatchingEntries('metrics/v1', '/pulls/o') ==
['/pulls/o/foo':1, '/pulls/o/bar':2, '/pulls/o/abc':3, '/pulls/o/abc/date/yyyy-mm-dd': 1]
localCounterProvider.getAllMatchingEntries('metrics/v1', 'pulls/o/*') ==
['pulls/o/foo.it':1, 'pulls/o/bar.es':2, 'pulls/o/abc.in':3, 'pulls/o/abc/date/yyyy-mm-dd': 1]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ class RedisCounterProviderTest extends Specification implements RedisTestContain

def 'should get correct org count' () {
when:
redisCounterProvider.inc('metrics/v1', 'builds/o/foo', 1)
redisCounterProvider.inc('metrics/v1', 'builds/o/bar', 1)
redisCounterProvider.inc('metrics/v1', 'builds/o/abc', 2)
redisCounterProvider.inc('metrics/v1', 'pulls/o/foo', 1)
redisCounterProvider.inc('metrics/v1', 'pulls/o/bar', 2)
redisCounterProvider.inc('metrics/v1', 'pulls/o/abc', 3)
redisCounterProvider.inc('metrics/v1', 'pulls/o/abc/date/yyyy-mm-dd', 1)
redisCounterProvider.inc('metrics/v1', 'builds/o/foo.com', 1)
redisCounterProvider.inc('metrics/v1', 'builds/o/bar.org', 1)
redisCounterProvider.inc('metrics/v1', 'builds/o/abc.it', 2)
redisCounterProvider.inc('metrics/v1', 'pulls/o/foo.es', 1)
redisCounterProvider.inc('metrics/v1', 'pulls/o/bar.in', 2)
redisCounterProvider.inc('metrics/v1', 'pulls/o/abc.au', 3)
redisCounterProvider.inc('metrics/v1', 'pulls/o/abc.com/date/yyyy-mm-dd', 1)

then:
redisCounterProvider.getAllMatchingEntries('metrics/v1', 'pulls/o') ==
['pulls/o/foo':1, 'pulls/o/bar':2, 'pulls/o/abc':3, 'pulls/o/abc/date/yyyy-mm-dd': 1]
redisCounterProvider.getAllMatchingEntries('metrics/v1', 'pulls/o/*') ==
['pulls/o/foo.es':1, 'pulls/o/bar.in':2, 'pulls/o/abc.au':3, 'pulls/o/abc.com/date/yyyy-mm-dd': 1]
}
}

0 comments on commit a3c9d79

Please sign in to comment.