Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create N time series #65

Closed
mdmoura opened this issue Mar 26, 2017 · 5 comments
Closed

Create N time series #65

mdmoura opened this issue Mar 26, 2017 · 5 comments

Comments

@mdmoura
Copy link

mdmoura commented Mar 26, 2017

I have the following classes:

public class Measure {
  public Int32 Id { get; set; }
  public Int32 UserId { get; set; }
  public Int32 MeasureTypeId { get; set; }
  public DateTime Created { get; set;  }
  public Decimal Value { get; set; }
}

public class User {
  public Int32 Id { get; set; }
  public String Name { get; set; }
  public ICollection<Measure> Measures { get; set; }
}

The property MeasureTypeId can be a number between 1 to 4.

I am creating 20 random Users as follows:

    List<User> users = new Faker<User>()
      .RuleFor(x => x.Id, () => 0)
      .RuleFor(x => x.Name, y => y.Person.FirstName)
      .Generate(20).ToList();  

I need to create 4 lists of Measures (one for each MeasureTypeId 1 to 4) with 60 points:

  • The created date is incremented day by day starting on 1 Janurary 2017;
  • The value is a random between 10 to 20.

Basically, I need to create 4 time series (one for each MeasureTypeId) for each user.

I am able to generate One or Many Measure ... I am just not sure how to create such time series.

Is there a straight forward way with Bogus?

@bchavez
Copy link
Owner

bchavez commented Mar 26, 2017

Hi @mdmoura ,

Seems straight forward:

[Test]
public void issue_65_create_n_time_series()
{
    var measureFaker = new Faker<Issue65Measure>()
        .RuleFor(x => x.Id, f => f.IndexGlobal)
        .RuleFor(x => x.MeasureTypeId, f => f.Random.Number(1, 4))
        .RuleFor(x => x.Created, f => DateTime.Parse("1/1/2017").AddDays(f.IndexVariable++))
        .RuleFor(x => x.Value, f => f.Random.Decimal(10, 20));


    var userFaker = new Faker<Isssue65User>()
        .RuleFor(x => x.Id, f => f.IndexVariable++)
        .RuleFor(x => x.Name, y => y.Person.FirstName)
        .RuleFor( x => x.Measures, (f, u) => measureFaker.Generate(4).ForEach( m => m.UserId = u.Id) );

    userFaker.Generate(20).ToList().Dump();
}

public class Issue65Measure
{
    public Int32 Id { get; set; }
    public Int32 UserId { get; set; }
    public Int32 MeasureTypeId { get; set; }
    public DateTime Created { get; set; }
    public Decimal Value { get; set; }
}

public class Isssue65User
{
    public Int32 Id { get; set; }
    public String Name { get; set; }
    public ICollection<Issue65Measure> Measures { get; set; }
}

Produces:

{
  "Id": 0,
  "Name": "Lee",
  "Measures": [
    {
      "Id": 1,
      "UserId": 0,
      "MeasureTypeId": 3,
      "Created": "2017-01-01T00:00:00",
      "Value": 18.580745825814430
    },
    {
      "Id": 2,
      "UserId": 0,
      "MeasureTypeId": 3,
      "Created": "2017-01-02T00:00:00",
      "Value": 19.016423713889170
    },
    {
      "Id": 3,
      "UserId": 0,
      "MeasureTypeId": 2,
      "Created": "2017-01-03T00:00:00",
      "Value": 18.706475281485580
    },
    {
      "Id": 4,
      "UserId": 0,
      "MeasureTypeId": 4,
      "Created": "2017-01-04T00:00:00",
      "Value": 19.49160031484980
    }
  ]
},
{
  "Id": 1,
  "Name": "Peyton",
  "Measures": [
    {
      "Id": 6,
      "UserId": 1,
      "MeasureTypeId": 3,
      "Created": "2017-01-05T00:00:00",
      "Value": 19.750085333245840
    },
    {
      "Id": 7,
      "UserId": 1,
      "MeasureTypeId": 3,
      "Created": "2017-01-06T00:00:00",
      "Value": 12.914435650647820
    },
    {
      "Id": 8,
      "UserId": 1,
      "MeasureTypeId": 3,
      "Created": "2017-01-07T00:00:00",
      "Value": 19.127092565934680
    },
    {
      "Id": 9,
      "UserId": 1,
      "MeasureTypeId": 2,
      "Created": "2017-01-08T00:00:00",
      "Value": 14.997773391659270
    }
  ]
},
...

