~mfisch/demometrics/minval

« back to all changes in this revision

Viewing changes to main.cpp

  • Committer: Matthew Fischer
  • Date: 2013-09-19 18:10:10 UTC
  • Revision ID: matthew.fischer@canonical.com-20130919181010-uz0kbvpvmykdn0ma
Adding a max value and some extra code cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
    return elems;
29
29
}
30
30
 
31
 
void add_metrics(std::string dataSourceId, std::string formatString, std::string emptyDataString)
 
31
void add_metrics(std::string dataSourceId, std::string formatString, std::string emptyDataString, int max)
32
32
{
33
33
    // We start with a manager object
34
34
    MetricManagerPtr manager(MetricManager::getInstance());
42
42
    MetricUpdatePtr update(metric->update());
43
43
 
44
44
    for (int i = 0; i < 31; ++i) {
45
 
        update->addData(rand()%20);
 
45
        update->addData(rand()%max);
46
46
    }
47
47
}
48
48
 
51
51
    std::string formatStrBase("<b>%1</b> ");
52
52
    std::string formatStr;
53
53
    std::vector<std::string> tokens;
 
54
    int max = 20;
 
55
    bool ok;
54
56
 
55
57
    QCoreApplication a(argc, argv);
56
58
    srand (time(NULL));
61
63
    while (std::getline(infile, line))
62
64
    {
63
65
        tokens = split(line,':');
64
 
        if (tokens.size() != 3) {
 
66
        if (tokens.size() != 4) {
65
67
            std::cout << "Malformed line in data file, skipping: " << line << std::endl;
66
68
            continue;
67
69
        }
68
70
 
69
71
        //prepend the %s portion
70
72
        formatStr = formatStrBase + tokens[1];
71
 
        add_metrics(tokens[0], formatStr, tokens[2]);
 
73
        max = (QString::fromStdString(tokens[3])).toLong(&ok, 10);
 
74
        if (!ok) {
 
75
            std::cout << "Error: Cannot convert " << tokens[3] << " to a number";
 
76
            max = 20;
 
77
        }
 
78
        add_metrics(tokens[0], formatStr, tokens[2], max);
72
79
    }
73
80
    return 0;
74
81
}