我正在研究一个自定义节拍,它解码一个二进制文件以提取数据并将其发送到 elasticsearch,复杂化正常但是当我运行它时,它给我一个无效持续时间“ns”的运行时间错误这是我的代码:
package beater
import (
"fmt"
"io/ioutil"
"math"
"time"
//"log"
"strconv"
"strings"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/publisher"
"github.com/bob96/hc34/config"
)
type Hc34 struct {
done chan struct{}
config config.Config
client publisher.Client
siteKey string
fileKey string
callEndDateTime string
callStartDateTime string
callerPartyNumber string
calledPartyNumber string
incomingTrunkKey string
outgoingTrunkKey string
blockNumber string
blockDateTime string
headerFunction int
headerUser int
callerCategory int
callerOrigineIdentity string
complementaryIdentity string
calledDigits string
traficType int
serviceType int
supportService int
chargeDuration int
chargeMeters int
outgoingTrunk string
reserved string
operatorIdentity string
operatorServiceType string
block string
filekey string
lastIndexTime time.Time
}
// Creates beater
func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
config := config.DefaultConfig
if err := cfg.Unpack(&config); err != nil {
return nil, fmt.Errorf("Error reading config file: %v", err)
}
bt := &Hc34{
done: make(chan struct{}),
config: config,
}
return bt, nil
}
func (bt *Hc34) Run(b *beat.Beat) error {
logp.Info("hc34 is running! Hit CTRL-C to stop it.")
bt.client = b.Publisher.Connect()
ticker := time.NewTicker(bt.config.Period)
//counter := 1
for {
now := time.Now()
bt.hc34DataHolderfunc(bt.config.Path)
bt.lastIndexTime = now
logp.Info("Event sent")
select {
case <-bt.done:
return nil
case <-ticker.C:
}
/*event := common.MapStr{
"@timestamp": common.Time(time.Now()),
"type": b.Name,
"counter": counter,
}
bt.client.PublishEvent(event)
logp.Info("Event sent")
counter++*/
}
}
func (bt *Hc34) Stop() {
bt.client.Close()
close(bt.done)
}
//checking error
func check(e error) {
if e != nil {
panic(e)
}
}
func (bt *Hc34) hc34DataHolderfunc(dirfile string) {
//reading the binary file
data, err := ioutil.ReadFile(dirfile)
check(err)
//t := f.ModTime()
bt.hc34decoderfunc(string(data))
event := common.MapStr{
"@timestamp": common.Time(time.Now()),
/*"siteKey": siteKey,
"fileKey": fileKey,*/
//"callEndDateTime": bt.callEndDateTime,
//"callStartDateTime": bt.callStartDateTime,
"callerPartyNumber": bt.callerPartyNumber,
"calledPartyNumber": bt.calledPartyNumber,
//"incomingTrunkKey": bt.incomingTrunkKey,
//"outgoingTrunkKey": bt.outgoingTrunkKey,
"blockNumber": bt.blockNumber,
"blockDateTime": bt.blockDateTime,
"headerFunction": bt.headerFunction,
"headerUser": bt.headerUser,
"callerCategory": bt.callerCategory,
"callerOrigineIdentity": bt.callerOrigineIdentity,
"complementaryIdentity": bt.complementaryIdentity,
"calledDigits": bt.calledDigits,
"traficType": bt.traficType,
"serviceType": bt.serviceType,
"supportService": bt.supportService,
"chargeDuration": bt.chargeDuration,
"chargeMeters": bt.chargeMeters,
//"outgoingTrunk": bt.outgoingTrunk,
"reserved": bt.reserved,
"peratorIdentity": bt.operatorIdentity,
"operatorServiceType": bt.operatorServiceType,
}
bt.client.PublishEvent(event)
}
type hc34decoder struct{
block string
siteKey string
filekey string
}
func (bt *Hc34) hc34decoderfunc(block string){
// Get header (32 bytes) and content (1932 bytes) from block
header :=block[0:64]
content :=block[64:len(block)]
// Get block key and date time from header
blockKey :=header[4:10]
yearBlock :=header[10:12]
//BDTdecode, err := hex.DecodeString(header[10:12])
bt.blockDateTime =bt.getDate(bt.getDateBlockHeader(header[10:22]))
/*if err != nil {
log.Fatal(err)
}*/
// process content to extract CDRs
start := 0
totalCdr := len(content) / 84
for z := 0; z < totalCdr; z++ {
record := content[start:start + 84]
start = start + 84
// Extract data from single record
//CEDdecode, err :=hex.DecodeString(record[10:12])
a:= hex2decimal(record[54:56])
headerFunction:= hex2decimal(record[0:2])
headerUser:= hex2decimal(record[2:4])
callerCategory:= hex2decimal(record[4:6])
callEndDateTime :=bt.getDate(bt.getDateCall(record[6:16], yearBlock))
callerOrigineIdentity:= strings.Replace(string(record[16:26]),"F","",-1)
complementaryIdentity := strings.Replace(string(record[26:36]),"F","",-1)
calledDigits:= strings.Replace(string(record[36:54]),"F","",-1)
traficType:= hex2decimal(strings.Replace(string(a),"F","",-1))
serviceType := hex2decimal(record[56:58])
supportService :=hex2decimal(record[58:60])
duration :=hex2decimal(record[60:64])
meters:= hex2decimal(record[64:70])
//outgoingTrunk :=strings.Replace(string(record[70:74]),"F", "",-1)
bt.reserved = record[74:78]
bt.operatorIdentity = strings.Replace(string(record[78:82]),"F", "",-1)
bt.operatorServiceType =record[82:84]
bt.blockNumber=blockKey
bt.callerPartyNumber = bt.getFormattedMsisdnHc34Caller(complementaryIdentity, callerOrigineIdentity)
bt.calledPartyNumber = bt.getFormattedMsisdnHc34Called(calledDigits)
bt.callStartDateTime = bt.getStartDate(callEndDateTime, int(duration) )
/*if err !=nil{
log.Fatal(err)
}*/
bt.headerFunction=int(headerFunction)
bt.headerUser=int(headerUser)
bt.callerCategory=int(callerCategory)
bt.traficType=int(traficType)
bt.serviceType=int(serviceType)
bt.supportService=int(supportService)
bt.chargeMeters=int(meters)
bt.chargeDuration=int(duration)
func (bt *Hc34) getStartDate(endTime string, duration int) string{
layout := "2006-01-02T15:04:05.000Z"
t, err := time.Parse(layout, endTime)
if err!=nil{
panic(err)
}
return (t.Add(time.Duration(-duration) * time.Second)).String()
}
/**
* Get the date of the call.
* @param input the quanted date
* @param year the year
* @return the date of the call
*/
func (bt *Hc34) getDateCall(input string,year string) string{
quant:= hex2decimal(input[1:4])
hour := input[4:len(input)]
return "20" + year + bt.getQuantDate(quant) + "" + hour
}
/**
* Get date of block header.
* @param input the quanted date
* @return the date block header
*/
func(bt *Hc34) getDateBlockHeader(input string) string{
year := input[0:2]
quant:= hex2decimal(input[3:6])
hour := input[6:len(input)];
/*if err!=nil{
panic(err)
}*/
return "20" + year + bt.getQuantDate(quant) + "" + hour
}
/**
* format date.
* @param date the date
* @return the formatted date
*/
func(bt *Hc34) getDate(date string) string{
year := date[0:4]
month := date[4:6]
day := date[6:8]
hour := date[8:10]
minute := date[10:12]
second := date[12:14]
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
/**
* Format quanted date.
* @param quant the quanted date
* @return the formatted date
*/
func(bt *Hc34) getQuantDate(quant int) string{
nanoDayOfYear:=string(quant*24*60*60*1000*1000*1000) + "ns"
durt,err:=time.ParseDuration(nanoDayOfYear)
t := time.Date(2009, time.January, 01, 00, 0, 0, 0, time.UTC)
t=t.Add(durt)
if err!=nil{
panic(err)
}
return string(t.Month())+""+string(t.Day())
}
func bin(i int, prefix bool) string {
i64 := int64(i)
if prefix {
return "0b" + strconv.FormatInt(i64, 2) // base 2 for binary
} else {
return strconv.FormatInt(i64, 2) // base 2 for binary
}
}
func bin2int(binStr string) int {
// base 2 for binary
result, _ := strconv.ParseInt(binStr, 2, 64)
return int(result)
}
func hex(i int, prefix bool) string {
i64 := int64(i)
if prefix {
return "0x" + strconv.FormatInt(i64, 16) // base 16 for hexadecimal
} else {
return strconv.FormatInt(i64, 16) // base 16 for hexadecimal
}
}
func hex2int(hexStr string) int {
// base 16 for hexadecimal
result, _ := strconv.ParseInt(hexStr, 16, 64)
return int(result)
}
func hex2decimal(hexStr string) int{
base16 := 16;
var val float64
for i:=0; i>len(hexStr);i++{
vala:=hex2int(string(base16))
valo:=hex2int(string(hexStr[len(hexStr)-i]))
val+=float64(valo)*math.Pow(float64(vala),float64(i))
}
return int(val)
}
这是我遇到的错误:
panic: time: invalid duration ns
goroutine 1 [running]:
github.com/bob96/hc34/beater.(*Hc34).getQuantDate(0xc42012a8c0, 0x0, 0x0, 0x9)
/home/hp/src/github.com/bob96/hc34/beater/hc34.go:354 +0x2cf
github.com/bob96/hc34/beater.(*Hc34).getDateBlockHeader(0xc42012a8c0, 0xc4202d000a, 0xc, 0x0, 0x0)
/home/hp/src/github.com/bob96/hc34/beater/hc34.go:324 +0x73
github.com/bob96/hc34/beater.(*Hc34).hc34decoderfunc(0xc42012a8c0, 0xc4202d0000, 0x2d128)
/home/hp/src/github.com/bob96/hc34/beater/hc34.go:190 +0x77
github.com/bob96/hc34/beater.(*Hc34).hc34DataHolderfunc(0xc42012a8c0, 0xc420103140, 0x5e)
/home/hp/src/github.com/bob96/hc34/beater/hc34.go:141 +0xd3
github.com/bob96/hc34/beater.(*Hc34).Run(0xc42012a8c0, 0xc420142420, 0xc4201424c8, 0xb)
/home/hp/src/github.com/bob96/hc34/beater/hc34.go:105 +0x109
github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat.(*Beat).launch(0xc420142420, 0x9a84c8, 0x0, 0x0)
/home/hp/src/github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat/beat.go:211 +0x706
github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat.Run(0x9855d5, 0x4, 0x0, 0x0, 0x9a84c8, 0xc4200001a0, 0xc4200001a0)
/home/hp/src/github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat/beat.go:136 +0x65
main.main()
/home/hp/src/github.com/bob96/hc34/main.go:12 +0x54
我首先想到的是日期函数不会从文件中获取任何字符串,因为我们不能有持续时间类型“ns”,但我们需要在“ns”之前有一个整数,就像这样:“500ns”。
抱歉,如果我把所有代码都放出来,我只是不明白我的问题到底在哪里。
谢谢你帮助我!
最佳答案
这一行看起来有问题:
nanoDayOfYear:=string(quant*24*60*60*1000*1000*1000) + "ns"
具体来说,string(...) 不会执行您想要的操作。大概您想将该数字转换为以 10 为基数的字符串。试试这个:
nanoDayOfYear:=strconv.FormatInt(quant*24*60*60*1000*1000*1000, 10) + "ns"
关于go - 自定义节拍运行错误: invalid duration "ns",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45500420/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r