dataset.go raw

   1  package dataset
   2  
   3  import (
   4  	"bytes"
   5  	"encoding/json"
   6  	"time"
   7  )
   8  
   9  // ExportType is a string enum
  10  type ExportType string
  11  
  12  const (
  13  	ExportTypeCSV  = ExportType("csv")
  14  	ExportTypeJSON = ExportType("json")
  15  	ExportTypeXLSX = ExportType("xlsx")
  16  )
  17  
  18  // Dataset wraps an NS1 /datasets resource
  19  type Dataset struct {
  20  	ID              string        `json:"id,omitempty"`
  21  	Name            string        `json:"name,omitempty"`
  22  	Datatype        *Datatype     `json:"datatype,omitempty"`
  23  	Repeat          *Repeat       `json:"repeat,omitempty"`
  24  	Timeframe       *Timeframe    `json:"timeframe,omitempty"`
  25  	ExportType      ExportType    `json:"export_type,omitempty"`
  26  	Reports         []*Report     `json:"reports,omitempty"`
  27  	RecipientEmails []string      `json:"recipient_emails,omitempty"`
  28  	CreatedAt       UnixTimestamp `json:"created_at,omitempty"`
  29  	UpdatedAt       UnixTimestamp `json:"updated_at,omitempty"`
  30  }
  31  
  32  type DatatypeType string
  33  
  34  const (
  35  	DatatypeTypeNumQueries   = DatatypeType("num_queries")
  36  	DatatypeTypeEBOTResponse = DatatypeType("num_ebot_response")
  37  	DatatypeTypeNXDResponse  = DatatypeType("num_nxd_response")
  38  	DatatypeTypeZeroQueries  = DatatypeType("zero_queries")
  39  )
  40  
  41  type DatatypeScope string
  42  
  43  const (
  44  	DatatypeScopeAccount       = DatatypeScope("account")
  45  	DatatypeScopeNetworkSingle = DatatypeScope("network_single")
  46  	DatatypeScopeRecordSingle  = DatatypeScope("record_single")
  47  	DatatypeScopeZoneSingle    = DatatypeScope("zone_single")
  48  	DatatypeScopeNetworkEach   = DatatypeScope("network_each")
  49  	DatatypeScopeRecordEach    = DatatypeScope("record_each")
  50  	DatatypeScopeZoneEach      = DatatypeScope("zone_each")
  51  	DatatypeScopeTopNZones     = DatatypeScope("top_n_zones")
  52  	DatatypeScopeTopNRecords   = DatatypeScope("top_n_records")
  53  )
  54  
  55  // Datatype wraps Dataset's "Datatype" attribute
  56  type Datatype struct {
  57  	Type  DatatypeType      `json:"type,omitempty"`
  58  	Scope DatatypeScope     `json:"scope,omitempty"`
  59  	Data  map[string]string `json:"data,omitempty"`
  60  }
  61  
  62  // RepeatsEvery is a string enum
  63  type RepeatsEvery string
  64  
  65  const (
  66  	RepeatsEveryWeek  = RepeatsEvery("week")
  67  	RepeatsEveryMonth = RepeatsEvery("month")
  68  	RepeatsEveryYear  = RepeatsEvery("year")
  69  )
  70  
  71  // Repeat wraps Dataset's "Repeat" attribute
  72  type Repeat struct {
  73  	Start        UnixTimestamp `json:"start,omitempty"`
  74  	RepeatsEvery RepeatsEvery  `json:"repeats_every,omitempty"`
  75  	EndAfterN    int32         `json:"end_after_n,omitempty"`
  76  }
  77  
  78  // TimeframeAggregation is a string enum
  79  type TimeframeAggregation string
  80  
  81  const (
  82  	TimeframeAggregationDaily         = TimeframeAggregation("daily")
  83  	TimeframeAggregationMontly        = TimeframeAggregation("monthly")
  84  	TimeframeAggregationBillingPeriod = TimeframeAggregation("billing_period")
  85  )
  86  
  87  // Timeframe wraps Dataset's "Timeframe" attribute
  88  type Timeframe struct {
  89  	Aggregation TimeframeAggregation `json:"aggregation,omitempty"`
  90  	Cycles      *int32               `json:"cycles,omitempty"`
  91  	From        *UnixTimestamp       `json:"from,omitempty"`
  92  	To          *UnixTimestamp       `json:"to,omitempty"`
  93  }
  94  
  95  // ReportStatus is a string enum
  96  type ReportStatus string
  97  
  98  const (
  99  	ReportStatusQueued     = ReportStatus("queued")
 100  	ReportStatusGenerating = ReportStatus("generating")
 101  	ReportStatusAvailable  = ReportStatus("available")
 102  	ReportStatusFailed     = ReportStatus("failed")
 103  )
 104  
 105  // Report wraps Dataset's "Report" attribute
 106  type Report struct {
 107  	ID        string        `json:"id,omitempty"`
 108  	Status    ReportStatus  `json:"status,omitempty"`
 109  	Start     UnixTimestamp `json:"start,omitempty"`
 110  	End       UnixTimestamp `json:"end,omitempty"`
 111  	CreatedAt UnixTimestamp `json:"created_at,omitempty"`
 112  }
 113  
 114  // NewDataset takes the properties for a Dataset and creates a new instance
 115  func NewDataset(
 116  	id string,
 117  	name string,
 118  	datatype *Datatype,
 119  	repeat *Repeat,
 120  	timeframe *Timeframe,
 121  	exportType ExportType,
 122  	reports []*Report,
 123  	recipientEmails []string,
 124  	createdAt UnixTimestamp,
 125  	updatedAt UnixTimestamp,
 126  ) *Dataset {
 127  	return &Dataset{
 128  		ID:              id,
 129  		Name:            name,
 130  		Datatype:        datatype,
 131  		Repeat:          repeat,
 132  		Timeframe:       timeframe,
 133  		ExportType:      exportType,
 134  		Reports:         reports,
 135  		RecipientEmails: recipientEmails,
 136  		CreatedAt:       createdAt,
 137  		UpdatedAt:       updatedAt,
 138  	}
 139  }
 140  
 141  // NewDatatype takes the properties for a Datatype and creates a new instance
 142  func NewDatatype(
 143  	dtype DatatypeType,
 144  	scope DatatypeScope,
 145  	data map[string]string,
 146  ) *Datatype {
 147  	return &Datatype{
 148  		Type:  dtype,
 149  		Scope: scope,
 150  		Data:  data,
 151  	}
 152  }
 153  
 154  // NewRepeat takes the properties for a Repeat and creates a new instance
 155  func NewRepeat(
 156  	start UnixTimestamp,
 157  	repeatsEvery RepeatsEvery,
 158  	endAfterN int32,
 159  ) *Repeat {
 160  	return &Repeat{
 161  		Start:        start,
 162  		RepeatsEvery: repeatsEvery,
 163  		EndAfterN:    endAfterN,
 164  	}
 165  }
 166  
 167  // NewTimeframe takes the properties for a Timeframe and creates a new instance
 168  func NewTimeframe(
 169  	aggregation TimeframeAggregation,
 170  	cycles *int32,
 171  	from *UnixTimestamp,
 172  	to *UnixTimestamp,
 173  ) *Timeframe {
 174  	return &Timeframe{
 175  		Aggregation: aggregation,
 176  		Cycles:      cycles,
 177  		From:        from,
 178  		To:          to,
 179  	}
 180  }
 181  
 182  // NewReport takes the properties for a Report and creates a new instance
 183  func NewReport(
 184  	id string,
 185  	status ReportStatus,
 186  	start UnixTimestamp,
 187  	end UnixTimestamp,
 188  	createdAt UnixTimestamp,
 189  ) *Report {
 190  	return &Report{
 191  		ID:        id,
 192  		Status:    status,
 193  		Start:     start,
 194  		End:       end,
 195  		CreatedAt: createdAt,
 196  	}
 197  }
 198  
 199  // UnixTimestamp represents a timestamp field that comes as a string-based unix timestamp
 200  type UnixTimestamp time.Time
 201  
 202  func (ut *UnixTimestamp) UnmarshalJSON(data []byte) error {
 203  	var unix int64
 204  	data = bytes.Replace(data, []byte(`"`), []byte(""), -1)
 205  	if err := json.Unmarshal(data, &unix); err != nil {
 206  		return err
 207  	}
 208  	*ut = UnixTimestamp(time.Unix(unix, 0))
 209  	return nil
 210  }
 211  
 212  func (ut *UnixTimestamp) MarshalJSON() ([]byte, error) {
 213  	return json.Marshal(time.Time(*ut).Unix())
 214  }
 215