Example #1
0
        private static void configurePushTransitions(
            ITransitionConfigurator transitions,
            ITogglApi api,
            ITogglDataSource dataSource,
            IAnalyticsService analyticsService,
            ILeakyBucket minutesLeakyBucket,
            IRateLimiter rateLimiter,
            IScheduler scheduler,
            StateResult entryPoint,
            DependencyContainer dependencyContainer)
        {
            var delayState             = new WaitForAWhileState(scheduler, analyticsService);
            var pushNotificationsToken = new SyncPushNotificationsTokenState(dependencyContainer.PushNotificationsTokenStorage, api, dependencyContainer.PushNotificationsTokenService, dependencyContainer.TimeService, dependencyContainer.RemoteConfigService);

            transitions.ConfigureTransition(entryPoint, pushNotificationsToken);

            var pushingWorkspaces  = configureCreateOnlyPush(transitions, pushNotificationsToken.Done, dataSource.Workspaces, analyticsService, api.Workspaces, minutesLeakyBucket, rateLimiter, delayState, Workspace.Clean, Workspace.Unsyncable);
            var pushingUsers       = configurePushSingleton(transitions, pushingWorkspaces.NoMoreChanges, dataSource.User, analyticsService, api.User, minutesLeakyBucket, rateLimiter, delayState, User.Clean, User.Unsyncable);
            var pushingPreferences = configurePushSingleton(transitions, pushingUsers.NoMoreChanges, dataSource.Preferences, analyticsService, api.Preferences, minutesLeakyBucket, rateLimiter, delayState, Preferences.Clean, Preferences.Unsyncable);
            var pushingTags        = configureCreateOnlyPush(transitions, pushingPreferences.NoMoreChanges, dataSource.Tags, analyticsService, api.Tags, minutesLeakyBucket, rateLimiter, delayState, Tag.Clean, Tag.Unsyncable);
            var pushingClients     = configureCreateOnlyPush(transitions, pushingTags.NoMoreChanges, dataSource.Clients, analyticsService, api.Clients, minutesLeakyBucket, rateLimiter, delayState, Client.Clean, Client.Unsyncable);
            var pushingProjects    = configureCreateOnlyPush(transitions, pushingClients.NoMoreChanges, dataSource.Projects, analyticsService, api.Projects, minutesLeakyBucket, rateLimiter, delayState, Project.Clean, Project.Unsyncable);
            var pushingTimeEntries = configurePush(transitions, pushingProjects.NoMoreChanges, dataSource.TimeEntries, analyticsService, api.TimeEntries, api.TimeEntries, api.TimeEntries, minutesLeakyBucket, rateLimiter, delayState, TimeEntry.Clean, TimeEntry.Unsyncable);

            transitions.ConfigureTransition(delayState.Done, pushingWorkspaces);
            transitions.ConfigureTransition(pushingTimeEntries.NoMoreChanges, new DeadEndState());
        }
Example #2
0
        public RateLimiter(ILeakyBucket leakyBucket, IScheduler scheduler)
        {
            Ensure.Argument.IsNotNull(leakyBucket, nameof(leakyBucket));
            Ensure.Argument.IsNotNull(scheduler, nameof(scheduler));

            this.leakyBucket = leakyBucket;
            this.scheduler   = scheduler;
        }
 public FetchJustTimeEntriesSinceState(
     ITogglApi api,
     ISinceParameterRepository since,
     ITimeService timeService,
     ILeakyBucket leakyBucket,
     IRateLimiter limiter)
     : base(api, leakyBucket, since, timeService)
 {
     this.limiter = limiter;
 }
 public TheStartMethod()
 {
     sinceParameters = Substitute.For <ISinceParameterRepository>();
     api             = Substitute.For <ITogglApi>();
     timeService     = Substitute.For <ITimeService>();
     leakyBucket     = Substitute.For <ILeakyBucket>();
     leakyBucket.TryClaimFreeSlots(Arg.Any <int>(), out _).Returns(true);
     rateLimiter = Substitute.For <IRateLimiter>();
     rateLimiter.WaitForFreeSlot().Returns(Observable.Return(Unit.Default));
     timeService.CurrentDateTime.Returns(now);
     state = new FetchAllSinceState(api, sinceParameters, timeService, leakyBucket, rateLimiter);
 }
        public BaseFetchSinceState(
            ITogglApi api,
            ILeakyBucket leakyBucket,
            ISinceParameterRepository since,
            ITimeService timeService)
        {
            this.since       = since;
            this.leakyBucket = leakyBucket;
            this.timeService = timeService;

            Api = api;
        }
