package main

import (
	"sync"
	"time"
)

/**
增加数据监控服务，每15分钟保存一次抽取情况
**/
var IS = &InfoStatus{
	time.Now().Unix(), 0, map[string]int{}, &sync.Mutex{}}

func init() {
	go IS.Save()
}

type InfoStatus struct {
	Starttime int64
	Endtime   int64
	Val       map[string]int
	Lock      *sync.Mutex
}

func (is *InfoStatus) Add(t string) {
	is.Lock.Lock()
	is.Val[t]++
	is.Lock.Unlock()
}

func (is *InfoStatus) Save() {
	is.Lock.Lock()
	is.Endtime = time.Now().Unix()
	save := map[string]interface{}{}
	all := 0
	for _, v := range is.Val {
		all += v
	}
	if all > 0 {
		save["receive"] = all
		save["starttime"] = is.Starttime
		save["endtime"] = is.Endtime
		save["flag"] = "projectser"
		is.Val = map[string]int{}
		go MQFW.Save("datamonitor", save)
	}
	is.Starttime = is.Endtime
	is.Lock.Unlock()
	time.AfterFunc(15*time.Minute, is.Save)
}
