Ejemplo n.º 1
0
        void Should_generate_id_with_correct_format()
        {
            string id          = "date_20170808";
            var    generatedId = AppUsageRecord.GetGeneratedId(new DateTime(2017, 8, 8));

            Assert.Equal(id, generatedId);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Get(DateTime date)
        {
            var appUsage = await _repository.GetAsync(AppUsageRecord.GetGeneratedId(date));

            var processInfoCollection = Mapper.Map <IEnumerable <ProcessInfo>, IEnumerable <ProcessInfoDto> >(appUsage.ActiveApps.Select(c => c.Value));

            return(Ok(processInfoCollection));
        }
        private void PersistentRecords(object state)
        {
            Debug.WriteLine($"{nameof(PersistentRecords)} is running on thread id={Thread.CurrentThread.ManagedThreadId}");

            if (_isIdle)
            {
                return;
            }

            if (_mainRepositoryUnavailable)
            {
                var result = TryGetFromMainRepository();

                if (result.Item2 == false)
                {
                    BackupLocally();
                    return;
                }
                else
                {
                    _mainRepositoryUnavailable = false;

                    if (result.Item1 != null)
                    {
                        _bufferedTodayAppRecord.MergeWith(result.Item1);
                        _backupRepository.Delete(_bufferedTodayAppRecord.Id);
                    }
                }
            }

            try
            {
                var todayPersistentRecord = _repository.Get(AppUsageRecord.GetGeneratedId(DateTime.Now));
                if (todayPersistentRecord != null)
                {
                    todayPersistentRecord.ActiveApps = _bufferedTodayAppRecord.ActiveApps;

                    _repository.Update(todayPersistentRecord, todayPersistentRecord.Id);
                }
                else
                {
                    _repository.Add(_bufferedTodayAppRecord);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error when persisting data: {ex.Message}");

                LoggingService.LogException(ex);

                _mainRepositoryUnavailable = true;

                this.PersistentRecords(state);
            }


            Debug.WriteLine("Persisting data sucessfully");
        }
Ejemplo n.º 4
0
        void Should_instantiate_with_correct_properties()
        {
            var date   = new DateTime(2018, 1, 1);
            var id     = "date_20180101";
            var record = new AppUsageRecord(date);

            Assert.Equal(record.Date, date);
            Assert.Equal(record.Id, id);
            Assert.True(record.ActiveApps.Count == 0);
        }
Ejemplo n.º 5
0
        public void Update(AppUsageRecord record)
        {
            var store = DocumentStoreHolder.RavenStore;

            if (store.WasDisposed)
            {
                return;
            }

            using (var session = store.OpenSession())
            {
                store.DatabaseCommands.Put(record.Id, null, RavenJObject.FromObject(record), new RavenJObject());

                session.SaveChanges();
            }
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> GetPredictedForDate(DateTime date)
        {
            var appUsage = await _appUsageRecordRepository.GetAsync(AppUsageRecord.GetGeneratedId(date));

            var processInfoCollection = appUsage.ActiveApps.Select(pair => pair.Value).ToArray();
            var dtos = Mapper.Map <ProcessInfoLabeledItemDto[]>(processInfoCollection);

            var results = _predictKarmaService.Predict(processInfoCollection);

            dtos.Merge(results, (dto, category) =>
            {
                dto.Category = category;
            });

            return(Ok(dtos));
        }
Ejemplo n.º 7
0
        public void Add(AppUsageRecord record)
        {
            var store = DocumentStoreHolder.RavenStore;

            if (store.WasDisposed)
            {
                return;
            }

            using (var session = store.OpenSession())
            {
                session.Store(record, record.Id);

                session.SaveChanges();
            }
        }
Ejemplo n.º 8
0
        private void MonitorUserActiveProcess(object state)
        {
            if (DateTime.Now.Date != _bufferedTodayAppRecord.Date)
            {
                _bufferedTodayAppRecord = new AppUsageRecord(DateTime.Now);
            }

            if (this._isIdle = IsUserIdle())
            {
                return;
            }

            var activeProcess = ProcessUtils.GetActiveProcess();

            if (activeProcess != null)
            {
                var processInfoId = ProcessInfoFactory.CreateId(activeProcess);
                var activeApps    = _bufferedTodayAppRecord.ActiveApps;

                ProcessInfo processInfo = null;
                if (!string.IsNullOrWhiteSpace(processInfoId) && activeApps.ContainsKey(processInfoId))
                {
                    processInfo = activeApps[processInfoId];
                    processInfo.TotalAmountOfTime += TimeSpan.FromSeconds(Settings.MonitorInterval);
                }
                else
                {
                    processInfo = ProcessInfoFactory.Create(activeProcess, Settings.MonitorInterval);
                    activeApps.Add(processInfo.Id, processInfo);
                }

                if (_currentProcessId != processInfoId)
                {
                    if (!string.IsNullOrEmpty(_currentProcessId))
                    {
                        var oldProcessInfo = activeApps[_currentProcessId];
                        oldProcessInfo.EndCurrentSession(DateTime.Now.TimeOfDay);
                    }

                    processInfo.StartNewSession(DateTime.Now.TimeOfDay);
                }

                _currentProcessId = processInfoId;
            }
        }
Ejemplo n.º 9
0
        private Tuple <AppUsageRecord, bool> TryGetFromMainRepository()
        {
            AppUsageRecord appRecord = null;

            try
            {
                appRecord = _repository.Get(AppUsageRecord.GetGeneratedId(DateTime.Now));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);

                return(new Tuple <AppUsageRecord, bool>(null, false));
            }


            return(new Tuple <AppUsageRecord, bool>(appRecord, true));
        }
Ejemplo n.º 10
0
        public void Update(AppUsageRecord entity, object id)
        {
            string savesDir = savesFolderPath;

            if (!Directory.Exists(savesDir))
            {
                Directory.CreateDirectory(savesDir);
            }

            string fileName = Path.Combine(savesDir, (string)id + ".bin");

            using (var saveFileStream = File.OpenWrite(fileName))
            {
                BinaryFormatter serializer = new BinaryFormatter();
                serializer.Serialize(saveFileStream, entity);
                Debug.WriteLine("Backing up locally successfully");
            }
        }
Ejemplo n.º 11
0
        public AppUsageRecord Get(object id)
        {
            var path = Path.Combine(savesFolderPath, (string)id + ".bin");

            if (File.Exists(path))
            {
                Debug.WriteLine("Reading local backup");
                using (var openFileStream = File.OpenRead(path))
                {
                    BinaryFormatter deserializer = new BinaryFormatter();
                    AppUsageRecord  backup       = (AppUsageRecord)deserializer.Deserialize(openFileStream);

                    return(backup);
                }
            }

            return(null);
        }
Ejemplo n.º 12
0
        void BufferPersistentRecords_should_return_from_main_repository()
        {
            var mockMainRepository   = new Mock <IAppUsageRecordRepository>();
            var mockBackupRepository = new Mock <IAppUsageRecordRepository>();
            TrackingUserApplicationService _service = new TrackingUserApplicationService(mockMainRepository.Object, mockBackupRepository.Object);

            var appUsageRecord = new AppUsageRecord(DateTime.Now);

            mockMainRepository.Setup(r => r.Get(AppUsageRecord.GetGeneratedId(DateTime.Now))).Returns(appUsageRecord);
            mockBackupRepository.Setup(r => r.Get(AppUsageRecord.GetGeneratedId(DateTime.Now))).Returns <AppUsageRecord>(null);

            _service.BufferPersistentRecords();

            var bufferedRecord = _service.GetBufferedRecord();

            Assert.Equal(appUsageRecord.Id, bufferedRecord.Id);
            Assert.Equal(appUsageRecord.Date, bufferedRecord.Date);
            Assert.Equal(appUsageRecord.ActiveApps, bufferedRecord.ActiveApps);
        }
Ejemplo n.º 13
0
        void Should_merge_with_other_record_correctly()
        {
            var record1 = new AppUsageRecord(new DateTime(2018, 1, 1));

            record1.ActiveApps.Add(new KeyValuePair <string, ProcessInfo>("key1", new ProcessInfo("key1")
            {
                TotalAmountOfTime = TimeSpan.FromSeconds(1)
            }));

            var record2 = new AppUsageRecord(new DateTime(2018, 1, 1));

            record2.ActiveApps.Add(new KeyValuePair <string, ProcessInfo>("key2", new ProcessInfo("key2")
            {
                TotalAmountOfTime = TimeSpan.FromSeconds(2)
            }));


            record1.MergeWith(record2);

            Assert.Equal("key2", record1.ActiveApps["key2"].Id);
            Assert.Equal(TimeSpan.FromSeconds(2), record1.ActiveApps["key2"].TotalAmountOfTime);
        }
Ejemplo n.º 14
0
        public void BufferPersistentRecords()
        {
            var todayRecordId = AppUsageRecord.GetGeneratedId(DateTime.Now);

            try
            {
                _bufferedTodayAppRecord = _repository.Get(todayRecordId);
                var backupRecord = _backupRepository.Get(todayRecordId);

                if (_bufferedTodayAppRecord == null)
                {
                    _bufferedTodayAppRecord = new AppUsageRecord(DateTime.Now);
                }

                if (backupRecord != null)
                {
                    _bufferedTodayAppRecord.MergeWith(backupRecord);
                    _backupRepository.Delete(todayRecordId);
                }
            }
            catch (TimeoutException ex)
            {
                Debug.WriteLine(ex.Message);
                _mainRepositoryUnavailable = true;

                var backupRecord = _backupRepository.Get(todayRecordId);
                if (backupRecord == null)
                {
                    _bufferedTodayAppRecord = new AppUsageRecord(DateTime.Now);
                }
                else
                {
                    _bufferedTodayAppRecord = backupRecord;
                }
            }
        }
Ejemplo n.º 15
0
        private static void TrainingRecordData()
        {
            var mongoClient = new MongoClient("mongodb://*****:*****@"[^a-zA-Z]", " ").Trim().ToLowerInvariant().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Where(d => d.Count() < 20).ToArray();
            var trainingDataCollection     = database.GetCollection <ProcessInfoLabeledItem>("training_data");
            var records    = trainingDataCollection.Find(Builders <ProcessInfoLabeledItem> .Filter.Empty).ToList();
            var vocabulary = records.Select(c => c.Title + " " + c.Process).SelectMany(filter).Distinct().OrderBy(str => str).ToList();

            List <string> x = records.Select(item => item.Title + " " + item.Process).ToList();

            double[] y = records.Select(item => (double)item.Category).ToArray();

            var problemBuilder = new TextClassificationProblemBuilder();

            problemBuilder.RefineText = filter;
            var problem = problemBuilder.CreateProblem(x, y, vocabulary.ToList());

            const int C     = 1;
            var       model = new C_SVC(problem, KernelHelper.LinearKernel(), C);
            var       _predictionDictionary = new Dictionary <Karma, string> {
                { Karma.Bad, "Bad" }, { Karma.Good, "Good" }, { Karma.Neutral, "Neutral" }
            };

            var newXs = database.GetCollection <AppUsageRecord>("daily_records").Find(Builders <AppUsageRecord> .Filter.Eq(f => f.Id, AppUsageRecord.GetGeneratedId(DateTime.Now))).FirstOrDefault().ActiveApps.Select(c => c.Value).Select(c => c.MainWindowTitle + " " + c.ProcessName);

            foreach (var _x in newXs)
            {
                var newX       = TextClassificationProblemBuilder.CreateNode(_x, vocabulary, problemBuilder.RefineText);
                var predictedY = model.Predict(newX);
                Console.WriteLine($"For title {_x}");
                Console.WriteLine($"The prediction is {_predictionDictionary[(Karma)predictedY]}");
            }
        }
 public async Task UpdateAsync(AppUsageRecord entity, object id)
 {
     var collection = _mongoDb.GetCollection <AppUsageRecord>(_appSettings.DailyRecordCollectionName);
     await collection.ReplaceOneAsync(Builders <AppUsageRecord> .Filter.Eq(f => f.Id, (string)id), entity);
 }
 public async Task AddAsync(AppUsageRecord entity)
 {
     var collection = _mongoDb.GetCollection <AppUsageRecord>(_appSettings.DailyRecordCollectionName);
     await collection.InsertOneAsync(entity, null);
 }
        public void Update(AppUsageRecord entity, object id)
        {
            var collection = MongoDatabaseHolder.Database.GetCollection <AppUsageRecord>(CollectionName);

            var result = collection.ReplaceOne(Builders <AppUsageRecord> .Filter.Eq(f => f.Id, (string)id), entity);
        }
Ejemplo n.º 19
0
 public void Update(AppUsageRecord entity, object id)
 {
     throw new System.NotImplementedException();
 }
Ejemplo n.º 20
0
        private static async Task Verify()
        {
            KarmaPredictor predictor = new KarmaPredictor();

            if (!predictor.IsModelTrained())
            {
                return;
            }

            predictor.LoadModel();

            Func <string, string[]> filter = w => Regex.Replace(w, @"[^a-zA-Z]", " ").Trim().ToLowerInvariant().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Where(d => d.Count() < 20).ToArray();

            var client             = new MongoClient("mongodb://localhost:27017");
            var db                 = client.GetDatabase("active_app_record");
            var trainingCollection = db.GetCollection <AppUsageRecord>("daily_records");
            var cursor             = await trainingCollection.FindAsync(Builders <AppUsageRecord> .Filter.Eq(f => f.Id, AppUsageRecord.GetGeneratedId(DateTime.Now)));

            var trainingData = await cursor.FirstOrDefaultAsync();


            var processInfos = trainingData.ActiveApps.Select(f => f.Value).ToArray();

            var predictions = predictor.Predict(processInfos);

            for (int i = 0; i < processInfos.Length; i++)
            {
                Console.WriteLine($"for window title [{processInfos[i].MainWindowTitle}] result: {predictions[i]}");
            }
        }
        public void Add(AppUsageRecord record)
        {
            var recordCollection = MongoDatabaseHolder.Database.GetCollection <AppUsageRecord>(CollectionName);

            recordCollection.InsertOne(record);
        }