@@ -11,65 +11,20 @@ export class GitHubService {
1111 this . token = token ;
1212 }
1313
14- // 🔧 Ajuste de zona horaria: convierte fecha UTC a hora local de Bogotá (UTC-5)
15- private adjustToBogotaDate ( dateString : string ) : string {
16- // Si es solo una fecha (YYYY-MM-DD), ya está en el formato correcto
17- // porque GitHub devuelve las fechas de contribución en la zona local del usuario
18- if ( / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / . test ( dateString ) ) {
19- console . log ( `🔄 Fecha ya procesada: ${ dateString } (Colombia)` ) ;
20- return dateString ;
21- }
22- // Si tiene hora (ISO 8601), convertir de UTC a Colombia
23- const date = new Date ( dateString ) ;
24- // Convertir a milisegundos en zona horaria de Colombia (UTC-5)
25- const COLOMBIA_OFFSET_MS = - 5 * 60 * 60 * 1000 ;
26- const colombiaTime = new Date ( date . getTime ( ) + COLOMBIA_OFFSET_MS ) ;
27- // Extraer año, mes, día en Colombia
28- const year = colombiaTime . getUTCFullYear ( ) ;
29- const month = String ( colombiaTime . getUTCMonth ( ) + 1 ) . padStart ( 2 , '0' ) ;
30- const day = String ( colombiaTime . getUTCDate ( ) ) . padStart ( 2 , '0' ) ;
31- const localDateKey = `${ year } -${ month } -${ day } ` ;
32- console . log ( `🔄 Conversión: ${ dateString } (UTC) -> ${ localDateKey } (Colombia)` ) ;
33- return localDateKey ;
34- }
35-
36- // Obtiene los commits de los últimos 7 días
14+ /**
15+ * Obtiene los commits de los últimos 7 días
16+ */
3717 async getLastWeekCommits ( ) : Promise < number [ ] > {
38- console . log ( "🌐 GitHubService: Iniciando petición a API" ) ;
39-
40- // Zona horaria de Colombia: UTC-5
41- const COLOMBIA_OFFSET_HOURS = - 5 ;
42-
43- // Obtener fecha y hora actual en Colombia
44- const nowUTC = new Date ( ) ;
45- const nowColombia = new Date ( nowUTC . getTime ( ) + COLOMBIA_OFFSET_HOURS * 60 * 60 * 1000 ) ;
46- console . log ( "🕐 Hora actual UTC:" , nowUTC . toISOString ( ) ) ;
47- console . log ( "🕐 Hora actual Colombia:" , nowColombia . toISOString ( ) ) ;
48-
49- // Inicio del día actual en Colombia (00:00:00)
50- const startOfToday = new Date ( nowColombia ) ;
18+ const now = new Date ( ) ;
19+ const startOfToday = new Date ( now ) ;
5120 startOfToday . setHours ( 0 , 0 , 0 , 0 ) ;
5221
5322 // Inicio hace 6 días (para tener exactamente 7 días: hoy + 6 anteriores)
5423 const startDate = new Date ( startOfToday ) ;
5524 startDate . setDate ( startDate . getDate ( ) - 6 ) ;
5625
57- // Usar la hora actual como límite (no el inicio del día siguiente)
58- const endDate = nowColombia ;
59-
60- // Convertir a UTC para la API de GitHub
61- const fromDateUTC = new Date ( startDate . getTime ( ) - COLOMBIA_OFFSET_HOURS * 60 * 60 * 1000 ) ;
62- const toDateUTC = new Date ( endDate . getTime ( ) - COLOMBIA_OFFSET_HOURS * 60 * 60 * 1000 ) ;
63-
64- const fromDate = fromDateUTC . toISOString ( ) ;
65- const toDate = toDateUTC . toISOString ( ) ;
66-
67- console . log ( "📅 Rango solicitado (Colombia):" ) ;
68- console . log ( ` Desde: ${ startDate . toLocaleString ( 'es-CO' ) } (00:00)` ) ;
69- console . log ( ` Hasta: ${ endDate . toLocaleString ( 'es-CO' ) } (AHORA)` ) ;
70- console . log ( "📅 Rango enviado a API (UTC):" ) ;
71- console . log ( " Desde:" , fromDate ) ;
72- console . log ( " Hasta:" , toDate ) ;
26+ const fromDate = startDate . toISOString ( ) ;
27+ const toDate = now . toISOString ( ) ;
7328
7429 const query = `
7530 query {
@@ -105,46 +60,31 @@ export class GitHubService {
10560 }
10661
10762 const data = await response . json ( ) as GitHubResponse ;
108- const extractedData = this . extractLastSevenDays ( data , startDate , endDate ) ;
63+ const extractedData = this . extractDays ( data , startDate , 7 ) ;
10964
110- console . log ( "✅ Datos de commits por día ( 7 días) :" , extractedData ) ;
65+ console . log ( "✅ Commits últimos 7 días:" , extractedData ) ;
11166 console . log ( "📊 Total commits:" , extractedData . reduce ( ( a , b ) => a + b , 0 ) ) ;
11267 return extractedData ;
11368 } catch ( error ) {
11469 console . error ( "❌ Error fetching GitHub data:" , error ) ;
115- return [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
70+ return Array ( 7 ) . fill ( 0 ) ;
11671 }
11772 }
11873
11974 /**
12075 * Obtiene los commits de los últimos 30 días
12176 */
12277 async getLastMonthCommits ( ) : Promise < number [ ] > {
123- console . log ( "🌐 GitHubService: Iniciando petición a API (30 días)" ) ;
124- const COLOMBIA_OFFSET_HOURS = - 5 ;
125- const nowUTC = new Date ( ) ;
126- const nowColombia = new Date ( nowUTC . getTime ( ) + COLOMBIA_OFFSET_HOURS * 60 * 60 * 1000 ) ;
127- console . log ( "🕐 Hora actual Colombia:" , nowColombia . toISOString ( ) ) ;
128-
129- const startOfToday = new Date ( nowColombia ) ;
78+ const now = new Date ( ) ;
79+ const startOfToday = new Date ( now ) ;
13080 startOfToday . setHours ( 0 , 0 , 0 , 0 ) ;
13181
13282 // Inicio hace 29 días (para tener 30 días: hoy + 29 anteriores)
13383 const startDate = new Date ( startOfToday ) ;
13484 startDate . setDate ( startDate . getDate ( ) - 29 ) ;
13585
136- const endDate = nowColombia ;
137-
138- // Convertir a UTC para la API
139- const fromDateUTC = new Date ( startDate . getTime ( ) - COLOMBIA_OFFSET_HOURS * 60 * 60 * 1000 ) ;
140- const toDateUTC = new Date ( endDate . getTime ( ) - COLOMBIA_OFFSET_HOURS * 60 * 60 * 1000 ) ;
141-
142- const fromDate = fromDateUTC . toISOString ( ) ;
143- const toDate = toDateUTC . toISOString ( ) ;
144-
145- console . log ( "📅 Rango (30 días):" ) ;
146- console . log ( " Desde:" , startDate . toLocaleString ( 'es-CO' ) ) ;
147- console . log ( " Hasta:" , endDate . toLocaleString ( 'es-CO' ) ) ;
86+ const fromDate = startDate . toISOString ( ) ;
87+ const toDate = now . toISOString ( ) ;
14888
14989 const query = `
15090 query {
@@ -180,75 +120,47 @@ export class GitHubService {
180120 }
181121
182122 const data = await response . json ( ) as GitHubResponse ;
183- const extractedData = this . extractLastThirtyDays ( data , startDate , endDate ) ;
123+ const extractedData = this . extractDays ( data , startDate , 30 ) ;
184124
185- console . log ( "✅ Datos de commits ( 30 días) :" , extractedData ) ;
125+ console . log ( "✅ Commits últimos 30 días:" , extractedData ) ;
186126 console . log ( "📊 Total commits:" , extractedData . reduce ( ( a , b ) => a + b , 0 ) ) ;
187-
188127 return extractedData ;
189128 } catch ( error ) {
190129 console . error ( "❌ Error fetching GitHub data:" , error ) ;
191- // Retornar array vacío de 30 días
192130 return Array ( 30 ) . fill ( 0 ) ;
193131 }
194132 }
195133
196134 /**
197- * Extrae los últimos 30 días de la respuesta
135+ * Extrae los últimos N días de la respuesta
198136 */
199- private extractLastThirtyDays ( response : GitHubResponse , startDateColombia : Date , endDateColombia : Date ) : number [ ] {
137+ private extractDays ( response : GitHubResponse , startDate : Date , numDays : number ) : number [ ] {
200138 const calendar = response . data . viewer . contributionsCollection . contributionCalendar ;
201- const allDays : GitHubContributionDay [ ] = [ ] ;
202- calendar . weeks . forEach ( week => {
203- allDays . push ( ...week . contributionDays ) ;
204- } ) ;
205- // Construir mapa: fecha local (YYYY-MM-DD Bogotá) -> commits
206- const mapByLocalDate : Record < string , number > = { } ;
207- allDays . forEach ( day => {
208- const localKey = this . adjustToBogotaDate ( day . date ) ;
209- mapByLocalDate [ localKey ] = ( mapByLocalDate [ localKey ] || 0 ) + day . contributionCount ;
210- } ) ;
211- // Generar array de 30 días
212- const result : number [ ] = [ ] ;
213- for ( let i = 0 ; i < 30 ; i ++ ) {
214- const d = new Date ( startDateColombia ) ;
215- d . setDate ( startDateColombia . getDate ( ) + i ) ;
216- const key = this . adjustToBogotaDate ( d . toISOString ( ) ) ;
217- const count = mapByLocalDate [ key ] || 0 ;
218- result . push ( count ) ;
219- }
220- return result ;
221- }
222139
223- // Extrae los últimos 7 días de la respuesta (corrige el desfase de día)
224- private extractLastSevenDays ( response : GitHubResponse , startDateColombia : Date , endDateColombia : Date ) : number [ ] {
225- const calendar = response . data . viewer . contributionsCollection . contributionCalendar ;
140+ // Recopilar todos los días de todas las semanas
226141 const allDays : GitHubContributionDay [ ] = [ ] ;
227-
228142 calendar . weeks . forEach ( week => {
229143 allDays . push ( ...week . contributionDays ) ;
230144 } ) ;
231145
232- // Construir mapa: fecha local (YYYY-MM-DD Bogotá ) -> commits
233- const mapByLocalDate : Record < string , number > = { } ;
146+ // Crear mapa: fecha (YYYY-MM-DD) -> cantidad de commits
147+ const commitsByDate : Record < string , number > = { } ;
234148 allDays . forEach ( day => {
235- const localKey = this . adjustToBogotaDate ( day . date ) ;
236- mapByLocalDate [ localKey ] = ( mapByLocalDate [ localKey ] || 0 ) + day . contributionCount ;
149+ commitsByDate [ day . date ] = day . contributionCount ;
237150 } ) ;
238151
239- console . log ( "🔁 Mapa de commits por fecha (Bogotá):" , mapByLocalDate ) ;
240-
241- // Generar array en orden desde startDateColombia hasta endDateColombia (7 días)
152+ // Generar array con las fechas esperadas
242153 const result : number [ ] = [ ] ;
243- for ( let i = 0 ; i < 7 ; i ++ ) {
244- const d = new Date ( startDateColombia ) ;
245- d . setDate ( startDateColombia . getDate ( ) + i ) ;
246- const key = this . adjustToBogotaDate ( d . toISOString ( ) ) ;
247- const count = mapByLocalDate [ key ] || 0 ;
248- console . log ( `📆 Día ${ i } : ${ key } -> ${ count } ` ) ;
154+ for ( let i = 0 ; i < numDays ; i ++ ) {
155+ const date = new Date ( startDate ) ;
156+ date . setDate ( startDate . getDate ( ) + i ) ;
157+
158+ // Formato YYYY-MM-DD
159+ const dateKey = date . toISOString ( ) . split ( 'T' ) [ 0 ] ;
160+ const count = commitsByDate [ dateKey ] || 0 ;
249161 result . push ( count ) ;
250162 }
251163
252164 return result ;
253165 }
254- }
166+ }
0 commit comments