// Process processes SeisComPML files. Converts them to a msg.Quake, checks the quality, sends them // to an AWS SNS topic as a msg.Haz encoded as JSON. func (s *sc3) Process() bool { s.Quake = msg.ReadSC3ML07(sc3SpoolDir + "/" + s.f) if s.Err() != nil { log.Println(s.Err()) return false } s.Quake.Site = sc3Site s.RxLog() if !s.Publish() { log.Println(s.Err()) return false } h := msg.Haz{Quake: &s.Quake} b, err := h.Encode() if err != nil { s.SetErr(fmt.Errorf("WARN: not sending %s - encoding err %s.", s.f, err.Error())) return false } s.TxLog() err = sn.Publish(msg.Raw{Body: string(b)}, 3) if err != nil { s.SetErr(err) return true } return false }
func procSC3ML(sc3ml <-chan os.FileInfo) { for fi := range sc3ml { log.Println(fi.Name()) q := msg.ReadSC3ML07(spoolDir + "/" + fi.Name()) if q.Err() != nil { log.Println("WARN ignoring errored SC3ML: " + fi.Name() + " " + q.Err().Error()) continue } err := db.SaveQuake(q) if err != nil { log.Fatal(err) } } }
// setup starts a db connection and test server then inits an http client. func setup() { // load some test data. Needs a write user. var err error database.DBUser = "******" tdb, err := database.InitPG() if err != nil { log.Fatal(err) } tdb.Check() _, err = tdb.Exec("delete from haz.quake") if err != nil { log.Fatal(err) } _, err = tdb.Exec("delete from haz.quakeapi") if err != nil { log.Fatal(err) } _, err = tdb.Exec("delete from haz.quakehistory") if err != nil { log.Fatal(err) } q := msg.ReadSC3ML07("etc/test/files/2013p407387.xml") if err != nil { log.Fatal(err) } // stop the quake being deleted from haz.quakehistory and haz.quakeapi q.Time = time.Now().UTC() err = tdb.SaveQuake(q) if err != nil { log.Fatal(err) } q = msg.ReadSC3ML07("etc/test/files/2015p012816.xml") if err != nil { log.Fatal(err) } // stop the quake being deleted from haz.quakehistory and haz.quakeapi q.Time = time.Now().UTC() err = tdb.SaveQuake(q) if err != nil { log.Fatal(err) } tdb.Close() database.DBUser = "******" tdb, err = database.InitPG() if err != nil { log.Fatal(err) } tdb.Check() _, err = tdb.Exec("delete from impact.intensity_measured") if err != nil { log.Fatal(err) } _, err = tdb.Exec("delete from impact.intensity_reported") if err != nil { log.Fatal(err) } for _, m := range measuredTest { _, err = tdb.Exec("select impact.add_intensity_measured($1, $2, $3, $4, $5)", m.Source, m.Longitude, m.Latitude, m.Time, m.MMI) if err != nil { log.Fatal(err) } } for _, m := range reportedTest { _, err = tdb.Exec("select impact.add_intensity_reported($1, $2, $3, $4, $5, $6)", m.Source, m.Longitude, m.Latitude, m.Time, m.MMI, m.Comment) if err != nil { log.Fatal(err) } } tdb.Close() // switch back to the correct user for the tests. // hazard_r can read haz and impact. database.DBUser = "******" db, err = database.InitPG() if err != nil { log.Fatal(err) } err = db.Ping() if err != nil { log.Fatal(err) } ts = httptest.NewServer(handler()) client = &http.Client{} }
// setup starts a db connection and test server then inits an http client. func setup() { // load some test data. Needs a write user. var err error database.DBUser = "******" tdb, err := database.InitPG() if err != nil { log.Fatal(err) } tdb.Check() _, err = tdb.Exec("delete from haz.quake") if err != nil { log.Fatal(err) } _, err = tdb.Exec("delete from haz.quakeapi") if err != nil { log.Fatal(err) } _, err = tdb.Exec("delete from haz.quakehistory") if err != nil { log.Fatal(err) } q := msg.ReadSC3ML07("etc/test/files/2013p407387.xml") if err != nil { log.Fatal(err) } // stop the quake being deleted from haz.quakehistory and haz.quakeapi q.Time = time.Now().UTC() err = tdb.SaveQuake(q) if err != nil { log.Fatal(err) } q = msg.ReadSC3ML07("etc/test/files/2015p012816.xml") if err != nil { log.Fatal(err) } // stop the quake being deleted from haz.quakehistory and haz.quakeapi q.Time = time.Now().UTC() err = tdb.SaveQuake(q) if err != nil { log.Fatal(err) } tdb.Close() database.DBUser = "******" tdb, err = database.InitPG() if err != nil { log.Fatal(err) } tdb.Check() _, err = tdb.Exec("delete from impact.intensity_measured") if err != nil { log.Fatal(err) } _, err = tdb.Exec("delete from impact.intensity_reported") if err != nil { log.Fatal(err) } for _, m := range measuredTest { _, err = tdb.Exec("select impact.add_intensity_measured($1, $2, $3, $4, $5)", m.Source, m.Longitude, m.Latitude, m.Time, m.MMI) if err != nil { log.Fatal(err) } } for _, m := range reportedTest { _, err = tdb.Exec("select impact.add_intensity_reported($1, $2, $3, $4, $5, $6)", m.Source, m.Longitude, m.Latitude, m.Time, m.MMI, m.Comment) if err != nil { log.Fatal(err) } } tdb.Close() // switch back to the correct user for the tests. // hazard_r can read haz and impact. database.DBUser = "******" db, err = database.InitPG() if err != nil { log.Fatal(err) } err = db.Ping() if err != nil { log.Fatal(err) } ts = httptest.NewServer(handler()) client = &http.Client{} var sess *session.Session if sess, err = session.NewSession(); err != nil { log.Printf("ERROR creating AWS session 500s will be served %s", err) } // Credentials can come from environment var (e.g., for dev) // for testing. creds := credentials.NewChainCredentials( []credentials.Provider{ &credentials.EnvProvider{}, }) s3Client = s3.New(sess, &aws.Config{Credentials: creds}) }
// setup starts a db connection and test server then inits an http client. func setup(t *testing.T) { var err error database.DBUser = "******" tdb, err := database.InitPG() if err != nil { t.Fatal(err) } tdb.Check() _, err = tdb.Exec("delete from haz.quake") if err != nil { t.Fatal(err) } q := msg.ReadSC3ML07("etc/test/files/1542894.xml") if err != nil { t.Fatal(err) } err = tdb.SaveQuake(q) if err != nil { t.Fatal(err) } q = msg.ReadSC3ML07("etc/test/files/2190619.xml") if err != nil { t.Fatal(err) } err = tdb.SaveQuake(q) if err != nil { t.Fatal(err) } q = msg.ReadSC3ML07("etc/test/files/3366146.xml") if err != nil { t.Fatal(err) } err = tdb.SaveQuake(q) if err != nil { t.Fatal(err) } tdb.Close() // switch back to the correct user for the tests. // hazard_r can read haz and impact. database.DBUser = "******" db, err = database.InitPG() if err != nil { t.Fatal(err) } err = db.Ping() if err != nil { t.Fatal(err) } ts = httptest.NewServer(handler()) }