Skip to content

Commit

Permalink
refactored and added tests
Browse files Browse the repository at this point in the history
Signed-off-by: munishchouhan <[email protected]>
  • Loading branch information
munishchouhan committed Feb 5, 2025
1 parent 042b7a7 commit ab62cd6
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 157 deletions.
20 changes: 10 additions & 10 deletions src/main/groovy/io/seqera/wave/controller/MetricsController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -98,37 +98,37 @@ class MetricsController {

@Get(uri = "/v1alpha3/metrics/builds", produces = MediaType.APPLICATION_JSON)
HttpResponse<?> getBuildsMetrics(@Nullable @QueryValue String date, @Nullable @QueryValue String org, @Nullable @QueryValue String arch) {
if(!date && !org)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_BUILDS, arch))
if(!date && !org && !arch)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_BUILDS))
return HttpResponse.ok(metricsService.getOrgCount(MetricsConstants.PREFIX_BUILDS, date, org, arch))
}

@Get(uri = "/v1alpha3/metrics/pulls", produces = MediaType.APPLICATION_JSON)
HttpResponse<?> getPullsMetrics(@Nullable @QueryValue String date, @Nullable @QueryValue String org, @Nullable @QueryValue String arch) {
if(!date && !org)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_PULLS, arch))
if(!date && !org && !arch)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_PULLS))
return HttpResponse.ok(metricsService.getOrgCount(MetricsConstants.PREFIX_PULLS, date, org, arch))
}

@Get(uri = "/v1alpha3/metrics/fusion/pulls", produces = MediaType.APPLICATION_JSON)
HttpResponse<?> getFusionPullsMetrics(@Nullable @QueryValue String date, @Nullable @QueryValue String org, @Nullable @QueryValue String arch) {
if(!date && !org)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_FUSION, arch))
if(!date && !org && !arch)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_FUSION))
return HttpResponse.ok(metricsService.getOrgCount(MetricsConstants.PREFIX_FUSION, date, org, arch))

}

@Get(uri = "/v1alpha3/metrics/mirrors", produces = MediaType.APPLICATION_JSON)
HttpResponse<?> getMirrorsMetrics(@Nullable @QueryValue String date, @Nullable @QueryValue String org, @Nullable @QueryValue String arch) {
if(!date && !org)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_MIRRORS, arch))
if(!date && !org && !arch)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_MIRRORS))
return HttpResponse.ok(metricsService.getOrgCount(MetricsConstants.PREFIX_MIRRORS, date, org, arch))
}

@Get(uri = "/v1alpha3/metrics/scans", produces = MediaType.APPLICATION_JSON)
HttpResponse<?> getScansMetrics(@Nullable @QueryValue String date, @Nullable @QueryValue String org, @Nullable @QueryValue String arch) {
if(!date && !org)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_SCANS, arch))
if(!date && !org && !arch)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_SCANS))
return HttpResponse.ok(metricsService.getOrgCount(MetricsConstants.PREFIX_SCANS, date, org, arch))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,4 @@ interface MetricsService {
* @return GetOrgCountResponse
*/
GetOrgArchCountResponse getOrgCount(String metric, String date, String org, String arch)

/**
* Get counts of all organisations by arch
*
* @param metric
* @return GetOrgCountResponse
*/
GetOrgArchCountResponse getAllOrgCount(String metric, String arch)
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,6 @@ class MetricsServiceImpl implements MetricsService {
return response
}

@Override
GetOrgArchCountResponse getAllOrgCount(String metric, String arch){
if ( arch ) {
arch = resolveArch(arch)
final response = new GetOrgArchCountResponse(metric, arch, 0, [:])
final orgCounts = metricsCounterStore.getAllMatchingEntries("$metric/$PREFIX_ORG/*/$PREFIX_ARCH/$arch*")
for(def entry : orgCounts) {
// orgCounts also contains the records with date, so here it filter out the records with date
if(!entry.key.contains("/$PREFIX_DAY/") && arch == extractArchFromKey(entry.key)) {
response.count += entry.value
//split is used to extract the org name from the key like "metrics/o/seqera.io" => seqera.io
response.orgs.put(extractOrgFromArchKey(entry.key), entry.value)
}
}
return response
} else {
def response = getAllOrgCount(metric)
return new GetOrgArchCountResponse(response.metric, null, response.count, response.orgs)
}

}

@Override
GetOrgCountResponse getOrgCount(String metric, String date, String org) {
final response = new GetOrgCountResponse(metric, 0, [:])
Expand Down Expand Up @@ -125,14 +103,22 @@ class MetricsServiceImpl implements MetricsService {
// count is stored per date and per org, so it can be extracted from get method
response.count = metricsCounterStore.get(getKey(metric, date, org, arch)) ?: 0L
//when org and date is provided, return the org count for given date
if (org) {
if ( org ) {
response.orgs.put(org, response.count)
}else {
} else if ( date ){
// when date and arch are provide, scan the store and return the count for all orgs with given arch and date
final orgCounts = metricsCounterStore.getAllMatchingEntries("$metric/$PREFIX_ORG/*/$PREFIX_ARCH/$arch/$PREFIX_DAY/$date")
for (def entry : orgCounts) {
response.orgs.put(extractOrgFromKey(entry.key), entry.value)
}
} else {
// when only arch is provide, scan the store and return the count for all orgs with given arch
final orgCounts = metricsCounterStore.getAllMatchingEntries("$metric/$PREFIX_ORG/*/$PREFIX_ARCH/$arch*")
for (def entry : orgCounts) {
if (!entry.key.contains("/$PREFIX_DAY/")) {
response.orgs.put(extractOrgFromKey(entry.key), entry.value)
}
}
}
return response
} else {
Expand Down Expand Up @@ -175,11 +161,17 @@ class MetricsServiceImpl implements MetricsService {
metricsCounterStore.inc(key)
log.trace("increment metrics count of: $key")

//increment the count for the current day and arch

if ( arch ) {
//increment the count for the current day and arch
key = getKey(prefix, day, null, arch)
metricsCounterStore.inc(key)
log.trace("increment metrics count of: $key")

//increment the count for the current arch
key = getKey(prefix, null, null, arch)
metricsCounterStore.inc(key)
log.trace("increment metrics count of: $key")
}

if ( org ) {
Expand Down
Loading

0 comments on commit ab62cd6

Please sign in to comment.