Skip to content
Snippets Groups Projects
Commit 75076544 authored by Daniel Lyons's avatar Daniel Lyons
Browse files

Merge branch 'go-contacts-wrest' into '2.8.2-DEVELOPMENT'

Go contacts wrest

See merge request !1421
parents 6b5474a1 4988ed0a
No related branches found
No related tags found
2 merge requests!1452Merge 2.8.2 to main,!1421Go contacts wrest
Pipeline #11408 passed
module ssa/contacts_wrest
go 1.18
require (
github.com/go-sql-driver/mysql v1.7.1
github.com/lib/pq v1.10.9
gitlab.nrao.edu/ssa/gocapo v0.0.0-20230307183307-91ffd4356566
)
require (
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.13.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
This diff is collapsed.
/*
* Copyright (C) 2022 Associated Universities, Inc. Washington DC, USA.
*
* This file is part of NRAO Workspaces.
*
* Workspaces is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Workspaces is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Workspaces. If not, see <https://www.gnu.org/licenses/>.
*/
// Entry point for the contacts-wrest tool
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
wrest "ssa/contacts_wrest/wrest"
"strings"
)
func main() {
jsonMode := flag.Bool("j", false, "Output a JSON-friendly list of strings instead of human-friendly list")
// Parse the CLI arguments
flag.Parse()
// We must have one argument: the project code
if flag.NArg() != 1 {
fmt.Printf("Usage: %v [-j] <PROJECT-CODE>\n", os.Args[0])
os.Exit(1)
}
// Obtain the contacts for this project
proposalId := flag.Arg(0)
contacts, err := wrest.RetrieveContacts(proposalId)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to retrieve contacts for %s: %v\n", proposalId, err)
os.Exit(1)
}
if *jsonMode {
// In JSON mode, we output using the JSON marshaller
binary, _ := json.Marshal(contacts)
fmt.Println(string(binary))
} else {
// In non-JSON mode, we output something human-friendly
fmt.Println(strings.Join(contacts, ", "))
}
}
/*
* Copyright (C) 2022 Associated Universities, Inc. Washington DC, USA.
*
* This file is part of NRAO Workspaces.
*
* Workspaces is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Workspaces is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Workspaces. If not, see <https://www.gnu.org/licenses/>.
*/
package wrest
import (
"database/sql"
"fmt"
"gitlab.nrao.edu/ssa/gocapo/capo/config"
"gitlab.nrao.edu/ssa/gocapo/helpers"
"os"
"strconv"
"strings"
)
// databaseType represents the database driver we are using (Postgres or MySQL
// for now)
type databaseType string
const (
Postgres databaseType = "postgres"
MySQL databaseType = "mysql"
)
// defaultPort returns the default server port for the given database type. This
// is needed to connect
func (databaseType databaseType) defaultPort() int {
if databaseType == Postgres {
return 5432
} else {
return 3306
}
}
// jdbcPrefix return the JDBC string prefix that will sadly be prepended to the
// URL in the Capo profile
func (databaseType databaseType) jdbcPrefix() string {
if databaseType == Postgres {
return "jdbc:postgresql://"
} else {
return "jdbc:mysql://"
}
}
// connectionString formats the connection string for this database flavor, which
// we can then hand to the sql package.
func (flavor databaseFlavor) connectionString(host string, port int, user string, password string, dbName string) string {
if flavor.Type == Postgres {
return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbName)
} else {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", user, password, host, port, dbName)
}
}
// databaseFlavor represents the information we need to build a connection to
// some database, based on what we know a priori and from Capo
type databaseFlavor struct {
CapoPrefix string
UserCapoKey string
PasswordCapoKey string
UrlCapoKey string
Type databaseType
}
// archiveDB holds the information necessary to connect to the archive database
var archiveDB = databaseFlavor{
CapoPrefix: "edu.nrao.ssa.project.db",
UserCapoKey: "username",
PasswordCapoKey: "password",
UrlCapoKey: "url",
Type: Postgres,
}
// proposalsDB holds the information necessary to connect to the proposals database
var proposalsDB = databaseFlavor{
CapoPrefix: "my.nrao.jdbc",
UserCapoKey: "username",
PasswordCapoKey: "password",
UrlCapoKey: "URL",
Type: MySQL,
}
// getConnection uses the information in a particular database flavor to connect
// to a certain database
func getConnection(flavor databaseFlavor) (*sql.DB, error) {
// Get db info and build string to get connection
prop, err := config.InitConfig(os.Getenv("CAPO_PROFILE"), helpers.DefaultCapoPath)
if err != nil {
return nil, fmt.Errorf("unable to load CAPO config: %v", err)
}
// obtain the Capo settings for this flavor
settings := prop.SettingsForPrefix(flavor.CapoPrefix)
// get the user, password, and URL settings
user := settings.GetString(flavor.UserCapoKey)
password := settings.GetString(flavor.PasswordCapoKey)
url := settings.GetString(flavor.UrlCapoKey)
// remove the JDBC prefix
url = strings.TrimPrefix(url, flavor.Type.jdbcPrefix())
// split the URL into host/database sections
splitUrl := strings.Split(url, "/")
if len(splitUrl) != 2 {
return nil, fmt.Errorf("URL does not contain 'host/database' pattern: %s", url)
}
host := splitUrl[0]
dbName := splitUrl[1]
// split the host into host and port
splitHostPort := strings.Split(host, ":")
host = splitHostPort[0]
// use the default port if there is no port component, otherwise use the port component
port := flavor.Type.defaultPort()
if len(splitHostPort) > 1 {
port, err = strconv.Atoi(splitHostPort[1])
if err != nil {
return nil, fmt.Errorf("unable to convert %s to integer: %v", splitHostPort[1], err)
}
}
// build the connect string
connInfo := flavor.connectionString(host, port, user, password, dbName)
// open the database connection and check for errors
db, err := sql.Open(string(flavor.Type), connInfo)
if err != nil {
return nil, fmt.Errorf("unable to open the database connection to %s@%s: %v", dbName, host, err)
}
// attempt to connect to the database and check for errors
err = db.Ping()
if err != nil {
return nil, fmt.Errorf("unable to ping the database connection for %s@%s: %v", dbName, host, err)
}
// OK, we have a valid database connection
return db, nil
}
// GetArchiveDBConnection returns a connection to the archive database
func GetArchiveDBConnection() (*sql.DB, error) {
return getConnection(archiveDB)
}
// GetProposalsDBConnection returns a connection to the proposals database
func GetProposalsDBConnection() (*sql.DB, error) {
return getConnection(proposalsDB)
}
/*
* Copyright (C) 2022 Associated Universities, Inc. Washington DC, USA.
*
* This file is part of NRAO Workspaces.
*
* Workspaces is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Workspaces is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Workspaces. If not, see <https://www.gnu.org/licenses/>.
*/
/*
Package wrest looks up contact authors for a given project.
If this tool isn't working, the same information can be obtained by logging
into the OPT, opening the project in question, and clicking on the coauthors.
*/
package wrest
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"strings"
)
// RetrieveContacts for a given project code
func RetrieveContacts(projectCode string) ([]string, error) {
// Get the designated contact(s) for this project.
// step 1: retrieve the contact author globalIDs from the OPT
globalIds, err := retrieveGlobalIds(projectCode)
if err != nil {
return nil, fmt.Errorf("unable to retrieve global ids: %v", err)
}
// step 2: look up email addresses in the my.nrao.edu database
emails, err := retrieveEmailsForGlobalIds(globalIds)
if err != nil {
return nil, fmt.Errorf("unable to retrieve emails: %v", err)
}
return emails, nil
}
// retrieveGlobalIds for a given project code
func retrieveGlobalIds(projectCode string) ([]int, error) {
// connect to the database
connection, err := GetArchiveDBConnection()
if err != nil {
return nil, fmt.Errorf("unable to connect to archive database: %v", err)
}
// build the query
query := `select globalid
from project
join projectauthor p on project.pi = p.id
where projectcode = $1 and receivesemail
union
select globalid
from project
join projectauthor p on project.contactauthor = p.id
where projectcode = $1 and receivesemail
union
select globalid
from project
join coauthors c on project.id = c.project_id
join projectauthor p on c.projectauthor_id = p.id
where projectcode = $1 and receivesemail`
// close the database when we're done
defer func(connection *sql.DB) {
if err := connection.Close(); err != nil {
fmt.Printf("unable to close the database connection: %v\n", err)
}
}(connection)
// run the query and get the result object
rows, err := connection.Query(query, projectCode)
if err != nil {
return nil, fmt.Errorf("unable to execute project code query: %v", err)
}
// make a result slice
globalIds := make([]int, 0)
// clean up
defer rows.Close()
// walk through the list, scanning into a variable and appending to the result slice
for rows.Next() {
globalId := 0
if err := rows.Scan(&globalId); err != nil {
return globalIds, fmt.Errorf("unable to read global ID from the user query: %v", err)
}
globalIds = append(globalIds, globalId)
}
// done, return
return globalIds, nil
}
// retrieveEmailsForGlobalIds obtains the emails for the given slice of global IDs
func retrieveEmailsForGlobalIds(globalIds []int) ([]string, error) {
// Now that we have the global IDs, we can use them to look up contacts' email addresses
connection, err := GetProposalsDBConnection()
if err != nil {
return nil, fmt.Errorf("unable to connect to PST database: %v", err)
}
// build the IN list, this is so gross
inClause := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(globalIds)), ","), "[]")
// build the query
query := fmt.Sprintf(`select email
from email
where person_id IN (%s) and defaultEmail`, inClause)
// close the database when we're done
defer func(connection *sql.DB) {
if err := connection.Close(); err != nil {
fmt.Printf("unable to close the database connection: %v\n", err)
}
}(connection)
// run the query and get the result object
rows, err := connection.Query(query)
if err != nil {
return nil, fmt.Errorf("unable to execute the project code query: %v", err)
}
// make a result slice
emails := make([]string, 0)
// clean up
defer rows.Close()
// walk through the list, scanning into a variable and appending to the result slice
for rows.Next() {
var email string
err := rows.Scan(&email)
if err != nil {
return nil, fmt.Errorf("unable to read email from the email query: %v", err)
}
emails = append(emails, email)
}
// done, return
return emails, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment