Code Monkey home page Code Monkey logo

Comments (5)

bchavez avatar bchavez commented on May 19, 2024

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…"

from bogus.

mdmoura avatar mdmoura commented on May 19, 2024

@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

from bogus.

mdmoura avatar mdmoura commented on May 19, 2024

@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.

from bogus.

bchavez avatar bchavez commented on May 19, 2024

@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..."

from bogus.

mdmoura avatar mdmoura commented on May 19, 2024

@bchavez Yes, it looks better that way. Thanks

from bogus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.