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

refactored metrics 2 #549

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/main/groovy/io/seqera/wave/controller/MetricsController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ import io.micronaut.security.annotation.Secured
import io.micronaut.security.authentication.AuthorizationException
import io.micronaut.security.rules.SecurityRule
import io.seqera.wave.exception.BadRequestException
import io.seqera.wave.service.metric.MetricConstants
import io.seqera.wave.service.metric.MetricsConstants
import io.seqera.wave.service.metric.MetricsService
import io.seqera.wave.service.metric.model.GetBuildsCountResponse
import io.seqera.wave.service.metric.model.GetFusionPullsCountResponse
import io.seqera.wave.service.metric.model.GetPullsCountResponse

import jakarta.inject.Inject

import static io.micronaut.http.HttpHeaders.WWW_AUTHENTICATE
Expand All @@ -65,25 +63,25 @@ class MetricsController {
@Get(uri = "/v1alpha2/metrics/builds", produces = MediaType.APPLICATION_JSON)
HttpResponse<?> getBuildsMetrics(@Nullable @QueryValue String date, @Nullable @QueryValue String org) {
if(!date && !org)
return HttpResponse.ok(metricsService.getAllOrgCount(MetricConstants.PREFIX_BUILDS))
return HttpResponse.ok(metricsService.getAllOrgCount(MetricsConstants.PREFIX_BUILDS))
validateQueryParams(date)
return HttpResponse.ok(metricsService.getOrgCount(MetricConstants.PREFIX_BUILDS, date, org))
return HttpResponse.ok(metricsService.getOrgCount(MetricsConstants.PREFIX_BUILDS, date, org))
}

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

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

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package io.seqera.wave.service.metric
*
* @author Munish Chouhan <[email protected]>
*/
interface MetricConstants {
interface MetricsConstants {

static final public String PREFIX_FUSION = 'fusion'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import java.util.regex.Pattern

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.seqera.wave.service.metric.MetricConstants
import io.seqera.wave.service.metric.MetricsCounterStore
import io.seqera.wave.service.metric.MetricsService
import io.seqera.wave.service.metric.model.GetOrgCountResponse
import io.seqera.wave.tower.PlatformId
import jakarta.inject.Inject
import jakarta.inject.Singleton

import static io.seqera.wave.service.metric.MetricsConstants.*
/**
* Implements service to store and retrieve wave metrics from the counter store
*
Expand All @@ -52,13 +53,13 @@ class MetricsServiceImpl implements MetricsService {
@Override
GetOrgCountResponse getAllOrgCount(String metric){
final response = new GetOrgCountResponse(metric, 0, [:])
final orgCounts = metricsCounterStore.getAllMatchingEntries("$metric/$MetricConstants.PREFIX_ORG/*")
final orgCounts = metricsCounterStore.getAllMatchingEntries("$metric/$PREFIX_ORG/*")
for(def entry : orgCounts) {
// orgCounts also contains the records with org and date, so here it filter out the records with date
if(!entry.key.contains("/$MetricConstants.PREFIX_DAY/")) {
if(!entry.key.contains("/$PREFIX_DAY/")) {
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(entry.key.split("/$MetricConstants.PREFIX_ORG/").last(), entry.value)
response.orgs.put(entry.key.split("/$PREFIX_ORG/").last(), entry.value)
}
}
return response
Expand All @@ -76,7 +77,7 @@ class MetricsServiceImpl implements MetricsService {
response.orgs.put(org, response.count)
}else{
// when only date is provide, scan the store and return the count for all orgs on given date
final orgCounts = metricsCounterStore.getAllMatchingEntries("$metric/$MetricConstants.PREFIX_ORG/*/$MetricConstants.PREFIX_DAY/$date")
final orgCounts = metricsCounterStore.getAllMatchingEntries("$metric/$PREFIX_ORG/*/$PREFIX_DAY/$date")
for(def entry : orgCounts) {
response.orgs.put(extractOrgFromKey(entry.key), entry.value)
}
Expand All @@ -88,17 +89,17 @@ class MetricsServiceImpl implements MetricsService {

@Override
void incrementFusionPullsCounter(PlatformId platformId){
incrementCounter(MetricConstants.PREFIX_FUSION, platformId?.user?.email)
incrementCounter(PREFIX_FUSION, platformId?.user?.email)
}

@Override
void incrementBuildsCounter(PlatformId platformId){
incrementCounter(MetricConstants.PREFIX_BUILDS, platformId?.user?.email)
incrementCounter(PREFIX_BUILDS, platformId?.user?.email)
}

@Override
void incrementPullsCounter(PlatformId platformId) {
incrementCounter(MetricConstants.PREFIX_PULLS, platformId?.user?.email)
incrementCounter(PREFIX_PULLS, platformId?.user?.email)
}

protected void incrementCounter(String prefix, String email) {
Expand Down Expand Up @@ -127,13 +128,13 @@ class MetricsServiceImpl implements MetricsService {

protected static String getKey(String prefix, String day, String org){
if( day && org )
return "$prefix/$MetricConstants.PREFIX_ORG/$org/$MetricConstants.PREFIX_DAY/$day"
return "$prefix/$PREFIX_ORG/$org/$PREFIX_DAY/$day"

if( org )
return "$prefix/$MetricConstants.PREFIX_ORG/$org"
return "$prefix/$PREFIX_ORG/$org"

if( day )
return "$prefix/$MetricConstants.PREFIX_DAY/$day"
return "$prefix/$PREFIX_DAY/$day"

return null
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading