public static ChangeSummary DeSerialize(string filename)
        {
            string        content = System.IO.File.ReadAllText(filename);
            ChangeSummary cs      = Newtonsoft.Json.JsonConvert.DeserializeObject <ChangeSummary>(content);

            return(cs);
        }
Example #2
0
 private bool ShouldSync(ChangeSummary changes)
 {
     // Check if this user is affected and if so, sync.
     return(changes.Organizations.Overlaps(_versions.OrgVersions.Keys) ||
            changes.Repositories.Overlaps(_versions.RepoVersions.Keys) ||
            changes.Users.Contains(_user.UserId));
 }
Example #3
0
        private void Subscribe(IUserActor userActor)
        {
            Log.Info($"{_user.Login}");

            if (_syncSubscription != null)
            {
                throw new InvalidOperationException("Already subscribed to changes.");
            }

            var start = new ChangeSummary();

            start.Add(userId: _user.UserId);

            // Changes streamed from the queue
            _syncSubscription = _syncManager.Changes
                                .ObserveOn(TaskPoolScheduler.Default)
                                .StartWith(start) // Run an initial sync no matter what.
                                .Select(c =>
                                        Observable.FromAsync(() => _syncContext.Sync(c))
                                        .Catch <Unit, Exception>(LogError <Unit>))
                                .Concat() // Force sequential evaluation
                                .Subscribe();

            // Polling for updates
            _pollSubscription = _PollInterval
                                .ObserveOn(TaskPoolScheduler.Default)
                                .StartWith(0)
                                .Select(_ =>
                                        Observable.FromAsync(() => userActor.Sync())
                                        .Catch <Unit, Exception>(LogError <Unit>))
                                .Concat() // Force sequential evaluation
                                .Subscribe();
        }
        private static async Task <SubscriptionResponse> GetSubscriptionResponse(User user)
        {
            var messages = new List <SyncMessageBase>();

            var mockConnection = new Mock <ISyncConnection>();

            mockConnection
            .Setup(x => x.SendJsonAsync(It.IsAny <object>()))
            .Returns((object obj) => {
                Assert.IsInstanceOf <SyncMessageBase>(obj);
                messages.Add((SyncMessageBase)obj);
                return(Task.CompletedTask);
            });

            var principal     = new ShipHubPrincipal(user.Id, user.Login);
            var syncContext   = new SyncContext(principal, mockConnection.Object, new SyncVersions());
            var changeSummary = new ChangeSummary();

            changeSummary.Add(userId: user.Id);
            await syncContext.Sync(changeSummary);

            var result = messages
                         .Where(x => x.MessageType.Equals("subscription"))
                         .SingleOrDefault();

            Assert.IsNotNull(result, "Should have been sent a SubscriptionEntry.");

            return((SubscriptionResponse)result);
        }
Example #5
0
        public ChangeSummary GetSummaryOfChangesSince(string lastProcessedChangeItemId, List <string> filterStrings)
        {
            // lastProcessedChangeItemId is in a form such as "Defect:UCM0100019437:history:0"
            // Parse it and query for the record with that Id to get the time is was last changed
            string[]  identity            = UtilityMethods.ParseCQRecordMigrationItemId(lastProcessedChangeItemId);
            OAdEntity lastProcessedRecord = CQWrapper.GetEntity(m_userSession, identity[0], identity[1]);
            string    lastProcessedRecordAuthor;
            DateTime  lastProcessedRecordChangeDate;

            ClearQuestRecordItem.FindLastRevDtls(lastProcessedRecord, out lastProcessedRecordAuthor, out lastProcessedRecordChangeDate);

            string lastProcessedRecordChangeDateStr = lastProcessedRecordChangeDate.ToString("u").Replace("Z", ""); // using "ISO 8601" DateTime string format

            if (lastProcessedRecordChangeDateStr.LastIndexOf('.') >= 0)
            {
                lastProcessedRecordChangeDateStr = lastProcessedRecordChangeDateStr.Substring(0, lastProcessedRecordChangeDateStr.LastIndexOf('.')); // drop the millisec
            }

            ChangeSummary changeSummary = new ChangeSummary();

            changeSummary.ChangeCount = 0;
            changeSummary.FirstChangeModifiedTimeUtc = DateTime.MinValue;

            foreach (CQRecordFilter filter in m_filters)
            {
                CQRecordQueryBase recordQuery =
                    CQRecordQueryFactory.CreatQuery(m_userSession, filter, lastProcessedRecordChangeDateStr, this);

                foreach (OAdEntity record in recordQuery)
                {
                    // HACK HACK
                    if (record != null) // this if check is HACK
                    {
                        DateTime lastChangeDate;
                        string   lastAuthor;
                        ClearQuestRecordItem.FindLastRevDtls(record, out lastAuthor, out lastChangeDate);

                        // Make sure the lastChangeDate on this record is after the lastProcessedRecordChangeDate before counting it in the backclog
                        // because the query issued above is imprecise because the milliseconds are dropped
                        if (lastChangeDate > lastProcessedRecordChangeDate)
                        {
                            changeSummary.ChangeCount++;
                            DateTime lastChangeDateUtc = lastChangeDate.ToUniversalTime();
                            if (changeSummary.FirstChangeModifiedTimeUtc == DateTime.MinValue ||
                                lastChangeDateUtc < changeSummary.FirstChangeModifiedTimeUtc)
                            {
                                changeSummary.FirstChangeModifiedTimeUtc = lastChangeDateUtc;
                            }
                        }
                    }
                }
            }

            return(changeSummary);
        }
