Skip to content

Instantly share code, notes, and snippets.

@dustin
Created December 13, 2012 20:50
Show Gist options
  • Select an option

  • Save dustin/4279723 to your computer and use it in GitHub Desktop.

Select an option

Save dustin/4279723 to your computer and use it in GitHub Desktop.
Builds the "extrusion" kmz thing.
package main
import (
"archive/zip"
"flag"
"fmt"
"log"
"os"
"code.google.com/p/dsallings-couch-go"
"github.com/dustin/go-humanize"
)
const kml_top = `<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
`
const kml_bottom = ` </Document>
</kml>
`
const place = ` <Placemark>
<name>%d node %s cluster</name>
<description>
<![CDATA[First seen on %s. Has %v RAM
over the whole cluster. Currently on version %s.]]>
</description>
<Point>
<coordinates>%v,%v</coordinates>
</Point>
</Placemark>
`
type Cluster struct {
RamMax float64
Longitude float64
Latitude float64
OS string
NumNodes int
First string
Version string
}
type ResultRow struct {
Value Cluster
}
type Results struct {
Rows []ResultRow
}
func maybefatal(str string, err error) {
if err != nil {
log.Fatalf("%s: %v", str, err)
}
}
func humanRam(f float64) string {
return humanize.Bytes(uint64(f))
}
func main() {
dburl := flag.String("couch", "http://localhost:5984/cbstats",
"Stats location.")
filename := flag.String("kmz", "data.kmz", "File to create.")
flag.Parse()
db, err := couch.Connect(*dburl)
if err != nil {
log.Fatalf("Error connecting to couchdb: %v", err)
}
query_results := Results{}
err = db.Query("_design/geo/_view/vitals",
map[string]interface{}{"reduce": false},
&query_results)
maybefatal("Error querying couchdb", err)
log.Printf("Got %d rows", len(query_results.Rows))
f, err := os.Create(*filename + ".tmp")
maybefatal("Error creating file", err)
defer f.Close()
z := zip.NewWriter(f)
defer z.Close()
dockml, err := z.Create("doc.kml")
maybefatal("Error creating kml zip thing", err)
dockml.Write([]byte(kml_top))
for _, row := range query_results.Rows {
value := row.Value
fmt.Fprintf(dockml, place,
value.NumNodes, value.OS,
value.First, humanRam(value.RamMax),
value.Version,
value.Longitude, value.Latitude)
}
dockml.Write([]byte(kml_bottom))
os.Rename(*filename+".tmp", *filename)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment