generated from paul121/farm_contrib_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfarm_loocc.install
218 lines (206 loc) · 5.92 KB
/
farm_loocc.install
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
<?php
/**
* @file
* Install, update, and uninstall functions for farm_loocc.module.
*/
/**
* Implements hook_requirements().
*/
function farm_loocc_requirements($phase) {
$requirements = [];
// Only perform runtime checks.
if ($phase == 'runtime') {
// Check the LOOC-C API connection.
/** @var \Drupal\farm_loocc\LooccClientInterface $looc_client */
$looc_client = \Drupal::service('farm_loocc.loocc_client');
$status = $looc_client->ping();
if (!$status) {
$requirements['loocc'] = [
'title' => t('LOOC-C API Status'),
'value' => t('The LOOC-C API is not responding.'),
'severity' => REQUIREMENT_ERROR,
];
}
// Try an authenticated request.
else {
$success = (bool) $looc_client->getErfCobenefits('hir');
$requirements['loocc'] = [
'title' => t('LOOC-C API Status'),
'value' => $success ? t('Connected') : t('Not connected. Check your API key.'),
'severity' => $success ? REQUIREMENT_OK : REQUIREMENT_ERROR,
];
}
}
return $requirements;
}
/**
* Implements hook_schema().
*/
function farm_loocc_schema() {
// Build databse schema for farm_loocc tables.
$schema = [];
// Estimate base table.
$schema['farm_loocc_estimate'] = [
'description' => 'Base table for LOOC-C estimates.',
'fields' => [
'id' => [
'description' => 'Auto-incrementing identifier for the estimate.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'asset_id' => [
'description' => 'The location asset ID',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'timestamp' => [
'description' => 'Timestamp of the estimate creation.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'selected_method' => [
'description' => 'The method selected for the project.',
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
],
'project_length' => [
'description' => 'The length of the project.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 25,
],
'new_irrigation' => [
'description' => 'If the project will be using new irrigation methods.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'polygon_area' => [
'description' => 'The total polygon area used for carbon estimates.',
'type' => 'float',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0.0,
],
'bd_average' => [
'description' => 'The estimated average percent bulk density for the project area.',
'type' => 'float',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0.0,
],
'carbon_average' => [
'description' => 'The estimated average percent organic carbon for the project area.',
'type' => 'float',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0.0,
],
'carbon_target' => [
'description' => 'The target average percent organic carbon at the end of the project period.',
'type' => 'float',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0.0,
],
'warning_message' => [
'description' => 'Warning messages associated with the direct soil measurement estimates.',
'type' => 'text',
'not null' => FALSE,
],
],
'primary key' => [
'id',
],
'indexes' => [
'id' => ['id'],
'asset_id' => ['asset_id'],
],
// Foreign keys are only for documentation purposes.
'foreign keys' => [
'estimate_asset' => [
'table' => 'asset',
'columns' => ['asset_id' => 'id'],
],
],
];
$schema['farm_loocc_accu_estimate'] = [
'description' => 'Individual LOOC-C ERF method and project type ACCU estimates.',
'fields' => [
'estimate_id' => [
'description' => 'The ID of the base LOOC-C estimate.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'method_id' => [
'description' => "The ID of the estimate's ERF method or project type.",
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
],
'annual' => [
'description' => 'The estimated annual ACCU sequestration rate.',
'type' => 'float',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0.0,
],
'project' => [
'description' => 'The estimated total ACCUs sequestered over the project duration.',
'type' => 'float',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0.0,
],
'warning_message' => [
'description' => 'Warning messages associated with the direct soil measurement estimates.',
'type' => 'text',
'not null' => FALSE,
],
],
'primary key' => [
'estimate_id',
'method_id',
],
'indexes' => [
'estimate_id' => ['estimate_id'],
],
// Foreign keys are only for documentation purposes.
'foreign keys' => [
'base_estimate' => [
'table' => 'farm_loocc_estimate',
'columns' => ['estimate_id' => 'id'],
],
],
];
// Add lrf ratings to farm_loocc_accu_estimate.
$lrf_ratings = [
'great_barrier_reef',
'coastal_ecosystems',
'wetlands',
'threatened_ecosystems',
'threatened_wildlife',
'native_vegetation',
'summary',
];
foreach ($lrf_ratings as $rating) {
$schema['farm_loocc_accu_estimate']['fields'][$rating] = [
'description' => "LRF rating: $rating",
'type' => 'int',
'size' => 'tiny',
'unsigned' => FALSE,
'not null' => FALSE,
];
}
return $schema;
}