#!/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 => 2508, categories => [ 'Potholes', 'Other' ], name => 'Hackney Council' }, { area_id => 2636, categories => [ 'Potholes', 'Private', 'Extra' ], name => 'Isle of Wight Council' }, { area_id => 2566, categories => [ 'Fallen branch', 'Light Out', 'Light Dim', 'Fallen Tree', 'Damaged Tree' ], name => 'Peterborough City Council' }, { area_id => 2498, categories => [ 'Incorrect timetable', 'Glass broken', 'Mobile Crane Operation' ], name => 'TfL' }, { area_id => 2237, categories => [ 'Flytipping', 'Roads', 'Parks' ], name => 'Oxfordshire County Council' }, ) { $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"; } for my $cat_name ('Fallen Tree', 'Damaged Tree') { my $cat = FixMyStreet::DB->resultset('Contact')->find({ body => $bodies->{2566}, category => $cat_name, }); $cat->set_extra_metadata( group => 'Trees' ); $cat->update; } for my $cat_name ('Light Out', 'Light Dim') { my $cat = FixMyStreet::DB->resultset('Contact')->find({ body => $bodies->{2566}, category => $cat_name, }); $cat->set_extra_metadata( group => 'Street lighting' ); $cat->update; } 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_inspector_with_instruct = [@$perms_inspector, 'report_instruct']; 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 => 'Inspector Instructor', email => 'inspector-instructor@example.org', email_verified => 1, body => $body, permissions => $perms_inspector_with_instruct }, { 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($_->{class}) == $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);