Ejemplo n.º 1
0
func LoadPerf(fName string, since, until time.Time) ([]PerfLogEntry, error) {
	fIn, err := os.Open(fName)
	if err != nil {
		return nil, err
	}
	defer fIn.Close()

	dec := json.NewDecoder(fIn)
	var result []PerfLogEntry
	for {
		var entry PerfLogEntry
		err = dec.Decode(&entry)
		if err != nil {
			if strings.Contains(err.Error(), "EOF") {
				break
			} else {
				return nil, fmt.Errorf("Decode error: %v", err)
			}
		} else {
			if !since.Equal(time.Time{}) && since.After(entry.Timestamp) {
				continue
			}
			if !until.Equal(time.Time{}) && until.Before(entry.Timestamp) {
				continue
			}
			result = append(result, entry)
		}
	}
	return result, nil
}
Ejemplo n.º 2
0
func loadPlanFile() (coords []gopnik.TileCoord, err error) {
	var fin *os.File
	fin, err = os.Open(*planFile)
	if err != nil {
		err = fmt.Errorf("Failed to load plan file: %v", err)
		return
	}
	dec := json.NewDecoder(fin)
	err = dec.Decode(&coords)
	if err != nil {
		err = fmt.Errorf("Failed to decode plan file: %v", err)
		return
	}
	// Ensure metatile coord
	for i, oldCoord := range coords {
		coords[i] = app.App.Metatiler().TileToMetatile(&oldCoord)
	}
	return
}
Ejemplo n.º 3
0
// Parse configuration
func (self *app) parseConfig(input io.Reader) error {
	// Decode JSON
	dec := json.NewDecoder(input)
	if err := dec.Decode(self.config); err != nil {
		return fmt.Errorf("JSON error: %v", err)
	}

	// Validate config
	cfgStPtr := reflect.ValueOf(self.config)
	if cfgStPtr.Kind() != reflect.Ptr {
		return fmt.Errorf("Invalid config struct ref")
	}
	cfgSt := cfgStPtr.Elem()
	if cfgSt.Kind() != reflect.Struct {
		return fmt.Errorf("Invalid config struct")
	}

	// Parse application config
	if self.appName != "" {
		appCfg := cfgSt.FieldByName(self.appName)
		if !appCfg.IsValid() {
			return fmt.Errorf(`Field "%s" not found in config`, self.appName)
		}
		if appCfg.Kind() != reflect.Struct {
			return fmt.Errorf(`Invalid field "%s" struct`, self.appName)
		}
		self.appConfig = appCfg.Interface()
	}

	// Parse common config
	cCfg := cfgSt.FieldByName("CommonConfig")
	if !cCfg.IsValid() {
		return fmt.Errorf(`Field "CommonConfig" not found`)
	}
	cCfgConv, cCfgOk := cCfg.Interface().(CommonConfig)
	if !cCfgOk {
		return fmt.Errorf("Config is invalid")
	}
	self.commonConfig = cCfgConv

	return nil
}
Ejemplo n.º 4
0
func main() {
	cfg := gopnikprerenderlib.PrerenderGlobalConfig{
		Prerender: gopnikprerenderlib.PrerenderConfig{
			UIAddr:    ":8088",
			DebugAddr: ":8098",
		},
		CommonConfig: app.CommonConfig{
			MetaSize: 8,
			TileSize: 256,
		},
	}

	app.App.Configure("Prerender", &cfg)

	zooms, err := parseZoom(*zoom)
	if err != nil {
		log.Fatalf("Invalid zoom: %v", err)
	}

	var tags []string
	if *tagsF != "" {
		tags = strings.Split(*tagsF, ",")
	}

	var coords []gopnik.TileCoord

	if *doAppend {
		f, err := os.Open(*planFile)
		if err != nil {
			if !strings.Contains(err.Error(), "no such file or directory") {
				log.Fatalf("Failed to open plan file: %v", err)
			}
		} else {
			dec := json.NewDecoder(f)
			err = dec.Decode(&coords)
			if err != nil {
				log.Fatalf("Failed to parse plan file: %v", err)
			}
		}
	}

	if *allWorld {
		for _, zoom := range zooms {
			bbox := [4]float64{-85 /*latMin*/, 85 /*latMax*/, -180 /*lonMin*/, 180 /*lonMax*/}
			zoomCoords, err := genCoords(bbox, zoom)
			if err != nil {
				log.Fatal(err)
			}
			// append tags
			for i := 0; i < len(zoomCoords); i++ {
				zoomCoords[i].Tags = tags
			}
			coords = append(coords, zoomCoords...)
		}
	} else {
		if *csvFile != "" {
			csvCoords, err := readCSVFile(*csvFile, zooms)
			if err != nil {
				log.Fatal(err)
			}
			// append tags
			for i := 0; i < len(csvCoords); i++ {
				csvCoords[i].Tags = tags
			}
			coords = append(coords, csvCoords...)
		}

		for _, zoom := range zooms {
			flagCoords, err := latLon(zoom)
			if err != nil {
				log.Fatal(err)
			}
			// append tags
			for i := 0; i < len(flagCoords); i++ {
				flagCoords[i].Tags = tags
			}
			coords = append(coords, flagCoords...)
		}
	}

	if *subtractPerflog != "" {
		sinceT, err := parseTime(*subtractPerflogSince)
		if err != nil {
			log.Fatalf("Invalid since param: %v", err)
		}
		untilT, err := parseTime(*subtractPerflogUntil)
		if err != nil {
			log.Fatalf("Invalid unitl param: %v", err)
		}

		perfData, err := perflog.LoadPerf(*subtractPerflog, sinceT, untilT)
		if err != nil {
			log.Fatalf("Failed to load performance log: %v", err)
		}

		coords = subtractPerflogItems(coords, perfData)
	}

	if *shuffle {
		rand.Seed(time.Now().UnixNano())
		shuffleCoords(coords)
	}

	fout, err := os.OpenFile(*planFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		log.Fatalf("Failed to open plan file: %v", err)
	}
	enc := json.NewEncoder(fout)
	err = enc.Encode(coords)
	if err != nil {
		log.Fatalf("Failed to encode plan file: %v", err)
	}
}