public void SaveMonthAggDataTester()
        {
            AggResult result = CreateData("201502", AggType.Month);

            AggDataChangeCaculate caculate = new AggDataChangeCaculate();

            caculate.ProcessAggResult(ref result);

            Assert.IsNotNull(result.AggDataChanges);
            Assert.IsTrue(result.AggDataChanges.Count == result.AggDatas.Count);

            int num = 1;

            foreach (var aggdata in result.AggDataChanges)
            {
                foreach (var val in aggdata.Values)
                {
                    Assert.IsTrue(val == Convert.ToDouble(-10 * num));
                    num++;
                }
            }
            AggDataStorage storage = new AggDataStorage();

            Assert.IsTrue(storage.ProcessAggResult(ref result)); /// insert test
            Assert.IsTrue(storage.ProcessAggResult(ref result)); /// updatetest
        }
Beispiel #2
0
        public void NoLastestDataTester()
        {
            AggResult result = new AggResult(1, 1, "20150110", AggType.Day, 1);

            result.AggDatas = new List <AggData>();

            AggData data = new AggData();

            data.SensorId = 100;
            data.Values   = new List <double>();

            for (int i = 0; i < 3; i++)
            {
                data.Values.Add((i + 1) * 10);
            }
            result.AggDatas.Add(data);

            AggDataChangeCaculate caculate = new AggDataChangeCaculate();

            caculate.ProcessAggResult(ref result);

            Assert.IsNotNull(result.AggDataChanges);
            Assert.IsTrue(result.AggDataChanges.Count == result.AggDatas.Count);

            foreach (var aggdata in result.AggDataChanges)
            {
                foreach (var val in aggdata.Values)
                {
                    Assert.IsTrue(val == Convert.ToDouble(0));
                }
            }
        }
Beispiel #3
0
        public bool ProcessAggResult(ref AggResult result)
        {
            if (result == null || result.AggDatas.Count == 0)
            {
                Log.Info("agg data storage failed, para is null!");
                return(false);
            }

            SeclureCloudDbHelper dbHelper = SeclureCloudDbHelper.Instance();

            if (dbHelper == null)
            {
                Log.Info("agg data storage failed, dbhelper is null!");
                return(false);
            }

            Log.InfoFormat("struct:{0},factorId:{1},type:{2}, statrt data storage...", result.StructId, result.SafeFactorId, result.AggType);
            if (dbHelper.Accessor.SaveAggResult(result) > 0)
            {
                Log.InfoFormat("struct:{0},factorId:{1},type:{2}, data storage sucessful!", result.StructId, result.SafeFactorId, result.AggType);
                return(true);
            }
            else
            {
                Log.InfoFormat("struct:{0},factorId:{1},type:{2}, data storage failed!", result.StructId, result.SafeFactorId, result.AggType);
                return(false);
            }
        }
Beispiel #4
0
        /// <summary>
        ///保存聚集数据结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public int SaveAggResult(AggResult result)
        {
            if (result.AggDatas == null || result.AggDataChanges == null)
            {
                return(-1);
            }

            int dateTimeId = this.GetDateTimeId(result.AggType, result.TimeTag);

            if (dateTimeId < 1)
            {
                return(0);
            }

            List <string> sqlCmds = new List <string>();

            for (int i = 0; i < result.AggDatas.Count; i++)
            {
                sqlCmds.Add(
                    this.CreateAddOrUpdateAggDataSql(
                        result.StructId,
                        result.SafeFactorId,
                        dateTimeId,
                        result.AggType,
                        result.AggDatas[i],
                        result.AggDataChanges[i], result.ConfigId));
            }

            return(this.helper.ExecuteSqlTran(sqlCmds));
        }
Beispiel #5
0
        public virtual AggResult AggProcess(AggRawData datas)
        {
            if (datas == null || datas.Datas.Count == 0 || datas.Datas[0].Values.Count == 0)
            {
                return(null);
            }

            AggResult result = new AggResult(datas.StructId, datas.FactorId, datas.TimeTag, datas.Type, datas.ConfigId);

            result.AggDatas = new List <AggData>();
            int ColNum = datas.Datas[0].Values[0].Count; // 监测数据项个数
            int id     = 0;

            List <double> temp = new List <double>();

            foreach (var aggRawData in datas.Datas)
            {
                if (aggRawData.Values == null)
                {
                    continue;
                }

                id = aggRawData.SensorId;
                AggData aggData = new AggData();
                aggData.SensorId = aggRawData.SensorId;
                for (int i = 0; i < ColNum; i++)
                {
                    if (temp.Count > 0)
                    {
                        temp.Clear();
                    }
                    foreach (var value in aggRawData.Values)
                    {
                        if (i < aggRawData.Values[i].Count)
                        {
                            temp.Add(value[i]);
                        }
                    }
                    if (temp.Count > 0)
                    {
                        aggData.Values.Add(this.GetAggValue(temp));
                    }
                }
                if (aggData.Values.Count > 0)
                {
                    result.AggDatas.Add(aggData);
                }
            }

            result.LastAggDatas = datas.LastAggDatas;
            return(result);
        }
        public void AggResultDeepCopyTest()
        {
            AggResult result = AggDataStorageTester.CreateData("20150101", AggType.Day);
            AggResult copy   = ObjectHelper.DeepCopy(result);

            Assert.IsNotNull(copy);

            Assert.IsTrue(copy.AggType == result.AggType);
            Assert.IsTrue(copy.StructId == result.StructId);

            copy.LastAggDatas.Clear();
            Assert.IsFalse(copy.LastAggDatas.Count == result.LastAggDatas.Count);
        }
