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/>.
package integrations
import (
2020-01-04 02:03:59 +00:00
"bytes"
"encoding/csv"
2020-01-03 06:10:25 +00:00
"errors"
"fmt"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
"strconv"
"time"
)
const requiredSize = 17
type csvIntegration struct {
* types . Integration
}
var csvIntegrator = & csvIntegration { & types . Integration {
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-01-03 06:10:25 +00:00
Fields : [ ] * types . IntegrationField {
{
2020-01-03 07:57:42 +00:00
Name : "input" ,
Type : "textarea" ,
Description : "" ,
2020-01-03 06:10:25 +00:00
} ,
} ,
} }
var csvData [ ] [ ] string
func ( t * csvIntegration ) Get ( ) * types . Integration {
return t . Integration
}
func ( t * csvIntegration ) List ( ) ( [ ] * types . 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
}
var services [ ] * types . 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
func commaToService ( s [ ] string ) ( * types . Service , error ) {
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 ] )
}
newService := & types . Service {
Name : s [ 0 ] ,
Domain : s [ 1 ] ,
Expected : types . NewNullString ( s [ 2 ] ) ,
ExpectedStatus : int ( utils . ToInt ( s [ 3 ] ) ) ,
Interval : int ( utils . ToInt ( interval . Seconds ( ) ) ) ,
Type : s [ 5 ] ,
Method : s [ 6 ] ,
PostData : types . NewNullString ( s [ 7 ] ) ,
Port : int ( utils . ToInt ( s [ 8 ] ) ) ,
Timeout : int ( utils . ToInt ( timeout . Seconds ( ) ) ) ,
AllowNotifications : types . NewNullBool ( allowNotifications ) ,
Public : types . NewNullBool ( public ) ,
GroupId : int ( utils . ToInt ( s [ 13 ] ) ) ,
Headers : types . NewNullString ( s [ 14 ] ) ,
Permalink : types . NewNullString ( s [ 15 ] ) ,
VerifySSL : types . NewNullBool ( verifySsl ) ,
}
return newService , nil
}