@@ -2,11 +2,13 @@ import { DateTime } from 'luxon';
22import { SettingsConfigurable , ISettingsSubscriber , SettingsSubscription } from '../settings/ISettingsSubscriber' ;
33import { DefaultSettings , ProfileSettings } from '../settings/Settings' ;
44import { LoggerFactory } from '../telemetry/LoggerFactory' ;
5+ import { TelemetryService } from '../telemetry/TelemetryService' ;
56import { Closeable } from '../utils/Closeable' ;
67import { AwsRegion , getRegion } from '../utils/Region' ;
78import { CombinedSchemas } from './CombinedSchemas' ;
89import { GetSchemaTaskManager } from './GetSchemaTaskManager' ;
910import { RegionalSchemasType } from './RegionalSchemas' ;
11+ import { SamSchemasType , SamStoreKey } from './SamSchemas' ;
1012import { SchemaStore } from './SchemaStore' ;
1113
1214const StaleDaysThreshold = 7 ;
@@ -16,11 +18,18 @@ export class SchemaRetriever implements SettingsConfigurable, Closeable {
1618 private settingsSubscription ?: SettingsSubscription ;
1719 private settings : ProfileSettings = DefaultSettings . profile ;
1820 private readonly log = LoggerFactory . getLogger ( SchemaRetriever ) ;
21+ private readonly telemetry = TelemetryService . instance . get ( 'SchemaRetriever' ) ;
1922
2023 constructor (
2124 private readonly schemaTaskManager : GetSchemaTaskManager ,
2225 private readonly schemaStore : SchemaStore ,
23- ) { }
26+ ) {
27+ this . telemetry . registerGaugeProvider ( 'schema.public.maxAge' , ( ) => this . getPublicSchemaMaxAge ( ) , {
28+ unit : 'ms' ,
29+ } ) ;
30+
31+ this . telemetry . registerGaugeProvider ( 'schema.sam.maxAge' , ( ) => this . getSamSchemaAge ( ) , { unit : 'ms' } ) ;
32+ }
2433
2534 configure ( settingsManager : ISettingsSubscriber ) : void {
2635 // Clean up existing subscription if present
@@ -35,6 +44,7 @@ export class SchemaRetriever implements SettingsConfigurable, Closeable {
3544 // Initialize schemas with current region
3645 this . getRegionalSchemasIfMissing ( [ this . settings . region ] ) ;
3746 this . getRegionalSchemasIfStale ( ) ;
47+ this . getSamSchemasIfMissingOrStale ( ) ;
3848
3949 // Subscribe to profile settings changes
4050 this . settingsSubscription = settingsManager . subscribe ( 'profile' , ( newProfileSettings ) => {
@@ -125,4 +135,42 @@ export class SchemaRetriever implements SettingsConfigurable, Closeable {
125135 }
126136 }
127137 }
138+
139+ private getSamSchemasIfMissingOrStale ( ) {
140+ const existingValue = this . schemaStore . samSchemas . get < SamSchemasType > ( SamStoreKey ) ;
141+
142+ if ( existingValue === undefined ) {
143+ this . schemaTaskManager . runSamTask ( ) ;
144+ return ;
145+ }
146+
147+ const now = DateTime . now ( ) ;
148+ const lastModified = DateTime . fromMillis ( existingValue . lastModifiedMs ) ;
149+ const isStale = now . diff ( lastModified , 'days' ) . days >= StaleDaysThreshold ;
150+
151+ if ( isStale ) {
152+ this . schemaTaskManager . runSamTask ( ) ;
153+ }
154+ }
155+
156+ private getPublicSchemaMaxAge ( ) : number {
157+ let maxAge = 0 ;
158+ for ( const key of this . schemaStore . publicSchemas . keys ( 50 ) ) {
159+ const region = getRegion ( key ) ;
160+ const existingValue = this . getRegionalSchemasFromStore ( region ) ;
161+ if ( existingValue ) {
162+ const age = DateTime . now ( ) . diff ( DateTime . fromMillis ( existingValue . lastModifiedMs ) ) . milliseconds ;
163+ maxAge = Math . max ( maxAge , age ) ;
164+ }
165+ }
166+ return maxAge ;
167+ }
168+
169+ private getSamSchemaAge ( ) : number {
170+ const existingValue = this . schemaStore . samSchemas . get < SamSchemasType > ( SamStoreKey ) ;
171+ if ( ! existingValue ) {
172+ return 0 ;
173+ }
174+ return DateTime . now ( ) . diff ( DateTime . fromMillis ( existingValue . lastModifiedMs ) ) . milliseconds ;
175+ }
128176}
0 commit comments