Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
wip for /device/:id/environment endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
karenetheridge committed Jan 24, 2019
1 parent 50cd5ff commit b0ea120
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 4 deletions.
34 changes: 31 additions & 3 deletions json-schema/input.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,35 @@ definitions:
type: string
vendor_name:
type: string

EnvironmentData:
properties:
temp:
type: object
required:
- cpu0
- cpu1
properties:
cpu0:
type: integer
cpu1:
type: integer
exhaust:
type: integer
inlet:
type: integer
disks:
type: object
patternProperties:
^\S+$:
description: key = device_disk.serial_number
type: integer
voltage:
type: object
properties:
psu0:
type: number
psu1:
type: number
DeviceReport:
required:
- bios_version
Expand Down Expand Up @@ -121,7 +149,7 @@ definitions:
drive_type:
type: string
temp:
# TODO: move to different endpoint
# TODO: remove, once /device/:id/environment endpoint starts getting used
$ref: /definitions/int_or_stringy_int
enclosure:
type: string
Expand Down Expand Up @@ -220,7 +248,7 @@ definitions:
system_uuid:
$ref: /definitions/uuid
temp:
# TODO: move to different endpoint
# TODO: remove, once /device/:id/environment endpoint starts getting used
type: object
required:
- cpu0
Expand Down
90 changes: 90 additions & 0 deletions lib/Conch/Controller/DeviceEnvironment.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package Conch::Controller::DeviceEnvironment;

use Mojo::Base 'Mojolicious::Controller', -signatures;

use Role::Tiny::With;
with 'Conch::Role::MojoLog';

=pod
=head1 NAME
Conch::Controller::DeviceEnvironment
=head1 METHODS
=head2 process
Receives environment data for a particular device:
- records to the database
- dispatches to Circonus/Prometheus (TODO)
- sends to a validation (TODO) (what to do when validation fails?)
=cut

sub process ($c) {
my $data = $c->validate_input('EnvironmentData');
return if not $data;

my $device_rs = $c->stash('device_rs');

my %environment = (
$data->{temp} ? (
cpu0_temp => $data->{temp}{cpu0},
cpu1_temp => $data->{temp}{cpu1},
inlet_temp => $data->{temp}{inlet},
exhaust_temp => $data->{temp}{exhaust},
) : (),
$data->{voltage} ? (
psu0_voltage => $data->{voltage}{psu0},
psu1_voltage => $data->{voltage}{psu1},
) : (),
);

$device_rs->related_resultset('device_environment')->update_or_create({
%environment,
updated => \'now()',
}) if keys %environment;

if ($data->{disks} and keys $data->{disks}->%*) {
foreach my $disk_serial (keys $data->{disks}->%*) {
my $disk = $device_rs->related_resultset('device_disks')->find(
{ serial_number => $disk_serial },
{ key => 'device_disk_serial_number_key' },
);
if (not $disk) {
$c->log->debug('received environment data for non-existent disk: device id '
.$c->stash('device_id').", serial number $disk_serial");
next;
}

$disk->update({
temp => $data->{disks}{$disk_serial}{temp},
updated => \'now()',
});
}
}

$c->log->info('recorded environment data for device '.$c->stash('device_id'));

# TODO: send to Circonus/Prometheus.

# TODO: run validations?
}

1;
__END__
=pod
=head1 LICENSING
Copyright Joyent, Inc.
This Source Code Form is subject to the terms of the Mozilla Public License,
v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
one at http://mozilla.org/MPL/2.0/.
=cut
# vim: set ts=4 sts=4 sw=4 et :
3 changes: 2 additions & 1 deletion lib/Conch/Controller/DeviceReport.pm
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,14 @@ sub _record_device_configuration {

$log->info("Created Device Spec for Device ".$device->id);

# TODO: stop doing this, and send all temp data to /device/:id/environment instead.
if ($dr->{temp}) {
$device->related_resultset('device_environment')->update_or_create({
cpu0_temp => $dr->{temp}->{cpu0},
cpu1_temp => $dr->{temp}->{cpu1},
inlet_temp => $dr->{temp}->{inlet},
exhaust_temp => $dr->{temp}->{exhaust},
# TODO: not setting psu0_voltage, psu1_voltage
# note: not setting psu0_voltage, psu1_voltage
updated => \'NOW()',
});
$c->log->info("Recorded environment for Device ".$device->id);
Expand Down
5 changes: 5 additions & 0 deletions lib/Conch/Route/Device.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Sets up the routes for /device:
GET /device/:device_id/interface/:interface_name
GET /device/:device_id/interface/:interface_name/:field
POST /device/:device_id/environment
=cut

sub routes {
Expand Down Expand Up @@ -133,6 +135,9 @@ sub routes {
# GET /device/:device_id/interface/:interface_name/:field
$with_interface_name->get('/:field_name')->to('#get_one_field');
}

# POST /device/:device_id/environment
$with_device->post('/environment')->to('device_environment#process');
}
}

Expand Down

0 comments on commit b0ea120

Please sign in to comment.