aboutsummaryrefslogtreecommitdiffstats
path: root/bin/fixmystreet.com/fixture
blob: f4a9fbef15949ec3611822df7ce3431f30e92080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/env perl
#
# This script will create a test body and its categories, covering the area
# provided, and users associated with that body, which should help testing
# of report interactions.

use strict;
use warnings;
use v5.14;
use utf8;

BEGIN {
    use File::Basename qw(dirname);
    use File::Spec;
    my $d = dirname(File::Spec->rel2abs($0));
    require "$d/../../setenv.pl";
}

use List::Util qw(shuffle);
use Path::Tiny;
use FixMyStreet;
use FixMyStreet::Cobrand;
use FixMyStreet::PhotoStorage;
use FixMyStreet::DB::Factories;
use Getopt::Long::Descriptive;

my ($opt, $usage) = describe_options(
    '%c %o',
    [ 'area-id=i', "MapIt area ID to create body for", { required => 1 } ],
    [ 'name:s', "Name of body to use (defaults to MapIt area name)" ],
    [ 'empty', "Empty all tables of the database first" ],
    [ 'commit', "Actually commit changes to the database" ],
    [ 'nonrandom', "Output things in a fixed manner, for testing" ],
    [ 'test_fixtures', "Generate test specific fixtures" ],
    [ 'coords=s', "Co-ordinates to use instead of example postcode" ],
    [ 'help', "print usage message and exit", { shortcircuit => 1 } ],
);
print($usage->text), exit if $opt->help;

FixMyStreet::DB::Factories->setup($opt);

# Body + categories
my $categories = [
    'Abandoned vehicles',
    'Bus stops',
    'Dog fouling',
    'Flyposting',
    'Flytipping',
    'Footpath/bridleway away from road',
    'Graffiti',
    'Parks/landscapes',
    'Pavements',
    'Potholes',
    'Public toilets',
    'Roads/highways',
    'Road traffic signs',
    'Rubbish (refuse and recycling)',
    'Street cleaning',
    'Street lighting',
    'Street nameplates',
    'Traffic lights',
    'Trees',
    'Other',
];
my $body = FixMyStreet::DB::Factory::Body->find_or_create({
    area_id => $opt->area_id,
    categories => $categories,
    $opt->name ? (name => $opt->name) : (),
});
say "Created body " . $body->name . " for MapIt area ID " . $opt->area_id . ', categories ' . join(', ', @$categories);

for my $cat (qw/Overflowing Broken Missing/) {
    my $child_cat = FixMyStreet::DB::Factory::Contact->find_or_create({
        body => $body,
        category => $cat
    });
    $child_cat->set_extra_metadata( group => 'Bins' );
    $child_cat->update;
}

for my $cat ('Dropped Kerbs', 'Skips') {
    my $child_cat = FixMyStreet::DB::Factory::Contact->find_or_create({
        body => $body,
        category => $cat
    });
    $child_cat->set_extra_metadata( group => 'Licensing' );
    $child_cat->set_extra_fields(
        { description => 'Start date', code => 'start_date', datatype => 'string', fieldtype => 'date' },
        { description => 'End date', code => 'end_date', datatype => 'string', fieldtype => 'date' }
    );
    $child_cat->update;
}

