首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux AWK统计出账

linux AWK统计出账

作者头像
葫芦
发布2019-04-17 16:27:44
2.8K0
发布2019-04-17 16:27:44
举报
文章被收录于专栏:葫芦葫芦
#!/bin/awk -f

BEGIN{
    MAX = 0
    MIN = 9999999
    
    CCID=ENVIRON["ccid"]
    T=ENVIRON["date"]
    O=ENVIRON["opened"]
    SQLNAME=ENVIRON["SQL"]
    price=ENVIRON["price"]
    cost_total = ENVIRON["cost"]
    
    no_close = 0
    invalid_ebit_counts = 0
    valid_ebit_counts = 0
    total_gt5 = 0
    total_gt90 = 0
    close_only = 0   
    opened = 0
}

NF < 5 { next }

NF == 5 && !/close/{
	no_close_acct[$1] = 0;
    no_close++;
    opened++;
    next
}

NF == 5 && !/open/ {
    close_only++;
    next
}

/open/ {
    line++
}

/open/ && /close/{
    ## 初始化当前用户的计数器
    lt_5min[$1] = 0
    gt_90min[$1] = 0
    gt_5min[$1] = 0

    for (i=7;i<=NF;i+=2) {

        ## 计算 open 总数
        if ($i == "speedup/open") {
            opened++
        }

        if (($i == "speedup/close") && ($(i-2) == "speedup/open")) {
            x = $(i-1) - $(i-3) # 单次加速时长
            y = $(i-3) - $(i-5) # 两次加速之间间隔多久,查过86400秒要重新计算费用
            ## 单用户累计耗时
            total_elapse[$1]+=x
            if (x < 300) {
                ## 单用户单次耗时小于5分钟
                lt_5min[$1]++
            }

            if (x >= 5400 ) {
                ## 单次加速超过5分钟的用户
                if (y < 86400) {
                    ## 如果没有跨天,直接次数+2
                    gt_90min[$1]++
                } else {
                    ## 如果两次请求已经跨天
                    cross_day_90min[$1]+=2
                }
            }

            if (x >= 300 && x < 5400) {
                #gt_5min[$1]++

                if (y >= 86400) {
                    cross_day_5min[$1]++
                } else {
                    gt_5min[$1]++
                }
            }
        }
    }

    ## 算一个累计值
    if (total_elapse[$1] > 300)  {
        total_gt5++
    } else {
        total_lt5++
    }

    ## 判断该用户记多少次
    if (gt_90min[$1] > 0) {
        valid_ebit_counts += 2
        valid_ebit_counts += cross_day_90min[$1]
        total_gt90++
    } else {
        if (gt_5min[$1] > 0) {
            valid_ebit_counts++
            valid_ebit_counts += cross_day_5min[$1]
        } else {
            invalid_ebit_counts++
        }
    }
}

/open/ && !/close/ { ## 未收到close请求的数量
    opened++;
    no_close++
    no_close_acct[$1] = 0
}

/close/ && !/open/  {
    ## 只有 close 请求的记录数
    close_only++

     #if (($(NF-1) - 1454256000) >300) {
     #   close_only_valid++
     #}
    
}
END{
    total = no_close + valid_ebit_counts
    
    if (! price) {

        if (total < 50000) {
            price = 0.5
        }

        if (total >= 50000) {
            price = 0.4
        }

        if (total >= 200000) {
            price = 0.3
        }

        if (total >= 1000000) {
            price = 0.2
        }
    }

    cost = total * price
    opened += line
    
    if (CCID == 0) {
        cost_total = cost
    }
    
    print "合理计费用户次数2(按单次时长计算):" no_close + valid_ebit_counts
    print "    单次加速超过5分钟(含超过1.5h)的用户数: " valid_ebit_counts
    print "    未请求关闭的用户数: " no_close
    print "    总成本: " cost
    print "    单价: " price
    print "其中总 open 次数: " opened
    print "其中总 open 次数2: " O
    print "单次加速时长超过1.5h 的用户数: " total_gt90
    print "单次加速时长小于5分钟的用户数: " invalid_ebit_counts
    print "仅有关闭请求的用户:" close_only
    print ""
    #print "合理计费用户次数1(按累计时长计算):" no_close + total_gt5 + close_only
    #print "累计加速时长超过5分钟的用户数(含超过1.5h的用户): " total_gt5
    #print "累计加速时长 未 超过5分钟的用户数: " total_lt5
    
    
    l = "INSERT INTO ebit_accounting VALUES (" CCID ",\047"T"\047," 
    l = l " " no_close + valid_ebit_counts ","cost", 1," price ", 0, "
    l = l " " O ","no_close"," total_gt90 "," valid_ebit_counts - total_gt90 ","
    l = l " " 100*total/O ", " 100*cost/cost_total ")"
    print l > SQLNAME
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/09/21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档