Example #6
0
 public async Task Sync(ChangeSummary changes)
 {
     if (!ShouldSync(changes))
     {
         Log.Debug(() => $"User {_user.UserId} not syncing for changes {changes}");
         return;
     }
     Log.Debug(() => $"User {_user.UserId} is syncing for changes {changes}");
     await SendSubscriptionEntry();
     await SendSyncResponse();
     await RecordUsage();
 }
Example #7
0
        public async Task Run(IAsyncCollector <ChangeMessage> notifyChanges)
        {
            using (var context = new ShipHubContext()) {
                // Get all the tokens
                var tokens = await context.Tokens
                             .AsNoTracking()
                             .Where(x => x.Version < TokenVersion)
                             .ToArrayAsync();

                if (tokens.Any())
                {
                    Log.Info($"{tokens.Length} tokens need to be rolled.");

                    foreach (var token in tokens)
                    {
                        var speedLimit = Task.Delay(1000);
                        try {
                            var newToken = await ResetToken(token.Token);

                            if (newToken == null)
                            {
                                // Delete the single token
                                await context.DeleteUserAccessToken(token.UserId, token.Token);

                                Log.Info("Deleted expired token.");
                            }
                            else
                            {
                                // Replace the token
                                await context.RollUserAccessToken(token.UserId, token.Token, newToken, TokenVersion);

                                Log.Info("Updated valid token.");
                            }

                            var cs = new ChangeSummary();
                            cs.Add(userId: token.UserId);
                            await notifyChanges.AddAsync(new ChangeMessage(cs));
                        } catch (Exception e) {
                            Log.Exception(e, $"Error rolling token for {token.UserId}:{token.Version}");
                        }

                        await speedLimit;
                    }

                    Log.Info($"Done processing tokens.");
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ChangeSetAggregator{TObject, TKey}"/> class.
        /// </summary>
        /// <param name="source">The source.</param>
        public ChangeSetAggregator(IObservable <IChangeSet <TObject> > source)
        {
            var published = source.Publish();

            Data = published.AsObservableList();

            var results    = published.Subscribe(updates => _messages.Add(updates), ex => _error = ex);
            var summariser = published.CollectUpdateStats().Subscribe(summary => _summary = summary);
            var connected  = published.Connect();

            _disposer = Disposable.Create(() =>
            {
                Data.Dispose();
                connected.Dispose();
                summariser.Dispose();
                results.Dispose();
            });
        }
        public ChangeSummary GetSummaryOfChangesSince(string lastProcessedChangeItemId, List <string> filterStrings)
        {
            ChangeSummary changeSummary = new ChangeSummary();

            changeSummary.ChangeCount = 0;
            changeSummary.FirstChangeModifiedTimeUtc = DateTime.MinValue;

            DateTime timeLastItemProcessedUtc = DateTime.MinValue;

            try
            {
                timeLastItemProcessedUtc = DateTime.Parse(lastProcessedChangeItemId);
                timeLastItemProcessedUtc = timeLastItemProcessedUtc.ToUniversalTime();
            }
            catch (FormatException)
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture,
                                                          TfsFileSystemResources.InvalidChangeItemIdFormat, lastProcessedChangeItemId));
            }

            foreach (string filterPath in filterStrings)
            {
                if (Directory.Exists(filterPath))
                {
                    foreach (string path in Directory.GetDirectories(filterPath, "*", SearchOption.AllDirectories))
                    {
                        CheckFileSystemItemForChangeSinceTime(path, timeLastItemProcessedUtc, ref changeSummary);
                    }
                    CheckFileSystemItemForChangeSinceTime(filterPath, timeLastItemProcessedUtc, ref changeSummary);

                    foreach (string path in Directory.GetFiles(filterPath, "*", SearchOption.AllDirectories))
                    {
                        CheckFileSystemItemForChangeSinceTime(path, timeLastItemProcessedUtc, ref changeSummary);
                    }
                }
                else if (File.Exists(filterPath))
                {
                    CheckFileSystemItemForChangeSinceTime(filterPath, timeLastItemProcessedUtc, ref changeSummary);
                }
            }

            return(changeSummary);
        }
