33using jcdcdev . Umbraco . ReadingTime . Core . Models ;
44using jcdcdev . Umbraco . ReadingTime . Core . PropertyEditors ;
55using jcdcdev . Umbraco . ReadingTime . Infrastructure . Persistence ;
6+ using Microsoft . Extensions . Logging ;
67using Umbraco . Cms . Core . Models ;
78using Umbraco . Cms . Core . Services ;
89using Umbraco . Extensions ;
@@ -15,29 +16,38 @@ public class ReadingTimeService : IReadingTimeService
1516 private readonly ReadingTimeValueProviderCollection _convertors ;
1617 private readonly IDataTypeService _dataTypeService ;
1718 private readonly IReadingTimeRepository _readingTimeRepository ;
19+ private readonly ILogger _logger ;
1820
1921 public ReadingTimeService (
2022 IContentService contentService ,
2123 ReadingTimeValueProviderCollection convertors ,
2224 IReadingTimeRepository readingTimeRepository ,
23- IDataTypeService dataTypeService )
25+ IDataTypeService dataTypeService ,
26+ ILogger < ReadingTimeService > logger )
2427 {
2528 _contentService = contentService ;
2629 _convertors = convertors ;
2730 _readingTimeRepository = readingTimeRepository ;
2831 _dataTypeService = dataTypeService ;
32+ _logger = logger ;
2933 }
3034
3135 public async Task < ReadingTimeDto ? > GetAsync ( Guid key , Guid dataTypeKey ) => await _readingTimeRepository . Get ( key , dataTypeKey ) ;
36+
3237 public async Task < ReadingTimeDto ? > GetAsync ( Guid key , int dataTypeId ) => await _readingTimeRepository . Get ( key , dataTypeId ) ;
3338
34- public async Task < int > DeleteAsync ( Guid key ) => await _readingTimeRepository . DeleteAsync ( key ) ;
39+ public async Task < int > DeleteAsync ( Guid key )
40+ {
41+ _logger . LogDebug ( "Deleting reading time for {Key}" , key ) ;
42+ return await _readingTimeRepository . DeleteAsync ( key ) ;
43+ }
3544
3645 public async Task ScanTree ( int homeId )
3746 {
3847 var content = _contentService . GetById ( homeId ) ;
3948 if ( content == null )
4049 {
50+ _logger . LogWarning ( "Content with id {HomeId} not found" , homeId ) ;
4151 return ;
4252 }
4353
@@ -63,7 +73,20 @@ public async Task ScanTree(int homeId)
6373 moreRecords = ( page + 1 ) * 100 <= totalRecords ;
6474 }
6575
66- await Process ( current ) ;
76+ if ( current . Published )
77+ {
78+ await Process ( current ) ;
79+ }
80+ }
81+ }
82+
83+ public async Task ScanAll ( )
84+ {
85+ var root = _contentService . GetRootContent ( ) . ToList ( ) ;
86+ _logger . LogInformation ( "Scanning {Count} root content items" , root . Count ) ;
87+ foreach ( var content in root )
88+ {
89+ await ScanTree ( content . Id ) ;
6790 }
6891 }
6992
@@ -75,6 +98,7 @@ public async Task Process(IContent item)
7598 return ;
7699 }
77100
101+ _logger . LogDebug ( "Processing {Id}:{Item}" , item . Id , item . Name ) ;
78102 foreach ( var property in props )
79103 {
80104 await ProcessPropertyEditor ( item , property ) ;
@@ -86,12 +110,14 @@ private async Task ProcessPropertyEditor(IContent item, IProperty readingTimePro
86110 var dataType = _dataTypeService . GetDataType ( readingTimeProperty . PropertyType . DataTypeId ) ;
87111 if ( dataType == null )
88112 {
113+ _logger . LogWarning ( "DataType not found for property {PropertyId}" , readingTimeProperty . Id ) ;
89114 return ;
90115 }
91116
92117 var config = dataType . ConfigurationAs < ReadingTimeConfiguration > ( ) ;
93118 if ( config == null )
94119 {
120+ _logger . LogWarning ( "Configuration not found for property {PropertyId}" , readingTimeProperty . Id ) ;
95121 return ;
96122 }
97123
@@ -100,13 +126,16 @@ private async Task ProcessPropertyEditor(IContent item, IProperty readingTimePro
100126 var propertyType = readingTimeProperty . PropertyType ;
101127 if ( propertyType . VariesByCulture ( ) )
102128 {
129+ _logger . LogDebug ( "Processing culture variants for {Id}:{Item}" , item . Id , item . Name ) ;
103130 foreach ( var culture in item . AvailableCultures )
104131 {
132+ _logger . LogDebug ( "Processing culture {Culture}" , culture ) ;
105133 var model = GetModel ( item , culture , null , config ) ;
106134 models . Add ( model ) ;
107135 }
108136 }
109137
138+ _logger . LogDebug ( "Processing invariant variant for {Id}:{Item}" , item . Id , item . Name ) ;
110139 var invariant = GetModel ( item , null , null , config ) ;
111140 models . Add ( invariant ) ;
112141
@@ -134,14 +163,24 @@ private ReadingTimeVariantDto GetModel(IContent item, string? culture, string? s
134163 foreach ( var property in item . Properties )
135164 {
136165 var convertor = _convertors . FirstOrDefault ( x => x . CanConvert ( property . PropertyType ) ) ;
166+ if ( convertor == null )
167+ {
168+ _logger . LogDebug ( "No convertor found for {PropertyId}:{PropertyEditorAlias}" , property . Id , property . PropertyType . PropertyEditorAlias ) ;
169+ continue ;
170+ }
171+
172+ _logger . LogDebug ( "Processing property {PropertyId}:{PropertyEditorAlias}" , property . Id , property . PropertyType . PropertyEditorAlias ) ;
173+
137174 var cCulture = property . PropertyType . VariesByCulture ( ) ? culture : null ;
138175 var cSegment = property . PropertyType . VariesBySegment ( ) ? segment : null ;
139176 var readingTime = convertor ? . GetReadingTime ( property , cCulture , cSegment , item . AvailableCultures , config ) ;
140177 if ( ! readingTime . HasValue )
141178 {
179+ _logger . LogDebug ( "No reading time found for {PropertyId}:{PropertyEditorAlias}" , property . Id , property . PropertyType . PropertyEditorAlias ) ;
142180 continue ;
143181 }
144182
183+ _logger . LogDebug ( "Reading time found for {PropertyId}:{PropertyEditorAlias} ({Time})" , property . Id , property . PropertyType . PropertyEditorAlias , readingTime . Value ) ;
145184 time += readingTime . Value ;
146185 }
147186
0 commit comments