2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

sensor.cpp 4.3 KB
Newer Older
1
2
3
//================================================================================
// Name        : sensor.cpp
// Author      : Michael Ott, Daniele Tafani
Micha Müller's avatar
Micha Müller committed
4
// Contact     : info@dcdb.it
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Copyright   : Leibniz Supercomputing Centre
// Description : Source file for Sensor class.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2017-2019 Leibniz Supercomputing Centre
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//================================================================================
27
28

#include <iostream>
29
30
31
#include <functional>
#include <numeric>
#include <algorithm>
32
33
34
35
36
#include <dcdb/sensor.h>
#include <dcdb/virtualsensor.h>

namespace DCDB {

37
  Sensor::Sensor(Connection* connection, const std::string& publicName) {
38
    /* Initialize the SensorConfig interface */
Michael Ott's avatar
Michael Ott committed
39
40
41
    sensorConfig = new SensorConfig(connection);

    this->connection = connection;
42
43
      
    /* Retrieve publicSensor info */
Michael Ott's avatar
Michael Ott committed
44
    switch (sensorConfig->getPublicSensorByName(publicSensor, publicName.c_str())) {
45
46
47
48
49
50
51
52
53
54
55
56
    case DCDB::SC_OK:
      break;
    case DCDB::SC_INVALIDSESSION:
      std::cout << "Invalid session." << std::endl;
      return;
    case DCDB::SC_UNKNOWNSENSOR:
      std::cout << "Unknown sensor: " << publicName << std::endl;
      return;
    default:
      std::cout << "Unknown error." << std::endl;
      return;
    }
Michael Ott's avatar
Michael Ott committed
57
  }
58
  
59
  Sensor::Sensor(Connection* connection, const PublicSensor& sensor) {
60
61
62
63
64
65
    /* Initialize the SensorConfig interface */
    sensorConfig = new SensorConfig(connection);
    
    this->connection = connection;
    publicSensor = sensor;
  }
Michael Ott's avatar
Michael Ott committed
66
67
68
69
70

  Sensor::~Sensor() {
    delete sensorConfig;
  }

71
  void Sensor::query(std::list<SensorDataStoreReading>& result, TimeStamp& start, TimeStamp& end, QueryAggregate aggregate, uint64_t tol_ns) {
Michael Ott's avatar
Michael Ott committed
72
    SensorDataStore sensorDataStore(connection);
73
74

    if (publicSensor.is_virtual) {
Michael Ott's avatar
Michael Ott committed
75
        VSensor vSen(connection, publicSensor);
76
        vSen.query(result, start, end);
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
	if ((result.size() > 0) && (aggregate != AGGREGATE_NONE)) {
	    switch(aggregate) {
		case AGGREGATE_MIN:
		    result.begin()->value = std::min_element(result.begin(), result.end())->value;;
		    break;
		case AGGREGATE_MAX:
		    result.begin()->value = std::max_element(result.begin(), result.end())->value;;
		    break;
		case AGGREGATE_AVG: {
		    int64_t sum = 0;
		    for (auto r: result) {
			sum+= r.value;
		    }
		    result.begin()->value = sum / result.size();
		    break; }
		case AGGREGATE_SUM: {
		    int64_t sum = 0;
		    for (auto r: result) {
			sum+= r.value;
		    }
		    result.begin()->value = sum;
		    break;}
		case AGGREGATE_COUNT:
		    result.begin()->value = result.size();
		    break;
102
103
104
		default:
		    std::cout << "Unknown aggregate " << aggregate << " in Sensor::query()" << std::endl;
		    break;
105
106
107
	    }
	    result.erase(std::next(result.begin()), result.end());
	}
108
109
    }
    else {
110
        /* Iterate over the sensorIds within the query interval and output the results in CSV format */
111
        if(start.getRaw() != end.getRaw()) {
112
113
114
115
116
117
118
	    uint16_t wsStart = start.getWeekstamp();
	    uint16_t wsEnd   = end.getWeekstamp();
	    
	    SensorId sid(publicSensor.name);
	    for(uint16_t ws=wsStart; ws<=wsEnd; ws++) {
		sid.setRsvd(ws);
                sensorDataStore.query(result, sid, start, end, aggregate);
119
120
            }
        } else {
121
122
123
	    SensorId sid(publicSensor.name);
	    sid.setRsvd(start.getWeekstamp());
            sensorDataStore.fuzzyQuery(result, sid, start, tol_ns);
124
125
126
        }
    }
  }
127
  
128
} /* namespace DCDB */