-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathingest-findings
executable file
·122 lines (87 loc) · 2.93 KB
/
ingest-findings
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
#!/usr/bin/env perl
=head1 NAME
ingest-findings -- Ingest yaml for findings though the GCIS API.
=head1 OPTIONS
=head2 B<url>
GCIS url, e.g. http://data-stage.globalchange.gov
=head2 B<report>
report, e.g. nca3
=head2 B<yaml_file>
File with YAML to upload.
=head2 B<chapter_number>
Chapter number.
=head2 B<dry_run>, B<n>
Dry run.
=head2 B<delete_extras>
Delete extra findings in the chapter? (default: yes)
=head2 EXAMPLES
# ingest findings for chapter 5 to local server
./ingest-findings -u http://localhost:3000 -c 5 -r nca3
=cut
use YAML::XS qw/Load Dump/;
use Path::Class qw/file/;
use Data::Dumper;
use Getopt::Long qw/GetOptions/;
use Pod::Usage qw/pod2usage/;
use Encode;
use Gcis::Client;
use v5.14;
GetOptions(
'url=s' => \(my $url),
'report=s' => \(my $report),
'yaml_file=s' => \(my $yaml_file),
'chapter_number=s' => \(my $chapter_number),
'dry_run|n' => \(my $dry_run),
'delete_extras=s' => \(my $delete_extras = 1),
'help' => sub { pod2usage(verbose => 1 ) },
) or die pod2usage("invalid options.");
$delete_extras = 0 if $delete_extras =~ /^n/i;
die "missing chapter number" unless $chapter_number;
$yaml_file //= sprintf("./findings/ch%02d.yaml",$chapter_number);
die "missing report" unless $report;
die "missing yaml_file" unless $yaml_file;
die "missing url" unless $url;
my $c = Gcis::Client->new;
$c->url($url);
$c->find_credentials->login;
my $new = Load(scalar (file($yaml_file)->slurp ));
my %chmap = $c->get_chapter_map($report);
my $chapter = $chmap{$chapter_number};
my $old = $c->get("/report/$report/chapter/$chapter/finding?all=1") or die $c->error;
my %to_delete;
for my $old (@$old) {
my $existing_uri = "/report/$report/chapter/$old->{chapter_identifier}/finding/$old->{identifier}";
$to_delete{$existing_uri} = 1;
}
my $count = 0;
for my $new (@$new) {
die "invalid field, are these figures instead of findings?" if defined($new->{caption});
my ($match) = grep { $_->{ordinal}==$new->{ordinal} } @$old;
if ($match) {
$new->{identifier} = $match->{identifier};
} else {
say "could not find finding $new->{ordinal}, assigning random identifier";
$new->{identifier} = 'fixme_'.int((rand 1)*1000);
}
delete $new->{refs};
my $existing_uri = "/report/$report/chapter/$chapter/finding/$new->{identifier}";
delete $to_delete{$existing_uri};
my $existing = $c->get($existing_uri);
my $uri = $existing ? $existing_uri : "/report/$report/finding";
if ($dry_run) {
say "Not sending finding $new->{ordinal} to $uri ($new->{identifier})";
next;
}
$c->post( $uri =>
{
chapter_identifier => $chapter,
report_identifier => $report,
%$new,
}
);
}
for my $uri (keys %to_delete) {
say $dry_run ? "Not deleting $uri" : "Deleting $uri";
next if $dry_run;
$c->delete($uri) or die "could not delete $uri";
}