Example #6
0
 public FetchAllSinceState(
     ITogglApi api,
     ISinceParameterRepository since,
     ITimeService timeService,
     ILeakyBucket leakyBucket,
     IRateLimiter limiter)
 {
     this.api         = api;
     this.since       = since;
     this.timeService = timeService;
     this.leakyBucket = leakyBucket;
     this.limiter     = limiter;
 }
Example #7
0
        private static void configurePullTimeEntriesTransitions(
            ITransitionConfigurator transitions,
            ITogglApi api,
            ITogglDataSource dataSource,
            ITogglDatabase database,
            IAnalyticsService analyticsService,
            ITimeService timeService,
            ILeakyBucket leakyBucket,
            IRateLimiter rateLimiter,
            ILastTimeUsageStorage lastTimeUsageStorage,
            StateResult entryPoint)
        {
            var fetchTimeEntries = new FetchJustTimeEntriesSinceState(api, database.SinceParameters, timeService, leakyBucket, rateLimiter);
            var ensureFetchTimeEntriesSucceeded = new EnsureFetchListSucceededState <ITimeEntry>();

            var placeholderStateFactory = new CreatePlaceholdersStateFactory(
                dataSource,
                analyticsService,
                lastTimeUsageStorage,
                timeService);
            var createWorkspacePlaceholder = placeholderStateFactory.ForWorkspaces();
            var createProjectPlaceholder   = placeholderStateFactory.ForProjects();
            var createTaskPlaceholder      = placeholderStateFactory.ForTasks();
            var createTagPlaceholder       = placeholderStateFactory.ForTags();

            var persistTimeEntries =
                new PersistListState <ITimeEntry, IDatabaseTimeEntry, IThreadSafeTimeEntry>(dataSource.TimeEntries, TimeEntry.Clean);
            var updateTimeEntriesSinceDate = new UpdateSinceDateState <ITimeEntry>(database.SinceParameters);
            var timeEntriesAnalytics       = new TimeEntriesAnalyticsState(analyticsService);


            transitions.ConfigureTransition(entryPoint, fetchTimeEntries);
            transitions.ConfigureTransition(fetchTimeEntries.PreventOverloadingServer, new DeadEndState());
            transitions.ConfigureTransition(fetchTimeEntries.Done, ensureFetchTimeEntriesSucceeded);
            transitions.ConfigureTransition(ensureFetchTimeEntriesSucceeded.ErrorOccured, new FailureState());

            transitions.ConfigureTransition(ensureFetchTimeEntriesSucceeded.Done, timeEntriesAnalytics);

            transitions.ConfigureTransition(timeEntriesAnalytics.Done, createWorkspacePlaceholder);
            transitions.ConfigureTransition(createWorkspacePlaceholder.Done, createProjectPlaceholder);
            transitions.ConfigureTransition(createProjectPlaceholder.Done, createTaskPlaceholder);
            transitions.ConfigureTransition(createTaskPlaceholder.Done, createTagPlaceholder);

            transitions.ConfigureTransition(createTagPlaceholder.Done, persistTimeEntries);
            transitions.ConfigureTransition(persistTimeEntries.Done, updateTimeEntriesSinceDate);
            transitions.ConfigureTransition(updateTimeEntriesSinceDate.Done, new DeadEndState());
        }
        public DeleteEntityState(
            IDeletingApiClient <TModel> api,
            IAnalyticsService analyticsService,
            IDataSource <TThreadsafeModel, TDatabaseModel> dataSource,
            ILeakyBucket leakyBucket,
            IRateLimiter limiter)
            : base(analyticsService)
        {
            Ensure.Argument.IsNotNull(api, nameof(api));
            Ensure.Argument.IsNotNull(dataSource, nameof(dataSource));
            Ensure.Argument.IsNotNull(leakyBucket, nameof(leakyBucket));
            Ensure.Argument.IsNotNull(limiter, nameof(limiter));

            this.api         = api;
            this.dataSource  = dataSource;
            this.leakyBucket = leakyBucket;
            this.limiter     = limiter;
        }
        private static LookForChangeToPushState <TDatabase, TThreadsafe> configureCreateOnlyPush <TModel, TDatabase, TThreadsafe>(
            ITransitionConfigurator transitions,
            IStateResult entryPoint,
            IDataSource <TThreadsafe, TDatabase> dataSource,
            IAnalyticsService analyticsService,
            ICreatingApiClient <TModel> creatingApi,
            ILeakyBucket minutesLeakyBucket,
            IRateLimiter rateLimiter,
            WaitForAWhileState waitForAWhileState,
            Func <TModel, TThreadsafe> toClean,
            Func <TThreadsafe, string, TThreadsafe> toUnsyncable)
            where TModel : IIdentifiable, ILastChangedDatable
            where TDatabase : class, TModel, IDatabaseSyncable
            where TThreadsafe : class, TDatabase, IThreadSafeModel
        {
            var lookForChange      = new LookForChangeToPushState <TDatabase, TThreadsafe>(dataSource);
            var chooseOperation    = new ChooseSyncOperationState <TThreadsafe>();
            var create             = new CreateEntityState <TModel, TDatabase, TThreadsafe>(creatingApi, dataSource, analyticsService, minutesLeakyBucket, rateLimiter, toClean);
            var processClientError = new ProcessClientErrorState <TThreadsafe>();
            var unsyncable         = new MarkEntityAsUnsyncableState <TThreadsafe>(dataSource, toUnsyncable);

            transitions.ConfigureTransition(entryPoint, lookForChange);
            transitions.ConfigureTransition(lookForChange.ChangeFound, chooseOperation);
            transitions.ConfigureTransition(chooseOperation.CreateEntity, create);

            transitions.ConfigureTransition(chooseOperation.UpdateEntity, new InvalidTransitionState($"Updating is not supported for {typeof(TModel).Name} during Push sync."));
            transitions.ConfigureTransition(chooseOperation.DeleteEntity, new InvalidTransitionState($"Deleting is not supported for {typeof(TModel).Name} during Push sync."));
            transitions.ConfigureTransition(chooseOperation.DeleteEntityLocally, new InvalidTransitionState($"Deleting locally is not supported for {typeof(TModel).Name} during Push sync."));

            transitions.ConfigureTransition(create.ClientError, processClientError);
            transitions.ConfigureTransition(create.ServerError, new FailureState());
            transitions.ConfigureTransition(create.UnknownError, new FailureState());

            transitions.ConfigureTransition(create.PreventOverloadingServer, waitForAWhileState);

            transitions.ConfigureTransition(processClientError.UnresolvedTooManyRequests, new FailureState());
            transitions.ConfigureTransition(processClientError.Unresolved, unsyncable);

            transitions.ConfigureTransition(create.EntityChanged, new InvalidTransitionState($"Entity cannot have changed since updating is not supported for {typeof(TModel).Name} during Push sync."));
            transitions.ConfigureTransition(create.Done, lookForChange);
            transitions.ConfigureTransition(unsyncable.Done, lookForChange);

            return(lookForChange);
        }