Example #10
0
        /// <summary>
        /// Persists the change summary (which is really just a health service event in the sHR)
        /// </summary>
        public SVC.Core.DataTypes.VersionedDomainIdentifier Persist(System.Data.IDbConnection conn, System.Data.IDbTransaction tx, System.ComponentModel.IComponent data, bool isUpdate)
        {
            // Change summary cast
            ChangeSummary cs = data as ChangeSummary;

            //if(isUpdate)
            //    return cs.AlternateIdentifier; // can't update a change summary

            // copy fields to hsr
            ChangeSummary hsr = new ChangeSummary()
            {
                ChangeType          = cs.ChangeType,
                EffectiveTime       = cs.EffectiveTime,
                AlternateIdentifier = cs.AlternateIdentifier,
                Context             = cs.Context,
                Id           = cs.Id,
                IsMasked     = cs.IsMasked,
                Status       = cs.Status,
                Timestamp    = cs.Timestamp,
                LanguageCode = cs.LanguageCode
            };

            // persist HSR
            hsr.AlternateIdentifier = CreateHSRRecord(conn, tx, hsr);
            hsr.Id = Convert.ToDecimal(hsr.AlternateIdentifier.Identifier);
            hsr.VersionIdentifier = Convert.ToDecimal(hsr.AlternateIdentifier.Version);

            // Is there any sort of linkage we need to create
            if (hsr.Site != null && hsr.Site.Container != null &&
                !(hsr.Site as HealthServiceRecordSite).IsSymbolic)
            {
                new RegistrationEventPersister().LinkHSRRecord(conn, tx, hsr);
            }

            // Persist components
            cs.AlternateIdentifier = hsr.AlternateIdentifier;
            cs.Id = hsr.Id;
            cs.VersionIdentifier = hsr.VersionIdentifier;
            DbUtil.PersistComponents(conn, tx, isUpdate, this, cs);

            return(hsr.AlternateIdentifier);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DistinctChangeSetAggregator{TValue}"/> class.
        /// </summary>
        /// <param name="source">The source.</param>
        public DistinctChangeSetAggregator(IObservable <IDistinctChangeSet <TValue> > source)
        {
            var published = source.Publish();

            var error   = published.Subscribe(updates => { }, ex => _error = ex);
            var results = published.Subscribe(updates => _messages.Add(updates));

            _data = published.AsObservableCache();
            var summariser = published.CollectUpdateStats().Subscribe(summary => _summary = summary);

            var connected = published.Connect();

            _disposer = Disposable.Create(() =>
            {
                connected.Dispose();
                summariser.Dispose();
                results.Dispose();
                error.Dispose();
            });
        }
Example #12
0
        public SyncManager(IServiceBusFactory serviceBusFactory)
        {
            var messages = Observable
                           .Create <ChangeSummary>(async observer => {
                var client           = await serviceBusFactory.SubscriptionClientForName(ShipHubTopicNames.Changes);
                client.PrefetchCount = _BatchSize;

                // TODO: convert to batches?
                client.OnMessage(message => {
                    var changes = WebJobInterop.UnpackMessage <ChangeMessage>(message);
                    observer.OnNext(new ChangeSummary(changes));
                }, new OnMessageOptions()
                {
                    AutoComplete     = true,
                    AutoRenewTimeout = TimeSpan.FromMinutes(1), // Has to be less than 5 or subscription will idle and expire
                    // Should be at least be the number of partitions
                    MaxConcurrentCalls = 16
                });

                // When disconnected, stop listening for changes.
                return(Disposable.Create(() => client.Close()));
            })
                           .SubscribeOn(TaskPoolScheduler.Default)
                           .Publish()
                           .RefCount();

            var urgent    = messages.Where(x => x.IsUrgent);
            var coalesced = messages
                            .Where(x => !x.IsUrgent)
                            .Buffer(_WindowTimeout)
                            .Where(x => x.Count > 0)
                            .Select(x => ChangeSummary.UnionAll(x));

            Changes = Observable
                      .Merge(urgent, coalesced)
                      .SubscribeOn(TaskPoolScheduler.Default)
                      .Publish()
                      .RefCount();
        }
        public string Serialize(IList <Change> changes)
        {
            // build the change summary:

            ChangeSummary summary = new ChangeSummary();

            summary.ModelName       = _doc.Title;
            summary.ModelPath       = _doc.PathName;
            summary.NumberOfChanges = changes.Count;
            summary.PreviousFile    = _filename;
            summary.ComparisonDate  = DateTime.Now;
            summary.Changes         = changes;
            summary.ModelSummary    = _categoryCount;
            summary.LevelNames      = _allLevels.OrderBy(a => a.Elevation).Select(a => a.Name).ToList();


            string result = Newtonsoft.Json.JsonConvert.SerializeObject(summary);

            //var serialize = new System.Web.Script.Serialization.JavaScriptSerializer();
            //string result = serialize.Serialize(summary);

            return(result);
        }
        public virtual void Poll()
        {
            m_monitorWatcher.LogVerbose(String.Format(CultureInfo.InvariantCulture,
                                                      MigrationToolkitResources.PollingEndpoint, RTMigrationSource.FriendlyName));

            Stopwatch stopWatch = Stopwatch.StartNew();
            string    lastMigratedChangeName = GetLastMigratedChangeName();

            stopWatch.Stop();
            m_monitorWatcher.LogVerbose(String.Format(MigrationToolkitResources.TimeForGetLastMigratedChangeName, stopWatch.Elapsed.TotalMilliseconds));

            if (string.IsNullOrEmpty(lastMigratedChangeName))
            {
                m_monitorWatcher.LogVerbose(String.Format(CultureInfo.InvariantCulture,
                                                          MigrationToolkitResources.GetLastMigratedChangeNameReturnedEmpty,
                                                          RTMigrationSource.FriendlyName));
            }
            else
            {
                m_monitorWatcher.LogVerbose(String.Format(MigrationToolkitResources.LastMigratedItemName, lastMigratedChangeName));

                DateTime      pollTimeUtc   = DateTime.UtcNow;
                Stopwatch     stopWatch2    = Stopwatch.StartNew();
                ChangeSummary changeSummary = SyncMonitorProvider.GetSummaryOfChangesSince(lastMigratedChangeName, m_filterStrings);
                stopWatch2.Stop();
                m_monitorWatcher.LogVerbose(String.Format(MigrationToolkitResources.TimeForGetSummaryOfChangesSince, stopWatch2.Elapsed.TotalMilliseconds));

                TimeSpan latencyTimeSpan = pollTimeUtc - changeSummary.FirstChangeModifiedTimeUtc;
                if (latencyTimeSpan.TotalSeconds < 0)
                {
                    latencyTimeSpan = new TimeSpan(0);
                }

                if (changeSummary.ChangeCount > 0 && latencyTimeSpan.TotalSeconds > (double)Int32.MaxValue)
                {
                    // The Latency value is to big to be stored as a 32 bit int and must be wrong; return without storing a LatencyPoll row
                    m_monitorWatcher.LogWarning(String.Format(CultureInfo.InvariantCulture,
                                                              MigrationToolkitResources.SyncMonitorNotStoringInvalidLatencyPollData, RTMigrationSource.FriendlyName, lastMigratedChangeName));
                    return;
                }

                RTLatencyPoll latencyPoll =
                    RTLatencyPoll.CreateRTLatencyPoll(
                        0,
                        pollTimeUtc,
                        (changeSummary.ChangeCount == 0) ? DateTime.UtcNow : changeSummary.FirstChangeModifiedTimeUtc,
                        (changeSummary.ChangeCount == 0) ? 0 : Convert.ToInt32(latencyTimeSpan.TotalSeconds),
                        changeSummary.ChangeCount);

                latencyPoll.LastMigratedChange = lastMigratedChangeName;

                // latencyPoll.MigrationSourceReference.Attach(this.RTMigrationSource);
                latencyPoll.MigrationSource = this.RTMigrationSource;

                Context.AddToRTLatencyPollSet(latencyPoll);

                Context.TrySaveChanges();

                m_monitorWatcher.LogVerbose(String.Format(CultureInfo.InvariantCulture,
                                                          MigrationToolkitResources.SuccessfullyPolledEndpoint,
                                                          RTMigrationSource.FriendlyName, latencyPoll.Latency, latencyPoll.BacklogCount));
            }
        }
Example #15
0
 /// <summary>
 /// Equalses the specified other.
 /// </summary>
 /// <param name="other">The other.</param>
 /// <returns></returns>
 public bool Equals(ChangeSummary other)
 {
     return(_index == other._index && Equals(Latest, other.Latest) && Equals(Overall, other.Overall));
 }
Example #16
0
		/// <summary>
		/// Equalses the specified other.
		/// </summary>
		/// <param name="other">The other.</param>
		/// <returns></returns>
		public bool Equals(ChangeSummary other)
		{
			return _index == other._index && Equals(Latest, other.Latest) && Equals(Overall, other.Overall);
		}
Example #17
0
        public ChangeSummary GetSummaryOfChangesSince(string lastProcessedChangeItemId, List <string> filterStrings)
        {
            ChangeSummary changeSummary = new ChangeSummary();

            changeSummary.ChangeCount = 0;
            changeSummary.FirstChangeModifiedTimeUtc = DateTime.MinValue;

            // Now find all history records for changes since the High Water Mark
            m_hwmDelta.Reload();
            DateTime since = m_hwmDelta.Value;
            List <CCHistoryRecord> historyRecordList = m_clearCaseServer.GetHistoryRecords(m_configurationService.Filters, since, false);

            long lastProcessedEventId;

            try
            {
                lastProcessedEventId = long.Parse(lastProcessedChangeItemId);
            }
            catch (FormatException)
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture,
                                                          CCResources.InvalidChangeItemIdFormat, lastProcessedChangeItemId));
            }

            CCHistoryRecord nextHistoryRecordToBeMigrated = null;

            // Of all of the history records since the HWM, find the oldest one (based on EventId which increases over time)
            foreach (CCHistoryRecord historyRecord in historyRecordList)
            {
                // We only want to count history records with OperationTypes of Checkin and Rmname for the backlog,
                // not individual element operation or other operations such as mklabel that are not processed by the ClearCaseAnalysisProvider
                // *** NOTE: If the ClearCaseAnalysisProvider is changed to process other OperationTypes, this code needs to change as well ***
                // TODO: Should we create a common static list of processed OperationTypes so that there is just one place to update?
                if (historyRecord.EventId > lastProcessedEventId &&
                    (historyRecord.OperationType == OperationType.Checkin || historyRecord.OperationType == OperationType.Rmname))
                {
                    // Don't count DirectoryVersion check-in records in the backlog as these may produce false positives in the backlog
                    if (!string.Equals(historyRecord.OperationDescription, OperationDescription.DirectoryVersion, StringComparison.Ordinal))
                    {
                        changeSummary.ChangeCount++;
                        if (nextHistoryRecordToBeMigrated == null || historyRecord.EventId < nextHistoryRecordToBeMigrated.EventId)
                        {
                            nextHistoryRecordToBeMigrated = historyRecord;
                        }

                        /* Uncomment for debugging
                         * Console.WriteLine(String.Format(CultureInfo.InvariantCulture,
                         *  "CCSyncMonitorProvider including backlog item for history record: EventId: {0}, OperationType: {1}, AbsoluteVobPath: {2}, VersionTime: {3}",
                         *  historyRecord.EventId, historyRecord.OperationType, historyRecord.AbsoluteVobPath, historyRecord.VersionTime));
                         */
                    }
                }
            }

            if (nextHistoryRecordToBeMigrated != null)
            {
                changeSummary.FirstChangeModifiedTimeUtc = nextHistoryRecordToBeMigrated.VersionTime.ToUniversalTime();
            }

            return(changeSummary);
        }
        private void CheckFileSystemItemForChangeSinceTime(string itemPath, DateTime comparisonTimeUtc, ref ChangeSummary changeSummary)
        {
            FileInfo fileInfo            = new FileInfo(itemPath);
            DateTime fileModifiedTimeUtc = fileInfo.CreationTimeUtc > fileInfo.LastWriteTimeUtc ? fileInfo.CreationTimeUtc : fileInfo.LastWriteTimeUtc;

            if (fileModifiedTimeUtc > comparisonTimeUtc)
            {
                changeSummary.ChangeCount++;
                if (changeSummary.FirstChangeModifiedTimeUtc == DateTime.MinValue || fileModifiedTimeUtc < changeSummary.FirstChangeModifiedTimeUtc)
                {
                    changeSummary.FirstChangeModifiedTimeUtc = fileModifiedTimeUtc;
                }
            }
        }
