statping/integrators/csv_file.go

133 lines
4.0 KiB
Go
Raw Normal View History

2020-01-03 06:10:25 +00:00
// Statping
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statping
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2020-03-04 10:29:00 +00:00
package integrators
2020-01-03 06:10:25 +00:00
import (
2020-01-04 02:03:59 +00:00
"bytes"
"encoding/csv"
2020-01-03 06:10:25 +00:00
"errors"
"fmt"
2020-03-04 10:29:00 +00:00
"github.com/hunterlong/statping/types/integrations"
"github.com/hunterlong/statping/types/null"
"github.com/hunterlong/statping/types/services"
2020-01-03 06:10:25 +00:00
"github.com/hunterlong/statping/utils"
"strconv"
"time"
)
const requiredSize = 17
type csvIntegration struct {
2020-03-04 10:29:00 +00:00
*integrations.Integration
2020-01-03 06:10:25 +00:00
}
2020-03-04 10:29:00 +00:00
var CsvIntegrator = &csvIntegration{&integrations.Integration{
2020-01-03 06:10:25 +00:00
ShortName: "csv",
Name: "CSV File",
2020-01-03 07:57:42 +00:00
Icon: "<i class=\"fas fa-file-csv\"></i>",
2020-01-04 02:03:59 +00:00
Description: "Import multiple services from a CSV file. Please have your CSV file formatted with the correct amount of columns based on the <a href=\"https://raw.githubusercontent.com/hunterlong/statping/master/source/tmpl/bulk_import.csv\">example file on Github</a>.",
2020-03-04 10:29:00 +00:00
Fields: []*integrations.IntegrationField{
2020-01-03 06:10:25 +00:00
{
2020-01-03 07:57:42 +00:00
Name: "input",
Type: "textarea",
Description: "",
2020-01-03 06:10:25 +00:00
},
},
}}
var csvData [][]string
2020-03-04 10:29:00 +00:00
func (t *csvIntegration) Get() *integrations.Integration {
2020-01-03 06:10:25 +00:00
return t.Integration
}
2020-03-04 10:29:00 +00:00
func (t *csvIntegration) List() ([]*services.Service, error) {
2020-01-03 07:57:42 +00:00
data := Value(t, "input").(string)
2020-01-04 02:03:59 +00:00
buf := bytes.NewReader([]byte(data))
r := csv.NewReader(buf)
records, err := r.ReadAll()
if err != nil {
return nil, err
2020-01-03 06:10:25 +00:00
}
2020-03-04 10:29:00 +00:00
var services []*services.Service
2020-01-04 02:03:59 +00:00
for k, v := range records[1:] {
2020-01-03 06:10:25 +00:00
s, err := commaToService(v)
if err != nil {
2020-01-04 02:03:59 +00:00
log.Errorf("error on line %v: %v", k, err)
continue
2020-01-03 06:10:25 +00:00
}
services = append(services, s)
}
return services, nil
}
// commaToService will convert a CSV comma delimited string slice to a Service type
// this function is used for the bulk import services feature
2020-03-04 10:29:00 +00:00
func commaToService(s []string) (*services.Service, error) {
2020-01-03 06:10:25 +00:00
if len(s) != requiredSize {
err := fmt.Errorf("file has %v columns of data, not the expected amount of %v columns for a service", len(s), requiredSize)
return nil, err
}
interval, err := time.ParseDuration(s[4])
if err != nil {
return nil, errors.New("could not parse internal duration: " + s[4])
}
timeout, err := time.ParseDuration(s[9])
if err != nil {
return nil, errors.New("could not parse timeout duration: " + s[9])
}
allowNotifications, err := strconv.ParseBool(s[11])
if err != nil {
return nil, errors.New("could not parse allow notifications boolean: " + s[11])
}
public, err := strconv.ParseBool(s[12])
if err != nil {
return nil, errors.New("could not parse public boolean: " + s[12])
}
verifySsl, err := strconv.ParseBool(s[16])
if err != nil {
return nil, errors.New("could not parse verifiy SSL boolean: " + s[16])
}
2020-03-04 10:29:00 +00:00
newService := &services.Service{
2020-01-03 06:10:25 +00:00
Name: s[0],
Domain: s[1],
2020-03-04 10:29:00 +00:00
Expected: null.NewNullString(s[2]),
2020-01-03 06:10:25 +00:00
ExpectedStatus: int(utils.ToInt(s[3])),
Interval: int(utils.ToInt(interval.Seconds())),
Type: s[5],
Method: s[6],
2020-03-04 10:29:00 +00:00
PostData: null.NewNullString(s[7]),
2020-01-03 06:10:25 +00:00
Port: int(utils.ToInt(s[8])),
Timeout: int(utils.ToInt(timeout.Seconds())),
2020-03-04 10:29:00 +00:00
AllowNotifications: null.NewNullBool(allowNotifications),
Public: null.NewNullBool(public),
2020-01-03 06:10:25 +00:00
GroupId: int(utils.ToInt(s[13])),
2020-03-04 10:29:00 +00:00
Headers: null.NewNullString(s[14]),
Permalink: null.NewNullString(s[15]),
VerifySSL: null.NewNullBool(verifySsl),
2020-01-03 06:10:25 +00:00
}
return newService, nil
}