Exemplo n.º 1
0
        async private void AirtableSendRecords()
        {
            int RecordCount  = 0;
            int NewCount     = 0;
            int UpdatedCount = 0;

            using (AirtableBase airtableBase = new AirtableBase(App.AirtableKey, App.AirtableBase))
            {
                List <Fields>   newRecordList     = new List <Fields>();
                List <IdFields> updatedRecordList = new List <IdFields>();
                foreach (EventTeamMatch match in App.Database.GetEventTeamMatchesAsync().Result)
                {
                    // only send matches from this event
                    if (match.EventKey != App.currFRCEventKey)
                    {
                        continue;
                    }
                    // only send matches from this device
                    if (match.DeviceName != App.KindleName)
                    {
                        continue;
                    }
                    RecordCount++;
                    if (string.IsNullOrEmpty(match.AirtableId))
                    {
                        Fields  fields = new Fields();
                        JObject jo     = match.ToJson();
                        foreach (KeyValuePair <string, object> kv in jo.ToList())
                        {
                            if (kv.Key == "Id" || kv.Key == "AirtableId")
                            {
                                continue;
                            }
                            fields.AddField(kv.Key, kv.Value);
                        }
                        newRecordList.Add(fields);
                    }
                    else
                    {
                        if (match.Changed % 2 == 0) // even, don't upload
                        {
                            continue;
                        }
                        match.Changed++; // make even
                        IdFields fields = new IdFields(match.AirtableId.ToString());
                        JObject  jo     = match.ToJson();
                        foreach (KeyValuePair <string, object> kv in jo.ToList())
                        {
                            if (kv.Key == "Id" || kv.Key == "AirtableId")
                            {
                                continue;
                            }
                            fields.AddField(kv.Key, kv.Value);
                        }
                        updatedRecordList.Add(fields);
                    }
                }
                if (newRecordList.Count > 0)
                {
                    int tempCount = await AirtableSendNewRecords(airtableBase, newRecordList);

                    if (tempCount < 0)
                    {
                        return; // error, exit out
                    }
                    NewCount += tempCount;
                }
                if (updatedRecordList.Count > 0)
                {
                    int tempCount = await AirtableSendUpdatedRecords(airtableBase, updatedRecordList);

                    if (tempCount < 0)
                    {
                        return; // error, exit out
                    }
                    UpdatedCount += tempCount;
                }
            }
            Label_Results.Text  = $"Records found: {RecordCount}\r\n";
            Label_Results.Text += $"New records: {NewCount}\r\n";
            Label_Results.Text += $"Updated records: {UpdatedCount}";
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get IdFields List from trigger table
        /// </summary>
        /// <param name="dbProvider">dbProvider that used to get field info</param>
        /// <param name="dbAdapterName">DBAdapter name</param>
        /// <param name="table">table that read from trigger table</param>
        /// <param name="lastSerial">the last serial number of trigger table from which read</param>
        /// <returns></returns>
        internal static List <IdFields> GetIdFieldsList(Data.DBProvider dbProvider, string dbAdapterName,
                                                        System.Data.DataTable table, out long lastSerial)
        {
            lastSerial = -1;

            HashSet <string> fieldsSetWithTokenizedFields    = new HashSet <string>();
            HashSet <string> fieldsSetWithoutTokenizedFields = new HashSet <string>();
            List <string>    tempFields = new List <string>(128);
            List <IdFields>  result     = new List <IdFields>(table.Rows.Count);

            foreach (System.Data.DataRow row in table.Rows)
            {
                long id = long.Parse(row["id"].ToString());
                lastSerial = long.Parse(row["Serial"].ToString());
                string fields = row["Fields"].ToString();

                tempFields.Clear();
                bool hasTokenized = false;

                //check fields
                foreach (string field in fields.Split(new char[] { ',' }))
                {
                    string f = field.Trim().ToLower();

                    if (f == "")
                    {
                        continue;
                    }

                    Data.Field dbField = dbProvider.GetField(f);

                    if (dbField == null)
                    {
                        continue;
                    }

                    if (dbField.IndexType == Hubble.Core.Data.Field.Index.Tokenized)
                    {
                        hasTokenized = true;
                    }

                    tempFields.Add(f);
                }

                //Fill hash set
                if (hasTokenized)
                {
                    foreach (string field in tempFields)
                    {
                        if (!fieldsSetWithTokenizedFields.Contains(field))
                        {
                            fieldsSetWithTokenizedFields.Add(field);
                        }
                    }
                }
                else
                {
                    foreach (string field in tempFields)
                    {
                        if (!fieldsSetWithoutTokenizedFields.Contains(field))
                        {
                            fieldsSetWithoutTokenizedFields.Add(field);
                        }
                    }
                }

                result.Add(new IdFields(id, hasTokenized));
            }

            //Get new fields string
            string fieldsWithTokenized    = GetFieldsStringFromHashSet(fieldsSetWithTokenizedFields, dbAdapterName);
            string fieldsWithoutTokenized = GetFieldsStringFromHashSet(fieldsSetWithoutTokenizedFields, dbAdapterName);

            foreach (IdFields idFields in result)
            {
                if (idFields.HasTokenizedFields)
                {
                    idFields.Fields = fieldsWithTokenized;
                }
                else
                {
                    idFields.Fields = fieldsWithoutTokenized;
                }
            }

            //Merge same Id
            result.Sort();

            if (result.Count > 0)
            {
                IdFields last = result[0];

                for (int i = 1; i < result.Count; i++)
                {
                    if (result[i].Equals(last))
                    {
                        result[i] = null;
                        continue;
                    }
                    else
                    {
                        last = result[i];
                    }
                }
            }

            return(result);
        }