Example #19
0
 protected bool Equals(ChangeSummary other)
 {
     return(_index == other._index && Equals(_latest, other._latest) && Equals(_overall, other._overall));
 }
        //private void btnView_Click(object sender, RoutedEventArgs e)
        //{
        //    if (!this.Dispatcher.CheckAccess())
        //    {
        //        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.SystemIdle, TimeSpan.FromSeconds(1),
        //           new System.Action(delegate()
        //           {
        //               btnView_Click(sender, e);
        //           }
        //        ));
        //    }
        //    else
        //    {
        //        try
        //        {
        //            Button b = sender as Button;
        //            if (b == null)
        //                return;

        //            AttachedComparison comp = b.DataContext as AttachedComparison;
        //            if (comp == null)
        //                return;

        //            ProcessStartInfo startInfo = new ProcessStartInfo();
        //            startInfo.FileName = "WINWORD.EXE";
        //            startInfo.Arguments = "\"" + comp.RedlineRtfPath + "\"";
        //            Process.Start(startInfo);
        //        }
        //        catch (Exception ex)
        //        {
        //            Logger.LogError(ex);
        //            Forms.MessageBox.Show(ex.Message);
        //        }
        //    }
        //}

        #endregion

        private void _summaryControl_OnTreeAction(object sender, ChangeSummary.TreeActionEventArgs e)
        {
            TerUserControl control = windowsFormsHost.Child as TerUserControl;
            if (control != null)
            {
                control.GotoChange(e.FirstChangeNum, e.LastChangeNum, true);
            }
        }
        public ChangeSummary GetSummaryOfChangesSince(string lastProcessedChangeItemId, List <string> filterStrings)
        {
            if (string.IsNullOrEmpty(lastProcessedChangeItemId))
            {
                throw new ArgumentException("lastProcessedChangeItemId");
            }

            ChangeSummary changeSummary = new ChangeSummary();

            changeSummary.ChangeCount = 0;
            changeSummary.FirstChangeModifiedTimeUtc = DateTime.MinValue;

            string[] changeItemIdParts = lastProcessedChangeItemId.Split(new char[] { ':' });
            if (changeItemIdParts.Length != 2)
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture,
                                                          TfsWITAdapterResources.InvalidChangeItemIdFormat, lastProcessedChangeItemId));
            }

            int      lastProcessedWorkItemId = 0;
            WorkItem lastProcessedWorkItem   = null;

            try
            {
                lastProcessedWorkItemId = int.Parse(changeItemIdParts[0]);
                lastProcessedWorkItem   = m_workItemStore.GetWorkItem(lastProcessedWorkItemId);
                int revNumber = -1;
                try
                {
                    revNumber = int.Parse(changeItemIdParts[1]);
                }
                catch (FormatException)
                {
                }
                DateTime changedDate;
                if (revNumber > 0)
                {
                    Revision rev = lastProcessedWorkItem.Revisions[revNumber - 1];
                    changedDate = (DateTime)rev.Fields[CoreField.ChangedDate].Value;
                }
                else
                {
                    changedDate = lastProcessedWorkItem.ChangedDate;
                }
                changeSummary.FirstChangeModifiedTimeUtc = changedDate.ToUniversalTime();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format(CultureInfo.InvariantCulture,
                                                "Tfs2008WitAdapter.TfsWITSyncMonitorProvider: Exception finding last processed work item (Id {0}): {1}",
                                                lastProcessedWorkItemId, ex.ToString()));
            }

            WorkItem nextWorkItemToBeMigrated = null;

            foreach (string filterString in filterStrings)
            {
                Dictionary <string, object> context = new Dictionary <string, object>();
                string        columnList            = "[System.Id], [System.ChangedDate]";
                StringBuilder conditionBuilder      = new StringBuilder(filterString);
                if (!string.IsNullOrEmpty(m_projectName))
                {
                    context.Add("project", m_projectName);
                    if (conditionBuilder.Length > 0)
                    {
                        conditionBuilder.Append(" AND ");
                    }
                    conditionBuilder.Append("[System.TeamProject] = @project");
                }
                if (!changeSummary.FirstChangeModifiedTimeUtc.Equals(default(DateTime)))
                {
                    if (conditionBuilder.Length > 0)
                    {
                        conditionBuilder.Append(" AND ");
                    }
                    conditionBuilder.AppendFormat(CultureInfo.InvariantCulture, "[System.ChangedDate] > '{0:u}'", changeSummary.FirstChangeModifiedTimeUtc);
                }
                string orderBy = "[System.Id]";
                string wiql    = TfsWITQueryBuilder.BuildWiqlQuery(columnList, conditionBuilder.ToString(), orderBy);

                // Run query with date precision off
                Query wiq = new Query(m_workItemStore, wiql, context, false);

                // Retrieve all results
                ICancelableAsyncResult car       = wiq.BeginQuery();
                WorkItemCollection     workItems = wiq.EndQuery(car);

                foreach (WorkItem wi in workItems)
                {
                    if (!m_translationService.IsSyncGeneratedItemVersion(
                            wi.Id.ToString(),
                            wi.Rev.ToString(),
                            new Guid(m_migrationSource.InternalUniqueId)))
                    {
                        changeSummary.ChangeCount++;
                        if (nextWorkItemToBeMigrated == null || wi.ChangedDate < nextWorkItemToBeMigrated.ChangedDate)
                        {
                            nextWorkItemToBeMigrated = wi;
                        }
                    }
                }
            }

            if (nextWorkItemToBeMigrated != null)
            {
                changeSummary.FirstChangeModifiedTimeUtc = nextWorkItemToBeMigrated.ChangedDate.ToUniversalTime();
            }

            return(changeSummary);
        }