Beispiel #7
0
        public void MissSomeLastAggDataTester()
        {
            AggResult result = new AggResult(1, 1, "20150110", AggType.Day, 1);

            result.AggDatas     = new List <AggData>();
            result.LastAggDatas = new List <AggData>();
            AggData data     = new AggData();
            AggData lastData = new AggData();

            data.SensorId     = 100;
            data.Values       = new List <double>();
            lastData.SensorId = 100;
            lastData.Values   = new List <double>();

            for (int i = 0; i < 3; i++)
            {
                data.Values.Add((i + 1) * 10);
            }
            for (int i = 0; i < 2; i++)
            {
                lastData.Values.Add((i + 1) * 20);
            }
            result.AggDatas.Add(data);
            result.LastAggDatas.Add(lastData);

            AggDataChangeCaculate caculate = new AggDataChangeCaculate();

            caculate.ProcessAggResult(ref result);

            Assert.IsNotNull(result.AggDataChanges);
            Assert.IsTrue(result.AggDataChanges.Count == result.AggDatas.Count);

            // int num = 1;
            foreach (var aggdata in result.AggDataChanges)
            {
                for (int i = 0; i < aggdata.Values.Count; i++)
                {
                    if (i != aggdata.Values.Count - 1)
                    {
                        Assert.IsTrue(aggdata.Values[i] == Convert.ToDouble(-10 * (i + 1)));
                    }
                    else
                    {
                        Assert.IsTrue(aggdata.Values[i] == Convert.ToDouble(0));
                    }
                }
            }
        }
Beispiel #8
0
        public bool ProcessAggResult(ref AggResult result)
        {
            if (result == null)
            {
                Log.Info("agg data change caculate failed, para is null!");
                return(false);
            }

            Log.InfoFormat("struct:{0},factorId:{1},type:{2}, statrt AggDataChangeCaculate...", result.StructId, result.SafeFactorId, result.AggType);

            List <AggData> lastAggDatas = result.LastAggDatas;

            result.AggDataChanges = new List <AggData>();
            foreach (AggData aggData in result.AggDatas)
            {
                AggData lasAggRawData;
                AggData dataChange = new AggData();
                dataChange.SensorId = aggData.SensorId;
                ///存在上次聚集数据
                if (GetLastAggData(aggData.SensorId, lastAggDatas, out lasAggRawData))
                {
                    for (int i = 0; i < aggData.Values.Count; i++)
                    {
                        if (i < lasAggRawData.Values.Count)
                        {
                            dataChange.Values.Add(aggData.Values[i] - lasAggRawData.Values[i]);
                        }
                        else
                        {
                            dataChange.Values.Add(defalultValue);
                        }
                    }
                }
                else /// 无上次聚集数据
                {
                    for (int i = 0; i < aggData.Values.Count; i++)
                    {
                        dataChange.Values.Add(defalultValue);
                    }
                    Log.InfoFormat("sensorid:{0},timetag:{1},type:{2}, has no last agg data", aggData.SensorId, result.TimeTag, result.AggType);
                }
                result.AggDataChanges.Add(dataChange);
            }
            Log.InfoFormat("struct:{0},factorId:{1},type:{2}, end AggDataChangeCaculate...", result.StructId, result.SafeFactorId, result.AggType);
            return(true);
        }
        public void MinProcessTester()
        {
            ProcessFactory.Init();
            IAggProcess process = ProcessFactory.GetAggProcess(AggWay.Min);

            AggResult result = process.AggProcess(CreateAggRawData());

            Assert.IsNotNull(result);

            foreach (var aggdata in result.AggDatas)
            {
                Assert.IsTrue(aggdata.Values.Count == 3);
                foreach (var value in aggdata.Values)
                {
                    Assert.IsTrue(value == Convert.ToDouble(100));
                }
            }
        }
        public void OnAggResultProduced(AggResult result)
        {
            if (result == null || result.AggDatas.Count == 0)
            {
                Log.Info("has no new agg data, agg consumers process finished!");
                return;
            }
            Log.InfoFormat("struct:{0},factorId:{1},type:{2}, agg consumers process start...", result.StructId, result.SafeFactorId, result.AggType);
            AggResult resultCopy = ObjectHelper.DeepCopy(result);

            foreach (var Consumer in ConsumerQueues)
            {
                bool ret = Consumer.ProcessAggResult(ref resultCopy);

                if (!ret)
                {
                    Log.ErrorFormat("{0},Process aggresult failed!", Consumer.GetConsumerName());
                }
            }
            Log.InfoFormat("struct:{0},factorId:{1},type:{2}, agg consumers process finished", result.StructId, result.SafeFactorId, result.AggType);
        }
        public static AggResult CreateData(string timeTag, AggType type)
        {
            AggResult result = new AggResult(90, 42, timeTag, type, 1);

            result.AggDatas     = new List <AggData>();
            result.LastAggDatas = new List <AggData>();
            AggData data     = new AggData();
            AggData lastData = new AggData();

            data.SensorId     = 1758;
            data.Values       = new List <double>();
            lastData.SensorId = 1758;
            lastData.Values   = new List <double>();

            for (int i = 0; i < 2; i++)
            {
                data.Values.Add((i + 1) * 10);
                lastData.Values.Add((i + 1) * 20);
            }
            result.AggDatas.Add(data);
            result.LastAggDatas.Add(lastData);
            return(result);
        }