github.com/Financial-Times/publish-availability-monitor@v1.12.0/checks/preCheck.go (about)

     1  package checks
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/Financial-Times/go-logger/v2"
     7  	"github.com/Financial-Times/publish-availability-monitor/config"
     8  	"github.com/Financial-Times/publish-availability-monitor/content"
     9  	"github.com/Financial-Times/publish-availability-monitor/envs"
    10  	"github.com/Financial-Times/publish-availability-monitor/metrics"
    11  )
    12  
    13  type PreCheck func(
    14  	publishedContent content.Content,
    15  	tid string,
    16  	publishDate time.Time,
    17  	appConfig *config.AppConfig,
    18  	metricContainer *metrics.History,
    19  	environments *envs.Environments,
    20  	log *logger.UPPLogger,
    21  ) (bool, *SchedulerParam)
    22  
    23  func MainPreChecks() []PreCheck {
    24  	return []PreCheck{mainPreCheck}
    25  }
    26  
    27  func mainPreCheck(
    28  	publishedContent content.Content,
    29  	tid string,
    30  	publishDate time.Time,
    31  	appConfig *config.AppConfig,
    32  	metricContainer *metrics.History,
    33  	environments *envs.Environments,
    34  	log *logger.UPPLogger,
    35  ) (bool, *SchedulerParam) {
    36  	uuid := publishedContent.GetUUID()
    37  	validationEndpointKey := publishedContent.GetType()
    38  	var validationEndpoint string
    39  	var found bool
    40  	var username string
    41  	var password string
    42  
    43  	if validationEndpoint, found = appConfig.ValidationEndpoints[validationEndpointKey]; found {
    44  		username, password = envs.GetValidationCredentials()
    45  	}
    46  
    47  	logEntry := log.WithUUID(uuid).WithTransactionID(tid)
    48  
    49  	valRes := publishedContent.Validate(validationEndpoint, tid, username, password, log)
    50  	if !valRes.IsValid {
    51  		logEntry.Info("Message is INVALID, skipping...")
    52  		return false, nil
    53  	}
    54  
    55  	logEntry.Info("Message is VALID.")
    56  
    57  	if isMessagePastPublishSLA(publishDate, appConfig.Threshold) {
    58  		logEntry.Info("Message is past publish SLA, skipping.")
    59  		return false, nil
    60  	}
    61  
    62  	return true, &SchedulerParam{
    63  		contentToCheck:  publishedContent,
    64  		publishDate:     publishDate,
    65  		tid:             tid,
    66  		isMarkedDeleted: valRes.IsMarkedDeleted,
    67  		metricContainer: metricContainer,
    68  		environments:    environments,
    69  	}
    70  }
    71  
    72  func isMessagePastPublishSLA(date time.Time, threshold int) bool {
    73  	passedSLA := date.Add(time.Duration(threshold) * time.Second)
    74  	return time.Now().After(passedSLA)
    75  }