Example #22
0
        public async Task <bool> InternalSync(DataUpdater updater)
        {
            if (_syncLimit.CurrentCount != 0)
            {
                throw new InvalidOperationException($"{nameof(InternalSync)} requires the sync semaphore be held.");
            }

            var metaDataMeaningfullyChanged = false;

            try {
                // NOTE: The following requests are (relatively) infrequent and important for access control (repos/orgs)
                // Give them high priority.

                // Update this user's org memberships
                if (_orgMetadata.IsExpired())
                {
                    var orgs = await _github.OrganizationMemberships(cacheOptions : _orgMetadata, priority : RequestPriority.Interactive);

                    if (orgs.IsOk)
                    {
                        metaDataMeaningfullyChanged = true;
                        await updater.SetUserOrganizations(_userId, orgs.Date, orgs.Result);

                        _orgActors = orgs.Result
                                     .ToDictionary(x => x.Organization.Id, x => _grainFactory.GetGrain <IOrganizationActor>(x.Organization.Id));

                        // Also re-evaluate their linked repos
                        Interlocked.Increment(ref _linkedReposDesired);
                    }

                    // Don't update until saved.
                    _orgMetadata = GitHubMetadata.FromResponse(orgs);
                }

                // Update this user's repo memberships
                var savedLinkCurrent = _linkedReposCurrent;
                var savedLinkDesired = _linkedReposDesired;

                if ((savedLinkCurrent < savedLinkDesired) || _repoMetadata.IsExpired())
                {
                    var repos = await _github.Repositories(_repoMetadata, RequestPriority.Interactive);

                    if (repos.IsOk)
                    {
                        metaDataMeaningfullyChanged = true;
                        Interlocked.Increment(ref _syncReposDesired);

                        await updater.SetUserRepositories(_userId, repos.Date, repos.Result);

                        _linkedRepos = repos.Result.Select(x => x.Id).ToHashSet();
                        // don't update _repoActors yet
                    }

                    // Don't update until saved.
                    _repoMetadata = GitHubMetadata.FromResponse(repos);
                    Interlocked.CompareExchange(ref _linkedReposCurrent, savedLinkDesired, savedLinkCurrent);
                }

                // Update this user's sync repos
                var savedReposCurrent = _syncReposCurrent;
                var savedReposDesired = _syncReposDesired;
                if (savedReposCurrent < savedReposDesired)
                {
                    IEnumerable <g.Repository>         updateRepos          = null;
                    IDictionary <long, GitHubMetadata> combinedRepoMetadata = null;
                    var date = DateTimeOffset.UtcNow; // Not *technically* correct, but probably ok

                    if (_syncSettings?.Include.Any() == true)
                    {
                        // Request all "included" repos to verify access
                        var repoReqs = _syncSettings.Include
                                       .Where(x => !_linkedRepos.Contains(x)) // Exclude linked repos (access already known)
                                       .Select(x => (RepoId: x, Request: _github.Repository(x, _includeRepoMetadata.Val(x), RequestPriority.Interactive)))
                                       .ToArray();
                        await Task.WhenAll(repoReqs.Select(x => x.Request));

                        // Collect the "successful" responses
                        // Check explicitly for 404, since 502s are so common :/
                        var successful = repoReqs
                                         .Where(x => x.Request.Status == TaskStatus.RanToCompletion)
                                         .Where(x => x.Request.Result.Status != HttpStatusCode.NotFound)
                                         .ToArray();

                        _includeRepoMetadata = successful.ToDictionary(x => x.RepoId, x => GitHubMetadata.FromResponse(x.Request.Result));

                        updateRepos = successful
                                      .Where(x => x.Request.Result.IsOk)
                                      .Select(x => x.Request.Result.Result)
                                      .ToArray();

                        // now union/intersect all the things
                        combinedRepoMetadata = new Dictionary <long, GitHubMetadata>();
                        foreach (var repoId in _syncSettings.Include)
                        {
                            if (_linkedRepos.Contains(repoId))
                            {
                                combinedRepoMetadata.Add(repoId, null);
                            }
                            else if (_includeRepoMetadata.ContainsKey(repoId))
                            {
                                combinedRepoMetadata.Add(repoId, _includeRepoMetadata[repoId]);
                            }
                            // else drop it
                        }
                    }

                    var syncRepoMetadata = await updater.UpdateAccountSyncRepositories(
                        _userId,
                        _syncSettings?.AutoTrack ?? true,
                        date,
                        updateRepos,
                        combinedRepoMetadata,
                        _syncSettings?.Exclude);

                    _repoActors = syncRepoMetadata.Keys
                                  .ToDictionary(x => x, x => _grainFactory.GetGrain <IRepositoryActor>(x));

                    _includeRepoMetadata = syncRepoMetadata
                                           .Where(x => !_linkedRepos.Contains(x.Key))
                                           .ToDictionary(x => x.Key, x => x.Value);

                    // TODO: Actually detect changes.
                    var cs = new ChangeSummary();
                    cs.Add(userId: _userId);
                    updater.UnionWithExternalChanges(cs);

                    Interlocked.CompareExchange(ref _syncReposCurrent, savedReposDesired, savedReposCurrent);
                }
            } catch (GitHubRateException) {
                // nothing to do
            }

            // We do this here so newly added repos and orgs sync immediately

            // Sync repos
            foreach (var repo in _repoActors.Values)
            {
                repo.Sync().LogFailure(_userInfo);
            }

            // Sync orgs
            foreach (var org in _orgActors.Values)
            {
                org.Sync().LogFailure(_userInfo);
            }

            return(metaDataMeaningfullyChanged);
        }
