func getItem( sID int64, stype string, trailk *context.Context, ctx appengine.Context, keyTool *key.Tool) (*item, error) { var err error var s *source.Source var bestest float64 = 0 var worstest float64 = 0 i := &item{} switch stype { case "Source": s, err = source.NewSources(trailk, ctx, keyTool).Source(sID).Get() case "Reference": s, err = source.NewSources(trailk, ctx, keyTool).Reference(sID).Get() default: return nil, ErrUnknownSourceType } if err != nil { return nil, err } i.Source = s for _, r := range s.Records(100) { if bestest < r.Max { bestest = r.Max } if worstest > r.Min || (r.Min != 0 && worstest == 0) { worstest = r.Min } i.Records = append(i.Records, &record{ Average: math.Ceil(r.Average / 1024), Max: math.Ceil(r.Max / 1024), Min: math.Ceil(r.Min / 1024), Timeout: math.Ceil(float64(r.Timeout.Nanoseconds() / time.Millisecond.Nanoseconds())), Repeated: r.Repeated, Time: r.Time, }) } if len(i.Records) < 1 { i.EmptyRecord = true } else { i.EmptyRecord = false } i.Maxest = math.Ceil(bestest / 1024) i.Minest = math.Ceil(worstest / 1024) return i, nil }
func addSource( trailk *context.Context, c appengine.Context, keyTool *key.Tool, fetcher *urlfetch.Fetcher, data *sourceAddData) (*datastore.Key, error) { var s *source.Source switch data.Type { case "Source": s = source.NewSources(trailk, c, keyTool).Make("Source") case "Reference": s = source.NewSources(trailk, c, keyTool).Make("Reference") default: return nil, errors.New("Unknown source type") } if len(data.Name) < config.Source_NameMinLen { return nil, errors.New(fmt.Sprintf( "Source name can't be shorter than %d", config.Source_NameMinLen)) } else if len(data.Name) > config.Source_NameMaxLen { return nil, errors.New(fmt.Sprintf( "Source name can't be longer than %d", config.Source_NameMaxLen)) } if len(data.URL) < config.Source_URLMinLen { return nil, errors.New(fmt.Sprintf( "Source URL can't be shorter than %d", config.Source_URLMinLen)) } else if len(data.URL) > config.Source_URLMaxLen { return nil, errors.New(fmt.Sprintf( "Source URL can't be longer than %d", config.Source_URLMaxLen)) } matched, matchErr := regexp.MatchString( "([a-z0-9]+)://([a-zA-Z0-9.-_/]+)", data.URL) if matchErr != nil { return nil, errors.New(fmt.Sprintf( "Source URL contains invalid format: %v", matchErr)) } if !matched { return nil, errors.New("Source URL contains invalid format") } _, fetchErr := fetcher.Get(data.URL, config.Benchmark_ConnectionTimeoutMax) if fetchErr != nil { return nil, errors.New(fmt.Sprintf( "Sorry, we can't access to the URL. Error: %v", fetchErr)) } s.Name = data.Name s.URL = data.URL s.Updated = trailk.Time().Now() s.Sampled = 0 return s.Insert() }