if ($opt->test_fixtures) {
    my $bodies;
    foreach (
        { area_id => 2234, categories => ['Shelter Damaged', 'Very Urgent'], name => 'Northamptonshire County Council' },
        { area_id => 2217, categories => ['Flytipping', 'Roads', 'Parks'], name => 'Buckinghamshire Council' },
        { area_id => 2257, categories => ['Flytipping', 'Graffiti'], name => 'Chiltern District Council' },
        { area_id => 2397, categories => [ 'Graffiti' ], name => 'Northampton Borough Council' },
        { area_id => 2483, categories => [ 'Potholes', 'Other' ], name => 'Hounslow Borough Council' },
        { area_id => 2636, categories => [ 'Potholes', 'Private', 'Extra' ], name => 'Isle of Wight Council' },
        { area_id => 2566, categories => [ 'Fallen branch' ], name => 'Peterborough City Council' },
        { area_id => 2498, categories => [ 'Incorrect timetable', 'Glass broken', 'Mobile Crane Operation' ], name => 'TfL' },
    ) {
        $bodies->{$_->{area_id}} = FixMyStreet::DB::Factory::Body->find_or_create($_);
        my $cats = join(', ', @{$_->{categories}});
        say "Created body $_->{name} for MapIt area ID $_->{area_id}, categories $cats";
    }

    my $child_cat = FixMyStreet::DB->resultset("Contact")->find({
        body => $bodies->{2234},
        category => 'Very Urgent',
    });
    $child_cat->set_extra_fields({
        code => 'emergency',
        datatype => 'string',
        description => 'Please call us instead, it is very urgent.',
        order => 1,
        variable => 'false',
        disable_form => 'true',
    });
    $child_cat->update;

    $child_cat = FixMyStreet::DB->resultset("Contact")->find({
        body => $bodies->{2217},
        category => 'Flytipping',
    });
    $child_cat->set_extra_fields({
        code => 'road-placement',
        datatype => 'singlevaluelist',
        description => 'Is the fly-tip located on',
        order => 100,
        required => 'true',
        variable => 'true',
        values => [
            { key => 'road', name => 'The road' },
            { key => 'off-road', name => 'Off the road/on a verge' },
        ],
    });
    $child_cat->update;

    $child_cat = FixMyStreet::DB->resultset("Contact")->find({
        body => $bodies->{2217},
        category => 'Parks',
    });
    $child_cat->set_extra_fields({
        code => 'site_code',
        datatype => 'string',
        description => 'site code',
        order => 100,
        required => 'false',
        variable => 'true',
        automated => 'hidden_field',
    });
    $child_cat->update;

    $child_cat = FixMyStreet::DB->resultset("Contact")->find({
        body => $bodies->{2483},
        category => 'Other',
    });
    $child_cat->update({
        non_public => 1
    });

    $child_cat = FixMyStreet::DB->resultset("Contact")->find({
        body => $bodies->{2636},
        category => 'Potholes',
    });
    $child_cat->update({
        send_method => 'Triage'
    });
    $child_cat = FixMyStreet::DB->resultset("Contact")->find({
        body => $bodies->{2636},
        category => 'Private',
    });
    $child_cat->update({
        non_public => 1,
        send_method => 'Triage'
    });
    $child_cat = FixMyStreet::DB->resultset("Contact")->find({
        body => $bodies->{2636},
        category => 'Extra',
    });
    $child_cat->update({
        send_method => 'Triage'
    });
    $child_cat->set_extra_fields({
        code => 'extra',
        datatype => 'string',
        order => 1,
        variable => 'true',
    });
    $child_cat->update;

    $child_cat = FixMyStreet::DB->resultset("Contact")->find({
        body => $bodies->{2566},
        category => 'Fallen branch',
    });
    $child_cat->set_extra_fields(
        {
            code => 'emergency',
            datatype => 'singlevaluelist',
            description => 'Is it blocking a footpath or a highway?',
            order => 0,
            variable => 'true',
            required => 'true',
            values => [
                { key => 'yes', name => 'Yes', disable => 1, disable_message => 'Please phone customer services to report this problem.' },
                { key => 'no', name => 'No' },
            ]
        },
        {
            code => 'private_land',
            datatype => 'singlevaluelist',
            description => 'Is this problem on private land?',
            order => 0,
            variable => 'true',
            required => 'true',
            values => [
                { key => 'yes', name => 'Yes', disable => 1, disable_message => 'The council do not have powers to address issues on private land.' },
                { key => 'no', name => 'No' },
            ]
        }
    );
    $child_cat->update;

    FixMyStreet::DB->resultset('BodyArea')->create({ body_id => $bodies->{2498}->id, area_id => 2457 });
    FixMyStreet::DB->resultset('BodyArea')->create({ body_id => $bodies->{2498}->id, area_id => 2483 });
    $child_cat = FixMyStreet::DB->resultset("Contact")->find({ body => $bodies->{2498}, category => 'Incorrect timetable' });
    $child_cat->set_extra_metadata(group => ['Bus Stops and Shelters']);
    $child_cat->update;
    $child_cat = FixMyStreet::DB->resultset("Contact")->find({ body => $bodies->{2498}, category => 'Glass broken' });
    $child_cat->set_extra_metadata(group => ['Bus Stops and Shelters']);
    $child_cat->update;
}

FixMyStreet::DB::Factory::ResponseTemplate->create({
    body => $body, title => 'Generic',
    text => 'Thank you for your report, we will be in touch with an update soon.' });
FixMyStreet::DB::Factory::ResponseTemplate->create({
    body => $body, title => 'Fixed', state => 'fixed - council',
    text => 'Thank you for reporting that issue, it has now been resolved.' });
