public Process GetProcessFromWindowHandle(IntPtr handle) { const string applicationFrameHostName = "ApplicationFrameHost"; var process = windowProcessCache.Get(handle); if (process != default && process.ProcessName != applicationFrameHostName) { return(process); } windowNativeApi.GetWindowThreadProcessId(handle, out var processId); if (processId == CurrentProcessInformation.CurrentProcess.Id) { return(CurrentProcessInformation.CurrentProcess); } process = Process.GetProcessById((int)processId); if (process.ProcessName == applicationFrameHostName) { process = GetProcessFromUniversalWindowsApplication(handle, process); } windowProcessCache.Set(handle, process); return(process); }
private async Task <string> GetQueryData(string fileName) { if (_cache != null) { var queryStr = await _cache.Get(fileName); if (queryStr != null) { return(queryStr); } } // Get the full query file as provided by the configuration var fullPath = Path.Combine(_rootQueriesDir, $"{fileName}.sql"); if (!File.Exists(fullPath)) { // Lookup in the current path if not found in the root path: var relativePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), fullPath); if (!File.Exists(relativePath)) { throw new FileNotFoundException($"DatabaseWrapper - Query file not found: {fullPath}. Relative lookup failed as well: {relativePath}"); } fullPath = relativePath; } byte[] buffer = null; using (var fs = File.OpenRead(fullPath)) { buffer = new byte[fs.Length]; await fs.ReadAsync(buffer, 0, buffer.Length); } var contents = Encoding.UTF8.GetString(buffer); if (_cache != null) { await _cache.Set(fileName, contents); } return(contents); }
public Process GetProcessFromWindowHandle(IntPtr handle) { var process = windowProcessCache.Get(handle); if (process != default) { return(process); } windowNativeApi.GetWindowThreadProcessId(handle, out var processId); if (processId == CurrentProcessInformation.CurrentProcess.Id) { return(CurrentProcessInformation.CurrentProcess); } process = Process.GetProcessById((int)processId); windowProcessCache.Set(handle, process); return(process); }
public async Task <IDataSource> GetDataSourceAsync() { var activeWindowHandle = activeWindowService.ActiveWindowHandle; var activeWindowTitle = activeWindowService.GetWindowTitleFromWindowHandle(activeWindowHandle); var process = activeWindowService.GetProcessFromWindowHandle(activeWindowHandle); var processName = process.ProcessName + ".exe"; var iconBytesBig = dataSourceIconCacheLarge.Get(activeWindowHandle); if (iconBytesBig == null) { var windowIconBig = await GetWindowIconAsync(process, activeWindowHandle); iconBytesBig = imagePersistenceService.ConvertBitmapSourceToByteArray(windowIconBig); dataSourceIconCacheLarge.Set(activeWindowHandle, iconBytesBig); } var iconBytesSmall = dataSourceIconCacheSmall.Get(activeWindowHandle); if (iconBytesSmall == null) { var windowIconSmall = await GetWindowIconAsync(process, activeWindowHandle, false); iconBytesSmall = imagePersistenceService.ConvertBitmapSourceToByteArray(windowIconSmall); dataSourceIconCacheSmall.Set(activeWindowHandle, iconBytesSmall); } var dataSource = new DataSource( iconBytesBig, iconBytesSmall, activeWindowTitle, processName); return(dataSource); }
public IDataSource GetDataSource() { lock (this) { var activeWindowHandle = activeWindowService.ActiveWindowHandle; var processName = activeWindowService. GetProcessFromWindowHandle(activeWindowHandle) .ProcessName + ".exe"; var iconBytesBig = dataSourceIconCacheLarge.Get(activeWindowHandle); if (iconBytesBig == null) { var windowIconBig = GetWindowIcon(activeWindowHandle); iconBytesBig = imagePersistenceService.ConvertBitmapSourceToByteArray(windowIconBig); dataSourceIconCacheLarge.Set(activeWindowHandle, iconBytesBig); } var iconBytesSmall = dataSourceIconCacheSmall.Get(activeWindowHandle); if (iconBytesSmall == null) { var windowIconSmall = GetWindowIcon(activeWindowHandle, false); iconBytesSmall = imagePersistenceService.ConvertBitmapSourceToByteArray(windowIconSmall); dataSourceIconCacheSmall.Set(activeWindowHandle, iconBytesSmall); } var dataSource = new DataSource( iconBytesBig, iconBytesSmall, activeWindowService.GetWindowTitleFromWindowHandle(activeWindowHandle), processName); return(dataSource); } }
/// <summary> /// Store a value in the cache. /// </summary> /// <typeparam name="T">Type the value to be stored.</typeparam> /// <param name="cache">The cache instance to operate on.</param> /// <param name="key">Key to identify the value by.</param> /// <param name="val">Value to be stored.</param> /// <param name="expires">Absolute time at which the value should no longer be accessible from the cache.</param> public static void Set <T>(this IKeyValueCache cache, string key, T val, DateTime expires) { cache.Set(key, val, expires.ToSafeUniversalTime().Subtract(DateTime.UtcNow)); }
/// <summary> /// Store a value in the cache. /// </summary> /// <remarks> /// This method will set the value and leave expiration up to the cache implementation. /// </remarks> /// <typeparam name="T">Type the value to be stored.</typeparam> /// <param name="cache">The cache instance to operate on.</param> /// <param name="key">Key to identify the value by.</param> /// <param name="val">Value to be stored.</param> public static void Set <T>(this IKeyValueCache cache, string key, T val) { cache.Set(key, val, TimeSpan.MinValue); }
public SearchResult CacheQuery(XDoc searchDoc, SearchQuery query, TrackingInfo trackingInfo) { var key = GetQueryKey(query.LuceneQuery); var documents = searchDoc["document"]; if (trackingInfo != null) { if (!trackingInfo.QueryId.HasValue) { // new tracked query _log.Debug("creating new tracked query"); trackingInfo.QueryId = _session.SearchAnalytics_LogQuery(query, searchDoc["parsedQuery"].AsText, _user.ID, (uint)documents.ListLength, trackingInfo.PreviousQueryId); } else { _log.Debug("recreating tracked query that fell out of cache"); } } else { _log.Debug("untracked query"); } var results = new SearchResultRankCalculator( _settings.GetValue("search/rating-promote-boost", RATING_PROMOTE_BOOST), _settings.GetValue("search/rating-demote-boost", RATING_DEMOTE_BOOST), _settings.GetValue("search/rating-count-threshold", RATING_COUNT_THRESHOLD), _settings.GetValue("search/rating-rank-midpoint", RATING_RANK_MIDPOINT), _settings.GetValue("search/search-popularity-boost", SEARCH_POPULARITY_BOOST), _settings.GetValue("search/search-popularity-threshold", SEARCH_POPULARITY_THRESHOLD) ); _log.Debug("building rank calculator set"); foreach (var entry in documents) { try { SearchResultType type; var typeId = entry["id.file"].AsUInt; if (typeId.HasValue) { type = SearchResultType.File; } else { typeId = entry["id.comment"].AsUInt; if (typeId.HasValue) { type = SearchResultType.Comment; } else { typeId = entry["id.user"].AsUInt; if (typeId.HasValue) { type = SearchResultType.User; } else { typeId = entry["id.page"].AsUInt; if (typeId.HasValue) { type = SearchResultType.Page; } else { _log.WarnFormat("dropping unsupported result item {0}", entry["type"]); continue; } } } } results.Add( typeId.Value, type, entry["title"].AsText, entry["score"].AsDouble ?? 0, DbUtils.ToDateTime(entry["date.edited"].AsText), entry["rating.score"].AsDouble, entry["rating.count"].AsInt ?? 0 ); } catch (Exception e) { // skip any item we cannot process, but log debug to see if there is a bad value pattern that's not related to stale index data _log.DebugFormat("unable to parse lucene result value because of '{0} ({1})' from: {2}", e.GetType(), e.Message, entry.ToString()); } } if (IsAdaptiveSearchEnabled) { var popularity = _session.SearchAnalytics_GetPopularityRanking(query.GetOrderedTermsHash()); results.ComputeRank(popularity); } else { _log.Debug("ranking disabled in non-commercial version"); } var searchResults = new SearchResult(searchDoc["parsedQuery"].AsText, results.ToArray()); _log.DebugFormat("putting results into cache with key: {0}", key); _cache.Set(key, searchResults, TimeSpan.FromSeconds(_settings.GetValue("search/set-cache-time", 120d))); return(searchResults); }