func saveUpgradeData(opts *Upgradeable, ulog string, duration time.Duration) error { log.Debugf("%s in (%s)\n%s", cmd.Colorfy(opts.B.GetFullName(), "cyan", "", "bold"), cmd.Colorfy(duration.String(), "green", "", "bold"), cmd.Colorfy(ulog, "yellow", "", "")) return nil }
func (c *Client) ImageSetMembers(imageset *ImageSet, timeout time.Duration) (result []*Image, err error) { chImages := make(chan *Image) for _, member := range imageset.Members { log.Println(len(imageset.Members), "images to get:", member.ID, c) go func(url string) { image, err := c.Image(url) if err != nil { return } chImages <- image }(member.ID) } images := []*Image{} for { select { case image := <-chImages: log.Println("Received:", image.ID) images = append(images, image) if len(images) == len(imageset.Members) { return images, nil } case <-time.After(timeout): if timeout > time.Duration(0) { return nil, fmt.Errorf("Image retrieval timed out after %sms", timeout.String()) } } } }
func (c *Client) StreamAppLogs(app, filter string, follow bool, since time.Duration, output io.WriteCloser) error { return c.Stream(fmt.Sprintf("/apps/%s/logs", app), map[string]string{ "Filter": filter, "Follow": fmt.Sprintf("%t", follow), "Since": since.String(), }, nil, output) }
// StreamRackLogs streams the logs for a Rack func (c *Client) StreamRackLogs(filter string, follow bool, since time.Duration, output io.WriteCloser) error { return c.Stream("/system/logs", map[string]string{ "Filter": filter, "Follow": fmt.Sprintf("%t", follow), "Since": since.String(), }, nil, output) }
// SetDuration adds or modifies the configuration for a given duration at a // specific key. func (c *Config) SetDuration(key string, value time.Duration) { c.mu.RLock() { c.m[key] = value.String() } c.mu.RUnlock() }
func saveDeployData(opts *DeployOpts, imageId, dlog string, duration time.Duration, deployError error) error { log.Debugf("%s in (%s)\n%s", cmd.Colorfy(opts.B.GetFullName(), "cyan", "", "bold"), cmd.Colorfy(duration.String(), "green", "", "bold"), cmd.Colorfy(dlog, "yellow", "", "")) //if there are deployments to track as follows in outputs: {} then do it here. //Riak: code to save the status of a deploy (created.) // deploy : // name: // status: /*deploy := DeployData { App: opts.App.Name, Timestamp: time.Now(), Duration: duration, Commit: opts.Commit, Image: imageId, Log: log, } if opts.Commit != "" { deploy.Origin = "git" } else if opts.Image != "" { deploy.Origin = "rollback" } else { deploy.Origin = "app-deploy" } if deployError != nil { deploy.Error = deployError.Error() } return db.Store(compid or assmid, &struct) */ return nil }
func saveLifecycleData(li *LifecycleOpts, llog string, elapsed time.Duration) error { log.Debugf("%s in (%s)\n%s", cmd.Colorfy(li.B.GetFullName(), "cyan", "", "bold"), cmd.Colorfy(elapsed.String(), "green", "", "bold"), cmd.Colorfy(llog, "yellow", "", "")) return nil }
// SendReadTimeout instructs the peer to simulate a read timeout. It then waits // for acknowledgement of the timeout, buffering any packets received since // then. The packets are then returned. func (p *packetAdaptor) SendReadTimeout(d time.Duration) ([][]byte, error) { p.log("Simulating read timeout: "+d.String(), nil) payload := make([]byte, 1+8) payload[0] = opcodeTimeout binary.BigEndian.PutUint64(payload[1:], uint64(d.Nanoseconds())) if _, err := p.Conn.Write(payload); err != nil { return nil, err } var packets [][]byte for { opcode, err := p.readOpcode() if err != nil { return nil, err } switch opcode { case opcodeTimeoutAck: p.log("Received timeout ACK", nil) // Done! Return the packets buffered and continue. return packets, nil case opcodePacket: // Buffer the packet for the caller to process. packet, err := p.readPacketBody() if err != nil { return nil, err } p.log("Simulating dropped packet", packet) packets = append(packets, packet) default: return nil, fmt.Errorf("unexpected opcode '%d'", opcode) } } }
// TODO: Place at the bottom of the content instead of at the bottom of the window func AddFooter(page *onthefly.Page, footerText, footerTextColor, footerColor string, elapsed time.Duration) (*onthefly.Tag, error) { body, err := page.GetTag("body") if err != nil { return nil, err } div := body.AddNewTag("div") div.AddAttrib("id", "notice") div.AddStyle("position", "fixed") div.AddStyle("bottom", "0") div.AddStyle("left", "0") div.AddStyle("width", "100%") div.AddStyle("display", "block") div.AddStyle("padding", "0") div.AddStyle("margin", "0") div.AddStyle("background-color", footerColor) div.AddStyle("font-size", "0.6em") div.AddStyle("text-align", "right") div.AddStyle("box-shadow", "1px -2px 3px rgba(0,0,0, .5)") innerdiv := div.AddNewTag("div") innerdiv.AddAttrib("id", "innernotice") innerdiv.AddStyle("padding", "0 2em 0 0") innerdiv.AddStyle("margin", "0") innerdiv.AddStyle("color", footerTextColor) innerdiv.AddContent("Generated in " + elapsed.String() + " | " + footerText) return div, nil }
func client(wg *sync.WaitGroup, host string, port, len int, duration time.Duration, chStat chan int) { defer func() { if r := recover(); r != nil { log.Printf("error: %s", r) } log.Printf("disconnected") wg.Done() }() log.Printf("connecting to %s:%d, len %d, duration %s", host, port, len, duration.String()) conn, err := utp.DialTimeout(fmt.Sprintf("%s:%d", host, port), time.Second) if err != nil { panic(err) } defer conn.Close() log.Printf("connected") buf := bytes.Repeat([]byte("H"), len) ts := time.Now() for time.Since(ts) < duration { n, err := conn.Write(buf) if err != nil { if err == io.EOF { break } panic(err) } chStat <- n } }
// RegisterServiceLocal registers the service as running on the current node. func RegisterServiceLocal( service string, instanceID string, target string, ttl time.Duration) (err error) { consul := config.GetConsulClient() agent := consul.Agent() // Parse target into address + port. addressSplit := strings.Split(target, ":") if len(addressSplit) != 2 { return fmt.Errorf("Invalid address") } port, err := strconv.Atoi(addressSplit[1]) if err != nil { return fmt.Errorf("Invalid address") } address := addressSplit[0] return agent.ServiceRegister(&consulapi.AgentServiceRegistration{ Name: service, ID: instanceID, Address: address, Port: port, Check: &consulapi.AgentServiceCheck{ TTL: ttl.String(), Status: "passing", }, }) }
// NewIngestionProcess creates a Process for ingesting data. func NewIngestionProcess(git *gitinfo.GitInfo, tileDir, datasetName string, ri ingester.ResultIngester, config map[string]string, every time.Duration, nCommits int, minDuration time.Duration, statusDir, metricName string) ProcessStarter { return func() { i, err := ingester.NewIngester(git, tileDir, datasetName, ri, nCommits, minDuration, config, statusDir, metricName) if err != nil { glog.Fatalf("Failed to create Ingester: %s", err) } glog.Infof("Starting %s ingester. Run every %s.", datasetName, every.String()) // oneStep is a single round of ingestion. oneStep := func() { glog.Infof("Running ingester: %s", datasetName) err := i.Update() if err != nil { glog.Error(err) } glog.Infof("Finished running ingester: %s", datasetName) } // Start the ingester. go func() { oneStep() for _ = range time.Tick(every) { oneStep() } }() } }
func bench(target string) { fmt.Println("begin bench to: " + target + " ...") succ := 0 fail := 0 var t time.Duration = 0 for i := 0; i < 10 || t.Seconds() < 30; i++ { start := time.Now() resp, err := net.NewHttpGet(target) if err == nil { defer resp.Close() _, err := resp.ReadAll() if err == nil { succ++ end := time.Now() t += end.Sub(start) continue } } fail++ } fmt.Printf("succ: %d\n", succ) if succ > 0 { fmt.Println("used: " + t.String()) fmt.Println("avg: " + time.Duration(int64(t)/int64(succ)).String()) } fmt.Printf("fail: %d\n", fail) }
// Helper function for creating an alert on low throughput, aka deadman's switch. // // - Threshold -- trigger alert if throughput drops below threshold in points/interval. // - Interval -- how often to check the throughput. // - Expressions -- optional list of expressions to also evaluate. Useful for time of day alerting. // // Example: // var data = stream.from()... // // Trigger critical alert if the throughput drops below 100 points per 10s and checked every 10s. // data.deadman(100.0, 10s) // //Do normal processing of data // data.... // // The above is equivalent to this // Example: // var data = stream.from()... // // Trigger critical alert if the throughput drops below 100 points per 10s and checked every 10s. // data.stats(10s) // .derivative('collected') // .unit(10s) // .nonNegative() // .alert() // .id('node \'stream0\' in task \'{{ .TaskName }}\'') // .message('{{ .ID }} is {{ if eq .Level "OK" }}alive{{ else }}dead{{ end }}: {{ index .Fields "collected" | printf "%0.3f" }} points/10s.') // .crit(lamdba: "collected" <= 100.0) // //Do normal processing of data // data.... // // The `id` and `message` alert properties can be configured globally via the 'deadman' configuration section. // // Since the AlertNode is the last piece it can be further modified as normal. // Example: // var data = stream.from()... // // Trigger critical alert if the throughput drops below 100 points per 1s and checked every 10s. // data.deadman(100.0, 10s).slack().channel('#dead_tasks') // //Do normal processing of data // data.... // // You can specify additional lambda expressions to further constrain when the deadman's switch is triggered. // Example: // var data = stream.from()... // // Trigger critical alert if the throughput drops below 100 points per 10s and checked every 10s. // // Only trigger the alert if the time of day is between 8am-5pm. // data.deadman(100.0, 10s, lambda: hour("time") >= 8 AND hour("time") <= 17) // //Do normal processing of data // data.... // func (n *node) Deadman(threshold float64, interval time.Duration, expr ...tick.Node) *AlertNode { dn := n.Stats(interval). Derivative("emitted").NonNegative() dn.Unit = interval an := dn.Alert() critExpr := &tick.BinaryNode{ Operator: tick.TokenLessEqual, Left: &tick.ReferenceNode{ Reference: "emitted", }, Right: &tick.NumberNode{ IsFloat: true, Float64: threshold, }, } // Add any additional expressions for _, e := range expr { critExpr = &tick.BinaryNode{ Operator: tick.TokenAnd, Left: critExpr, Right: e, } } an.Crit = critExpr // Replace NODE_NAME with actual name of the node in the Id. an.Id = strings.Replace(n.pipeline().deadman.Id(), nodeNameMarker, n.Name(), 1) // Set the message on the alert node. an.Message = strings.Replace(n.pipeline().deadman.Message(), intervalMarker, interval.String(), 1) return an }
// Send will update the given body with the paramters expected by a Pandora server // and return the Mid generated by the server or an error. func (mb *Mailbox) Send(from, to string, delay time.Duration, body url.Values) (string, error) { var msg pandora.Message msg.Empty(body) msg.SetSender(from) msg.SetReceiver(to) msg.SetClientTime(time.Now()) msg.Set("delay", delay.String()) res, err := mb.Client.PostForm(mb.BaseUrl+"/send", body) if err != nil { return "", err } defer res.Body.Close() if res.StatusCode != http.StatusOK { return "", fmt.Errorf("invalid status code: %v", res.StatusCode) } buf, err := ioutil.ReadAll(res.Body) if err != nil { return "", err } values, err := url.ParseQuery(string(buf)) if err != nil { return "", err } if len(values.Get("error")) > 0 { return "", errors.New(values.Get("error")) } return values.Get("mid"), nil }
// Fetch asks for the server for a message func (mb *Mailbox) Fetch(from string, lockFor time.Duration) (url.Values, error) { // now, try to consume the message msgToFetch := make(url.Values) msgToFetch.Set(pandora.KeyReceiver, from) msgToFetch.Set(pandora.KeyLeaseTime, lockFor.String()) res, err := mb.Client.PostForm(mb.BaseUrl+"/fetch", msgToFetch) if err != nil { return nil, err } defer res.Body.Close() if res.StatusCode == http.StatusNoContent { return nil, ErrNoData } if res.StatusCode != http.StatusOK { return nil, fmt.Errorf("invalid status code: %v", res.StatusCode) } buf, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err } respValues, err := url.ParseQuery(string(buf)) if err != nil { return nil, err } if len(respValues.Get("mid")) == 0 { return nil, fmt.Errorf("mid empty") } return respValues, nil }
func saveDeployData(opts *StateOpts, dlog string, duration time.Duration) error { log.Debugf("%s in (%s)\n%s", cmd.Colorfy(opts.B.GetFullName(), "cyan", "", "bold"), cmd.Colorfy(duration.String(), "green", "", "bold"), cmd.Colorfy(dlog, "yellow", "", "")) return nil }
func saveStateData(opts *StateChangeOpts, slog string, duration time.Duration, changeError error) error { log.Debugf("%s in (%s)\n%s", cmd.Colorfy(opts.B.GetFullName(), "cyan", "", "bold"), cmd.Colorfy(duration.String(), "green", "", "bold"), cmd.Colorfy(slog, "yellow", "", "")) if opts.B.Level == provision.BoxSome && opts.B.Repo.IsEnabled() { hookId, err := repository.Manager(opts.B.Repo.GetSource()).CreateHook(opts.B.Repo) if err != nil { return nil } comp, err := NewComponent(opts.B.Id) if err != nil { return err } if err = comp.setDeployData(DeployData{ Timestamp: time.Now(), Duration: duration, HookId: hookId, }); err != nil { return err } } return nil }
func respondSelected(w http.ResponseWriter, records interface{}, duration time.Duration) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{ "records": records, "duration": duration.String(), }) }
func calc_Duration_Done_All(Direction_id int) (string, string) { // var delta_work time.Duration var delta_all time.Duration tasks_work := GetTasks(Direction_id, true) for _, val := range tasks_work { var delta time.Duration var err error if val.IsDone { delta, err = time.ParseDuration(val.Duration) if err != nil { fmt.Printf("%v\n", err) } } else { delta, err = time.ParseDuration(val.Work_time) if err != nil { fmt.Printf("%v\n", err) } } delta_work += delta } tasks_all := GetTasks(Direction_id, false) for _, val := range tasks_all { delta, err := time.ParseDuration(val.Duration) if err != nil { fmt.Printf("%v\n", err) } delta_all += delta } return pretty_duration(delta_work.String()), pretty_duration(delta_all.String()) }
// Execute ... executes a command and returns the output // command: the command you wish to execute // args: an array of argument to pass to the command // timeout: a time.Duration to wait before killing off the command func Execute(timeout time.Duration, command string, args ...string) ([]byte, error) { var b bytes.Buffer if timeout.Seconds() <= 0 { timeout = time.Duration(10) * time.Second } glog.V(4).Infof("Attempting to execute the command: %s, args: [%s], timeout: %s", command, strings.Join(args, ","), timeout.String()) cmd := exec.Command(command, args...) cmd.Stdout = &b cmd.Stderr = &b cmd.Start() timer := time.AfterFunc(timeout, func() { err := cmd.Process.Kill() if err != nil { panic(err) } }) err := cmd.Wait() if err != nil { glog.Errorf("Failed to execute the command, error: %s", err) return []byte{}, err } // stop the timer timer.Stop() glog.V(5).Infof("Command output: %s", b.String()) return b.Bytes(), nil }
func (*PingCommand) Run(a subcommands.Application, args []string) int { start := time.Now() mgo := NewFromConfigFile("mgo.conf", true) pings := make([]time.Duration, 0) mgo.Discover(func(message Message) { ping := time.Since(start) pings = append(pings, ping) fmt.Printf("%-40s time=%s\n", message.Body.Senderid, ping.String()) }) var min, max, sum time.Duration min = pings[0] for _, ping := range pings { sum += ping if ping > max { max = ping } if ping < min { min = ping } } mean := time.Duration(int64(sum) / int64(len(pings))) fmt.Println() fmt.Println("--- ping statistics ---") fmt.Printf("%d replies max: %s min: %s avg: %s\n", len(pings), max.String(), min.String(), mean.String()) return 0 }
func saveBootData(boot *BootOpts, blog string, elapsed time.Duration) error { log.Debugf("%s in (%s)\n%s", cmd.Colorfy(boot.B.GetFullName(), "cyan", "", "bold"), cmd.Colorfy(elapsed.String(), "green", "", "bold"), cmd.Colorfy(blog, "yellow", "", "")) return nil }
// doShareUploadURL uploads files to the target. func doShareUploadURL(objectURL string, isRecursive bool, expiry time.Duration, contentType string) *probe.Error { clnt, err := newClient(objectURL) if err != nil { return err.Trace(objectURL) } // Generate pre-signed access info. uploadInfo, err := clnt.ShareUpload(isRecursive, expiry, contentType) if err != nil { return err.Trace(objectURL, "expiry="+expiry.String(), "contentType="+contentType) } // Generate curl command. curlCmd := makeCurlCmd(objectURL, isRecursive, uploadInfo) printMsg(shareMesssage{ ObjectURL: objectURL, ShareURL: curlCmd, TimeLeft: expiry, ContentType: contentType, }) // save shared URL to disk. return saveSharedURL(objectURL, curlCmd, expiry, contentType) }
func (s *Server) DoBatchRecording(name string, past time.Duration) (id string, err error) { v := url.Values{} v.Add("type", "batch") v.Add("name", name) v.Add("past", past.String()) r, err := http.Post(s.URL()+"/record?"+v.Encode(), "", nil) if err != nil { return } defer r.Body.Close() if r.StatusCode != http.StatusOK { err = fmt.Errorf("unexpected status code got %d exp %d", r.StatusCode, http.StatusOK) return } // Decode valid response type resp struct { RecordingID string `json:"RecordingID"` Error string `json:"Error"` } rp := resp{} d := json.NewDecoder(r.Body) d.Decode(&rp) if rp.Error != "" { err = errors.New(rp.Error) return } id = rp.RecordingID v = url.Values{} v.Add("id", id) _, err = s.HTTPGet(s.URL() + "/record?" + v.Encode()) return }
func respondDeleted(w http.ResponseWriter, n int, duration time.Duration) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{ "deleted": n, "duration": duration.String(), }) }
func Get_Statistic() []Statistic { // var result []Statistic for i := 0; i < 5; i += 1 { // map_direct_duration := make(map[string]time.Duration) dt_now := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC) //dur_day, _ := time.ParseDuration(fmt.Sprintf("%dh", 24 * i)) dt_now = dt_now.AddDate(0, 0, -1*i) //fmt.Printf("date:%v\n", dt_now) for _, val := range Task_map { DateDead, err := time.Parse("2006-01-02 15:04:05", val.DateDead) if err != nil { fmt.Printf("%v\n", err) } if DateDead.Unix() > dt_now.Unix() && DateDead.Unix() < dt_now.AddDate(0, 0, 1).Unix() { //fmt.Printf("DateDead:%v\n", DateDead) if val.IsDone { dur_task, err := time.ParseDuration(val.Duration) if err != nil { fmt.Printf("%v\n", err) } map_direct_duration[val.Direction] += dur_task } else { dur_task, err := time.ParseDuration(val.Work_time) if err != nil { fmt.Printf("%v\n", err) } if dur_task.Seconds() > 0 { map_direct_duration[val.Direction] += dur_task } } } } var list_info []Info_Direction var sum time.Duration for name, val := range map_direct_duration { var info Info_Direction info.Name = name info.Duration = pretty_duration(val.String()) list_info = append(list_info, info) sum += val } var sts Statistic sts.Date = print_date(dt_now) sts.List_Info_Direction = list_info sts.Sum_Duration = pretty_duration(sum.String()) //fmt.Printf("Statistic:%v\n", sts) if len(list_info) > 0 { result = append(result, sts) } } return result }
func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo, AdminKey string, totalTime time.Duration, encrypted bool, noprompt bool, locked bool, recentlyEdited []string) { originalText := currentText CodeType := getCodeType(title) if CodeType == "markdown" { CodeType = "" } r, _ := regexp.Compile("\\[\\[(.*?)\\]\\]") for _, s := range r.FindAllString(currentText, -1) { currentText = strings.Replace(currentText, s, "["+s[2:len(s)-2]+"](/"+s[2:len(s)-2]+"/view)", 1) } unsafe := blackfriday.MarkdownCommon([]byte(currentText)) pClean := bluemonday.UGCPolicy() pClean.AllowElements("img") pClean.AllowAttrs("alt").OnElements("img") pClean.AllowAttrs("src").OnElements("img") pClean.AllowAttrs("class").OnElements("a") pClean.AllowAttrs("href").OnElements("a") pClean.AllowAttrs("id").OnElements("a") pClean.AllowDataURIImages() html := pClean.SanitizeBytes(unsafe) html2 := string(html) r, _ = regexp.Compile("\\$\\$(.*?)\\$\\$") for _, s := range r.FindAllString(html2, -1) { html2 = strings.Replace(html2, s, "<span class='texp' data-expr='"+s[2:len(s)-2]+"'></span>", 1) } r, _ = regexp.Compile("\\$(.*?)\\$") for _, s := range r.FindAllString(html2, -1) { html2 = strings.Replace(html2, s, "<span class='texi' data-expr='"+s[1:len(s)-1]+"'></span>", 1) } html2 = strings.Replace(html2, "&#36;", "$", -1) html2 = strings.Replace(html2, "&#91;", "[", -1) html2 = strings.Replace(html2, "&#93;", "]", -1) html2 = strings.Replace(html2, "&35;", "#", -1) totalTimeString := totalTime.String() if totalTime.Seconds() < 1 { totalTimeString = "< 1 s" } if encrypted { CodeType = "asciiarmor" } c.HTML(http.StatusOK, "view.tmpl", gin.H{ "Title": title, "WikiName": RuntimeArgs.WikiName, "Body": template.HTML([]byte(html2)), "CurrentText": originalText, "Versions": versions, "TotalTime": totalTimeString, "AdminKey": AdminKey, "Encrypted": encrypted, "Locked": locked, "Prompt": noprompt, "LockedOrEncrypted": locked || encrypted, "Coding": len(CodeType) > 0, "CodeType": CodeType, "RecentlyEdited": recentlyEdited, }) }
func (s JSONStruct) SetDuration(dotPath string, value time.Duration) error { parent, lastKey, err := s.findParent(dotPath) if err != nil { return err } parent[lastKey] = value.String() return nil }
func (ur UptimeReports) totalMonthlyDowntime() string { var sum time.Duration for _, u := range ur { sum += time.Duration(u.MonthAgo.Totaldown) } sum = sum * time.Second return sum.String() }