static private void AddQuery(StravaXApiContext db, String AthleteId, DateTime DateFrom, DateTime DateTo, List <ActivityRangeQuery> queriesForPatient) { if (!isDateInList(DateFrom, queriesForPatient)) { if (db.ActivityQueriesDB.Find(AthleteId, DateFrom, DateTo) == null) { ActivityRangeQuery query = new ActivityRangeQuery(); query.AthleteId = AthleteId; query.DateFrom = DateFrom; query.DateTo = DateTo; db.ActivityQueriesDB.Add(query); Console.WriteLine($"add query for {query}"); } } }
static internal int WriteQueriesForAthletes(StravaXApi stravaXApi) { int ret = -1; Console.WriteLine("Create range queries."); using (StravaXApiContext db = new StravaXApiContext()) { stravaXApi.signIn(); try { // https://docs.microsoft.com/en-us/ef/ef6/querying/ // First retrieve all query objects to avoid "New transaction is not allowed because there are other threads running in the session." // Not best praxis but enought for Strava.XApi. IList <AthleteShort> AllAthletes = db.AthleteShortDB.ToList(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); foreach (AthleteShort Athlete in AllAthletes) { TimeSpan ts = stopWatch.Elapsed; try { WriteQueriesForAthlete(stravaXApi, db, Athlete.AthleteId); Console.WriteLine($"Athlete:{Athlete} run since:{ts.TotalSeconds}s"); } catch (PrivateAthleteException e) { // TODO AB#27 athlete should be marked as private to avoid second visit. // create a dummy Query to prevent next search ActivityRangeQuery arq = new ActivityRangeQuery(); arq.AthleteId = Athlete.AthleteId; arq.DateFrom = new DateTime(2019, 12, 01); arq.DateTo = new DateTime(2019, 12, 31); arq.Status = QueryStatus.Done; arq.Message = $"private athlete {Athlete.AthleteId}"; arq.StatusChanged = DateTime.Now; db.ActivityQueriesDB.Add(arq); Console.WriteLine($"SKIP: private athlete {Athlete.AthleteId} {e.Message}"); } catch (TooManyStravaRequestException e) { Console.WriteLine($"Too Many Queries detected {e.Message} need to wait some hours"); } catch (Exception e) { Console.WriteLine($"SKIP:{Athlete.AthleteId} {e.ToString()}"); } db.SaveChanges(); } ret = 0; } catch (Exception e) { Console.WriteLine($"ERROR:{e.ToString()}"); ret = -1; } finally { stravaXApi.Dispose(); } } return(ret); }