runtime usage service#960
Conversation
There was a problem hiding this comment.
Pull request overview
Implements a “runtime usage” pipeline end-to-end: persists daily aggregated runtime usage per flow/namespace, exposes it via GraphQL connections (with date filtering), and accepts updates via a new gRPC RuntimeUsage service.
Changes:
- Add
daily_runtime_usagespersistence (migration, model, associations) and a gRPC update service/handler to record/increment usage. - Expose daily runtime usage via GraphQL (
Namespace.dailyRuntimeUsages,Flow.dailyRuntimeUsages) plus aDatescalar and new GraphQL type. - Add request/service/type specs and generate GraphQL docs for the new schema surface and error code.
Reviewed changes
Copilot reviewed 33 out of 33 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| spec/services/runtimes/grpc/runtime_usage_update_service_spec.rb | Unit coverage for usage update service behavior and validation. |
| spec/requests/grpc/sagittarius/runtime_usage_service_spec.rb | gRPC request spec for RuntimeUsageService update behavior. |
| spec/requests/graphql/query/namespace_runtime_usages_query_spec.rb | GraphQL query coverage for namespace-level usage retrieval and filters. |
| spec/requests/graphql/query/flow_runtime_usages_query_spec.rb | GraphQL query coverage for flow-level usage retrieval and date filtering. |
| spec/graphql/types/namespace_type_spec.rb | Updates Namespace type field expectations to include dailyRuntimeUsages. |
| spec/graphql/types/flow_type_spec.rb | Adds Flow type spec including dailyRuntimeUsages. |
| spec/graphql/types/date_type_spec.rb | Adds tests for the new Types::DateType scalar coercion. |
| spec/graphql/types/daily_runtime_usage_type_spec.rb | Adds GraphQL type coverage for DailyRuntimeUsage. |
| spec/factories/daily_runtime_usages.rb | Factory for DailyRuntimeUsage records. |
| extensions/cloud/spec/graphql/types/cloud/types/namespace_type_spec.rb | Updates Cloud extension Namespace type field expectations. |
| docs/graphql/scalar/float.md | Adds/records Float scalar documentation entry. |
| docs/graphql/scalar/date.md | Adds Date scalar documentation entry. |
| docs/graphql/scalar/dailyruntimeusageid.md | Adds scalar doc entry for DailyRuntimeUsageID. |
| docs/graphql/object/namespace.md | Documents Namespace.dailyRuntimeUsages field and arguments. |
| docs/graphql/object/flow.md | Documents Flow.dailyRuntimeUsages field and arguments. |
| docs/graphql/object/dailyruntimeusageedge.md | Adds connection edge documentation for daily runtime usage. |
| docs/graphql/object/dailyruntimeusageconnection.md | Adds connection documentation for daily runtime usage. |
| docs/graphql/object/dailyruntimeusage.md | Adds object documentation for daily runtime usage. |
| docs/graphql/enum/errorcodeenum.md | Documents the new INVALID_RUNTIME_USAGE error code. |
| db/structure.sql | Reflects the new daily_runtime_usages table and indexes. |
| db/schema_migrations/20260510081622 | Adds checksum file for the new migration per repo convention. |
| db/migrate/20260510081622_create_daily_runtime_usage.rb | Migration creating daily_runtime_usages table and FK behavior. |
| app/services/runtimes/grpc/runtime_usage_update_service.rb | Implements the transactional upsert/increment logic for daily usage. |
| app/services/error_code.rb | Registers invalid_runtime_usage error code. |
| app/models/namespace.rb | Adds has_many :daily_runtime_usages association. |
| app/models/flow.rb | Adds has_many :daily_runtime_usages association. |
| app/models/daily_runtime_usage.rb | New model for daily runtime usage with validations and relations. |
| app/grpc/runtime_usage_handler.rb | New gRPC handler wiring RuntimeUsageService update to the new service. |
| app/graphql/types/namespace_type.rb | Adds daily_runtime_usages connection field and resolver with filters. |
| app/graphql/types/namespace_project_type.rb | Fixes project flow lookup to use GlobalID model_id with find_by. |
| app/graphql/types/flow_type.rb | Adds daily_runtime_usages connection field and resolver with date filters. |
| app/graphql/types/date_type.rb | Adds custom Date scalar (ISO8601) for GraphQL. |
| app/graphql/types/daily_runtime_usage_type.rb | Adds GraphQL object type for DailyRuntimeUsage with auth subject. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| t.references :namespace, null: false, foreign_key: { to_table: :namespaces, on_delete: :cascade } | ||
| t.date :day, null: false | ||
| t.decimal :usage, null: false, default: 0 | ||
|
|
| def update_usage(usage) | ||
| flow = Flow.includes(project: :namespace).find_by(id: usage_attribute(usage, :flow_id)) | ||
| return ServiceResponse.error(message: 'Flow not found', error_code: :flow_not_found) if flow.nil? | ||
|
|
||
| day = usage_day(usage) | ||
| amount = usage_amount(usage) | ||
| return invalid_usage_error('Usage amount must be greater than zero') unless amount&.positive? | ||
|
|
||
| db_usage = DailyRuntimeUsage.find_or_initialize_by( | ||
| namespace: flow.project.namespace, | ||
| flow: flow, | ||
| day: day | ||
| ) |
| db_usage = DailyRuntimeUsage.find_or_initialize_by( | ||
| namespace: flow.project.namespace, | ||
| flow: flow, | ||
| day: day | ||
| ) | ||
|
|
||
| return increment_usage(db_usage, amount) unless db_usage.persisted? | ||
|
|
||
| db_usage.with_lock { increment_usage(db_usage, amount) } | ||
| rescue ActiveRecord::RecordInvalid => e | ||
| invalid_usage_error(e.record.errors) | ||
| rescue ActiveRecord::RecordNotUnique | ||
| retry |
| def update(request, _call) | ||
| response = Runtimes::Grpc::RuntimeUsageUpdateService.new(usages: request.runtime_usage).execute | ||
|
|
||
| logger.debug("RuntimeUsageHandler#update response: #{response.inspect}") | ||
|
|
||
| Tucana::Sagittarius::RuntimeUsageResponse.new(success: response.success?) | ||
| end |
GitLab Pipeline ActionGeneral informationLink to pipeline: https://gitlab.com/code0-tech/development/sagittarius/-/pipelines/2520165948 Status: Failed Job summariesrspec: [cloud]Coverage report available at https://code0-tech.gitlab.io/-/development/sagittarius/-/jobs/14336380692/artifacts/tmp/coverage/index.html rspec: [ee]Coverage report available at https://code0-tech.gitlab.io/-/development/sagittarius/-/jobs/14336380691/artifacts/tmp/coverage/index.html rspec: [ce]Coverage report available at https://code0-tech.gitlab.io/-/development/sagittarius/-/jobs/14336380690/artifacts/tmp/coverage/index.html rubocop787 files inspected, no offenses detected |
Resolves: #779