Example #23
0
        /// <summary>
        /// Create an HSR record
        /// </summary>
        private VersionedDomainIdentifier CreateHSRRecord(IDbConnection conn, IDbTransaction tx, ChangeSummary hsr)
        {
            IDbCommand cmd = DbUtil.CreateCommandStoredProc(conn, tx);

            cmd.CommandText = "crt_hsr";

            // Get the terminology service
            ISystemConfigurationService iscs = ApplicationContext.ConfigurationService; //ApplicationContext.Current.GetService(typeof(ISystemConfigurationService)) as ISystemConfigurationService;

            // Parameters
            // classifier = 0x400 = Change Summary
            cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "hsr_cls_in", DbType.Decimal, RegistrationEventType.ComponentEvent | RegistrationEventType.Revise));
            // event type code
            cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "evt_typ_cd_id_in", DbType.Decimal, DbUtil.CreateCodedValue(conn, tx, hsr.ChangeType)));
            // refuted indicator
            cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "refuted_ind_in", DbType.Boolean, false));

            decimal?efftTimeId = null;

            if (hsr.EffectiveTime != null)
            {
                efftTimeId = DbUtil.CreateTimeset(conn, tx, hsr.EffectiveTime);
            }

            cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "efft_ts_set_id_in", DbType.Decimal, efftTimeId == null ? (object)DBNull.Value : efftTimeId.Value));
            // status code
            cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "status_cs_in", DbType.Decimal, hsr.Status == null ? (object)DBNull.Value : (int)hsr.Status));
            // authored time
            cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "aut_utc_in", DbType.DateTime, hsr.Timestamp == default(DateTime) ? (object)DBNull.Value : hsr.Timestamp));
            // language code
            cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "lang_cs_in", DbType.String, hsr.LanguageCode));

            // Execute the command
            IDataReader resultRdr = cmd.ExecuteReader();

            try
            {
                // Create the return value
                VersionedDomainIdentifier id = new VersionedDomainIdentifier();
                if (!resultRdr.Read())
                {
                    return(null);
                }

                id.Version    = Convert.ToString(resultRdr["VRSN_ID"]);
                id.Identifier = Convert.ToString(resultRdr["ID"]);
                id.Domain     = iscs.OidRegistrar.GetOid(ClientRegistryOids.EVENT_OID).Oid;

                return(id);
            }
            finally
            {
                resultRdr.Close();
            }
        }
