Пример #1
0
func initdb() {
	if *showquery {
		// don't need a database for printing queries
		return
	} else if *dbname == "" {
		log.Fatal("must specify database with -db flag")
	}

	var err error
	db, err = sql.Open("sqlite3", *dbname)
	fatalif(err)

	if *simidstr == "" {
		ids, err := query.SimIds(db)
		fatalif(err)
		simid = ids[0]
	} else {
		simid = uuid.Parse(*simidstr)
		if simid == nil {
			log.Fatalf("invalid simid '%s'", *simidstr)
		}
	}

	post.Process(db)
}
Пример #2
0
func doSims(cmd string, args []string) {
	ids, err := query.SimIds(db)
	fatalif(err)
	for _, id := range ids {
		info, err := query.SimStat(db, id)
		fatalif(err)
		fmt.Println(info)
	}
}
Пример #3
0
func main() {
	log.SetFlags(0)
	flag.Parse()

	if *help || flag.NArg() < 1 {
		fmt.Println("Usage: metric -db <cyclus-db> [opts] <command> [args...]")
		fmt.Println("Calculates metrics for cyclus simulation data in a sqlite database.")
		flag.PrintDefaults()
		fmt.Println("\nCommands:")
		for i := range cmds.Names {
			fmt.Printf("    %v: %v\n", cmds.Names[i], cmds.Helps[i])
		}
		return
	} else if *dbname == "" {
		log.Fatal("must specify database with db flag")
	}

	if *custom != "" {
		data, err := ioutil.ReadFile(*custom)
		fatalif(err)
		fatalif(json.Unmarshal(data, &customSql))
	}

	var err error
	db, err = sql.Open("sqlite3", *dbname)
	fatalif(err)
	defer db.Close()

	if *simidstr == "" {
		ids, err := query.SimIds(db)
		fatalif(err)
		simid = ids[0]
	} else {
		simid, err = hex.DecodeString(*simidstr)
		fatalif(err)
	}

	// post process if necessary
	fatalif(post.Prepare(db))
	simids, err := post.GetSimIds(db)
	fatalif(err)
	for _, simid := range simids {
		ctx := post.NewContext(db, simid)
		err := ctx.WalkAll()
		if !post.IsAlreadyPostErr(err) {
			fatalif(err)
		}
	}
	fatalif(post.Finish(db))

	// run command
	cmds.Execute(flag.Args())
}
Пример #4
0
func uploadInner(w http.ResponseWriter, r *http.Request, kill chan bool) {
	// parse database from multi part form data
	if err := r.ParseMultipartForm(MAX_MEMORY); err != nil {
		log.Println(err)
		http.Error(w, err.Error(), http.StatusForbidden)
		return
	}

	gotDb := false
	uid := r.URL.Path[len("/upload/"):]
	fname := uid + ".sqlite"
	for _, fileHeaders := range r.MultipartForm.File {
		for _, fileHeader := range fileHeaders {
			file, _ := fileHeader.Open()
			data, _ := ioutil.ReadAll(file)
			err := ioutil.WriteFile(fname, data, 0644)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				log.Print(err)
				return
			}
			defer func() { os.Remove(fname) }()
			gotDb = true
			break
		}
		break
	}
	if !gotDb {
		http.Error(w, "No file provided", http.StatusBadRequest)
		log.Print("received request with no file")
		return
	}

	select {
	case <-kill:
		return
	default:
	}

	// post process the database
	db, err := sql.Open("sqlite3", fname)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	defer db.Close()

	err = post.Prepare(db)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	defer post.Finish(db)

	simids, err := post.GetSimIds(db)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	ctx := post.NewContext(db, simids[0])
	if err := ctx.WalkAll(); err != nil {
		log.Println(err)
	}

	// get simid
	ids, err := query.SimIds(db)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	simid := ids[0]
	rs := &Results{}

	select {
	case <-kill:
		return
	default:
	}

	// create flow graph
	combineProto := false
	arcs, err := query.FlowGraph(db, simid, 0, -1, combineProto)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	dotname := fname + ".dot"
	dotf, err := os.Create(dotname)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	defer func() { os.Remove(dotname) }()

	fmt.Fprintln(dotf, "digraph ResourceFlows {")
	fmt.Fprintln(dotf, "    overlap = false;")
	fmt.Fprintln(dotf, "    nodesep=1.0;")
	fmt.Fprintln(dotf, "    edge [fontsize=9];")
	for _, arc := range arcs {
		fmt.Fprintf(dotf, "    \"%v\" -> \"%v\" [label=\"%v\\n(%.3g kg)\"];\n", arc.Src, arc.Dst, arc.Commod, arc.Quantity)
	}
	fmt.Fprintln(dotf, "}")
	dotf.Close()

	var buf bytes.Buffer
	cmd := exec.Command("dot", "-Tsvg", dotname)
	cmd.Stdout = &buf
	err = cmd.Run()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	rs.Flowgraph = buf.String()

	select {
	case <-kill:
		return
	default:
	}

	// create agents table
	rs.Agents, err = query.AllAgents(db, simid, "")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	// create Material transactions table
	sql := `
		SELECT TransactionId,Time,SenderId,ReceiverId,tr.ResourceId,Commodity,NucId,MassFrac*Quantity
		FROM Transactions AS tr
		INNER JOIN Resources AS res ON tr.ResourceId = res.ResourceId
		INNER JOIN Compositions AS cmp ON res.QualId = cmp.QualId
		WHERE tr.SimId = ? AND cmp.SimId = res.SimId AND res.SimId = tr.SimId
		AND res.Type = 'Material';
		`
	rows, err := db.Query(sql, simid)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	for rows.Next() {
		t := &Trans{}
		if err := rows.Scan(&t.Id, &t.Time, &t.Sender, &t.Receiver, &t.ResourceId, &t.Commod, &t.Nuc, &t.Qty); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			log.Print(err)
			return
		}
		rs.TransMats = append(rs.TransMats, t)
	}
	if err := rows.Err(); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	// create Material transactions table
	sql = `
		SELECT TransactionId,Time,SenderId,ReceiverId,tr.ResourceId,Commodity,Quality,Quantity
		FROM Transactions AS tr
		INNER JOIN Resources AS res ON tr.ResourceId = res.ResourceId
		INNER JOIN Products AS pd ON res.QualId = pd.QualId
		WHERE tr.SimId = ? AND pd.SimId = res.SimId AND res.SimId = tr.SimId
		AND res.Type = 'Product';
		`
	rows, err = db.Query(sql, simid)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	for rows.Next() {
		t := &ProdTrans{}
		if err := rows.Scan(&t.Id, &t.Time, &t.Sender, &t.Receiver, &t.ResourceId, &t.Commod, &t.Quality, &t.Qty); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			log.Print(err)
			return
		}
		rs.TransProds = append(rs.TransProds, t)
	}
	if err := rows.Err(); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	select {
	case <-kill:
		return
	default:
	}

	// render all results and save page
	rs.Uid = uid
	resultTmpl.Execute(w, rs)

	f, err := os.Create(uid + ".html")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	defer f.Close()
	resultTmpl.Execute(f, rs)
	close(kill)
}