Make binaries available
This commit is contained in:
106
cmd/elastic/main.go
Normal file
106
cmd/elastic/main.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
var (
|
||||
csvPath = flag.String("csv", "aips.csv", "Path to the CSV file containing AIP names")
|
||||
elasticSearch = flag.String("elastic", "http://127.0.0.1:62002", "elastic search URL")
|
||||
// mysqlConnString = flag.String("mysql", "root:12345@tcp(localhost:62001)/MCP", "mysql connection string")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
file, err := os.Open(*csvPath)
|
||||
assertNoErr(err)
|
||||
records, err := csv.NewReader(file).ReadAll()
|
||||
assertNoErr(err)
|
||||
|
||||
r := []string{}
|
||||
for _, record := range records {
|
||||
r = append(r, strings.TrimSpace(record[0]))
|
||||
}
|
||||
result, err := os.OpenFile("aip_names.txt", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
assertNoErr(err)
|
||||
|
||||
// Query mapping to know if it's raw or keyword: /aips/_mapping
|
||||
q := struct {
|
||||
Query struct {
|
||||
Term struct {
|
||||
Name string `json:"name.raw"`
|
||||
} `json:"term"`
|
||||
} `json:"query"`
|
||||
}{}
|
||||
for _, name := range r {
|
||||
q.Query.Term.Name = name
|
||||
var res ElasticAipResponse
|
||||
err = do(*elasticSearch+"/aips/_search", q, &res)
|
||||
assertNoErr(err)
|
||||
if res.Hits.Total == 1 {
|
||||
fmt.Println("AIP Found: " + res.Hits.Hits[0].Source.Name)
|
||||
_, err = result.WriteString(name + "\n")
|
||||
assertNoErr(err)
|
||||
} else if res.Hits.Total > 1 {
|
||||
fmt.Println("more than one match, this is not expeted")
|
||||
fmt.Println("Name: ", name)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func do(path string, payload, responsePayload any) error {
|
||||
var body io.Reader
|
||||
if payload != nil {
|
||||
jsonBody, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body = bytes.NewReader(jsonBody)
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, path, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resBody, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res.StatusCode >= 400 {
|
||||
fmt.Println(string(resBody))
|
||||
return fmt.Errorf("request failed: %s", http.StatusText(res.StatusCode))
|
||||
}
|
||||
return json.Unmarshal(resBody, responsePayload)
|
||||
}
|
||||
|
||||
func openDB(connStr string) *sql.DB {
|
||||
db, err := sql.Open("mysql", connStr)
|
||||
assertNoErr(err)
|
||||
err = db.Ping()
|
||||
assertNoErr(err)
|
||||
return db
|
||||
}
|
||||
|
||||
func assertNoErr(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
40
cmd/elastic/types.go
Normal file
40
cmd/elastic/types.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
type ElasticAipResponse struct {
|
||||
Took int `json:"took"`
|
||||
TimedOut bool `json:"timed_out"`
|
||||
Shards struct {
|
||||
Total int `json:"total"`
|
||||
Successful int `json:"successful"`
|
||||
Skipped int `json:"skipped"`
|
||||
Failed int `json:"failed"`
|
||||
} `json:"_shards"`
|
||||
Hits struct {
|
||||
Total int `json:"total"`
|
||||
MaxScore float64 `json:"max_score"`
|
||||
Hits []struct {
|
||||
Index string `json:"_index"`
|
||||
Type string `json:"_type"`
|
||||
ID string `json:"_id"`
|
||||
Score float64 `json:"_score"`
|
||||
Source struct {
|
||||
UUID string `json:"uuid"`
|
||||
Name string `json:"name"`
|
||||
FilePath string `json:"filePath"`
|
||||
Size float64 `json:"size"`
|
||||
FileCount int `json:"file_count"`
|
||||
Origin string `json:"origin"`
|
||||
Created int `json:"created"`
|
||||
Aicid any `json:"AICID"`
|
||||
IsPartOf any `json:"isPartOf"`
|
||||
CountAIPsinAIC any `json:"countAIPsinAIC"`
|
||||
Identifiers []any `json:"identifiers"`
|
||||
TransferMetadata []any `json:"transferMetadata"`
|
||||
Encrypted bool `json:"encrypted"`
|
||||
Accessionids []any `json:"accessionids"`
|
||||
Status string `json:"status"`
|
||||
Location string `json:"location"`
|
||||
} `json:"_source"`
|
||||
} `json:"hits"`
|
||||
} `json:"hits"`
|
||||
}
|
||||
Reference in New Issue
Block a user