Example #24
0
        /// <summary>
        /// De-persist the specified change summary
        /// </summary>
        public System.ComponentModel.IComponent DePersist(System.Data.IDbConnection conn, decimal identifier, System.ComponentModel.IContainer container, SVC.Core.ComponentModel.HealthServiceRecordSiteRoleType?role, bool loadFast)
        {
            // TODO: Ensure that when a parent with context conduction exists, to grab contextual data (authors, etc...) from the parent
            ChangeSummary retVal = new ChangeSummary();

            // Configuration service
            ISystemConfigurationService configService = ApplicationContext.ConfigurationService; //ApplicationContext.Current.GetService(typeof(ISystemConfigurationService)) as ISystemConfigurationService;

            // Get the health service event
            IDbCommand cmd = DbUtil.CreateCommandStoredProc(conn, null);

            try
            {
                cmd.CommandText = "get_hsr_crnt_vrsn";
                cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "hsr_id_in", DbType.Decimal, identifier));

                decimal tsId          = default(decimal),
                        cdId          = default(decimal),
                        rplcVersionId = default(decimal);

                // Read data
                IDataReader reader = cmd.ExecuteReader();
                try
                {
                    if (!reader.Read())
                    {
                        return(null);
                    }

                    retVal.Id = Convert.ToDecimal(reader["hsr_id"]);
                    retVal.VersionIdentifier   = Convert.ToDecimal(reader["hsr_vrsn_id"]);
                    retVal.AlternateIdentifier = new VersionedDomainIdentifier()
                    {
                        Domain     = configService.OidRegistrar.GetOid(ClientRegistryOids.EVENT_OID).Oid,
                        Identifier = retVal.Id.ToString(),
                        Version    = retVal.VersionIdentifier.ToString()
                    };
                    retVal.LanguageCode = reader["lang_cs"].ToString();
                    retVal.Timestamp    = DateTime.Parse(Convert.ToString(reader["aut_utc"]));
                    retVal.Status       = (StatusType)Convert.ToDecimal(reader["status_cs_id"]);
                    tsId          = reader["efft_ts_set_id"] == DBNull.Value ? default(decimal) : Convert.ToDecimal(reader["efft_ts_set_id"]);
                    cdId          = Convert.ToDecimal(reader["evt_typ_cd_id"]);
                    rplcVersionId = reader["rplc_vrsn_id"] == DBNull.Value ? default(decimal) : Convert.ToDecimal(reader["rplc_vrsn_id"]);
                }
                finally
                {
                    reader.Close();
                }

                // Read codes and times
                retVal.ChangeType = DbUtil.GetCodedValue(conn, null, cdId);
                if (tsId != default(decimal))
                {
                    retVal.EffectiveTime = DbUtil.GetEffectiveTimestampSet(conn, null, tsId);
                }

                if (container != null)
                {
                    container.Add(retVal);
                }

                if (role.HasValue && (role.Value & HealthServiceRecordSiteRoleType.ReplacementOf) == HealthServiceRecordSiteRoleType.ReplacementOf)
                {
                    ;
                }
                else
                {
                    DbUtil.DePersistComponents(conn, retVal, this, loadFast);
                }
            }
            finally
            {
                cmd.Dispose();
            }

            return(retVal);
        }