🍂 🍃 "I get a sense of weightlessness…"

@bchavez bchavez closed this as completed Mar 26, 2017
@mdmoura
Copy link
Author

mdmoura commented Mar 28, 2017

@bchavez That isn't exactly what I am trying to create ... I would need, for each day, one record of each MeasureType selected. Consider I have two measure types I would get for the first 3 days;

  "Measures": [
    {
      "Id": 1,
      "UserId": 0,
      "MeasureTypeId": 1,
      "Created": "2017-01-01T00:00:00",
      "Value": 18.580745825814430
    },
    {
      "Id": 2,
      "UserId": 0,
      "MeasureTypeId": 2,
      "Created": "2017-01-01T00:00:00",
      "Value": 28.580745825814430
    },
    {
      "Id": 3,
      "UserId": 0,
      "MeasureTypeId": 1,
      "Created": "2017-01-02T00:00:00",
      "Value": 12.580745825814430
    },
    {
      "Id": 4,
      "UserId": 0,
      "MeasureTypeId": 2,
      "Created": "2017-01-02T00:00:00",
      "Value": 16.580745825814430
    },
    {
      "Id": 5,
      "UserId": 0,
      "MeasureTypeId": 1,
      "Created": "2017-01-03T00:00:00",
      "Value": 10.580745825814430
    },
    {
      "Id": 6,
      "UserId": 0,
      "MeasureTypeId": 2,
      "Created": "2017-01-03T00:00:00",
      "Value": 10.580745825814430
    }
  ]

Thank You,
Miguel

@mdmoura
Copy link
Author

mdmoura commented Mar 28, 2017

@bchavez I am doing it using FinishWith:

    List<User> users = new Faker<User>()
      .RuleFor(x => x.Id, f => f.IndexVariable++)
      .RuleFor(x => x.Name, f => f.Person.FirstName)
      .FinishWith((f, x) => {
        x.Measures = Enumerable.Range(1, 8).SelectMany(y => Enumerable.Range(1, 200), (t, d) => 
          new Measure { 
            MeasureTypeId = t, 
            Created = DateTime.Now.AddDays(d), 
            Value = f.Random.Decimal(5, 20)
          }).ToList();
      })
      .Generate(20).ToList();

So for each user I am adding 200 points, one per day, for each MeasureType. I can't find a better way.

@bchavez
Copy link
Owner

bchavez commented Mar 28, 2017

@mdmoura LGTM, you could do this also:

using static System.Linq.Enumerable;
List<User> users = new Faker<User>()
     .RuleFor(x => x.Id, f => f.IndexVariable++)
     .RuleFor(x => x.Name, f => f.Person.FirstName)
     .RuleFor(x => x.Measures, (f, u) =>
         Range(1, 8).SelectMany(y => Range(1, 200),
             (t, d) => new Measure
                 {
                     MeasureTypeId = t,
                     Created = DateTime.Now.AddDays(d),
                     Value = f.Random.Decimal(5, 20)
                 }).ToList()
     )
     .Generate(20).ToList();

A bit more terse I'd say.

Thanks,
Brian

⛅ 👥 "Ain't no sunshine when she's gone... And she's always gone too long..."

@mdmoura
Copy link
Author

mdmoura commented Mar 29, 2017

@bchavez Yes, it looks better that way. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants