Beispiel #1
0
// Index indexes a contact into elasticsearch
func index(Contact *models.Contact, s *elastic.Client) error {
	id := strconv.Itoa(int(Contact.ID))
	if id == "" {
		logs.Error("id is nil")
		return errors.New("id is nil")
	}

	if Contact.Address.Longitude == "null" || Contact.Address.Latitude == "null" {
		logs.Debug("Could not index contact %s", id)
		return nil
	}

	if Contact.Address.Latitude != "" && Contact.Address.Longitude != "" {
		Contact.Address.Location = fmt.Sprintf("%s,%s", Contact.Address.Latitude, Contact.Address.Longitude)
	}

	_, err := s.Index().
		Index("contacts").
		Type("contact").
		Id(id).
		BodyJson(Contact).
		Do()
	if err != nil {
		logs.Critical(err)
		return err
	}

	logs.Debug("Contact %s indexed", id)

	return nil
}
Beispiel #2
0
//Find the last indexed Contact's ID
func findID(s *elastic.Client) (int, error) {
	termQuery := elastic.NewMatchAllQuery()
	searchResult, err := s.Search().
		Index("contacts").
		Query(&termQuery).
		Fields("_timestamp").
		Sort("_timestamp", false).
		Size(1).
		Pretty(true).
		Do()
	if err != nil {
		logs.Critical(err)
		return 0, err
	}

	var ID int
	if searchResult.Hits != nil {
		for _, hit := range searchResult.Hits.Hits {
			ID, err = strconv.Atoi(hit.Id)
			if err != nil {
				return 0, err
			}
		}
	} else {
		logs.Debug("No results, ID is 1")
		return 1, nil
	}

	return ID, nil
}
Beispiel #3
0
func indexFact(Fact models.Fact, client *elastic.Client) error {
	if Fact.Contact.Address.Latitude != "" && Fact.Contact.Address.Longitude != "" {
		Fact.Contact.Address.Location = fmt.Sprintf("%s,%s", Fact.Contact.Address.Latitude, Fact.Contact.Address.Longitude)
	}
	_, err := client.Index().
		Index("facts").
		Type("fact").
		BodyJson(Fact).
		Do()
	if err != nil {
		return err
	}

	return nil
}
Beispiel #4
0
// Index indexes a contact into elasticsearch
func index(Contact *models.Contact, s *elastic.Client) error {
	id := strconv.Itoa(int(Contact.ID))
	if id == "" {
		logs.Error("id is nil")
		return errors.New("id is nil")
	}

	_, err := s.Index().
		Index("contacts").
		Type("contact").
		Id(id).
		BodyJson(Contact).
		Do()
	if err != nil {
		logs.Critical(err)
		return err
	}

	logs.Debug("Indexed")

	return nil
}
Beispiel #5
0
func script(ctx *cli.Context) error {
	var err error
	var config settings.Config
	if ctx.String("config") != "" {
		config, err = settings.Parse(ctx.String("config"))
		if err != nil {
			logs.Error(err)
		}
	}

	logs.Level(logs.DebugLevel)

	dialect, args, err := config.SqlDB()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	logs.Debug("database type: %s", dialect)

	var db *gorm.DB
	if db, err = databases.InitGORM(dialect, args); err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	logs.Debug("connected to %s", args)

	if config.Migrate() {
		db.AutoMigrate(models.Models()...)
		logs.Debug("database migrated successfully")
	}

	db.LogMode(true)

	ElasticSettings, err := config.Elasticsearch()
	var client *elastic.Client
	client, err = dialElasticRetry(ElasticSettings.String())
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}

	// Use the IndexExists service to check if a specified index exists.
	exists, err := client.IndexExists("contacts").Do()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	if !exists {
		logs.Critical("No contacts index")
		os.Exit(1)
	}

	ID := config.Int("id")
	if ID == -1 {
		logs.Debug("Looking for ID")
		ID, err = findID(client)
		if err != nil {
			logs.Debug(err)
			os.Exit(1)
		}
	}
	logs.Debug("Last ID is : %d", ID)

	contacts, err := findContacts(ID, db)

	doContacts := config.Bool("contacts")
	doFacts := config.Bool("facts")
	if doContacts == false && doFacts == false {
		logs.Debug("Nothing to be done, please activate some options")
		os.Exit(1)
	}

	for _, contact := range contacts {
		id := contact.ID
		if id != 0 {
			contact, err := addAddresses(contact, db)
			if err == nil {
				if contact != nil {
					if doContacts {
						logs.Debug("Indexing contact %d : %s %s", id, contact.Surname, contact.Firstname)
						err = index(contact, client)
						if err != nil {
							logs.Critical(err)
						}
					}
					if doFacts {
						logs.Debug("Creating and indexing fact for contact %d : %s %s", id, contact.Surname, contact.Firstname)
						err = createFact(contact, client, db)
						if err != nil {
							logs.Critical(err)
						}
					}
				} else {
					logs.Debug("Could not index contact %d", id)
				}
			}
		}
	}

	return nil
}
Beispiel #6
0
func script(ctx *cli.Context) error {
	var err error
	var config settings.Config
	if ctx.String("config") != "" {
		config, err = settings.Parse(ctx.String("config"))
		if err != nil {
			logs.Error(err)
		}
	}

	logs.Level(logs.DebugLevel)

	dialect, args, err := config.SqlDB()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	logs.Debug("database type: %s", dialect)

	var db *gorm.DB
	if db, err = databases.InitGORM(dialect, args); err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	logs.Debug("connected to %s", args)

	if config.Migrate() {
		db.AutoMigrate(models.Models()...)
		logs.Debug("database migrated successfully")
	}

	db.LogMode(true)

	ElasticSettings, err := config.Elasticsearch()
	var client *elastic.Client
	client, err = dialElasticRetry(ElasticSettings.String())
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}

	// Use the IndexExists service to check if a specified index exists.
	exists, err := client.IndexExists("contacts").Do()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	if !exists {
		logs.Critical("No contacts index")
		os.Exit(1)
	}

	ID, err := findID(client)
	if err != nil {
		logs.Debug(err)
		os.Exit(1)
	}
	logs.Debug("Last ID is : %d", ID)

	contacts, err := findContacts(ID, db)

	for _, contact := range contacts {
		contact, err := addAddresses(contact, db)
		if err != nil {
			logs.Critical(err)
			os.Exit(1)
		}
		logs.Debug("Indexing contact %d : %s %s", contact.ID, contact.Surname, contact.Firstname)
		err = index(contact, client)
		if err != nil {
			logs.Critical(err)
			os.Exit(1)
		}
	}

	return nil
}