Browse Source

Add production trends to dashboard

Neal Wilson 6 years ago
parent
commit
4a10114e2b
1 changed files with 73 additions and 0 deletions
  1. 73
    0
      config/Windows/navigation.xml

+ 73
- 0
config/Windows/navigation.xml View File

@@ -476,6 +476,7 @@
476 476
             var query = new QSqlQuery;
477 477
             drawReminders(output, query);
478 478
             drawSchedule(output, query);
479
+            drawProductionTrends(output, query);
479 480
             drawLatest(output, query);
480 481
             drawRecentlyOut(output, query);
481 482
             drawLeastAvailable(output, query);
@@ -774,6 +775,78 @@
774 775
                 endCell(output, "" + c + TTR("navwindow", " unused coffees"));
775 776
             }
776 777
         }
778
+        var calculateChange = function(current, previous) {
779
+            var difference = Number(current) - Number(previous);
780
+            var retval;
781
+            if(difference > 0) {
782
+                retval = "+";
783
+            } else if(difference < 0) {
784
+                retval = "-";
785
+            }
786
+            retval += ((difference/Number(previous)) * 100).toFixed(1);
787
+            retval += "%";
788
+            return retval;
789
+        }
790
+        var outputChange = function(output, value) {
791
+            output.writeStartElement("td");
792
+            if(value[0] == '+') {
793
+                output.writeAttribute("style", "color: #008B00");
794
+            } else if(value[0] == '-') {
795
+                output.writeAttribute("style", "color: #8B0000");
796
+            }
797
+            output.writeCharacters(value);
798
+            output.writeEndElement();
799
+        }
800
+        var outputChangeRow = function(output, query, p1, p2) {
801
+            var u = unitData();
802
+            query.prepare("SELECT sum(roasted_quantity)/:conversion FROM roasting_log WHERE time > 'now'::date - '" + p1 + " days'::interval");
803
+            query.bind(":conversion", u.conversion);
804
+            query.exec();
805
+            query.next();
806
+            var current = Number(query.value(0));
807
+            output.writeTextElement("td", "" + current.toFixed(0) + u.unittext);
808
+            query.prepare("SELECT sum(roasted_quantity)/:conversion FROM roasting_log WHERE time > 'now'::date - '" + p2 + " days'::interval AND time < 'now'::date - '" + p1 + " days'::interval");
809
+            query.bind(":conversion", u.conversion);
810
+            query.exec();
811
+            query.next();
812
+            var previous = Number(query.value(0));
813
+            output.writeTextElement("td", "" + previous.toFixed(0) + u.unittext);
814
+            outputChange(output, calculateChange(current, previous));
815
+        }
816
+        var drawProductionTrends= function(output, query) {
817
+            startCell(output, TTR("navwindow", "Production Trends"));
818
+            startStage(output);
819
+            output.writeStartElement("table");
820
+            output.writeStartElement("tr");
821
+            output.writeTextElement("th", TTR("navwindow", "Period"));
822
+            output.writeTextElement("th", TTR("navwindow", "Production"));
823
+            output.writeTextElement("th", TTR("navwindow", "Previous"));
824
+            output.writeTextElement("th", TTR("navwindow", "Change"));
825
+            output.writeEndElement();
826
+            output.writeStartElement("tr");
827
+            output.writeTextElement("th", TTR("navwindow", "Today"));
828
+            outputChangeRow(output, query, 0, 1);
829
+            output.writeEndElement();
830
+            output.writeStartElement("tr");
831
+            output.writeTextElement("th", TTR("navwindow", "Last 7 Days"));
832
+            outputChangeRow(output, query, 6, 13);
833
+            output.writeEndElement();
834
+            output.writeStartElement("tr");
835
+            output.writeTextElement("th", TTR("navwindow", "Last 30 Days"));
836
+            outputChangeRow(output, query, 29, 59);
837
+            output.writeEndElement();
838
+            output.writeStartElement("tr");
839
+            output.writeTextElement("th", TTR("navwindow", "Last 90 Days"));
840
+            outputChangeRow(output, query, 89, 179);
841
+            output.writeEndElement();
842
+            output.writeStartElement("tr");
843
+            output.writeTextElement("th", TTR("navwindow", "Last 365 Days"));
844
+            outputChangeRow(output, query, 364, 729);
845
+            output.writeEndElement();
846
+            output.writeEndElement();
847
+            endStage(output);
848
+            endCell(output);
849
+        }
777 850
         refresh();
778 851
         dashboard.scriptLinkClicked.connect(function(url) {
779 852
             if(url == "reminders") {

Loading…
Cancel
Save