AerospikeTraining.Program.DayRangeForUser C# (CSharp) Method

DayRangeForUser() public method

public DayRangeForUser ( ) : void
return void
        public void DayRangeForUser()
        {
            // Get min and max dates
            int min;
            int max;
            string user;
            Console.WriteLine("Enter user (User-1):");
            user = Console.ReadLine().Trim();
            Console.WriteLine("\nEnter start day (0 is today, 365 is 1 year ago):");
            min = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter end day:");
            max = int.Parse(Console.ReadLine());
            if (min > max) {
                int temp = max;
                max = min;
                min = temp;
            }
            List<Key> KeyList = new List<Key> ();
            for (int y = min; y <= max; y ++) {
                DateTime EventDay = epoch.Subtract (TimeSpan.FromDays (y));
                String DateString = EventDay.ToString ("yyyy-MM-dd");
                String EventKeyString = user + ":" + DateString + ":" + 1;
                Key EventKey = new Key ("test", "user-events", EventKeyString);
                KeyList.Add (EventKey);
            }
            Key[] keys = KeyList.ToArray ();
            Record[] records = client.Get (null, keys);
            int count = 0;
            foreach (Record record in records) {
                if (record != null) {
                    Console.WriteLine (EventToString (record));
                    count++;
                }
            }
            Console.WriteLine("Records found: " + count);
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("User event data grouped by day\n");
            Program p = null;
            try
            {
                ClientPolicy policy = new ClientPolicy();

            //				policy.user = "******";
            //				policy.password = "******";
            //				policy.failIfNotConnected = true;
            //				p = new Program(new AerospikeClient(policy, "C-9a8d04af83.aerospike.io", 3200));
                p = new Program(new AerospikeClient(policy, "127.0.0.1", 3000));

                int feature;
                do {

                    // Present options
                    Console.WriteLine("What would you like to do:");
                    Console.WriteLine("1> Generate data");
                    Console.WriteLine("2> Simulate daily events");
                    Console.WriteLine("3> Get a day range of events for a user");
                    Console.WriteLine("0> Exit");
                    Console.Write("\nSelect 1-3 and hit enter:");
                    feature = int.Parse(Console.ReadLine());

                    if (feature != 0)
                    {
                        switch (feature)
                        {
                            case 1:
                                Console.WriteLine("********** Generate Data");
                                p.GenerateData();
                                break;
                            case 2:
                                Console.WriteLine("********** Simulate daily events");
                                p.SimulateDailyEvents();
                                break;
                            case 3:
                                Console.WriteLine("********** Get a day range of events for a user");
                                p.DayRangeForUser();
                                break;
                            case 0:
                                Console.WriteLine("Goodbye");
                                break;
                            default:
                                Console.WriteLine("********** Invalid Selection ");
                                break;
                        }
                    }
                } while (feature != 0);
            }
            catch (AerospikeException e)
            {
                Console.WriteLine("AerospikeException - Message: " + e.Message);
                Console.WriteLine("AerospikeException - StackTrace: " + e.StackTrace);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception - Message: " + e.Message);
                Console.WriteLine("Exception - StackTrace: " + e.StackTrace);
            }
            finally
            {
                if (p.client != null && p.client.Connected)
                {
                    // Close Aerospike server connection
                    p.client.Close();
                }
            }
        }