From dad6a303d4cbd3d35560284bd1c3f25ba9862629 Mon Sep 17 00:00:00 2001 From: Jakab Zeller Date: Wed, 19 Apr 2023 11:17:07 +0100 Subject: [PATCH] Fixed issue with started and error email templates from #2 - Added the CachedFieldTypeFromName function which returns the CachedFieldType from the given name (19/04/2023 - 11:15:26) - Added the ScoutState.GetIterableCachedFieldFromName + ScoutState.GetCachedFieldFromName methods to ScoutScate that utilise the CachedFieldTypeFromName function (19/04/2023 - 11:16:09) - Updated the started and error email templates to use these new methods rather than passing in the underlying integer representing a CachedFieldType (19/04/2023 - 11:16:56) --- cached_fields.go | 20 ++++++++++++++++++++ email/templates/error.txt | 8 ++++---- email/templates/started.txt | 6 +++--- state.go | 12 ++++++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/cached_fields.go b/cached_fields.go index 448d18d..731528e 100644 --- a/cached_fields.go +++ b/cached_fields.go @@ -1057,3 +1057,23 @@ func (cft CachedFieldType) Types() []CachedFieldType { StateType, } } + +// CachedFieldTypeFromName returns the CachedFieldType whose name is the given string. This is a case-insensitive match. +func CachedFieldTypeFromName(name string) CachedFieldType { + switch strings.ToLower(name) { + case "usertweettimestype": + return UserTweetTimesType + case "reddituserposttimestype": + return RedditUserPostTimesType + case "developersnapshotstype": + return DeveloperSnapshotsType + case "redditdevelopersnapshotstype": + return RedditDeveloperSnapshotsType + case "gameidstype": + return GameIDsType + case "deleteddeveloperstype": + return DeletedDevelopersType + default: + return StateType + } +} diff --git a/email/templates/error.txt b/email/templates/error.txt index 1f602a7..70f469f 100644 --- a/email/templates/error.txt +++ b/email/templates/error.txt @@ -1,7 +1,7 @@ -{{ $start := mustGet (.State.GetCachedField 4) "Start" }} -{{ $batchSize := mustGet (.State.GetCachedField 4) "BatchSize" }} -{{ $discoveryTweets := mustGet (.State.GetCachedField 4) "DiscoveryTweets" }} -{{ $phase := mustGet (.State.GetCachedField 4) "Phase" }} +{{ $start := mustGet (.State.GetCachedFieldFromName "State") "Start" }} +{{ $batchSize := mustGet (.State.GetCachedFieldFromName "State") "BatchSize" }} +{{ $discoveryTweets := mustGet (.State.GetCachedFieldFromName "State") "DiscoveryTweets" }} +{{ $phase := mustGet (.State.GetCachedFieldFromName "State") "Phase" }} Robo-scout has encountered an error during the Scout procedure which started at {{ stamp $start }} with batchSize = {{ $batchSize }}, and discoveryTweets = {{ $discoveryTweets }}. The error occurred in the {{ $phase.String }} phase at {{ stamp .Time }}: diff --git a/email/templates/started.txt b/email/templates/started.txt index 48bf329..8ecf23e 100644 --- a/email/templates/started.txt +++ b/email/templates/started.txt @@ -1,6 +1,6 @@ -{{ $start := mustGet (.State.GetCachedField 4) "Start" }} -{{ $batchSize := mustGet (.State.GetCachedField 4) "BatchSize" }} -{{ $discoveryTweets := mustGet (.State.GetCachedField 4) "DiscoveryTweets" }} +{{ $start := mustGet (.State.GetCachedFieldFromName "State") "Start" }} +{{ $batchSize := mustGet (.State.GetCachedFieldFromName "State") "BatchSize" }} +{{ $discoveryTweets := mustGet (.State.GetCachedFieldFromName "State") "DiscoveryTweets" }} Robo-scout has started the Scout procedure at {{ stamp $start }} with batchSize = {{ $batchSize }}, and discoveryTweets = {{ $discoveryTweets }}. {{ if .State.Loaded }}Current state was loaded from disk:{{ else }}Current state was just created, so we are probably starting from scratch today:{{ end }} diff --git a/state.go b/state.go index 641731d..7faef27 100644 --- a/state.go +++ b/state.go @@ -76,6 +76,12 @@ func (state *ScoutState) GetCachedField(fieldType CachedFieldType) CachedField { return state.cachedFields[fieldType] } +// GetCachedFieldFromName will return the CachedField of the given name of a CachedFieldType using +// CachedFieldTypeFromName. +func (state *ScoutState) GetCachedFieldFromName(fieldTypeName string) CachedField { + return state.cachedFields[CachedFieldTypeFromName(fieldTypeName)] +} + // GetIterableCachedFields will return a mapping of all the CachedField that are IterableCachedField. The mapping will // have keys that are CachedFieldType. func (state *ScoutState) GetIterableCachedFields() map[CachedFieldType]IterableCachedField { @@ -93,6 +99,12 @@ func (state *ScoutState) GetIterableCachedField(fieldType CachedFieldType) Itera return state.GetCachedField(fieldType).(IterableCachedField) } +// GetIterableCachedFieldFromName will return the IterableCachedField of the given name of a CachedFieldType using +// CachedFieldTypeFromName. +func (state *ScoutState) GetIterableCachedFieldFromName(fieldTypeName string) IterableCachedField { + return state.GetCachedFieldFromName(fieldTypeName).(IterableCachedField) +} + // MergeIterableCachedFields will merge each IterableCachedField in the given ScoutState into the referred to ScoutState // using the IterableCachedField.Merge method. func (state *ScoutState) MergeIterableCachedFields(stateToMerge *ScoutState) {