FixMyStreet::DB::Factory::ResponseTemplate->create({
    body => $body, title => 'Monitoring', state => 'unable to fix',
    text => 'Thank you for your enquiry. I have visited the site and assessed the problem that you have identified. Unfortunately it doesn’t meet our current intervention criteria so I cannot arrange a repair. We will continue to monitor this during our routine inspections.' });
FixMyStreet::DB::Factory::ResponseTemplate->create({
    body => $body, title => 'Taxed in date', state => 'unable to fix',
    text => 'We have investigated this report and found the vehicle to be taxed therefore no further action will be taken.  Please note that from October 2014 it is no longer a legal requirement to display a tax disc.' });
my $template = FixMyStreet::DB::Factory::ResponseTemplate->create({
    body => $body, title => 'Not responsible', state => 'not responsible',
    text => 'This report is not the responsibility of the council and will be passed to the relevant organisation.' });
$template->add_to_contacts($body->contacts->first);
my $priority = FixMyStreet::DB::Factory::ResponsePriority->create({ body => $body, description => '4 hour fix' });
FixMyStreet::DB::Factory::ResponsePriority->create({ body => $body, description => '28 day fix' });
FixMyStreet::DB::Factory::ResponsePriority->create({ body => $body, description => 'For information only' });
$priority->add_to_contacts($body->contacts->first);

# Users
say "Created users, all with password 'password':";
my %users;
my $perms_inspector = ['report_inspect', 'planned_reports'];
my $perms_cs = [
    'contribute_as_body', 'contribute_as_another_user',
    'moderate', 'view_body_contribute_details',
];
my $perms_cs_full = [
    'contribute_as_body', 'contribute_as_another_user',
    'moderate', 'view_body_contribute_details',
    'report_prefill', 'default_to_body'
];
foreach (
    { name => 'Inspector Gadget', email => 'inspector@example.org', email_verified => 1, body => $body, permissions => $perms_inspector },
    { name => 'Harriet Helpful', email_verified => 1, email => 'cs@example.org', body => $body, permissions => $perms_cs },
    { name => 'Andrew Agreeable', email_verified => 1, email => 'cs_full@example.org', body => $body, permissions => $perms_cs_full },
    { name => 'Super User', email_verified => 1, email => 'super@example.org', body => $body, permissions => [
        @$perms_cs, @$perms_inspector, 'report_edit',
        'category_edit', 'template_edit', 'responsepriority_edit',
        'user_assign_body', 'user_manage_permissions', 'user_edit',
    ] },
    { name => 'Wizard of Oz', email_verified => 1, email => 'admin@example.org', is_superuser => 't' },
    { name => "Norma User", email_verified => 1, email => 'user@example.org' },
) {
    $users{$_->{email}} = FixMyStreet::DB::Factory::User->find_or_create($_);
    my $perms = $_->{permissions} ? " (" . join(', ', @{$_->{permissions} || []}) . ")" : "";
    my $su = $_->{is_superuser} ? " (superuser)" : "";
    say "* $_->{email}$perms$su";
}

# Problems

my ($location, $lat, $lon);
if ($opt->coords) {
    $location = $opt->coords;
    ($lat, $lon) = split ',', $location;
} else {
    my $postcode = mySociety::MaPit::call('area/example_postcode', $opt->area_id);
    $postcode = mySociety::MaPit::call('postcode', $postcode);
    ($location, $lat, $lon) = map { $postcode->{$_} } qw/postcode wgs84_lat wgs84_lon/;
}

my $cobrand = 'default';
foreach (FixMyStreet::Cobrand->available_cobrand_classes) {
    my $sub = $_->{class} && $_->{class}->can('council_area_id');
    if ($sub && &$sub == $opt->area_id) {
        $cobrand = $_->{class}->moniker;
        last;
    }
}

FixMyStreet::PhotoStorage::backend->init();

my $user = $users{'user@example.org'};
my $num = $opt->nonrandom ? 21 : 50;
say "Created $num problems around '$location' in cobrand '$cobrand'";
my $confirmed = DateTime->today->subtract(days => 1)->add(hours => 8);

