blob: 74e03136c04bd69c7c922ddba07923671cff8ac3 [file] [log] [blame]
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @fileoverview Service class to represent and hold state for each instance
* of a shill service for a given log being processed.
*/
/**
* @constructor
*
* @param {String} serviceId Identifier for the new service instance.
* */
function Service(serviceId) {
this.serviceId = serviceId;
this.states = [];
this.isActive = false;
this.connections = 0;
this.graphData = [];
this.connectionTimes = [];
}
/**
* Method to add an update to a service.
*
* @param {Update} update Update object to add.
*/
Service.prototype.addUpdate = function(update) {
if (update.time == null) {
this.graphData.push({'x': 0, 'y': update.state});
return;
}
if (netlogSummary.isActive(update.state)) {
this.isActive = true;
}
if (this.states.length > 0) {
var lastUpdate = this.states[this.states.length - 1];
if (lastUpdate.state != update.state || update.failure) {
this.states.push(update);
this.graphData.push({'x': update.time, 'y': lastUpdate.state});
} else {
console.log('last state update was the same... ignoring');
return;
}
} else {
if (this.graphData.length > 0) {
this.graphData.push({'x': update.time, 'y': this.graphData[0].y});
}
this.states.push(update);
}
this.graphData.push({'x': update.time, 'y': update.state});
var connTimeLength = this.connectionTimes.length;
if (update.state == 'Associating') {
this.connections++;
this.connectionTimes.push({'start': Date.parse(update.time),
'end': null,
'type': '?'});
} else if (update.state == 'Failure') {
if (connTimeLength > 0) {
if (!this.connectionTimes[connTimeLength - 1].end) {
this.connectionTimes[connTimeLength - 1].end = Date.parse(update.time);
this.connectionTimes[connTimeLength - 1].type = 'F';
}
}
} else if (update.state == 'Online') {
if (connTimeLength > 0) {
if (!this.connectionTimes[connTimeLength - 1].end) {
this.connectionTimes[connTimeLength - 1].end = Date.parse(update.time);
this.connectionTimes[connTimeLength - 1].type = 'O';
}
}
}
};
/**
* Method to return most recent update for a service. Returns null if no
* state updates exist.
*
* @return {Update} The last update for this service.
*/
Service.prototype.getCurrentUpdate = function() {
if (this.states.length > 0)
return this.states[this.states.length - 1];
return null;
};