Example #25
0
 protected bool Equals(ChangeSummary other)
 {
     return _index == other._index && Equals(_latest, other._latest) && Equals(_overall, other._overall);
 }
        public ChangeSummary GetSummaryOfChangesSince(string lastProcessedChangeItemId, List <string> filterStrings)
        {
            ChangeSummary changeSummary = new ChangeSummary();

            changeSummary.ChangeCount = 0;
            changeSummary.FirstChangeModifiedTimeUtc = DateTime.MinValue;

            int       lastProcessedChangesetId = 0;
            Changeset lastProcessedChangeset;

            try
            {
                lastProcessedChangesetId = int.Parse(lastProcessedChangeItemId);
            }
            catch (FormatException)
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture,
                                                          TfsVCAdapterResource.InvalidChangeItemIdFormat, lastProcessedChangeItemId));
            }
            lastProcessedChangeset = m_tfsClient.GetChangeset(lastProcessedChangesetId, false, false);
            changeSummary.FirstChangeModifiedTimeUtc = lastProcessedChangeset.CreationDate.ToUniversalTime();

            Changeset nextChangesetToBeMigrated = null;

            if (m_tfsClient.GetLatestChangesetId() > lastProcessedChangesetId)
            {
                // Query history for each filter string path
                foreach (string path in filterStrings)
                {
                    VersionSpec versionSpecFrom = new ChangesetVersionSpec(lastProcessedChangesetId + 1);

                    IEnumerable changesets = m_tfsClient.QueryHistory(path, VersionSpec.Latest, 0, RecursionType.Full, null, versionSpecFrom, VersionSpec.Latest, Int32.MaxValue, false, true);

                    try
                    {
                        foreach (Changeset cs in changesets)
                        {
                            if (!m_translationService.IsSyncGeneratedItemVersion(
                                    cs.ChangesetId.ToString(),
                                    Constants.ChangeGroupGenericVersionNumber,
                                    new Guid(m_migrationSource.InternalUniqueId)) &&
                                !cs.Comment.Contains(m_skipComment))
                            {
                                changeSummary.ChangeCount++;

                                if (nextChangesetToBeMigrated == null || cs.ChangesetId < nextChangesetToBeMigrated.ChangesetId)
                                {
                                    nextChangesetToBeMigrated = cs;
                                }
                            }
                        }
                    }
                    catch (ItemNotFoundException)
                    {
                        // swallow if there are no changesets
                    }
                }
            }

            if (nextChangesetToBeMigrated != null)
            {
                changeSummary.FirstChangeModifiedTimeUtc = nextChangesetToBeMigrated.CreationDate.ToUniversalTime();
            }

            return(changeSummary);
        }