Example #10
0
        public UpdateEntityState(
            IUpdatingApiClient <TModel> api,
            IBaseDataSource <TThreadsafeModel> dataSource,
            IAnalyticsService analyticsService,
            ILeakyBucket leakyBucket,
            IRateLimiter limiter,
            Func <TModel, TThreadsafeModel> convertToThreadsafeModel)
            : base(analyticsService)
        {
            Ensure.Argument.IsNotNull(api, nameof(api));
            Ensure.Argument.IsNotNull(dataSource, nameof(dataSource));
            Ensure.Argument.IsNotNull(convertToThreadsafeModel, nameof(convertToThreadsafeModel));
            Ensure.Argument.IsNotNull(leakyBucket, nameof(leakyBucket));
            Ensure.Argument.IsNotNull(limiter, nameof(limiter));

            this.api        = api;
            this.dataSource = dataSource;
            this.convertToThreadsafeModel = convertToThreadsafeModel;
            this.leakyBucket = leakyBucket;
            this.limiter     = limiter;
        }
Example #11
0
 protected LeakyBucketTestsBase()
 {
     LeakyBucket = new LeakyBucket(TimeService, AnalyticsService, 1);
 }
        private static void configurePullTransitions(
            ITransitionConfigurator transitions,
            ITogglDatabase database,
            ITogglApi api,
            ITogglDataSource dataSource,
            ITimeService timeService,
            IAnalyticsService analyticsService,
            IScheduler scheduler,
            StateResult entryPoint,
            ILeakyBucket leakyBucket,
            IRateLimiter rateLimiter,
            ISyncStateQueue queue)
        {
            var delayState = new WaitForAWhileState(scheduler, analyticsService);

            var fetchAllSince = new FetchAllSinceState(api, database.SinceParameters, timeService, leakyBucket, rateLimiter);

            var ensureFetchWorkspacesSucceeded        = new EnsureFetchListSucceededState <IWorkspace>();
            var ensureFetchWorkspaceFeaturesSucceeded = new EnsureFetchListSucceededState <IWorkspaceFeatureCollection>();
            var ensureFetchTagsSucceeded        = new EnsureFetchListSucceededState <ITag>();
            var ensureFetchClientsSucceeded     = new EnsureFetchListSucceededState <IClient>();
            var ensureFetchProjectsSucceeded    = new EnsureFetchListSucceededState <IProject>();
            var ensureFetchTasksSucceeded       = new EnsureFetchListSucceededState <ITask>();
            var ensureFetchTimeEntriesSucceeded = new EnsureFetchListSucceededState <ITimeEntry>();
            var ensureFetchUserSucceeded        = new EnsureFetchSingletonSucceededState <IUser>();
            var ensureFetchPreferencesSucceeded = new EnsureFetchSingletonSucceededState <IPreferences>();

            var scheduleCleanUp = new ScheduleCleanUpState(queue);

            var detectGainingAccessToWorkspaces =
                new DetectGainingAccessToWorkspacesState(
                    dataSource.Workspaces,
                    analyticsService,
                    () => new HasFinsihedSyncBeforeInteractor(dataSource));

            var resetSinceParams = new ResetSinceParamsState(database.SinceParameters);

            var persistNewWorkspaces =
                new PersistNewWorkspacesState(dataSource.Workspaces);

            var detectLosingAccessToWorkspaces =
                new DetectLosingAccessToWorkspacesState(dataSource.Workspaces, analyticsService);

            var deleteRunningInaccessibleTimeEntry = new DeleteInaccessibleRunningTimeEntryState(dataSource.TimeEntries);

            var markWorkspacesAsInaccessible = new MarkWorkspacesAsInaccessibleState(dataSource.Workspaces);

            var persistWorkspaces =
                new PersistListState <IWorkspace, IDatabaseWorkspace, IThreadSafeWorkspace>(dataSource.Workspaces, Workspace.Clean);

            var updateWorkspacesSinceDate =
                new UpdateSinceDateState <IWorkspace>(database.SinceParameters);

            var detectNoWorkspaceState = new DetectNotHavingAccessToAnyWorkspaceState(dataSource);

            var persistWorkspaceFeatures =
                new PersistListState <IWorkspaceFeatureCollection, IDatabaseWorkspaceFeatureCollection, IThreadSafeWorkspaceFeatureCollection>(
                    dataSource.WorkspaceFeatures, WorkspaceFeatureCollection.From);

            var persistUser =
                new PersistSingletonState <IUser, IDatabaseUser, IThreadSafeUser>(dataSource.User, User.Clean);

            var noDefaultWorkspaceDetectingState = new DetectUserHavingNoDefaultWorkspaceSetState(dataSource, analyticsService);

            var trySetDefaultWorkspaceState = new TrySetDefaultWorkspaceState(timeService, dataSource);

            var persistTags =
                new PersistListState <ITag, IDatabaseTag, IThreadSafeTag>(dataSource.Tags, Tag.Clean);

            var updateTagsSinceDate = new UpdateSinceDateState <ITag>(database.SinceParameters);

            var persistClients =
                new PersistListState <IClient, IDatabaseClient, IThreadSafeClient>(dataSource.Clients, Client.Clean);

            var updateClientsSinceDate = new UpdateSinceDateState <IClient>(database.SinceParameters);

            var persistPreferences =
                new PersistSingletonState <IPreferences, IDatabasePreferences, IThreadSafePreferences>(dataSource.Preferences, Preferences.Clean);

            var persistProjects =
                new PersistListState <IProject, IDatabaseProject, IThreadSafeProject>(dataSource.Projects, Project.Clean);

            var updateProjectsSinceDate = new UpdateSinceDateState <IProject>(database.SinceParameters);

            var createProjectPlaceholders = new CreateArchivedProjectPlaceholdersState(dataSource.Projects, analyticsService);

            var persistTimeEntries =
                new PersistListState <ITimeEntry, IDatabaseTimeEntry, IThreadSafeTimeEntry>(dataSource.TimeEntries, TimeEntry.Clean);

            var updateTimeEntriesSinceDate = new UpdateSinceDateState <ITimeEntry>(database.SinceParameters);

            var persistTasks =
                new PersistListState <ITask, IDatabaseTask, IThreadSafeTask>(dataSource.Tasks, Task.Clean);

            var updateTasksSinceDate = new UpdateSinceDateState <ITask>(database.SinceParameters);

            var refetchInaccessibleProjects =
                new TryFetchInaccessibleProjectsState(dataSource.Projects, timeService, api.Projects);

            // start all the API requests first
            transitions.ConfigureTransition(entryPoint, fetchAllSince);

            // prevent overloading server with too many requests
            transitions.ConfigureTransition(fetchAllSince.PreventOverloadingServer, delayState);

            // detect gaining access to workspaces
            transitions.ConfigureTransition(fetchAllSince.Done, ensureFetchWorkspacesSucceeded);
            transitions.ConfigureTransition(ensureFetchWorkspacesSucceeded.Done, detectGainingAccessToWorkspaces);
            transitions.ConfigureTransition(detectGainingAccessToWorkspaces.Done, detectLosingAccessToWorkspaces);
            transitions.ConfigureTransition(detectGainingAccessToWorkspaces.NewWorkspacesDetected, resetSinceParams);
            transitions.ConfigureTransition(resetSinceParams.Done, persistNewWorkspaces);
            transitions.ConfigureTransition(persistNewWorkspaces.Done, fetchAllSince);

            // detect losing access to workspaces
            transitions.ConfigureTransition(detectLosingAccessToWorkspaces.Done, persistWorkspaces);
            transitions.ConfigureTransition(detectLosingAccessToWorkspaces.WorkspaceAccessLost, markWorkspacesAsInaccessible);
            transitions.ConfigureTransition(markWorkspacesAsInaccessible.Done, scheduleCleanUp);
            transitions.ConfigureTransition(scheduleCleanUp.Done, deleteRunningInaccessibleTimeEntry);
            transitions.ConfigureTransition(deleteRunningInaccessibleTimeEntry.Done, persistWorkspaces);

            // persist all the data pulled from the server
            transitions.ConfigureTransition(persistWorkspaces.Done, updateWorkspacesSinceDate);
            transitions.ConfigureTransition(updateWorkspacesSinceDate.Done, detectNoWorkspaceState);
            transitions.ConfigureTransition(detectNoWorkspaceState.Done, ensureFetchUserSucceeded);

            transitions.ConfigureTransition(ensureFetchUserSucceeded.Done, persistUser);
            transitions.ConfigureTransition(persistUser.Done, ensureFetchWorkspaceFeaturesSucceeded);

            transitions.ConfigureTransition(ensureFetchWorkspaceFeaturesSucceeded.Done, persistWorkspaceFeatures);
            transitions.ConfigureTransition(persistWorkspaceFeatures.Done, ensureFetchPreferencesSucceeded);

            transitions.ConfigureTransition(ensureFetchPreferencesSucceeded.Done, persistPreferences);
            transitions.ConfigureTransition(persistPreferences.Done, ensureFetchTagsSucceeded);

            transitions.ConfigureTransition(ensureFetchTagsSucceeded.Done, persistTags);
            transitions.ConfigureTransition(persistTags.Done, updateTagsSinceDate);
            transitions.ConfigureTransition(updateTagsSinceDate.Done, ensureFetchClientsSucceeded);

            transitions.ConfigureTransition(ensureFetchClientsSucceeded.Done, persistClients);
            transitions.ConfigureTransition(persistClients.Done, updateClientsSinceDate);
            transitions.ConfigureTransition(updateClientsSinceDate.Done, ensureFetchProjectsSucceeded);

            transitions.ConfigureTransition(ensureFetchProjectsSucceeded.Done, persistProjects);
            transitions.ConfigureTransition(persistProjects.Done, updateProjectsSinceDate);
            transitions.ConfigureTransition(updateProjectsSinceDate.Done, ensureFetchTasksSucceeded);

            transitions.ConfigureTransition(ensureFetchTasksSucceeded.Done, persistTasks);
            transitions.ConfigureTransition(persistTasks.Done, updateTasksSinceDate);
            transitions.ConfigureTransition(updateTasksSinceDate.Done, ensureFetchTimeEntriesSucceeded);

            transitions.ConfigureTransition(ensureFetchTimeEntriesSucceeded.Done, createProjectPlaceholders);
            transitions.ConfigureTransition(createProjectPlaceholders.Done, persistTimeEntries);
            transitions.ConfigureTransition(persistTimeEntries.Done, updateTimeEntriesSinceDate);
            transitions.ConfigureTransition(updateTimeEntriesSinceDate.Done, refetchInaccessibleProjects);
            transitions.ConfigureTransition(refetchInaccessibleProjects.FetchNext, refetchInaccessibleProjects);

            transitions.ConfigureTransition(refetchInaccessibleProjects.Done, noDefaultWorkspaceDetectingState);
            transitions.ConfigureTransition(noDefaultWorkspaceDetectingState.NoDefaultWorkspaceDetected, trySetDefaultWorkspaceState);
            transitions.ConfigureTransition(noDefaultWorkspaceDetectingState.Done, new DeadEndState());
            transitions.ConfigureTransition(trySetDefaultWorkspaceState.Done, new DeadEndState());

            // fail for server errors
            transitions.ConfigureTransition(ensureFetchWorkspacesSucceeded.ErrorOccured, new FailureState());
            transitions.ConfigureTransition(ensureFetchUserSucceeded.ErrorOccured, new FailureState());
            transitions.ConfigureTransition(ensureFetchWorkspaceFeaturesSucceeded.ErrorOccured, new FailureState());
            transitions.ConfigureTransition(ensureFetchPreferencesSucceeded.ErrorOccured, new FailureState());
            transitions.ConfigureTransition(ensureFetchTagsSucceeded.ErrorOccured, new FailureState());
            transitions.ConfigureTransition(ensureFetchClientsSucceeded.ErrorOccured, new FailureState());
            transitions.ConfigureTransition(ensureFetchProjectsSucceeded.ErrorOccured, new FailureState());
            transitions.ConfigureTransition(ensureFetchTasksSucceeded.ErrorOccured, new FailureState());
            transitions.ConfigureTransition(ensureFetchTimeEntriesSucceeded.ErrorOccured, new FailureState());
            transitions.ConfigureTransition(refetchInaccessibleProjects.ErrorOccured, new FailureState());

            // delay loop
            transitions.ConfigureTransition(delayState.Done, fetchAllSince);
        }