my @problem_data;
if ($opt->nonrandom) {
    my $data = FixMyStreet::DB::Factory::Problem->data;
    my @config = (
        { category => 'Potholes', count => 6, times => [ 1000, 2000, 3000 ] },
        { category => 'Street lighting', count => 5, times => [ 750, 2100, 2900, 4000 ] },
        { category => 'Graffiti', count => 5, times => [ 1501, 1500, 500 ] },
        { category => 'Other', count => 5, times => [ 6000, 14000 ] },
    );
    for my $c (@config) {
        my $category = $c->{category};
        for (my $i = 0; $i < $c->{count}; $i++) {
            my $time = $confirmed->clone->add(seconds => $c->{times}->[$i] || ( rand(7000) + 6000 ));
            push @problem_data, {
                title => $data->{titles}->{$category}->[$i],
                detail => $data->{descriptions}->{$category}->[$i],
                category => $category,
                confirmed => $time,
            };
        }
    }
} else {
    for (1..$num) {
        $confirmed->add(seconds => rand(7000));
        my $category = $categories->[int(rand(@$categories))];
        push @problem_data, {
            category => $category,
            confirmed => $confirmed,
        };
    }
}

my $problems = [];
for (0..$num-1) {
    push @$problems, FixMyStreet::DB::Factory::Problem->create_problem({
        body => $body,
        areas => ',' . $opt->area_id . ',',
        user => $user,
        postcode => $location,
        latitude => $lat,
        longitude => $lon,
        cobrand => $cobrand,
        lastupdate => $problem_data[$_]->{confirmed},
        %{$problem_data[$_]},
    });
}

for (1..5) {
    my $p = $problems->[int(rand(@$problems))];
    $users{'inspector@example.org'}->add_to_planned_reports($p);
}

# Flow comments
my @fixed_user = (
    'Brilliant! Seen to within two days and no more problems, thanks.',
    'Thank you for getting this sorted out so quickly.',
    'Walked past last Tuesday and it seems to have been fixed, hooray.',
    'Well it took a few emails back and forth to understand the exact location etc but pleased to say it’s all ok now.',
    'Yep, all good.',
    'The council deserve every penny of their salaries, honestly can’t give them higher praise for the way they’ve handled this issue. Bravo.',
    'All seems ok now, fingers crossed.',
    'Bish bash bosh. Sorted. Thanks so much.',
);

my @problems = $opt->nonrandom ? @$problems : shuffle(@$problems);

my @range = $opt->nonrandom ? (1, 7, 12) : (1..10);
for (@range) {
    my $problem = $problems[$_];
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        user => $users{'cs@example.org'},
        text => 'Could you please provide a better picture so we can locate this issue more easily.',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        user => $user,
        text => 'Here you go!',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        problem_state => 'investigating',
        user => $users{'cs@example.org'},
        text => 'Thanks, we are now looking into this.',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        problem_state => 'action scheduled',
        user => $users{'inspector@example.org'},
        text => 'Confirmed and scheduled for fixing.',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        problem_state => 'fixed - council',
        user => $users{'inspector@example.org'},
        text => 'Fixed',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $problem->update({ state => 'fixed - council' });
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        user => $user,
        text => $fixed_user[int(rand(@fixed_user))],
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $problem->update( { lastupdate => DateTime::Format::Pg->format_datetime($confirmed) } );
}

# Some 'still open' updates
my @open_user = (
    'I’m afraid this problem is recurrent. Could it be prioritised please?',
    'Ongoing issue.',
    'Council rang to say they’re aware and it’s on their list.',
    'Still awaiting news on this one.',
    'Council let me know it’s not a top priority, which TBH I do understand now they’ve talked it through.',
);

my $updates = [];
@range = $opt->nonrandom ? (13, 8, 2) : (11..20);
for my $i (@range) {
    $confirmed->add(seconds => rand(10000));
    my @range_u = $opt->nonrandom ? (1..$i) : (1);
    push @$updates, FixMyStreet::DB::Factory::Comment->create({
        problem => $problems[$i],
        user => $user,
        text => $open_user[int(rand(@open_user))],
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    }) for (@range_u);
    $problems[$i]->update( { lastupdate => DateTime::Format::Pg->format_datetime($confirmed) } );
}

# Some not responsible updates
@range = $opt->nonrandom ? (3, 9, 20) : (21..25);
for (@range) {
    my $problem = $problems[$_];
    $confirmed->add(seconds => rand(10000));
    push @$updates, FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        problem_state => 'not responsible',
        user => $users{'cs@example.org'},,
        text => 'This issue is outside our responsibility.',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $problem->update({ state => 'not responsible' });
}
say "Created updates on problems " . join(', ', map { $_->problem_id } @$updates);