github.com/bcampbell/scrapeomat@v0.0.0-20220820232205-23e64141c89e/store/filter.go (about)

     1  package store
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  )
     8  
     9  // Filter is a set of params for querying the store
    10  type Filter struct {
    11  	// date ranges are [from,to)
    12  	PubFrom   time.Time
    13  	PubTo     time.Time
    14  	AddedFrom time.Time
    15  	AddedTo   time.Time
    16  	// if empty, accept all publications (else only ones in list)
    17  	PubCodes []string
    18  	// exclude any publications in XPubCodes
    19  	XPubCodes []string
    20  	// Only return articles with ID > SinceID
    21  	SinceID int
    22  	// max number of articles wanted
    23  	Count int
    24  }
    25  
    26  // Describe returns a concise description of the filter for logging/debugging/whatever
    27  func (filt *Filter) Describe() string {
    28  	s := "[ "
    29  
    30  	if !filt.PubFrom.IsZero() && !filt.PubTo.IsZero() {
    31  		s += fmt.Sprintf("pub %s..%s ", filt.PubFrom.Format(time.RFC3339), filt.PubTo.Format(time.RFC3339))
    32  	} else if !filt.PubFrom.IsZero() {
    33  		s += fmt.Sprintf("pub %s.. ", filt.PubFrom.Format(time.RFC3339))
    34  	} else if !filt.PubTo.IsZero() {
    35  		s += fmt.Sprintf("pub ..%s ", filt.PubTo.Format(time.RFC3339))
    36  	}
    37  
    38  	if !filt.AddedFrom.IsZero() && !filt.AddedTo.IsZero() {
    39  		s += fmt.Sprintf("added %s..%s ", filt.AddedFrom.Format(time.RFC3339), filt.AddedTo.Format(time.RFC3339))
    40  	} else if !filt.AddedFrom.IsZero() {
    41  		s += fmt.Sprintf("added %s.. ", filt.AddedFrom.Format(time.RFC3339))
    42  	} else if !filt.AddedTo.IsZero() {
    43  		s += fmt.Sprintf("added ..%s ", filt.AddedTo.Format(time.RFC3339))
    44  	}
    45  
    46  	if len(filt.PubCodes) > 0 {
    47  		s += strings.Join(filt.PubCodes, "|") + " "
    48  	}
    49  
    50  	if len(filt.XPubCodes) > 0 {
    51  		foo := make([]string, len(filt.XPubCodes))
    52  		for i, x := range filt.XPubCodes {
    53  			foo[i] = "!" + x
    54  		}
    55  		s += strings.Join(foo, "|") + " "
    56  	}
    57  
    58  	if filt.Count > 0 {
    59  		s += fmt.Sprintf("cnt %d ", filt.Count)
    60  	}
    61  	if filt.SinceID > 0 {
    62  		s += fmt.Sprintf("since %d ", filt.SinceID)
    63  	}
    64  
    65  	s += "]"
    66  	return s
    67  }