<commit_before>// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Contact Group API support - Fetch, Create, Update, Delete, and Search
// See: https://login.circonus.com/resources/api/calls/contact_group

package api

import (
	"encoding/json"
	"fmt"
	"net/url"
	"regexp"
)

// ContactGroupAlertFormats define alert formats
type ContactGroupAlertFormats struct {
	LongMessage  string `json:"long_message"`
	LongSubject  string `json:"long_subject"`
	LongSummary  string `json:"long_summary"`
	ShortMessage string `json:"short_message"`
	ShortSummary string `json:"short_summary"`
}

// ContactGroupContactsExternal external contacts
type ContactGroupContactsExternal struct {
	Info   string `json:"contact_info"`
	Method string `json:"method"`
}

// ContactGroupContactsUser user contacts
type ContactGroupContactsUser struct {
	Info    string `json:"_contact_info,omitempty"`
	Method  string `json:"method"`
	UserCID string `json:"user"`
}

// ContactGroupContacts list of contacts
type ContactGroupContacts struct {
	External []ContactGroupContactsExternal `json:"external"`
	Users    []ContactGroupContactsUser     `json:"users"`
}

// ContactGroupEscalation defines escalations for severity levels
type ContactGroupEscalation struct {
	After           uint   `json:"after"`
	ContactGroupCID string `json:"contact_group"`
}

// ContactGroup defines a contactGroup
type ContactGroup struct {
	CID               string                   `json:"_cid,omitempty"`
	LastModified      uint                     `json:"_last_modified,omitempty"`
	LastModfiedBy     string                   `json:"_last_modified_by,omitempty"`
	AggregationWindow uint                     `json:"aggregation_window"`
	AlertFormats      ContactGroupAlertFormats `json:"alert_formats"`
	Contacts          ContactGroupContacts     `json:"contacts"`
	Escalations       []ContactGroupEscalation `json:"escalations"`
	Name              string                   `json:"name"`
	Reminders         []uint                   `json:"reminders"`
	Tags              []string                 `json:"tags"`
}

const (
	baseContactGroupPath = "/contact_group"
	contactGroupCIDRegex = "^" + baseContactGroupPath + "/[0-9]+$"
)

// FetchContactGroup retrieves a contact group definition
func (a *API) FetchContactGroup(cid CIDType) (*ContactGroup, error) {
	if cid == nil || *cid == "" {
		return nil, fmt.Errorf("Invalid contact group CID [none]")
	}

	groupCID := string(*cid)

	if matched, err := regexp.MatchString(contactGroupCIDRegex, groupCID); err != nil {
		return nil, err
	} else if !matched {
		return nil, fmt.Errorf("Invalid contact group CID [%s]", groupCID)
	}

	result, err := a.Get(groupCID)
	if err != nil {
		return nil, err
	}

	group := new(ContactGroup)<commit_msg>upd: add NewContactGroup function for sean<commit_after>// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Contact Group API support - Fetch, Create, Update, Delete, and Search
// See: https://login.circonus.com/resources/api/calls/contact_group

package api

import (
	"encoding/json"
	"fmt"
	"net/url"
	"regexp"
)

// ContactGroupAlertFormats define alert formats
type ContactGroupAlertFormats struct {
	LongMessage  string `json:"long_message"`
	LongSubject  string `json:"long_subject"`
	LongSummary  string `json:"long_summary"`
	ShortMessage string `json:"short_message"`
	ShortSummary string `json:"short_summary"`
}

// ContactGroupContactsExternal external contacts
type ContactGroupContactsExternal struct {
	Info   string `json:"contact_info"`
	Method string `json:"method"`
}

// ContactGroupContactsUser user contacts
type ContactGroupContactsUser struct {
	Info    string `json:"_contact_info,omitempty"`
	Method  string `json:"method"`
	UserCID string `json:"user"`
}

// ContactGroupContacts list of contacts
type ContactGroupContacts struct {
	External []ContactGroupContactsExternal `json:"external"`
	Users    []ContactGroupContactsUser     `json:"users"`
}

// ContactGroupEscalation defines escalations for severity levels
type ContactGroupEscalation struct {
	After           uint   `json:"after"`
	ContactGroupCID string `json:"contact_group"`
}

// ContactGroup defines a contactGroup
type ContactGroup struct {
	CID               string                   `json:"_cid,omitempty"`
	LastModified      uint                     `json:"_last_modified,omitempty"`
	LastModfiedBy     string                   `json:"_last_modified_by,omitempty"`
	AggregationWindow uint                     `json:"aggregation_window"`
	AlertFormats      ContactGroupAlertFormats `json:"alert_formats"`
	Contacts          ContactGroupContacts     `json:"contacts"`
	Escalations       []ContactGroupEscalation `json:"escalations"`
	Name              string                   `json:"name"`
	Reminders         []uint                   `json:"reminders"`
	Tags              []string                 `json:"tags"`
}

const (
	baseContactGroupPath = "/contact_group"
	contactGroupCIDRegex = "^" + baseContactGroupPath + "/[0-9]+$"
	numSeverityLevels    = 5
)

// NewContactGroup returns a ContactGroup
func (a *API) NewContactGroup() *ContactGroup {
	return &ContactGroup{
		Escalations: make([]ContactGroupEscalation, numSeverityLevels),
	}
}

// FetchContactGroup retrieves a contact group definition
func (a *API) FetchContactGroup(cid CIDType) (*ContactGroup, error) {
	if cid == nil || *cid == "" {
		return nil, fmt.Errorf("Invalid contact group CID [none]")
	}

	groupCID := string(*cid)

	if matched, err := regexp.MatchString(contactGroupCIDRegex, groupCID); err != nil {
		return nil, err
	} else if !matched {
		return nil, fmt.Errorf("Invalid contact group CID [%s]", groupCID)
	}

	result, err := a.Get(groupCID)
	if err != nil {
		return nil, err
	}

	group := new(ContactGroup)
{'fraction_non_alphanumeric': 0.10450268817204302, 'fraction_numerical': 0.0021841397849462364, 'mean_word_length': 3.65078125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2316237', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1718, 'n_words': 468}